Changeset 2383 in lmdz_wrf
- Timestamp:
- Mar 7, 2019, 10:06:44 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r2382 r2383 30960 30960 newvar = onewnc.createVariable(compressvarn, 'i', (compressvarn)) 30961 30961 cfvarvals = gen.variables_values(compressvarn) 30962 basicvardef(newvar, cfvarvals[1], cfvarvals[4] , cfvarvals[5])30962 basicvardef(newvar, cfvarvals[1], cfvarvals[4].replace('|',' '), cfvarvals[5]) 30963 30963 newvar.setncattr('compress', compressS) 30964 30964 newvar.setncattr('criteria', criteriaS) … … 31064 31064 #compress_data(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,LU_INDEX') 31065 31065 31066 31067 def CFfile_creation(values, ncfile, variables): 31068 """ Operation to create a file folowing CF-conventions 31069 values= [dimensions]:[lonlatbox] 31070 [dimensions]: lon|[dsizelon],lat|[dsizelat],...,[dimnN]:[dsizeN], ',' pairs of variable 31071 name [dimn] and [size] 31072 if [dsize] = 'None' (for UNLIMITED), give a third value with the real size 31073 [lonlatbox]: ',' separated list of the extremes of the domain [SWlon],[SWlat], 31074 [NElon],[NElat] or 'global' for a global file [assuing lon/lat regular 31075 projection] 31076 ncfile= name of the file 31077 variables= ',' list of variables to create [varn]:[dims]:[kind]:[varaddattributes] 31078 and its characterisitcs 31079 [varn]: CF name of the variable (attributes will be retrived from 31080 'variables_values.dat') 31081 [dims]: ',' python sorted list of dimensions of the variable 31082 [kind]: type of variable (standard netCDF4/C-like values, 'c', 'i', 'f', 31083 'f8',...) 31084 [varaddattributes]: ',' list of additional attributes and their values for 31085 the variable [attrn]|[attrv] 31086 """ 31087 fname = 'CFfile_creation' 31088 31089 if values == 'h': 31090 print fname + '_____________________________________________________________' 31091 print CFfile_creation.__doc__ 31092 quit() 31093 31094 expectargs = '[dimensions]:[lonlatbox]' 31095 31096 gen.check_arguments(fname,values,expectargs,':') 31097 31098 dimensions = gen.stringS_dictvar(values.split(':')[0], ',', '|') 31099 lonlatbox = values.split(':')[1] 31100 31101 if not dimensions.has_key('lon') or not dimensions.has_key('lat'): 31102 print errormsg 31103 print ' ' + fname + ": function does not deal with CF-files without 'lon' "+\ 31104 "and 'lat' dimensions !!" 31105 print " no value provide through variable [dimensions]= 'lon|[dimx]," + \ 31106 "lat|[dimy]" 31107 quit(-1) 31108 31109 onewnc = NetCDFFile(ncfile, 'w') 31110 31111 dimx = dimensions['lon'] 31112 dimy = dimensions['lat'] 31113 31114 # Assuming regular lon/lat projection 31115 if lonlatbox == 'global': 31116 ddx = 360./(dimx-1) 31117 ddx2 = ddx/2. 31118 lons = np.arange(-180.+ddx2,180.+ddx2,ddx) 31119 ddy = 180./(dimy-1) 31120 ddy2 = ddy/2. 31121 lats = np.arange(-90.+ddy2,90.+ddy2,ddy) 31122 elif lonlatbox.count(',') == 3: 31123 [lonSW, latSW, lonNE, latNE] = str_listk(lonlatbox, ',', 'F') 31124 ddx = (lonNE-lonSW)/(dimx-1) 31125 ddx2 = ddx/2. 31126 lons = np.arange(lonSW-ddx2,lonNE+ddx2,ddx) 31127 ddy = (latNE-latSW)/(dimy-1) 31128 ddy2 = ddy/2. 31129 lats = np.arange(latSW-ddy2,latNE+ddy2,ddy) 31130 31131 # Sorting lon/lat_bounds 31132 lon_bnds = np.zeros((2,dimx), dtype=np.float64) 31133 lat_bnds = np.zeros((2,dimy), dtype=np.float64) 31134 for i in range(dimx): 31135 lon_bnds[0,i] = lons[i]-ddx2 31136 lon_bnds[1,i] = lons[i]+ddx2 31137 for j in range(dimy): 31138 lat_bnds[0,j] = lats[j]-ddy2 31139 lat_bnds[1,j] = lats[j]+ddy2 31140 31141 # Dimensions 31142 newdim = onewnc.createDimension('lon', dimx) 31143 newdim = onewnc.createDimension('lat', dimy) 31144 newdim = onewnc.createDimension('bnds', 2) 31145 31146 # Additional dimensions 31147 for dn in dimensions: 31148 if dn != 'lon' and dn != 'lat': 31149 if dn == 'time': dl = None 31150 else: dl = dimensions[dn] 31151 newdim = onewnc.createDimension(dn, None) 31152 onewnc.sync() 31153 31154 # Creation of variable dimensions 31155 newvar = onewnc.createVariable('lon', 'f4', ('lon')) 31156 newvar[:] = lons[:] 31157 cfvarvals = gen.variables_values('lon') 31158 basicvardef(newvar, cfvarvals[1], cfvarvals[4].replace('|',' '), cfvarvals[5]) 31159 newvar.setncattr('axis', 'X') 31160 newvar.setncattr('_CoordinateAxisType', 'Lon') 31161 31162 newvar = onewnc.createVariable('lat', 'f4', ('lat')) 31163 newvar[:] = lats[:] 31164 cfvarvals = gen.variables_values('lat') 31165 basicvardef(newvar, cfvarvals[1], cfvarvals[4].replace('|',' '), cfvarvals[5]) 31166 newvar.setncattr('axis', 'Y') 31167 newvar.setncattr('_CoordinateAxisType', 'Lat') 31168 31169 onewnc.sync() 31170 31171 varns = variables.split(',') 31172 for varn in varns: 31173 print varn 31174 31175 dnames = [] 31176 dsize = [] 31177 for dim in dimensions: 31178 # print " Adding: '" + dim + "' ..." 31179 dimn = dim.split(':')[0] 31180 dimv = dim.split(':')[1] 31181 if dimv == 'None': 31182 if len(dim.split(':')) != 3: 31183 print errormsg 31184 print ' ' + fname + ": dimension '" + dimn + "' is None but the " + \ 31185 'size is requried!!' 31186 quit(-1) 31187 else: 31188 dv = None 31189 dsize.append(int(dim.split(':')[2])) 31190 else: 31191 dv = int(dimv) 31192 dsize.append(dv) 31193 31194 dnames.append(dimn) 31195 31196 newdim = onc.createDimension(dimn, dv) 31197 31198 onc.sync() 31199 31200 # Variable 31201 if kind == 'c': 31202 newvar = onc.createVariable(varn, 'c', tuple(dnames)) 31203 # newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 31204 if kind == 'f' or kind == 'f4': 31205 newvar = onc.createVariable(varn, 'f4', tuple(dnames), \ 31206 fill_value=gen.fillValueF) 31207 newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 31208 elif kind == 'f8': 31209 newvar = onc.createVariable(varn, 'f8', tuple(dnames), \ 31210 fill_value= gen.fillValueD) 31211 newvar[:] = np.zeros(tuple(dsize), dtype=np.float64) 31212 elif kind == 'i': 31213 newvar = onc.createVariable(varn, 'i', tuple(dnames), \ 31214 fill_value=gen.fillValueI) 31215 newvar[:] = np.zeros(tuple(dsize), dtype=int) 31216 else: 31217 print errormsg 31218 print ' ' + fname + ": variable kind '" + kind + "' not ready!!" 31219 quit(-1) 31220 31221 sname = attributes.split('@')[0].replace('!', ' ') 31222 lname = attributes.split('@')[1].replace('!', ' ') 31223 u = attributes.split('@')[2].replace('!', ' ') 31224 31225 newattr = basicvardef(newvar, sname, lname, u) 31226 31227 onc.sync() 31228 31229 # Global attributes 31230 newattr = set_attribute(onc, 'description', "file creation using " + fname ) 31231 onc.sync() 31232 onc.close() 31233 31234 #file_creation('time_counter:12,sizes:24|time@time@seconds since 1949-12-01 00:00:00|f8', 'test.nc', 't_instant') 31235 31236 31237 31066 31238 #quit() 31067 31239
Note: See TracChangeset
for help on using the changeset viewer.