def Regrid2deg(d): """ Regrid to 2deg (180lon*90lat) horizontal resolution Input - d: cdms variable Output - drg: cdms variable with 2deg horizontal resolution """ # Regridding tgrid = cdms.createUniformGrid(-89, 90, 2.0, 0, 180, 2.0, order='yx') orig_grid = d.getGrid() regridFunc = Regridder(orig_grid, tgrid) drg = MV.zeros((d.shape[0], tgrid.shape[0], tgrid.shape[1]), MV.float) for it in range(d.shape[0]): drg[it] = regridFunc(d[it]) # Dimension information time = d.getAxis(0) lat = tgrid.getAxis(0) lon = tgrid.getAxis(1) drg.setAxisList((time, lat, lon)) # Missing value (In case, missing value is changed after regridding) drg[drg >= d.missing_value] = d.missing_value mask = np.array(drg == d.missing_value) drg.mask = mask print('Complete regridding from', d.shape, 'to', drg.shape) return drg
def regrid (self, togrid, missing=None, order=None, mask=None): """return self regridded to the new grid. Keyword arguments are as for regrid.Regridder.""" from regrid2 import Regridder if togrid is None: return self else: fromgrid = self.getGrid() regridf = Regridder(fromgrid, togrid) result = regridf(self, missing=missing, order=order, mask=mask) return result
print 'Test 8: Regridding ...', ## lat = cdms2.createGaussianAxis(32) ## lon = cdms2.createUniformLongitudeAxis(0.0,64,360.0/64.) ## outgrid = cdms2.createRectGrid(lat,lon,'yx','gaussian') outgrid = cdms2.createGaussianGrid(32) f = cdms2.openDataset(os.path.join(get_sample_data_dir(), 'readonly.nc')) u = f.variables['u'] ingrid = u.getGrid() try: sh = ingrid.shape except: markError('Grid shape') regridf = Regridder(ingrid, outgrid) newu = regridf(u) if (abs(newu[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid', newu[0, 0, -1]) newu = u.regrid(outgrid) if (abs(newu[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid', newu[0, 0, -1]) # Regrid TV tv = u.subSlice(0) newtv = regridf(tv) if (abs(newtv[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid tv', newtv[0, 0, -1]) newtv = tv.regrid(outgrid) if (abs(newtv[0, 0, -1] - 488.4763488) > 1.e-3):
ver = 'v20200421' dir = '/p/user_pub/PCMDIobs/PCMDIobs2_clims/atmos/' + var + '/' + data + '/' nc = var + '_mon_' + data + '_BE_gn_' + period + '.' + ver + '.AC.nc' f = cdms.open(dir + nc) d = f[var] ogrid = d.getGrid() print(ogrid) # Target grid data dir = '/p/user_pub/PCMDIobs/PCMDIobs2_clims/atmos/pr/GPCP-2-3/' nc = 'pr_mon_GPCP-2-3_BE_gn_197901-201907.v20200421.AC.nc' ft = cdms.open(dir + nc) dt = ft['pr'] tgrid = dt.getGrid() print(tgrid) # Regridding regridFunc = Regridder(ogrid, tgrid) dr = regridFunc(d) # Write data out = cdms.open( var + '_mon_' + data + '_BE_gn_' + period + '.' + ver + '.AC_regrid.144x72.nc', 'w') out.write(dr) out.close() # In[2]: get_ipython().system('jupyter nbconvert --to script regridding.ipynb')
data2 = f2('tas', time=('1991-1-1', '1993-12-1')) print data1.shape # (48, 160, 320) print data2.shape # (48, 73, 144) grid1 = data1.getGrid() print grid1 print 'original ERA40 data shape: ', data1.shape # original ERA40 data shape: (48, 160, 320) grid2 = data2.getGrid() print grid2 regridfunc = Regridder(grid1, grid2) data1 = regridfunc(data1) print 'new ERA40 data shape: ', data1.shape cdutil.setTimeBoundsMonthly(data1) cdutil.setTimeBoundsMonthly(data2) start_time = cdtime.comptime(1991, 1, 1) end_time = cdtime.comptime(1993, 12, 1) ac1 = cdutil.ANNUALCYCLE.climatology(data1(time=(start_time, end_time, 'cob'))) ac2 = cdutil.ANNUALCYCLE.climatology(data2(time=(start_time, end_time, 'cob'))) print ac1 data1 = cdutil.ANNUALCYCLE.departures(data1, ref=ac1) data2 = cdutil.ANNUALCYCLE.departures(data2, ref=ac2)
sst = b('sst') b.close() print sst.shape x.clear() x.plot(sst) y = vcs.init() y.setcolormap('default') y.plot(tas) # get grid for regridding grid2 = sst.getGrid() # setup a regridding function (as: fromgrid, togrid) regridfunc = Regridder(grid2, grid1) # create new data with 'togrid' (5-deg) resolution by passing # the data with 'fromgrid' resolution into the funstion above sst_new = regridfunc(sst) tas_new = regridfunc(tas) x.clear() x.plot(sst_new) y.setcolormap('default') y.plot(tas_new) # extract a land/sea mask and regrid it to our desired 5 degree grid # (these data are percent land coverage [0-100]) file4 = os.path.join(sys.prefix, 'sample_data/geo.1deg.ctl') c = cdms2.open(file4)
def _oisst_daily(daily_sst_dir, daily_sst_filename, targ_grid_res): #--------------------------------------------------------------------- """ See file header. """ print('targ_grid_res: ', targ_grid_res) time_span_tag = daily_sst_filename.split('.')[2] time_units = 'days since ' + time_span_tag.split('-')[0] + '-1-1 00:00:00' year_start = int(time_span_tag.split('-')[0]) daily_ice_filename = daily_sst_filename.replace('sst', 'icec') # Create target grid. # For a ONEXONE target grid resolution with arguments: # (-90., 181, 1., 0., 360, 1.) # A grid will be created with: # latitude starting at -90 & going north 181 points, # with an increment of 1 degree; # longitude starting at 0E & going east 360 points, # with an increment of 1 degree. # The out_filename will reflect the designated resolution. #--------------------------------------------------------- args = TARG_GRID_RES_DICT[targ_grid_res]['args'] targ_grid = cdms2.createUniformGrid(args[0], args[1], args[2], args[3], args[4], args[5]) label = TARG_GRID_RES_DICT[targ_grid_res]['label'] out_filename = 'sst_daily_cdcunits' + label + time_span_tag + '.nc' fdaily_sst = cdms2.open(daily_sst_dir + '/' + daily_sst_filename) fdaily_ice = cdms2.open(daily_sst_dir + '/' + daily_ice_filename) input_grid = fdaily_sst.variables['sst'].getGrid() rg_in2targ = Regridder(input_grid, targ_grid) # Create file and variables for output. #-------------------------------------- #fout = NetCDF.NetCDFFile(out_filename, 'w') fout = Cdunif.CdunifFile(out_filename, 'w') lons = targ_grid.getLongitude()[:] lats = targ_grid.getLatitude()[:] fout.createDimension('lon', len(lons)) fout.createDimension('lat', len(lats)) fout.createDimension('time', None) sst_cpl = fout.createVariable('sst', 'f', ('time', 'lat', 'lon')) sst_cpl.long_name = 'sea surface temperature' sst_cpl.units = 'degrees_C' ifrac = fout.createVariable('ifrac', 'f', ('time', 'lat', 'lon')) ifrac.long_name = 'ice fraction' ifrac.units = 'fraction' lat = fout.createVariable('lat', 'd', ('lat', )) lat.long_name = 'latitude of grid cell center' lat.units = 'degrees_north' lon = fout.createVariable('lon', 'd', ('lon', )) lon.long_name = 'longitude of grid cell center' lon.units = 'degrees_east' time = fout.createVariable('time', 'd', ('time', )) time.long_name = 'time' time.units = time_units time.calendar = 'noleap' date = fout.createVariable('date', 'i', ('time', )) date.long_name = 'calendar date (YYYYMMDD)' datesec = fout.createVariable('datesec', 'i', ('time', )) datesec.long_name = 'seconds elapsed on calendar date' datesec.units = 'seconds' # Coordinate data. #----------------- lat[:] = lats lat.long_name = 'latitude' lat.units = 'degrees_north' lon[:] = lons lon.long_name = 'longitude' lon.units = 'degrees_east' sst_w = fdaily_sst.variables['sst'] ntimes = sst_w.shape[0] intime = sst_w.getTime() intime_units = intime.units intimes = intime[:] # Time loop. #----------- time_idx_out = -1 for time_idx in range(ntimes - 1): # Data is centered on time in file. #---------------------------------- mid_intime = intimes[time_idx] rtime = cdtime.reltime(mid_intime, intime_units) ctime = rtime.tocomp() new_reltime = ctime.torel(time_units, cdtime.NoLeapCalendar) new_ctime = new_reltime.tocomp() year = ctime.year if year < year_start: #======= continue #======= month = ctime.month day = ctime.day hour = ctime.hour minute = ctime.minute second = ctime.second # Change time units. #------------------- print ('time_idx_out, ctime, new_ctime: ', \ time_idx_out, ctime, new_ctime) time[time_idx_out] = new_reltime.value print('time[time_idx_out]: ', time[time_idx_out]) date[time_idx_out] = (year * 10000) + (month * 100) + day datesec[time_idx_out] = (hour * 60 * 60) + (minute * 60) + second data = fdaily_sst('sst', time=slice(time_idx, (time_idx + 1)), raw=1, squeeze=1) data_f = fill_msg(data, nscan=200) data_f = n.array(data_f, n.float32) print ('data_f min,max,mean: ', \ data_f.min(), data_f.max(), data_f.mean() ) data_f = rg_in2targ(data_f).filled() out_sst = data_f print ('out_sst min,max,mean: ', \ out_sst.min(), out_sst.max(), out_sst.mean() ) data = fdaily_ice('icec', time=slice(time_idx, (time_idx + 1)), raw=1, squeeze=1) data_f = data * 1.0 print ('data_f min,max,mean: ', \ data_f.min(), data_f.max(), data_f.mean() ) # Set ice to zero where missing - over land. #------------------------------------------- data_f = rg_in2targ(data_f).filled(0.0) out_ice = data_f print ('out_ice min,max,mean: ', \ out_ice.min(), out_ice.max(), out_ice.mean() ) sst_cpl[time_idx_out, :, :] = out_sst ifrac[time_idx_out, :, :] = out_ice time_idx_out = time_idx_out + 1 fout.sync() fout.close() return
var = newfile.split('/')[7] rep = '/' newdir = rep.join(newdir_tmp) #print(newdir) try: os.mkdir(newdir + '/' + target) except: print('cant make dir ' + newdir + '/' + target) try: os.mkdir(newdir + '/' + target + '/' + ver_out + '/') except: pass fc = cdms2.open(l) d = fc(var) orig_grid = d.getGrid() regridFunc = Regridder(orig_grid, tgrid) dn = regridFunc(d) dn.id = var g = cdms2.open(newfile, 'w+') g.write(dn) g.close() #print(newdir) print('done with ', newfile)