-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_data_iris.py
298 lines (258 loc) · 13.9 KB
/
read_data_iris.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import iris
from iris.util import equalise_attributes
import os
import numpy as np
import xarray as xr
import cf_units
import dask.array as da
from tqdm import tqdm
from scipy.stats import linregress
def data_directory(dataset, band=None, regridded=False, mask_surface_water=False):
if dataset == 'VOD' and band is None:
raise ValueError('Must supply band for VOD data')
surface_water_dir = mask_surface_water * 'filtered_surface_water/'
directories = {'IMERG': '/localscratch/wllf029/bethar/IMERG',
'VOD': f'/prj/nceo/bethar/VODCA_global/filtered/{surface_water_dir}{band}-band',
'SM': '/prj/swift/ESA_CCI_SM/year_files_v6.1_combined_GLOBAL/',
'IMERG_regridded': '/prj/nceo/bethar/IMERG/regrid_p25_global',
'SWAMPS': '/prj/nceo/bethar/SWAMPS_daily',
'NDVI': '/prj/nceo/bethar/MODIS-NDVI-16day'}
dataset_key = dataset+'_regridded' if regridded else dataset
directory = directories[dataset_key]
return directory
def data_filename(dataset, year, band=None, modis_sensor=None, regridded=False, mask_surface_water=False):
if dataset == 'VOD' and band is None:
raise ValueError('Must supply band for VOD data')
if dataset == 'NDVI' and modis_sensor is None:
raise ValueError('Must supply MODIS sensor (aqua/terra) for NDVI data')
surface_water_fname = mask_surface_water * 'surface_water_'
filenames = {'IMERG': f'IMERG.V06.{year}.daily.nc4',
'VOD': f'VOD-{band}-band_filtered_{surface_water_fname}{year}.nc',
'SM': f'{year}_volumetric_soil_moisture_daily.nc',
'IMERG_regridded': f'IMERG.V06.{year}.daily_p25.nc',
'SWAMPS': f'SWAMPS-{year}.nc',
'NDVI': f'/prj/nceo/bethar/MODIS-NDVI-16day/modis_{modis_sensor}_16-day_ndvi_0p25_{year}.nc'}
dataset_key = dataset+'_regridded' if regridded else dataset
file_path = os.path.join(data_directory(dataset, band=band, regridded=regridded, mask_surface_water=mask_surface_water), filenames[dataset_key])
return file_path
def crop_cube(cube, lon_west, lon_east, lat_south, lat_north):
return cube.extract(iris.Constraint(latitude=lambda cell: lat_south-1e-6 < cell < lat_north+1e-6,
longitude=lambda cell: lon_west-1e-6 < cell < lon_east+1e-6))
def pixel_top_left_to_centre(cube):
lats = cube.coord('latitude').points
resolution = lats[1] - lats[0]
new_lats = cube.coord('latitude').copy(lats - 0.5*resolution)
cube.replace_coord(new_lats)
lons = cube.coord('longitude').points
new_lons = cube.coord('longitude').copy(lons + 0.5*resolution)
cube.replace_coord(new_lons)
def roll_cube_longitude(cube):
"""Takes a cube which goes longitude 0-360 back to -180-180."""
lon = cube.coord('longitude')
cube.data = np.roll(cube.data, len(lon.points) // 2)
new_lons = lon.copy(lon.points - 180.)
cube.replace_coord(new_lons)
return cube
def fix_coords(cube):
calendar = cube.coord('time').units.calendar
common_time_unit = cf_units.Unit('days since 1970-01-01', calendar=calendar)
cube.coord('time').convert_units(common_time_unit)
times = cube.coord('time').points
if np.all(np.abs(times.astype(int) - times) < 1e-6): #put all times at 12Z so bounds fill day
new_time = cube.coord('time').copy(times + 0.5)
cube.replace_coord(new_time)
if np.all(np.diff(cube.coord('latitude').points)<0.): #make all latitude coordinate arrays ascending
cube = iris.util.reverse(cube, cube.coords('latitude'))
if cube.standard_name in ['air_temperature', 'land_binary_mask']: #ERA5 coordinates at top-left rather than centre of grid box
pixel_top_left_to_centre(cube)
if cube.coord('longitude').points.max() > 181.: #longitude on 0 to 360 instead of -180 to 180
cube = roll_cube_longitude(cube)
for coord_key in ['time', 'latitude', 'longitude']:
if cube.coord(coord_key).points.size > 1:
cube.coord(coord_key).bounds = None
cube.coord(coord_key).guess_bounds()
cube.coord(coord_key).bounds = np.round(cube.coord(coord_key).bounds, 3)
cube.coord(coord_key).points = np.round(cube.coord(coord_key).points, 3)
return cube
def read_land_sea_mask(lon_west=-180, lon_east=180, lat_south=-90, lat_north=90, regrid_cube=None):
filename = '/prj/nceo/bethar/ERA5/T2m/era5-land-sea-mask.nc'
land_sea_data = iris.load_cube(filename)[0]
land_sea_data = fix_coords(land_sea_data)
if regrid_cube:
regrid_scheme = iris.analysis.AreaWeighted(mdtol=0.5)
land_sea_data = land_sea_data.regrid(regrid_cube, regrid_scheme)
land_sea_data_crop = crop_cube(land_sea_data, lon_west, lon_east, lat_south, lat_north)
return land_sea_data_crop.data
def read_data_year(dataset, year, band=None, modis_sensor=None, regridded=False, mask_surface_water=False,
regrid_cube=None, lon_west=-180, lon_east=180, lat_south=-30, lat_north=30):
if dataset == 'VOD' and band is None:
raise ValueError('Must supply band for VOD data')
if dataset == 'NDVI' and modis_sensor is None:
raise ValueError('Must supply MODIS sensor (aqua/terra) for NDVI data')
field_keys = {'IMERG': 'precipitationCal',
'VOD': 'vod',
'SM': 'sm',
'SWAMPS': 'frac_surface_water',
'NDVI': 'NDVI'}
filename = data_filename(dataset, year, band=band, modis_sensor=modis_sensor, regridded=regridded,
mask_surface_water=mask_surface_water)
data_cube = iris.load_cube(filename, field_keys[dataset])
data_fix = fix_coords(data_cube)
data_crop = crop_cube(data_fix, lon_west, lon_east, lat_south, lat_north)
if regrid_cube:
regrid_scheme = iris.analysis.AreaWeighted(mdtol=0.5)
data_crop = data_crop.regrid(regrid_cube, regrid_scheme)
return data_crop
def read_data_all_years(dataset, band=None, modis_sensor=None, regridded=False,
mask_surface_water=False, regrid_cube=None,
min_year=2002, max_year=2016,
lon_west=-180, lon_east=180, lat_south=-30, lat_north=30):
years = np.arange(min_year, max_year + 1, dtype=np.int16)
all_years = iris.cube.CubeList([read_data_year(dataset, year, band=band, modis_sensor=modis_sensor,
mask_surface_water=mask_surface_water,
lon_west=lon_west, lon_east=lon_east,
regridded=regridded, regrid_cube=regrid_cube,
lat_south=lat_south, lat_north=lat_north) for year in years])
equalise_attributes(all_years)
all_data = all_years.concatenate_cube()
if dataset in ['IMERG', 'NDVI']:
all_data.transpose([0, 2, 1])
return all_data
def write_regridded_datasets(band, lon_west=-180, lon_east=180, lat_south=-30, lat_north=30,
min_year=2002, max_year=2016):
vod = read_data_all_years('VOD', band=band, min_year=min_year, max_year=max_year,
lon_west=lon_west, lon_east=lon_east, lat_south=lat_south, lat_north=lat_north)
years = np.arange(min_year, max_year + 1, dtype=np.int16)
for year in tqdm(years):
imerg_year = read_data_year('IMERG', year, regrid_cube=vod,
lon_west=lon_west, lon_east=lon_east, lat_south=lat_south, lat_north=lat_north)
iris.save(imerg_year, f'/prj/nceo/bethar/IMERG/regrid_p25_global/IMERG.V06.{year}.daily_p25.nc')
def all_regridded_datasets(band, lon_west=-180, lon_east=180, lat_south=-30, lat_north=30,
min_year=2002, max_year=2016):
vod = read_data_all_years('VOD', band=band, min_year=min_year, max_year=max_year,
lon_west=lon_west, lon_east=lon_east, lat_south=lat_south, lat_north=lat_north)
imerg = read_data_all_years('IMERG', band=band, regridded=True, min_year=min_year, max_year=max_year,
lon_west=lon_west, lon_east=lon_east, lat_south=lat_south, lat_north=lat_north)
return vod, chirps, imerg
def detrend_missing_values(data):
x = np.arange(data.size)
valid_idx = ~np.logical_or(data>998, data<-998)
if valid_idx.sum() > 0:
m, b, r_val, p_val, std_err = linregress(x[valid_idx], data[valid_idx])
detrended_data = data - (m*x + b)
else:
detrended_data = data
return detrended_data
def detrend_cube(cube, dimension='time'):
"""
Adapted from esmvalcore to work with missing values.
Detrend data along a given dimension.
Parameters
----------
cube: iris.cube.Cube
input cube.
dimension: str
Dimension to detrend
method: str
Method to detrend. Available: linear, constant. See documentation of
'scipy.signal.detrend' for details
Returns
-------
iris.cube.Cube
Detrended cube
"""
coord = cube.coord(dimension)
axis = cube.coord_dims(coord)[0]
detrended = da.apply_along_axis(
detrend_missing_values,
axis=axis,
arr=cube.lazy_data().rechunk([45, 500, 500]),
shape=(cube.shape[axis],)
)
return cube.copy(detrended)
def monthly_anomalies(cube, detrend=False):
if detrend:
cube = detrend_cube(cube)
dxr = xr.DataArray.from_iris(cube)
climatology = dxr.groupby("time.month").mean("time")
anomalies = (dxr.groupby("time.month") - climatology).to_iris()
anomalies.standard_name = cube.standard_name
anomalies.long_name = cube.long_name
anomalies.units = cube.units
calendar = anomalies.coord('time').units.calendar
common_time_unit = cf_units.Unit('days since 1970-01-01', calendar=calendar)
anomalies.coord('time').convert_units(common_time_unit)
for coord_key in ['time', 'latitude', 'longitude']:
anomalies.coord(coord_key).bounds = None
anomalies.coord(coord_key).guess_bounds()
anomalies.coord(coord_key).bounds = np.round(anomalies.coord(coord_key).bounds, 3)
anomalies.coord(coord_key).points = np.round(anomalies.coord(coord_key).points, 3)
return anomalies
def daily_anomalies(cube, detrend=False):
if detrend:
cube = detrend_cube(cube)
dxr = xr.DataArray.from_iris(cube)
month_day_str = xr.DataArray(dxr.indexes['time'].strftime('%m-%d'), coords=dxr.coords['time'].coords, name='month_day_str')
climatology = dxr.groupby(month_day_str).mean("time")
anomalies = (dxr.groupby(month_day_str) - climatology).to_iris()
anomalies.standard_name = cube.standard_name
anomalies.long_name = cube.long_name
anomalies.units = cube.units
calendar = anomalies.coord('time').units.calendar
common_time_unit = cf_units.Unit('days since 1970-01-01', calendar=calendar)
anomalies.coord('time').convert_units(common_time_unit)
for coord_key in ['time', 'latitude', 'longitude']:
anomalies.coord(coord_key).bounds = None
anomalies.coord(coord_key).guess_bounds()
anomalies.coord(coord_key).bounds = np.round(anomalies.coord(coord_key).bounds, 3)
anomalies.coord(coord_key).points = np.round(anomalies.coord(coord_key).points, 3)
return anomalies
def monthly_anomalies_normalised(cube, detrend=False):
if detrend:
cube = detrend_cube(cube)
dxr = xr.DataArray.from_iris(cube)
climatology_mean = dxr.groupby("time.month").mean("time")
climatology_std = dxr.groupby("time.month").std("time")
anomalies = xr.apply_ufunc(
lambda x, m, s: (x - m) / s,
dxr.groupby("time.month"),
climatology_mean,
climatology_std, dask='allowed'
).to_iris()
anomalies.standard_name = cube.standard_name
anomalies.long_name = cube.long_name
anomalies.units = cube.units
calendar = anomalies.coord('time').units.calendar
common_time_unit = cf_units.Unit('days since 1970-01-01', calendar=calendar)
anomalies.coord('time').convert_units(common_time_unit)
for coord_key in ['time', 'latitude', 'longitude']:
anomalies.coord(coord_key).bounds = None
anomalies.coord(coord_key).guess_bounds()
anomalies.coord(coord_key).bounds = np.round(anomalies.coord(coord_key).bounds, 3)
anomalies.coord(coord_key).points = np.round(anomalies.coord(coord_key).points, 3)
return anomalies
def daily_anomalies_normalised(cube, detrend=False):
if detrend:
cube = detrend_cube(cube)
dxr = xr.DataArray.from_iris(cube)
month_day_str = xr.DataArray(dxr.indexes['time'].strftime('%m-%d'), coords=dxr.coords['time'].coords, name='month_day_str')
climatology_mean = dxr.groupby(month_day_str).mean("time")
climatology_std = dxr.groupby(month_day_str).std("time")
anomalies = xr.apply_ufunc(
lambda x, m, s: (x - m) / s,
dxr.groupby(month_day_str),
climatology_mean,
climatology_std, dask='allowed'
).to_iris()
anomalies.standard_name = cube.standard_name
anomalies.long_name = cube.long_name
anomalies.units = cube.units
calendar = anomalies.coord('time').units.calendar
common_time_unit = cf_units.Unit('days since 1970-01-01', calendar=calendar)
anomalies.coord('time').convert_units(common_time_unit)
for coord_key in ['time', 'latitude', 'longitude']:
anomalies.coord(coord_key).bounds = None
anomalies.coord(coord_key).guess_bounds()
anomalies.coord(coord_key).bounds = np.round(anomalies.coord(coord_key).bounds, 3)
anomalies.coord(coord_key).points = np.round(anomalies.coord(coord_key).points, 3)
return anomalies