- Timestamp:
- Aug 30, 2016, 3:46:56 PM (9 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r1053 r1062 17 17 ## e.g. # nc_var.py -o cleaning_varsfile -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S T2,XLONG,XLAT,Times 18 18 ## e.g. # nc_var.py -o LMDZ_toCF -f LMDZ/AR40/vas_histins_1-1.nc 19 ## e.g. # nc_var.py -o CDO_toCF -f LMDZ/AR40/hfss_reproj-histins.nc 19 20 ## e.g. # nc_var.py -o pinterp -f wrfout_d01_2001-11-11_00\:00\:00 -S 100000.:97500.:95000.:92500.:90000.:85000.:80000.:75000.:70000.:65000.:60000.:55000.:50000.:45000.:40000.:35000.:30000.:25000.:20000.:15000.:10000.:5000.:2500.:1000.:500.:250.,1,0 -v WRFt,WRFrh 20 21 ## e.g. # nc_var.py -o pinterp -f /media/ExtDiskC_ext4/DATA/etudes/WRF_LMDZ/WaquaL_highres/short_copies/LMDZ/AR40/histins_19790101000000-19790304000000_short.nc -S 100000.:97500.:95000.:92500.:90000.:85000.:80000.:75000.:70000.:65000.:60000.:55000.:50000.:45000.:40000.:35000.:30000.:25000.:20000.:15000.:10000.:5000.:2500.:1000.:500.:250.,1,0 -v temp,ovap … … 29 30 import generic_tools as gen 30 31 31 operations=['addvals', ' chdimname', 'changevartype', 'checkallvars',\32 operations=['addvals', 'CDO_toCF', 'chdimname', 'changevartype', 'checkallvars', \ 32 33 'checkAllValues', 'checkNaNs', \ 33 34 'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum', \ … … 121 122 if oper == 'addvals': 122 123 ncvar.addvals(opts.values, opts.ncfile, opts.varname) 124 elif oper == 'CDO_toCF': 125 ncvar.CDO_toCF(opts.ncfile) 123 126 elif oper == 'chdimname': 124 127 ncvar.chdimname(opts.values, opts.ncfile) -
trunk/tools/nc_var_tools.py
r1055 r1062 1419 1419 1420 1420 ncf = NetCDFFile(ncfile,'a') 1421 ncvars = ncf.variables 1421 ncvars = ncf.variables.keys() 1422 1422 ncf.close() 1423 1423 … … 1425 1425 print ' ' + fname + ": File '" + ncfile + "' does not have variable: '" + \ 1426 1426 var + "' !!" 1427 ncf.close()1427 print ' variables:', ncvars 1428 1428 quit(-1) 1429 1429 … … 16760 16760 return 16761 16761 16762 def CDO_toCF(ncfile): 16763 """ Function to pass a CDO output file to CF-conventions 16764 ncfile= file to transform 16765 variables= variables to transform 16766 """ 16767 fname = 'CDO_toCF' 16768 16769 if ncfile == 'h': 16770 print fname + '_____________________________________________________________' 16771 print CDO_toCF.__doc__ 16772 quit() 16773 16774 ncf = NetCDFFile(ncfile,'a') 16775 ncdims = ncf.dimensions.keys() 16776 ncvars = ncf.variables.keys() 16777 16778 # renaming variables 'lon, lat' 16779 CFvardims = {'lon': 'lon2', 'lat': 'lat2'} 16780 for dimvarn in CFvardims.keys(): 16781 if gen.searchInlist(ncvars,dimvarn): 16782 newn = CFvardims[dimvarn] 16783 print warnmsg 16784 print fname + ": renaming variable '" + dimvarn + "' as '" + newn + "' !!" 16785 newname = ncf.renameVariable(dimvarn,newn) 16786 ncf.sync() 16787 16788 # CFing dimensions 16789 CFdims = {'x': 'lon', 'y': 'lat'} 16790 for dimn in CFdims.keys(): 16791 if gen.searchInlist(ncdims,dimn): 16792 # Making sure CF dimension is not in the file 16793 if not gen.searchInlist(ncdims,CFdims[dimn]): 16794 newname = ncf.renameDimension(dimn,CFdims[dimn]) 16795 ncf.sync() 16796 16797 ncvars = ncf.variables.keys() 16798 # CFing variables 'lon, lat' 16799 olon2 = ncf.variables['lon2'] 16800 dimslon2 = olon2.dimensions 16801 newvar = ncf.createVariable('lon','f8',dimslon2) 16802 newvar[:] = olon2[:] 16803 olonattr = olon2.ncattrs() 16804 for attrn in olonattr: 16805 attrv = olon2.getncattr(attrn) 16806 set_attribute(newvar,attrn,attrv) 16807 16808 olat2 = ncf.variables['lat2'] 16809 dimslat2 = olat2.dimensions 16810 newvar = ncf.createVariable('lat','f8',dimslat2) 16811 newvar[:] = olat2[:] 16812 olatattr = olat2.ncattrs() 16813 for attrn in olatattr: 16814 attrv = olat2.getncattr(attrn) 16815 set_attribute(newvar,attrn,attrv) 16816 16817 ncvars = ncf.variables.keys() 16818 # Re-arranging coordinates attribute for all the variables 16819 for varn in ncvars: 16820 varobj = ncf.variables[varn] 16821 varattrs = varobj.ncattrs() 16822 if gen.searchInlist(varattrs, unicode('coordinates')): 16823 attrv = varobj.getncattr('coordinates') 16824 newcoord = str(attrv) 16825 for dimn in CFdims.keys(): 16826 newcoord = newcoord.replace(dimn, CFdims[dimn]) 16827 newattr = set_attribute(varobj, 'coordinates', newcoord) 16828 16829 ncf.sync() 16830 16831 # We should removing spurious lon2, lat2, but this would make too much... 16832 16833 ncf.close() 16834 16835 print fname + ": succesfull CFication of CDO file '" + ncfile + "' !!" 16836 16837 return 16838 16762 16839 def cleaning_varsfile(values, filen): 16763 16840 """ Function to keep a list of varibales from a file
Note: See TracChangeset
for help on using the changeset viewer.