collocation in time
Added by Stavroula Biri over 3 years ago
Hello,
I am using cdo in python. I would like to collocate ASCAT wind products with ERA5 wind components in space and time.
The code I am using first interpolates ERA5 on to ASCAT grid (cdo remapnn) and then selects times (that are in a list) closer to the full hour of ERA5 in a for loop for each point defined by (lon, lat). This for loop is very slow. Is there a way to interpolate in time avoiding the for loop?
Thank you in advance. Your assistance would be greatly appreciated.
I attach my code below:
- load era5 lon, lat
fid = nc.Dataset("./u10_ANT_2016.nc")
latE = np.array(fid.variables['latitude'][:], dtype=float)
lonE = np.array(fid.variables['longitude'][:], dtype=float)
fid.close()
x = np.append(np.vstack([np.array(date[i].year) for i in range(365)]),
np.vstack([np.array(date[i].month) for i in range(365)]), axis=1)
axis=1)
- merge date time arrays for asc, desc
d_asc = np.empty(tod_asc.shape, dtype='U32')
d_desc = np.empty(tod_desc.shape, dtype='U32')
for i in range(365):
for iy in range(len(lat)):
for ix in range(len(lon)):
if (~np.isnan(tod_asc[i, iy, ix])):
tm = int(np.round(tod_asc[i, iy, ix]))
if (tm == 24):
tm = 0
d_asc[i, iy, ix] = (str(x[i, 0])+'-'+str(x[i, 1]).zfill(2)
'-'+str(x[i, 2]).zfill(2)'T'
str(tm).zfill(2)":00:00")
if (~np.isnan(tod_desc[i, iy, ix])):
d_desc[i, iy, ix] = (str(x[i, 0])+'-'+str(x[i, 1]).zfill(2)
'-'+str(x[i, 2]).zfill(2)'T' +
str(int(np.round(tod_desc[i, iy,
ix]))).zfill(2) +
":00:00")
np.save('d_asc', d_asc)
np.save('d_desc', d_desc) - allocate arrays for collocation
ue_asc = np.empty(tod_asc.shape, dtype=float)*np.nan
- first convert lon -180:180 to 0:360 and invert latitudes
cdo.sellonlatbox('0.0,360.0,-90.0,-40.0', input='-invertlat u10_ANT_2016.nc',
output='u10_ANT_2016_lon360.nc', options='-f nc') - generate ascat grid (terminal)
- cdo griddes ./ascat_surfacewind/DATA/cropped/ascat_2016.nc > ascat_grid.txt
- remapnn to ascat grid
cdo.remapnn('ascat_grid.txt',
input='-sellonlatbox,0.0,360.0,-90.0,-40.0, '
'-invertlat u10_ANT_2016.nc',
output='u10_ANT_2016_gASCAT.nc', options='-f nc') - collocation loop (veeery slow)
for (iy, _) in enumerate(lat): #! FIXME
for (ix, _) in enumerate(lon):
if (not np.all(np.isnan(u_asc[:, iy, ix]))):
print(iy, ix)
k = np.where(~np.isnan(tod_asc[:, iy, ix]))
tmp = [e for e in d_asc[:, iy, ix].tolist() if e]
cdo.select("date="+','.join(str(a) for a in tmp),
input='u10_ANT_2016_gASCAT.nc', output='tmp.nc',
options='-f nc')
del tmp
fid = nc.Dataset("tmp.nc")
ue_asc[k, iy, ix] = np.squeeze(
np.array(fid.variables["u10"])[:, iy, ix])
fid.close()
os.system('rm tmp.nc')