重新网格化/重新映射GCM数据

时间:2018-10-02 10:20:08

标签: python

我从CMIP5数据库下载的netCDF文件中有降水数据。我可以制作文件的子集,并获得下面给出的属性。这些数据具有2.5 X 3.75度的空间分辨率。现在,我需要将其转换为0.05度的空间分辨率。有没有人可以通过编写《如何使用Python做到这一点》来帮助我。

请记住,我在Windows计算机上使用python 3.7。 CDO或NCO不适用于Windows。数据属性在这里。

    Account[] accounts = AccountManager.get(getContext())
.getAccountsByType("com.google") 

如果有人可以帮助我,我将不胜感激。预先感谢。

1 个答案:

答案 0 :(得分:0)

我可以用一些随机数据提出这样的解决方案,在此我将数据从一种分辨率重新网格化到另一种分辨率。

#!/usr/bin/env ipython
# ---------------------
import numpy as np
from netCDF4 import Dataset,num2date,date2num
# -----------------------------
ntime,nlon,nlat=10,10,10;
lonin=np.linspace(0.,1.,10);
latin=np.linspace(0.,1.,10);
dataout=np.random.random((ntime,nlat,nlon));
unout='seconds since 2018-01-01 00:00:00'
# ---------------------
# make data:
ncout=Dataset('in.nc','w','NETCDF3_CLASSIC');
ncout.createDimension('lon',nlon);
ncout.createDimension('lat',nlat);
ncout.createDimension('time',None);
ncout.createVariable('lon','float32',('lon'));ncout.variables['lon'][:]=lonin;
ncout.createVariable('lat','float32',('lat'));ncout.variables['lat'][:]=latin;
ncout.createVariable('time','float64',('time'));ncout.variables['time'].setncattr('units',unout);ncout.variables['time'][:]=np.linspace(0,3600*ntime,ntime);
ncout.createVariable('randomdata','float32',('time','lat','lon'));ncout.variables['randomdata'][:]=dataout;
ncout.close()
# ----------------------
# regrid:
from scipy.interpolate import griddata
lonout=np.linspace(0.,1.,20);
latout=np.linspace(0.,1.,20);
ncout=Dataset('out.nc','w','NETCDF3_CLASSIC');
ncout.createDimension('lon',np.size(lonout));
ncout.createDimension('lat',np.size(latout));
ncout.createDimension('time',None);
ncout.createVariable('lon','float32',('lon'));ncout.variables['lon'][:]=lonout;
ncout.createVariable('lat','float32',('lat'));ncout.variables['lat'][:]=latout;
ncout.createVariable('time','float64',('time'));ncout.variables['time'].setncattr('units',unout);ncout.variables['time'][:]=np.linspace(0,3600*ntime,ntime);
ncout.createVariable('randomdata','float32',('time','lat','lon'));
ncin=Dataset('in.nc');
lonin=ncin.variables['lon'][:];latin=ncin.variables['lat'][:];
lonmin,latmin=np.meshgrid(lonin,latin);
lonmout,latmout=np.meshgrid(lonout,latout);
for itime in range(np.size(ncin.variables['time'][:])):
    zout=griddata((lonmin.flatten(),latmin.flatten()),ncin.variables['randomdata'][itime,:,:].flatten(),(lonmout,latmout),'linear');
    ncout.variables['randomdata'][itime,:]=zout;
ncin.close();ncout.close()