Changeset 1782 in lmdz_wrf for trunk/tools/ORforcing_reconstruct.py
- Timestamp:
- Feb 26, 2018, 6:33:54 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/ORforcing_reconstruct.py
r1750 r1782 15 15 16 16 parser = OptionParser() 17 parser.add_option("-F", "--filename", dest="fn", help="name of files (overwrites -f option)", \ 18 metavar="VALUE") 17 19 parser.add_option("-f", "--fileHEader", dest="fh", help="header of files", \ 18 20 metavar="VALUE") 21 parser.add_option("-i", "--indices", dest="indn", help="name of the variable indices", \ 22 metavar="VALUE") 19 23 parser.add_option("-L", "--latitude", dest="latn", help="name of the variable latitiude", \ 20 24 metavar="VALUE") 21 25 parser.add_option("-l", "--longitude", dest="lonn", help="name of the variable longitiude", \ 22 26 metavar="VALUE") 27 parser.add_option("-t", "--TransposeVariables", dest="tvars", help="whether variables should be trasposed", \ 28 metavar="VALUE") 23 29 parser.add_option("-v", "--Variables", dest="varns", help="',' separated list of variables", \ 24 30 metavar="VALUE") … … 30 36 ####### ###### ##### #### ### ## # 31 37 32 filen = opts.fh + opts.year + '.nc' 38 if opts.fn is None: 39 filen = opts.fh + opts.year + '.nc' 40 else: 41 filen = opts.fn 33 42 34 43 # Variable whcih provides the indices of a 1D vector from the dimy, dimx space 35 indvar = 'land'44 indvar = opts.indn 36 45 37 46 # 2D longitude, latitude matrices … … 41 50 # Range to retrieve 42 51 Xmin=-90.25 43 #Xmin='all'52 Xmin='all' 44 53 Xmax=-33.25 45 54 Ymin=-67.25 … … 91 100 latv = olat[:] 92 101 93 vecdimn = oind.dimensions[0]94 102 if len(olon.dimensions) == 2: 103 dimx = lonv.shape[1] 104 dimy = lonv.shape[0] 105 veclon = lonv.reshape(dimx*dimy) 106 veclat = latv.reshape(dimx*dimy) 95 107 Xdimn = olon.dimensions[1] 96 108 Ydimn = olon.dimensions[0] 97 dimx = lonv.shape[1] 98 dimy = lonv.shape[0] 99 elif len(olon.dimensions) == 1: 100 Xdimn = olon.dimensions[0] 101 Ydimn = olat.dimensions[0] 102 dimx = lonv.shape[0] 103 dimy = latv.shape[0] 104 lonv, latv = np.meshgrid(lonv, latv) 105 106 # Fortran indices, first 1 107 indv = indv - 1 108 109 veclon = np.zeros((len(indv)), dtype=np.float) 110 veclat = np.zeros((len(indv)), dtype=np.float) 111 112 # Construct veclon, veclat 113 for iid in range(len(indv)): 114 iy = int((indv[iid]-1)/dimx) 115 ix = int(indv[iid] - iy*dimx - 1) 116 veclon[iid] = lonv[iy,ix] 117 veclat[iid] = latv[iy,ix] 109 else: 110 veclon = lonv.copy() 111 veclat = latv.copy() 112 Xdimn = 'x' 113 Ydimn = 'y' 114 115 vecdimn = oind.dimensions[0] 118 116 119 117 if not gen.searchInlist(availProj, matProj): … … 157 155 dimy = int((Ymax - Ymin+resY)/resY) 158 156 159 matindt, matXt, matYt = Sci.module_scientific.reconstruct_matrix(vectorxpos=veclon, \ 160 vectorypos=veclat, dvec=veclon.shape[0], xmin=Xmin, xmax=Xmax, ymin=Ymin,ymax=Ymax,\ 161 dmatx=dimx, dmaty=dimy, matproj=matProj, maxdiff=maxdiff) 157 print 'Xmin:', Xmin, 'Xmax:', Xmax, 'Ymin:', Ymin, 'Ymax:', Ymax, 'maxdiff:', maxdiff 158 159 matindt, matXt, matYt, matdifft = Sci.module_scientific.reconstruct_matrix( \ 160 vectorxpos=veclon, vectorypos=veclat, dvec=veclon.shape[0], xmin=Xmin, xmax=Xmax, \ 161 ymin=Ymin,ymax=Ymax, dmatx=dimx, dmaty=dimy, matproj=matProj, maxdiff=maxdiff) 162 162 163 163 matind = matindt.transpose() 164 Nfound = np.sum(matind != -1) 165 Nstations = veclon.shape[0] 166 print ' Nfound:', Nfound, ' number of stations:', Nstations 167 168 if Nfound*1. / Nstations < 0.8: 169 print gen.errormsg 170 print ' '+main + ': only ', '{:.2f}'.format(Nfound*100./Nstations),\ 171 '% of points ' + 'have been found !!' 172 print ' this is not enough. Something must went wrong!' 173 print ' Longitudes Latitudes _______' 174 for i in range(veclon.shape[0]): 175 print ' ', veclon[i], veclat[i] 176 dx2 = dimx/2 177 dy2 = dimy/2 178 print ' dx2, dy2 -/+ 5 fraction of destiny longitudes _______' 179 for j in range(-5,5): 180 print matXt[dx2-5:dx2+5,dy2+j] 181 print ' dx2, dy2 -/+ 5 fraction of destiny latitudes _______' 182 for j in range(-5,5): 183 print matYt[dx2-5:dx2+5,dy2+j] 184 print ' dx2, dy2 -/+ 5 fraction of indices equivalency _______' 185 for j in range(-5,5): 186 print matindt[dx2-5:dx2+5,dy2+j] 187 print ' min distance lon(dy2,dx2)=', matXt[dx2,dy2], ':', \ 188 np.min(veclon - matXt[dx2,dy2]) 189 print ' min distance lat(dy2,dx2)=', matYt[dx2,dy2], ':', \ 190 np.min(veclat - matYt[dx2,dy2]) 191 print ' longitude borders:', Xmin, Xmax, 'dX:', resX 192 print ' latitude borders:', Ymin, Ymax, 'dY:', resY 193 #quit(-1) 194 164 195 # Fortran like, First 1 165 196 matind = np.where(matind != -1, matind - 1, matind) … … 189 220 ncvar.set_attribute(newvar, 'coordinates', 'lon lat') 190 221 222 # Variable differences 223 newvar = onewnc.createVariable('vec1D_matdiff', 'i', ('y', 'x'), fill_value=-1) 224 newvar[:] = matdifft.transpose() 225 ncvar.basicvardef(newvar,'vec1D_matdiff', 'matrix differences respect 1D ', 'degrees') 226 ncvar.set_attribute(newvar, 'coordinates', 'lon lat') 227 191 228 # Looking for equivalencies in the 1D vector 192 229 matlonlat = matind.copy() … … 207 244 if not onewnc.variables.has_key(vn): 208 245 ovar = onc.variables[vn] 209 indn = ovar.dimensions 246 if gen.Str_Bool(opts.tvars): 247 indn0 = ovar.dimensions 248 indn = list(indn0)[::-1] 249 else: 250 indn = ovar.dimensions 210 251 vardims = [] 211 252 shapevar = [] … … 252 293 quit(-1) 253 294 254 print ' reconstructing:', vn, ' ...'295 print ' reconstructing:', vn, ' shape:', newvar.shape, '...' 255 296 # Filling variable. It would be faster if we can avoid this loop... I'm feeling lazy! 256 297 if not gen.searchInlist(vardims,'x') and not gen.searchInlist(vardims,'y'): 257 newvar[:] = ovar[:] 298 if gen.Str_Bool(opts.tvars): 299 newvar[:] = ovar[:].transpose() 300 else: 301 newvar[:] = ovar[:] 258 302 else: 259 ovart = ovar[:].transpose() 303 if gen.Str_Bool(opts.tvars): 304 ovart = ovar[:] 305 else: 306 ovart = ovar[:].transpose() 307 print ' Lluis shapes ovart:', ovart.shape, 'newvar:', newvar.shape 260 308 if newvar.dtype == type(float(2.)) or newvar.dtype == type(np.float(2.)) \ 261 309 or newvar.dtype == type(np.float32(2)) or \ … … 280 328 # Global attributes 281 329 for atn in onc.ncattrs(): 282 atv = o var.getncattr(atn)330 atv = onc.getncattr(atn) 283 331 ncvar.set_attribute(onewnc, atn, atv) 284 332 onewnc.sync() … … 287 335 288 336 # Reconstructing times 289 otime = onewnc.variables['time']290 ncvar.set_attribute(otime, 'units', 'seconds since ' + opts.year + '-01-01 00:00:00')337 #otime = onewnc.variables['time'] 338 #ncvar.set_attribute(otime, 'units', 'seconds since ' + opts.year + '-01-01 00:00:00') 291 339 292 340 onewnc.sync()
Note: See TracChangeset
for help on using the changeset viewer.