Changeset 1360 in lmdz_wrf
- Timestamp:
- Dec 2, 2016, 3:32:12 PM (9 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r1311 r1360 33 33 ## e.g. # nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@add|wrfout_d01_2001-11-11_00:00:00|T2%west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@subc,273.15|wrfout_d01_2001-11-11_00:00:00|None' -v 'tempC,air!temperature,C' 34 34 ## e.g. # nc_var.py -o compute_opersvarsfiles -S 'lon|lon|-1;lat|lat|-1;time_counter|time_counter|-1@forwrdderiv,1,1,2|/ccc/store/cont003/gen7593/fitaborl/etudes/DynamicoESM/aquaplanet/AR40/19800101000000-19810101000000/histday.nc|t2m' -v 'tasderiv,x-derivative|of|air|temperature,K 35 35 ## e.g. # nc_var.py -o getvars_tofile -S soils_param_Zobler_textXIOS.nc -f soils_param_colorXIOS.nc -v 'soiltext,soiltexttypes' 36 36 37 37 from optparse import OptionParser … … 83 83 # get_Variables: Function to get a list of variables from an existing file 84 84 # getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values 85 # getvars_tofile: Function to get variables from a file and pass them to another one 85 86 # grattr: Function to read a global atribute 86 87 # grmattr: Removing a global attribute … … 158 159 'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_attribute', \ 159 160 'get_namelist_vars', 'get_Variables', \ 160 'getvalues_lonlat', 'g rattr',\161 'getvalues_lonlat', 'getvars_tofile', 'grattr', \ 161 162 'grmattr', 'idims', 'igattrs', 'increaseDimvar', 'isgattrs', \ 162 163 'isvattrs', 'ivars', 'ivattrs', 'LMDZ_toCF', 'maskvar', 'model_characteristics', \ … … 326 327 elif oper == 'getvalues_lonlat': 327 328 ncvar.getvalues_lonlat(opts.values, opts.ncfile) 329 elif oper == 'getvars_tofile': 330 ncvar.getvars_tofile(opts.values, opts.ncfile, opts.varname) 328 331 elif oper == 'grattr': 329 332 ncvar.grattr(opts.values, opts.ncfile) -
trunk/tools/nc_var_tools.py
r1358 r1360 71 71 # get_Variables: Function to get a list of variables from an existing file 72 72 # getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values 73 # getvars_tofile: Function to get variables from a file and pass them to another one 73 74 # grattr: Function to read a global atribute 74 75 # grmattr: Removing a global attribute … … 17958 17959 return 17959 17960 17961 def getvars_tofile(values,ncfile,varname): 17962 """ Function to get variables from a file and pass them to another one 17963 values= [getfile] 17964 getfile: name of the file from which variables have to be taken 17965 ncfile= name of the file to receive the variables 17966 varname= ',' list of variables to get ('all', for all variables) 17967 NOTE: all coincident dimensions, variables will not be taken 17968 """ 17969 fname = 'getvars_tofile' 17970 17971 if values == 'h': 17972 print fname + '_____________________________________________________________' 17973 print getvars_tofile.__doc__ 17974 quit() 17975 17976 #arguments = '[getfile]' 17977 #gen.check_arguments(fname, values, arguments, ',') 17978 17979 #getfile = values.split(',')[0] 17980 getfile = values 17981 17982 if not os.path.isfile(getfile): 17983 print errormsg 17984 print ' ' + fname + ": file from which get variables '" + getfile + \ 17985 "' does not exist !!" 17986 quit(-1) 17987 17988 ionc = NetCDFFile(getfile, 'r') 17989 ronc = NetCDFFile(ncfile, 'a') 17990 17991 # Getting list of variables to get 17992 ivarns = ionc.variables.keys() 17993 if varname == 'all': 17994 varns = list(ivarns) 17995 else: 17996 varns = varname.split(',') 17997 17998 print ' ' + fname + ": getting variables from file '" + getfile + "' to '" + \ 17999 ncfile + ' _______' 18000 rvarns = ronc.variables.keys() 18001 for vn in varns: 18002 rdimns = list(ronc.dimensions) 18003 if not gen.searchInlist(ivarns,vn): 18004 print errormsg 18005 print ' ' + fname + ": file '" + getfile + "' does not have variable '"+\ 18006 vn + "' !!" 18007 print ' available variables:', ivarns 18008 quit(-1) 18009 if not gen.searchInlist(rvarns,vn): 18010 print ' ' + fname + ": adding variable '" + vn + "' ..." 18011 18012 iovar = ionc.variables[vn] 18013 vardims = list(iovar.dimensions) 18014 for vd in vardims: 18015 if not gen.searchInlist(rdimns,vd): 18016 ovd = ionc.dimensions[vd] 18017 if ovd.isunlimited(): 18018 dsize = None 18019 else: 18020 dsize = len(ovd) 18021 18022 print ' ' + fname + ": adding dimension '" + vd + "' (", \ 18023 dsize, ') ...' 18024 ronc.createDimension(vd,dsize) 18025 varvals = iovar[:] 18026 vartype = iovar.dtype 18027 varattrs = iovar.ncattrs() 18028 if gen.searchInlist(varattrs, '_FillValue'): 18029 fillval = iovar.getncattr('_FillValue') 18030 newvar = ronc.createVariable(vn, vartype, vardims, fill_value=fillval) 18031 varattrs.remove('_FillValue') 18032 else: 18033 newvar = ronc.createVariable(vn, vartype, vardims) 18034 newvar[:] = varvals[:] 18035 for attrn in varattrs: 18036 attrv = iovar.getncattr(attrn) 18037 newattr = set_attribute(newvar,attrn,attrv) 18038 ronc.sync() 18039 18040 ionc.close() 18041 18042 # Global values 18043 newattr = set_attribute(ronc, 'PyNCplot', 'http://www.xn--llusfb-5va.cat/python/PyNCplot/') 18044 newattr = set_attribute(ronc, 'script', fname) 18045 newattr = set_attribute(ronc, 'get_file', getfile) 18046 newattr = set_attribute(ronc, 'get_variables', varname) 18047 18048 ronc.sync() 18049 ronc.close() 18050 18051 return 17960 18052 17961 18053 #quit()
Note: See TracChangeset
for help on using the changeset viewer.