Changeset 1009 in lmdz_wrf
- Timestamp:
- Aug 11, 2016, 6:19:51 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1008 r1009 6 6 # Importing generic tools file 'generic_tools.py' 7 7 import generic_tools as gen 8 import subprocess as sub 8 9 9 10 ####### Contents: … … 16469 16470 refDateTime = values.split(':')[2] 16470 16471 Tunits = values.split(':')[3] 16471 16472 # CFing time 16473 WRF_CFtime_creation(refDateTime+','+Tunits, ncfile, 'time') 16474 16472 16475 16473 # CFing lon,lat 16476 16474 WRF_CFlonlat_creation('lon,lat', ncfile, wrfvl+','+wrfvL) … … 16484 16482 for dimn in CFdims.keys(): 16485 16483 if gen.searchInlist(ncdims,dimn): 16484 print fname + ' Lluis dimn:', dimn, 'CFdims:', CFdims[dimn] 16486 16485 # Making sure CF dimension is not in the file 16487 16486 if not gen.searchInlist(ncdims,CFdims[dimn]): 16488 16487 newname = ncf.renameDimension(dimn,CFdims[dimn]) 16488 16489 ncf.sync() 16490 ncf.close() 16491 16492 # CFing time 16493 WRF_CFtime_creation(refDateTime+','+Tunits, ncfile, 'time') 16494 16495 ncf = NetCDFFile(ncfile,'a') 16489 16496 16490 16497 # dimension variables which do not need to attach CF attributes, otherwise values … … 16541 16548 16542 16549 oldvars = onc.variables.keys() 16543 for oldv in oldvars: 16550 16551 notinfile = list(set(keepvars).difference(set(oldvars))) 16552 if len(notinfile) > 0: 16553 print warnmsg 16554 print ' ' + fname + ': variables to keep:', notinfile, "are not in file '"+ \ 16555 filen + "' !!" 16556 print ' variables to keep:', keepvars 16557 print ' variables in file:', oldvars 16558 16559 for oldv in keepvars: 16544 16560 newdims = newonc.dimensions 16545 16561 newvars = newonc.variables.keys() 16546 16562 16547 if not gen.searchInlist(newvars, oldv): 16563 if not gen.searchInlist(newvars, oldv) and gen.searchInlist(oldvars, oldv): 16564 print ' ' + fname + ": keeping variable '" + oldv + "' ..." 16548 16565 oldov = onc.variables[oldv] 16549 16566 oldvdim = oldov.dimensions 16550 vinf = ncvar.variable_inf(onc,oldv) 16551 16552 if not gen.searchInlist(newdims,olvdim): 16553 odim = onc.dimensions[olvdim] 16554 if odim.isunlimited: 16555 newdim = newonc.createDimension(None) 16556 else: 16557 newdim = newonc.createDimension(len(odim)) 16558 16559 newvar = newonc.createVariable(oldv, ncvar.nctype(vinf.dtype), vind.dimns) 16567 vinf = variable_inf(oldov) 16568 16569 for oldd in oldvdim: 16570 if not gen.searchInlist(newdims,oldd): 16571 odim = onc.dimensions[oldd] 16572 if odim.isunlimited(): 16573 newdim = newonc.createDimension(oldd, None) 16574 else: 16575 newdim = newonc.createDimension(oldd, len(odim)) 16576 16577 newvar = newonc.createVariable(oldv, nctype(vinf.dtype), vinf.dimns) 16560 16578 newvar[:] = oldov[:] 16561 16579 for attrn in vinf.attributes: 16562 16580 attrv = oldov.getncattr(attrn) 16563 newattr = ncvar.set_attribute(newvar,attrn,attrv)16581 newattr = set_attribute(newvar,attrn,attrv) 16564 16582 16565 16583 newonc.sync() … … 16568 16586 # Global attributes 16569 16587 for gattrn in onc.ncattrs(): 16570 gattrval = onc.getncattr(gattr )16571 newattr = ncvar.set_attribute(newonc, gattr, gattrval)16588 gattrval = onc.getncattr(gattrn) 16589 newattr = set_attribute(newonc, gattrn, gattrval) 16572 16590 16573 16591 newonc.sync() … … 16575 16593 onc.close() 16576 16594 16577 sout = sub.call('mv ' + filen + ' ' + ofile, shell=True) 16595 sout = sub.call('mv ' + ofile + ' ' + filen, shell=True) 16596 print fname + ": succesfully cleaned '" + filen + "' !!" 16578 16597 16579 16598 return 16580 16599 16581 16582 16600 #quit()
Note: See TracChangeset
for help on using the changeset viewer.