Changeset 533 in lmdz_wrf for trunk/tools
- Timestamp:
- Jun 26, 2015, 5:17:00 PM (10 years ago)
- Location:
- trunk/tools
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r508 r533 34 34 'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues', \ 35 35 'sorttimesmat', 'spacemean', 'statcompare_files', 'submns', 'subyrs', 'TimeInf', \ 36 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', 'varaddref', \ 36 'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', \ 37 'varaddref', \ 37 38 'var_creation', 'varout', 'varoutold', 'varrmattr', 'varrm', 'vrattr', \ 38 39 'WRF_d0Nref', \ … … 218 219 elif oper == 'TimeInf': 219 220 ncvar.TimeInf(opts.ncfile, opts.varname) 221 elif oper == 'TimeSplitmat': 222 ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname) 220 223 elif oper == 'timemean': 221 224 ncvar.timemean(opts.values, opts.ncfile, opts.varname) -
trunk/tools/nc_var_tools.py
r513 r533 10 10 11 11 fillValue = 1.e20 12 fillValueF = 1.e20 13 fillValueC = '-' 14 fillValueI = -99999 12 15 13 16 def filexist(filen, emsg, fmsg): … … 15957 15960 return 15958 15961 15962 def TimeSplitmat(values, ncfile, vn): 15963 """ Function to transform a file with CFtimes to a matrix [Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond] 15964 values= [tdim]:[tvdim]:[seltimes] 15965 tdim= name of the dimension time 15966 tvdim= name of the variable with the time values 15967 seltimes= ',' list of which times ('y', 'm', 'd', 'H','M', 'S') one wants to split the values 15968 desired final order 15969 ncfile= netcdf file to use 15970 vn= name of the variable, 'all' for all variables 15971 """ 15972 import datetime as dt 15973 fname = 'TimeSplitmat' 15974 15975 ofile = 'TimeSplitmat.nc' 15976 15977 if values == 'h': 15978 print fname + '_____________________________________________________________' 15979 print TimeSplitmat.__doc__ 15980 quit() 15981 15982 arguments = '[tdim]:[tvdim]:[seltimes]' 15983 check_arguments(fname, len(values.split(':')), arguments, ':', \ 15984 len(arguments.split(','))) 15985 15986 tdim = values.split(':')[0] 15987 tvdim = values.split(':')[1] 15988 seltimes = values.split(':')[2] 15989 15990 of = NetCDFFile(ncfile, 'r') 15991 15992 stimes = seltimes.split(',') 15993 if len(stimes) != 2: 15994 print errormsg 15995 print fname + ': number of splitting times',len(stimes),'not ready!!' 15996 quit(-1) 15997 15998 if vn == 'all': 15999 vns = of.variables.keys() 16000 else: 16001 vns = [vn] 16002 16003 otime = of.variables[tvdim] 16004 dt = otime.shape[0] 16005 if not searchInlist(otime.ncattrs(), 'units'): 16006 print errormsg 16007 print ' ' + fname + ": varible time '" + dimtv + "' has not units!!" 16008 quit(-1) 16009 tunits = otime.getncattr('units') 16010 16011 mattimes = CFtimes_datetime(of,tvdim) 16012 16013 Nyears = len(np.unique(mattimes[:,0])) 16014 Nmonths = len(np.unique(mattimes[:,1])) 16015 Ndays = len(np.unique(mattimes[:,2])) 16016 Nhours = len(np.unique(mattimes[:,3])) 16017 Nminutes = len(np.unique(mattimes[:,4])) 16018 Nseconds = len(np.unique(mattimes[:,5])) 16019 16020 newdimns = [] 16021 newdimvs = [] 16022 firstmat = -1 16023 secondmat = -1 16024 for st in stimes: 16025 if st == 'y': 16026 newdimns.append('years') 16027 newdimvs.append(Nyears) 16028 if firstmat == -1: 16029 firstmat = 0 16030 else: 16031 secondmat = 0 16032 elif st == 'm': 16033 newdimns.append('months') 16034 newdimvs.append(Nmonths) 16035 if firstmat == -1: 16036 firstmat = 1 16037 else: 16038 secondmat = 1 16039 elif st == 'd': 16040 newdimns.append('days') 16041 newdimvs.append(Ndays) 16042 if firstmat == -1: 16043 firstmat = 2 16044 else: 16045 secondmat = 2 16046 elif st == 'H': 16047 newdimns.append('hours') 16048 newdimvs.append(Nhours) 16049 if firstmat == -1: 16050 firstmat = 3 16051 else: 16052 secondmat = 3 16053 elif st == 'M': 16054 newdimns.append('minutes') 16055 newdimvs.append(Nminutes) 16056 if firstmat == -1: 16057 firstmat = 4 16058 else: 16059 secondmat = 4 16060 elif st == 'S': 16061 newdimns.append('seconds') 16062 newdimvs.append(Nseconds) 16063 if firstmat == -1: 16064 firstmat = 5 16065 else: 16066 secondmat = 5 16067 else: 16068 print errormsg 16069 print ' ' + fname + ": selected time '" + st + "' not ready!!" 16070 quit(-1) 16071 16072 print ' ' + fname + ':initial selection of:',newdimvs 16073 print ' ' + newdimns[0],':',np.unique(mattimes[:,firstmat]) 16074 print ' ' + newdimns[1],':',np.unique(mattimes[:,secondmat]) 16075 16076 # We can have still multiple values (for given days, multiple months...). Assuming 16077 # 30days months 16078 newdimvs[1] = dt/newdimvs[0] 16079 print ' re-arranged to:',newdimvs 16080 newtime = np.zeros(tuple(newdimvs), dtype=np.float) 16081 16082 # Openning new file 16083 onewfile = NetCDFFile(ofile, 'w') 16084 16085 # Dimensions 16086 newdim = onewfile.createDimension(newdimns[0], newdimvs[0]) 16087 newdim = onewfile.createDimension(newdimns[1], newdimvs[1]) 16088 newdim = onewfile.createDimension('time', None) 16089 16090 splitS = seltimes.replace(',','') 16091 16092 for Vn in vns: 16093 print Vn + '...' 16094 oVn = of.variables[Vn] 16095 16096 # Removing time dimension from the variable dimensions 16097 Vndims = oVn.dimensions 16098 if searchInlist(Vndims, tdim) and Vn != tvdim: 16099 varNOtdimns = [] 16100 varNOtdimvs = [] 16101 for idn in Vndims: 16102 if idn != tdim: 16103 varNOtdimns.append(idn) 16104 varNOtdimvs.append(len(of.dimensions[idn])) 16105 if not searchInlist(onewfile.dimensions.keys(), idn): 16106 newdim = onewfile.createDimension(idn, \ 16107 len(of.dimensions[idn])) 16108 16109 newvarv = np.ones(tuple(newdimvs+varNOtdimvs), dtype=np.float)*fillValueF 16110 16111 d1 = 0 16112 d2 = -1 16113 16114 d1val = mattimes[0,firstmat] 16115 d2val = mattimes[0,secondmat] 16116 16117 secondvals = [] 16118 16119 for ddt in range(dt): 16120 if mattimes[ddt,firstmat] > d1val: 16121 d1 = d1 + 1 16122 d1val = mattimes[ddt,firstmat] 16123 d2 = 0 16124 else: 16125 d2 = d2 + 1 16126 if d1 == 0: secondvals.append(mattimes[ddt,secondmat]) 16127 16128 slicevar = [] 16129 slicenewvar = [] 16130 16131 slicenewvar.append(d1) 16132 slicenewvar.append(d2) 16133 for idn in Vndims: 16134 if idn != tdim: 16135 slicevar.append(slice(0,len(of.dimensions[idn]))) 16136 slicenewvar.append(slice(0,len(of.dimensions[idn]))) 16137 else: 16138 slicevar.append(ddt) 16139 16140 # print ddt,d1,d2,mattimes[ddt,:],'var:',slicevar,'new:',slicenewvar 16141 16142 newvarv[tuple(slicenewvar)] = oVn[tuple(slicevar)] 16143 newtime[d1,d2] = otime[ddt] 16144 16145 newvar = onewfile.createVariable(Vn + '_' + splitS, 'f', \ 16146 tuple(newdimns + varNOtdimns), fill_value = fillValueF) 16147 newattr = newvar.setncattr('desc', Vn + 'splitted by:' + newdimns[0] + \ 16148 ' & ' + newdimns[1]) 16149 newvar[:] = newvarv 16150 onewfile.sync() 16151 else: 16152 newvar = oVn 16153 16154 # Variable time 16155 newvar = onewfile.createVariable('time', 'f8', tuple(newdimns)) 16156 newvar[:] = newtime 16157 basicvardef(newvar, 'time', 'time', tunits) 16158 16159 # Variables selection 16160 idn=0 16161 for newd in newdimns: 16162 print newd, onewfile.dimensions.keys() 16163 newvar = onewfile.createVariable(newd, 'f', (newd)) 16164 if idn == 0: 16165 newvar[:] = np.unique(mattimes[:,firstmat]) 16166 else: 16167 newvar[:] = secondvals 16168 16169 set_attribute(newvar, 'desc', 'Selected ' + newdimns[idn]) 16170 idn = idn + 1 16171 16172 # Global attributes 16173 onewfile.setncattr('script', fname) 16174 onewfile.setncattr('version', '1.0') 16175 onewfile.setncattr('author', 'L. Fita') 16176 onewfile.setncattr('institution', 'Laboratoire de Meteorology Dynamique') 16177 onewfile.setncattr('university', 'Pierre et Marie Curie') 16178 onewfile.setncattr('country', 'France') 16179 onewfile.setncattr('description', 'transform a file with CFtimes to a matrix ' +\ 16180 '[Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond]') 16181 16182 onewfile.sync() 16183 onewfile.close() 16184 print fname + ": Successful writting of file '" + ofile + "' !!" 16185 16186 return 16187 16188 #fileobj = NetCDFFile('/home/lluis/PY/ERAI_pl199501_130-133.nc', 'r') 16189 #TimeSplitmat(fileobj, 'time', 'time', 'all', 'd,H') 16190 15959 16191 #quit() 15960 16192 -
trunk/tools/variables_values.dat
r532 r533 165 165 LRH2M, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu 166 166 TSrhs, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu 167 rh, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu (from TS files to netcdf) 168 rhs, huss, specific_humidity, 0., 1., specific|humidty|at|2m, 1, BuPu (from TS files to netcdf) 167 169 i, i, iced_water_mixing_ratio, 0., 0.0003, iced|water|mixing|ratio, kgkg-1, Purples 168 170 QICE, i, iced_water_mixing_ratio, 0., 0.0003, iced|water|mixing|ratio, kgkg-1, Purples
Note: See TracChangeset
for help on using the changeset viewer.