def save_Anomaly_as_npz(fy, ly, var, product_n = 3): a = subroutine.read_meta_data('var') formal_name = subroutine.celldata(a, 'var', var, 'formal_name') dim = subroutine.celldata(a, 'var', var, 'dim') _,title_name,_=subroutine.product_n_to_name(product_n) tmpyear = 2000 for month in range(1,13): _,strmonth=subroutine.strym(2000,month) Clim=subroutine.load_npz('Ave_of_' + formal_name + '_m'+strmonth+'_' + str(fy) + '-' + str(ly) + '_'+title_name) print 'month = ', month start = time.time() for year in range(fy,ly+1): stryear,strmonth=subroutine.strym(year,month) j=year-fy if dim == '3D': data=subroutine.get_data(year,month,var,0,title_name) elif dim == '2D': data=subroutine.get_data(year,month,var,1,title_name) else: raise Exception('your dim is not valid!') Anomaly=data-Clim DIR = subroutine.get_DIR(year, month, formal_name, product_n) subroutine.check_and_make_DIR(DIR) subroutine.save_npz(Anomaly,DIR+'Anomaly_from_Mean_Annual_Cycle_Year'+str(fy)+'-'+str(ly),data_dir_flg='NO') print 'elapsed time:', time.time() - start
def get_grid_value(var): import netCDF4 import subroutine a=subroutine.read_meta_data('var') fn=subroutine.celldata(a,'var',var,'WOA01_fn') vn=subroutine.celldata(a,'var',var,'WOA01_vn') ncfile=subroutine.dat_dir()+'WOA01/'+filen+'.nc' nc=netCDF4.Dataset(ncfile,'r') var_p=nc.variables['Z'][:] xgrid=nc.variables['X'][:] ygrid=nc.variables['Y'][:] nc.close() return [xgrid,ygrid,var_p]
def compare_40_Interim(month,var): import netCDF4 import numpy as np import subroutine import datetime as dt a=subroutine.read_meta_data('var') filename=subroutine.celldata(a,'var',var,'ORAS4_fn') ncfile40=subroutine.dat_dir()+'ORAS4/NetCDF/ERA-40_1989.nc' ncfileInterim=subroutine.dat_dir()+'ORAS4/NetCDF/ERA-Interim_1989.nc' nc40=netCDF4.Dataset(ncfile40,'r') ncInterim=netCDF4.Dataset(ncfileInterim,'r') if filename=='tp': # 1988年以前の降水量のデータは、convectiveとlargescaleの和で算出する data40=nc40.variables['lsp'][month-1,:,:]+nc40.variables['cp'][month-1,:,:] dataInterim=ncInterim.variables['lsp'][month-1,:,:]+ncInterim.variables['cp'][month-1,:,:] else: data40=nc40.variables[filename][month-1,:,:] dataInterim=ncInterim.variables[filename][month-1,:,:] # add_offset=nc.variables[filename].add_offset # scale_factor=nc.variables[filename].scale_factor # data=data*scale_factor+add_offset # print 'scale_factor=',scale_factor # print 'add_offset=',add_offset nc40.close() ncInterim.close() return data40[::-1,:],dataInterim[::-1,:]
def nc_read(year,month,var,depth): # 1地点につき1つのデータしかないものに関してはdepthに何を入れても構わない。 # 46層あるデータに関しては、depth=1とすると、5メートル水深でのデータが得られるようにする。 import netCDF4 import numpy as np import subroutine import scipy as sp a=subroutine.read_meta_data('var') filename=subroutine.celldata(a,'var',var,'ORAS4_fn') ncfile=subroutine.dat_dir()+'ORAS4/NetCDF/'+filename+'_oras4_1m_'+\ str(year)+'_grid_1x1.nc' # print ncfile nc=netCDF4.Dataset(ncfile,'r') if var=='ht': # 海面高度の時だけ、データが2次元になる。 data=nc.variables[filename][month-1,:,:] else: # それ以外のデータは3次元。 if depth != 0: data=nc.variables[filename][month-1,depth-1,:,:] else: data=np.ones((ny,nx,nz)) for k in range(0,nz): data[:,:,k]=nc.variables[filename][month-1,k,:,:] nc.close() data[np.where(data>=10000)]=sp.nan # 欠損値のところはnanに。 return data
def save_climatology_or_interannual_variation_as_npz(data_allmonth, fy, ly, var, Ave_or_Std, product_n = 3): a=subroutine.read_meta_data('var') formal_name = subroutine.celldata(a, 'var', var, 'formal_name') _, title_name, _ = subroutine.product_n_to_name(product_n) for month in range(1, 13): _, strmonth = subroutine.strym(2000, month) subroutine.save_npz(data_allmonth[:, :, :, month - 1], Ave_or_Std + '_of_' + \ formal_name + '_m' + strmonth + '_' + str(fy) + '-' + str(ly) + \ '_' + title_name)
def nc_read(month,var,depth): import numpy as np import netCDF4 import subroutine a=subroutine.read_meta_data('var') fn=subroutine.celldata(a,'var',var,'WOA01_fn') vn=subroutine.celldata(a,'var',var,'WOA01_vn') ncfile=subroutine.dat_dir()+'WOA01/'+filen+'.nc' nc=netCDF4.Dataset(ncfile,'r') if depth != 0: data=nc.variables[vn][month-1,depth-1,:,:] else: x,y,z=get_grid_value() nz,nx,ny=z.size,x.size,y.size data=np.ones((ny,nx,nz)) for k in range(0,nz): data_2D=nc.variables[vn][month-1,k,:,:] data[:,:,k]=np.reshape(data_2D,(ny,nx)) nc.close() return data
def get_data(year,month,var): import netCDF4 import numpy as np import subroutine import datetime as dt a=subroutine.read_meta_data('var') filename=subroutine.celldata(a,'var',var,'ERA_fn') if year <= 1967: ncfile=subroutine.dat_dir()+'ERA/NetCDF/ERA-40_1958-1967.nc' elif year >= 1968 and year <= 1977: ncfile=subroutine.dat_dir()+'ERA/NetCDF/ERA-40_1968-1977.nc' elif year >= 1978 and year<=1988: ncfile=subroutine.dat_dir()+'ERA/NetCDF/ERA-40_1978-1988.nc' else: ncfile=subroutine.dat_dir()+'ERA/NetCDF/ERA-Interim.nc' nc=netCDF4.Dataset(ncfile,'r') time=nc.variables['time'][:] units=nc.variables['time'].units dtime=netCDF4.num2date(time,units=units) for i in range(0,dtime.size): if getattr(dtime[i],'year')==year and getattr(dtime[i],'month')==month: if year <=1988 and filename=='tp': # 1988年以前の降水量のデータは、convectiveとlargescaleの和で算出する data=nc.variables['lsp'][i,:,:]+nc.variables['cp'][i,:,:] elif year<=1988 and filename=='sff': data=(nc.variables['e'][i,:,:]+nc.variables['ro'][i,:,:]+\ nc.variables['lsp'][i,:,:]+nc.variables['cp'][i,:,:])*2 # 何故か2倍することで丁度いい elif filename=='sff': # 淡水フラックス data=nc.variables['e'][i,:,:]+nc.variables['ro'][i,:,:]+\ nc.variables['tp'][i,:,:] elif filename=='q': # 熱フラックス data=nc.variables['sshf'][i,:,:]+nc.variables['slhf'][i,:,:]+\ nc.variables['ssr'][i,:,:]+nc.variables['str'][i,:,:] else: data=nc.variables[filename][i,:,:] # print i,dtime[i] # add_offset=nc.variables[filename].add_offset # scale_factor=nc.variables[filename].scale_factor # data=data*scale_factor+add_offset # print 'scale_factor=',scale_factor # print 'add_offset=',add_offset if filename=='sff': # 何故か100倍することでオーダーが合う data=data*100 break nc.close() return data[::-1,:]
def get_grid_value(var): import netCDF4 import subroutine import numpy as np a=subroutine.read_meta_data('var') fn=subroutine.celldata(a,'var',var,'RG_fn') ncfile=subroutine.dat_dir()+'Roemmich_Gilson/NetCDF/RG_ArgoClim_'+fn+'_2015.nc' nc=netCDF4.Dataset(ncfile,'r') zgrid=nc.variables['PRESSURE'][:] xgrid=nc.variables['LONGITUDE'][:] xgrid=np.r_[-360+xgrid[340:],xgrid[:340]] ygrid=nc.variables['LATITUDE'][:] nc.close() return [xgrid,ygrid,zgrid]
def nc_read(year,month,var,depth): import numpy as np import scipy as sp import netCDF4 import subroutine Threshold=-999 a=subroutine.read_meta_data('var') fn=subroutine.celldata(a,'var',var,'RG_fn') ncfile=subroutine.dat_dir()+'Roemmich_Gilson/NetCDF/RG_ArgoClim_'+fn+'_2015.nc' nc=netCDF4.Dataset(ncfile,'r') vn1=subroutine.celldata(a,'var',var,'RG_vn1') vn2=subroutine.celldata(a,'var',var,'RG_vn2') time=(year-2004)*12+month-1 # 2004年1月であれば、time=0、2014年12月であれば、time=131 if depth != 0: meandata=nc.variables[vn1][depth-1,:,:] meandata[np.where(meandata<=Threshold)]=sp.nan # 欠損値のところはnanに。 anomdata=nc.variables[vn2][time,depth-1,:,:] anomdata[np.where(anomdata<=Threshold)]=sp.nan # 欠損値のところはnanに。 data=meandata+anomdata data=np.c_[data[:,340:],data[:,:340]] else: data=np.ones((ny,nx,nz)) meandata=nc.variables[vn1][:,:,:] meandata[np.where(meandata<=Threshold)]=sp.nan # 欠損値のところはnanに。 anomdata=nc.variables[vn2][time,:,:,:] anomdata[np.where(anomdata<=Threshold)]=sp.nan # 欠損値のところはnanに。 ndata=meandata+anomdata ndata=np.c_[ndata[:,:,340:],ndata[:,:,:340]] for k in range(0,nz): data[:,:,k]=ndata[k,:,:] nc.close() return data
def nc_read(year,month,var,depth): import numpy as np import scipy as sp import netCDF4 import subroutine a=subroutine.read_meta_data('var') vn=subroutine.celldata(a,'var',var,'MOAA_GPV_vn') stryear,strmonth=subroutine.strym(year,month) ncfile=subroutine.dat_dir()+'MOAA_GPV/NetCDF/TS_'+stryear+strmonth+'_GLB.nc' nc=netCDF4.Dataset(ncfile,'r') if depth != 0: data=nc.variables[vn][depth-1,:,:] else: data=np.ones((ny,nx,nz)) for k in range(0,nz): data[:,:,k]=nc.variables[vn][k,:,:] nc.close() data[np.where(data>=10000)]=sp.nan # 欠損値のところはnanに。 return data
def make_climatology_or_interannual_variation_of_all_month_and_depth(fy, ly, var, Ave_or_Std, product_n = 3): xgrid, ygrid, zgrid = subroutine.product_grid_info(var, 'data', product_n) a=subroutine.read_meta_data('var') d=subroutine.celldata(a,'var',var,'dim') if d == '2D': data_allmonth = np.zeros((ygrid.size, xgrid.size, 12)) depth_d = 1 elif d == '3D': data_allmonth = np.zeros((ygrid.size, xgrid.size, zgrid.size, 12)) depth_d = 0 else: raise Exception('data dimension is not valid!') for month in range(1, 13): data = make_climatology_or_interannual_variation_of_a_month(fy, ly, month, var, depth_d, Ave_or_Std, product_n) if data.ndim == 3: data_allmonth[:, :, :, month - 1] = data[:, :, :] elif data.ndim == 2: data_allmonth[:, :, month - 1] = data[:, :] return data_allmonth
def get_grid_value(var): import netCDF4 import subroutine import numpy as np if var=='w': filename='thetao' else: a=subroutine.read_meta_data('var') filename=subroutine.celldata(a,'var',var,'ORAS4_fn') ncfile=subroutine.dat_dir()+'ORAS4/NetCDF/'+filename+'_oras4_1m_1958_grid_1x1.nc' nc=netCDF4.Dataset(ncfile,'r') var_p=nc.variables['depth'][:] xgrid=nc.variables['lon'][:] ygrid=nc.variables['lat'][:] nc.close() if var=='w': var_h=np.zeros(var_p.size) for i in range(var_p.size): var_h[i]=2*var_p[i] if i==0 else 2*var_p[i]-var_h[i-1] var_p=var_h return [xgrid,ygrid,var_p]
def load_Anomaly_of_npz(year, month, var, fy = 1990, ly = 2011, product_n = 3): a = subroutine.read_meta_data('var') formal_name = subroutine.celldata(a, 'var', var, 'formal_name') DIR = subroutine.get_DIR(year, month, formal_name, product_n) data = subroutine.load_npz(DIR + 'Anomaly_from_Mean_Annual_Cycle_Year' + str(fy) + '-' + str(ly), data_dir_flg = 'NO') return data