Changeset 738 in lmdz_wrf
- Timestamp:
- May 2, 2016, 7:35:35 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r737 r738 9298 9298 varn= ',' list of names of the variables ('all', for all variables) 9299 9299 """ 9300 import numpy.ma as ma 9300 9301 fname = 'sellonlatbox' 9302 9303 if values == 'h': 9304 print fname + '_____________________________________________________________' 9305 print sellonlatbox.__doc__ 9306 quit() 9307 9308 arguments = '[lonName],[latName],[lonSW],[latSW],[lonNE],[latNE]' 9309 check_arguments(fname,values,arguments,',') 9310 9311 ofile = 'sellonlatbox_' + varn + '.nc' 9312 9313 lonn = values.split(',')[0] 9314 latn = values.split(',')[1] 9315 9316 lonSW = np.float(values.split(',')[2]) 9317 latSW = np.float(values.split(',')[3]) 9318 lonNE = np.float(values.split(',')[4]) 9319 latNE = np.float(values.split(',')[5]) 9320 9321 objfile = NetCDFFile(ncfile, 'r') 9322 if not objfile.variables.has_key(lonn): 9323 print errormsg 9324 print ' ' + fname + ": file '" + ncfile + "' des not have longitude " + \ 9325 "variable '" + lonn + "' !!" 9326 quit(-1) 9327 if not objfile.variables.has_key(latn): 9328 print errormsg 9329 print ' ' + fname + ": file '" + ncfile + "' des not have latitude " + \ 9330 "variable '" + lonn + "' !!" 9331 quit(-1) 9332 9333 9334 lonobj = objfile.variables[lonn] 9335 latobj = objfile.variables[latn] 9336 9337 loninf = variable_inf(lonobj) 9338 latinf = variable_inf(latobj) 9339 9340 lonv = lonobj[:] 9341 latv = latobj[:] 9342 9343 if varn == 'all': 9344 varns = objfile.variables 9345 elif varn.find(',') != -1: 9346 varns = varn.split(',') 9347 else: 9348 varns = [varn] 9349 9350 # Lon/lat box 9351 nalon = lonv.min() 9352 nalat = latv.min() 9353 xalon = lonv.max() 9354 xalat = latv.max() 9355 9356 if lonSW < nalon: 9357 print errormsg 9358 print ' ' + fname + ': longitude SW corner:', lonSW,' too small!!' 9359 print ' min lon data:', nalon, 'increase SW corner longitude' 9360 quit(-1) 9361 if latSW < nalat: 9362 print errormsg 9363 print ' ' + fname + ': latitude SW corner:', latSW,' too small!!' 9364 print ' min lat data:', nalat, 'increase SW corner latitude' 9365 quit(-1) 9366 if lonNE > xalon: 9367 print errormsg 9368 print ' ' + fname + ': longitude NE corner:', lonNE,' too large!!' 9369 print ' max lon data:', xalon, 'decrease NE corner longitude' 9370 quit(-1) 9371 if latNE > xalat: 9372 print errormsg 9373 print ' ' + fname + ': latitude NE corner:', latNE,' too large!!' 9374 print ' max lat data:', xalat, 'decrease NE corner latitude' 9375 quit(-1) 9376 9377 malon = ma.masked_outside(lonv,lonSW,lonNE) 9378 malat = ma.masked_outside(latv,latSW,latNE) 9379 9380 if len(lonv.shape) == 1: 9381 dimx = lonv.shape[0] 9382 dimy = latv.shape[0] 9383 malonlat = np.zeros((dimy,dimx), dtype=lonv.dtype) 9384 for i in range(dimx): 9385 malonlat[:,i] = malon[i] + malat 9386 elif len(lonv.shape) == 2: 9387 dimx = lonv.shape[1] 9388 dimy = lonv.shape[0] 9389 malonlat = malon.mask + malat.mask 9390 malonv = ma.array(lonv, mask=malonlat) 9391 malatv = ma.array(latv, mask=malonlat) 9392 elif len(lonv.shape) == 3: 9393 print warnmsg 9394 print ' ' + fname + ': assuming that lon/lat is constant to the first dimension!!' 9395 print ' shape lon:', lonv.shape, 'lat:', latv.shape 9396 dimx = lonv.shape[2] 9397 dimy = lonv.shape[1] 9398 malonlat = np.zeros((dimy,dimx), dtype=bool) 9399 malonlat = malon.mask[0,:,:] + malat.mask[0,:,:] 9400 malonv = ma.array(lonv, mask=malon) 9401 malatv = ma.array(latv, mask=malon) 9402 else: 9403 print errormsg 9404 print ' ' + fname + ': matrix shapes not ready!!' 9405 print ' shape lon:',lonv.shape,'lat:',latv.shape 9406 quit(-1) 9407 9408 # Size of the new domain 9409 nlon = malon.min() 9410 nlat = malat.min() 9411 xlon = malon.max() 9412 xlat = malat.max() 9413 9414 print ' ' + fname + ': data box: SW', nlon, nlat,'NE:',xlon,xlat 9415 9416 ilon = dimx 9417 for j in range(dimy): 9418 for i in range(dimx): 9419 if malonlat[j,i] == False and i < ilon: 9420 ilon = i 9421 elon = 0 9422 for j in range(dimy): 9423 for i in range(dimx): 9424 if malonlat[j,i] == False and i > elon: 9425 elon = i 9426 9427 ilat = dimy 9428 for j in range(dimy): 9429 for i in range(dimx): 9430 if malonlat[j,i] == False and j < ilat: 9431 ilat = j 9432 elat = 0 9433 for j in range(dimy): 9434 for i in range(dimx): 9435 if malonlat[j,i] == False and j > elat: 9436 elat = j 9437 9438 newdimx = elon - ilon + 1 9439 newdimy = elat - ilat + 1 9440 print ' ' + fname + ': new lon size:', ilon, ',', elon, 'new dimx:', newdimx 9441 print ' ' + fname + ': new lat size:', ilat, ',', elat, 'new dimy:', newdimy 9442 9443 newncobj = NetCDFFile(ofile, 'w') 9444 9445 # Creation of lon,lat related dims 9446 newdim = newncobj.createDimension(lonn, newdimx) 9447 newdim = newncobj.createDimension(latn, newdimy) 9448 9449 for vn in varns: 9450 print 'Adding "' + vn + "' ..." 9451 if not objfile.variables.has_key(lonn): 9452 print errormsg 9453 print ' ' + fname + ": file '" + ncfile + "' des not have variable '" + \ 9454 vn + "' !!" 9455 quit(-1) 9456 if not searchInlist(objfile.variables, vn): 9457 print errormsg 9458 print ' ' + fname + ': variable name "' + vn + '" is not in file "' + \ 9459 ncfile + '" !!!!!' 9460 quit(-1) 9461 9462 varobj = objfile.variables[vn] 9463 varinf = variable_inf(varobj) 9464 9465 # Adding variable dimensions 9466 varslice = [] 9467 for dimn in varinf.dimns: 9468 if not searchInlist(newncobj.dimensions, dimn): 9469 newncobj.createDimension(dimn, len(objfile.dimensions[dimn])) 9470 if dimn == lonn: 9471 varslice.append(slice(ilon,elon+1)) 9472 elif dimn == latn: 9473 varslice.append(slice(ilat,elat+1)) 9474 else: 9475 varslice.append(slice(0,len(objfile.dimensions[dimn]))) 9476 9477 if varinf.FillValue is not None: 9478 newvar = newncobj.createVariable(vn, nctype(varinf.dtype), varinf.dimns) 9479 else: 9480 newvar = newncobj.createVariable(vn, nctype(varinf.dtype), varinf.dimns, \ 9481 fill_value = varinf.FillValue) 9482 newvar[:] = varobj[tuple(varslice)] 9483 9484 for atrn in varinf.attributes: 9485 if atrn != '_FillValue': 9486 attrv = varobj.getncattr(atrn) 9487 newattr = set_attributek(newvar, atrn, attrv, type(attrv)) 9488 9489 newncobj.sync() 9490 9491 for atrn in objfile.ncattrs(): 9492 attrv = objfile.getncattr(atrn) 9493 newattr = set_attributek(newncobj, atrn, attrv, type(attrv)) 9494 9495 # global attributes 9496 newncobj.setncattr('author', 'L. Fita') 9497 newncobj.setncattr('institution', 'Laboratire de Meteorologie Dynamique') 9498 newncobj.setncattr('university', 'Pierre Marie Curie - Jussieu') 9499 newncobj.setncattr('center', 'Centre National de Recherches Scientifiques') 9500 newncobj.setncattr('city', 'Paris') 9501 newncobj.setncattr('country', 'France') 9502 newncobj.setncattr('script', 'nc_var_tools.py') 9503 newncobj.setncattr('function', 'sellonlatbox') 9504 newncobj.setncattr('version', '1.0') 9505 9506 objfile.close() 9507 9508 newncobj.sync() 9509 newncobj.close() 9510 9511 print ' ' + fname + ': successful creation of file "' + ofile + '" !!!' 9512 9513 return 9514 9515 9516 def sellonlatboxold(values, ncfile, varn): 9517 """ Function to select a lotlan box from a data-set 9518 values= [lonName],[latName],[lonSW],[latSW],[lonNE],[latNE] 9519 [lonName]: name of the variable with the longitudes 9520 [latName]: name of the variable with the latitudes 9521 [lonSW],[latSW]: longitude and latitude of the SW vertex of the box 9522 [lonNE],[latNE]: longitude and latitude of the NE vertex of the box 9523 ncfile= netCDF file 9524 varn= ',' list of names of the variables ('all', for all variables) 9525 """ 9526 fname = 'sellonlatboxold' 9301 9527 9302 9528 if values == 'h': … … 18293 18519 18294 18520 if vartype == type('c'): 18521 ncs = 'c' 18522 elif vartype == '|S1': 18295 18523 ncs = 'c' 18296 18524 elif vartype == type(int(1)):
Note: See TracChangeset
for help on using the changeset viewer.