Changeset 2382 in lmdz_wrf for trunk/tools
- Timestamp:
- Mar 7, 2019, 9:05:54 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r2352 r2382 73 73 ## e.g. # nc_var.py -o except_fillValue -S 'orog:range,500.,7000.:None' -f /home/lluis/estudios/Andes/cmip_data/fx/orog_fx_ACCESS1-0_historical_r0i0p0.nc -v 'all' 74 74 ## e.g. # nc_var.py -o usefile_compute_slices_stats_areaweighted -S 'compute_slices_stats_areaweighted.nc@XLAT_M-HGT_M-rangefaces@Time|WRFtime:west_east|XLONG_M:south_north|XLAT_M:land_cat|INTrange@Time,land_cat' -f 'geo_em.d01.nc' -v 'LANDUSEF' 75 ## e.g. # nc_var.py -o compress_data -S 'XLAND:Time|0:single,1:X|west_east|XLONG,Y|south_north|XLAT' -f wrfout_d01_1995-01-01_00:00:00 -v T2,LU_INDEX,Times 75 76 76 77 from optparse import OptionParser … … 98 99 # chvarname: Changing the name of the variable 99 100 # cleaning_varsfile: Function to keep a list of varibales from a file 101 # compress_data: Function to compress (only provide values for a criteria) the values of a 2D 102 # file following a given criteria from one of its variables 100 103 # compute_deaccum: Function to compute deaccum: deaccumulation of a variable (VAR[t+1]-VAR[t]) 101 104 # compute_opersvarsfiles: Function to compute opersvarfiles: operation of variables from different files … … 228 231 'CFmorzization', 'chdimname', 'changevartype', \ 229 232 'checkallvars', 'checkAllValues', 'checkNaNs', \ 230 'chgtimestep', 'chvarname', 'cleaning_varsfile', 'comp ute_deaccum',\231 'compute_ opersvarsfiles',\233 'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compress_data', \ 234 'compute_deaccum', 'compute_opersvarsfiles', \ 232 235 'compute_opervaralltime', 'compute_opervartimes', 'compute_slices_stats', \ 233 236 'compute_slices_stats_areaweighted', 'compute_slice2Dstats', \ … … 367 370 elif oper == 'cleaning_varsfile': 368 371 ncvar.cleaning_varsfile(opts.values, opts.ncfile) 372 elif oper == 'compress_data': 373 ncvar.compress_data(opts.values, opts.ncfile, opts.varname) 369 374 elif oper == 'compute_deaccum': 370 375 ncvar.compute_deaccum(opts.values, opts.ncfile, opts.varname) -
trunk/tools/nc_var_tools.py
r2381 r2382 45 45 # cleaning_varsfile: Function to keep a list of varibales from a file 46 46 # cls_time_information: Classs to provide information about variable time 47 # compress_data: Function to compress (only provide values for a criteria) the values of a 2D 48 # file following a given criteria from one of its variables 47 49 # compute_deaccum: Function to compute deaccum: deaccumulation of a variable (VAR[t+1]-VAR[t]) 48 50 # compute_opersvarsfiles: Function to compute opersvarfiles: operation of variables from different files (OPER1.FILE1_VAR1 OPER2.FILE2_VAR2), operations are going to be secuentially made … … 30887 30889 compressv = gen.typemod(crit[1], ocompressvar.dtype) 30888 30890 compressmask = ma.masked_not_equal(compressvar,compressv) 30891 criteriaS = '== ' + crit[1] 30889 30892 elif crit[0] == 'range': 30890 30893 icompressv = gen.typemod(crit[1], ocompressvar.dtype) 30891 30894 ecompressv = gen.typemod(crit[2], ocompressvar.dtype) 30892 30895 compressmask = ma.masked_outside(compressvar,icompressv,ecompressv) 30896 criteriaS = '>= ' + crit[1] + ' & <= ' + crit[2] 30893 30897 else: 30894 30898 print errormsg … … 30934 30938 varslc, vard = SliceVarDict(oxvar,slicedims) 30935 30939 newvar = onewnc.createVariable(dnv[1], 'f', tuple(vard)) 30936 print 'oxvar dimensions:', oxvar.dimensions30937 print 'Lluis shapes: newvar', newvar.shape, 'slice', varslc, 'oxvar:', oxvar.shape, 'X', axisdims['X']30938 print 'Lluis slicedims:', slicedims30939 30940 newvar[:] = oxvar[tuple(varslc)] 30940 30941 ncattrs = oxvar.ncattrs() … … 30961 30962 basicvardef(newvar, cfvarvals[1], cfvarvals[4], cfvarvals[5]) 30962 30963 newvar.setncattr('compress', compressS) 30964 newvar.setncattr('criteria', criteriaS) 30965 30963 30966 newvar[:] = icc 30967 30968 if variable == 'all': 30969 varns = onc.variables.keys() 30970 else: 30971 varns = variable.split(',') 30972 30973 for varn in varns: 30974 if not onc.variables.has_key(varn): 30975 print errormsg 30976 print ' ' + fname + ": file '" + ncfile + "' does not have variable '"+ \ 30977 varn + "' !!" 30978 Varns = onc.variables.keys() 30979 Varns.sort() 30980 print ' available ones:', Varns 30981 onc.close() 30982 quit(-1) 30983 print " compressing '" + varn + "' ..." 30984 ovar = onc.variables[varn] 30985 ovardims = list(ovar.dimensions) 30986 unslcdims = ovardims + [] 30987 done = False 30988 for dn in spacedims: 30989 if not gen.searchInlist(ovardims, dn): 30990 print errormsg 30991 print ' ' + fname + ": variable does contain one of the space " + \ 30992 "dimensions: '" + dn + "' used to compress" 30993 print ' including variable without compression' 30994 add_vars(onc, onewnc, [varn]) 30995 done = True 30996 break 30997 else: 30998 unslcdims.remove(dn) 30999 if done: continue 31000 31001 # Shape of variable 31002 varshape = [] 31003 for dn in unslcdims: 31004 varshape.append(len(onc.dimensions[dn])) 31005 varrunshapes = gen.Nloops_1D(varshape) 31006 varshape = varshape + [Nvals] 31007 vardims = unslcdims + [compressvarn] 31008 31009 # Do we have all dimensions? 31010 for dn in vardims: 31011 if not onewnc.dimensions.has_key(dn): add_dims(onc, onewnc, [dn]) 31012 31013 varslcs = gen.provide_slices(ovardims, ovar.shape, unslcdims) 31014 31015 Nslcs = len(varslcs) 31016 varattrs = ovar.ncattrs() 31017 if gen.searchInlist(varattrs, '_FillValue'): 31018 fillv = ovar.getncattr('_FillValue') 31019 newvar = onewnc.createVariable(varn, ovar.dtype, tuple(vardims), \ 31020 fill_value=fillv) 31021 else: 31022 newvar = onewnc.createVariable(varn, ovar.dtype, tuple(vardims)) 31023 for attrn in varattrs: 31024 if attrn != '_FillValue': 31025 attrv = ovar.getncattr(attrn) 31026 newvar.setncattr(attrn, attrv) 31027 31028 onewnc.sync() 31029 31030 dimx = len(onewnc.dimensions[spacedims[1]]) 31031 dimy = len(onewnc.dimensions[spacedims[0]]) 31032 addvslice = [slice(0,dimy), slice(0,dimx)] 31033 31034 for islc in range(Nslcs): 31035 slcv = varslcs[islc] 31036 # Getting slice variable values 31037 varv = ovar[tuple(slcv)] 31038 # Compressing 31039 fvarv = varv[~compressmask.mask] 31040 31041 # Filling compressed variable with slice-compressed values 31042 fvslc = list(varrunshapes[islc]) 31043 newvar[tuple(fvslc)] = fvarv[:] 31044 31045 onewnc.sync() 31046 31047 # Global attributes 31048 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 31049 31050 fattrs = onc.ncattrs() 31051 for attrn in fattrs: 31052 attrv = onc.getncattr(attrn) 31053 onewnc.setncattr(attrn, attrv) 30964 31054 30965 31055 onc.close() … … 30971 31061 return 30972 31062 30973 30974 31063 #values = 'XLAND:Time|0:single,1:X|west_east|XLONG,Y|south_north|XLAT' 30975 30976 #compress_data(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2') 31064 #compress_data(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,LU_INDEX') 30977 31065 30978 31066 #quit()
Note: See TracChangeset
for help on using the changeset viewer.