-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathERA5_land.py
42 lines (35 loc) · 1.11 KB
/
ERA5_land.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
ERA5_land.py: A module for processing reanalysis ERA5-based soil moisture data.
(for monthly dataset)
"""
import os
import glob
import datetime
import numpy as np
import h5py
import netCDF4
from tqdm import tqdm
import calendar
def preprocess_lon_lat(lon, lat):
# lat, lon edit for standard data shape
lat = lat.reshape(-1, 1)
lat = np.repeat(lat, len(lon), axis=1)
lon -= 180
lon = np.tile(lon, (len(lat), 1))
return lon, lat
def correct_shape(var_data):
# Transpose due to ERA5's raw shape is (12, 1801, 3600) = (month, lat, lon)
var_data = np.transpose(var_data, (1,2,0))
# Rearrange (180~360) into (-180~0)
var_data_0_180 = var_data[:,:1800,:]
var_data_180_0 = var_data[:,1800:,:]
var_data = np.concatenate((var_data_180_0, var_data_0_180), axis=1)
return var_data
def create_mask(nc_data, var, var_data):
try:
# Replace Fill value into np.nan
fill_value = np.float64(nc_data.variables[var]._FillValue)
var_data[var_data == fill_value] = np.nan
except AttributeError:
print(f"{var} has no FillValue attribute!")
return var_data