Changeset 2061 in lmdz_wrf
- Timestamp:
- Aug 9, 2018, 10:00:17 PM (7 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r1978 r2061 27 27 ## e.g. # nc_var.py -o get_time -S '1422759600;seconds!since!1949-12-01!00:00:00' -f /home/lluis/PY/wrfout_d01_1995-01-01_00:00:00 -v WRFtime 28 28 ## e.g. # nc_var.py -o join_singlestation_obsfiles -S 'obs/sfc_CAM:OBSnetcdf' -v all 29 ## e.g. # nc_var.py -o join_sounding_obsfiles -S .:UWyoming_snd_1 -v all 29 30 30 31 ## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1 … … 128 129 # ivars: Give all the variable names of a file 129 130 # ivattrs: Give all the attributes of a variable and its type 130 # join_singlestation_obsfiles: Function to join files from 'single-station' `create_OBSnetcdf.py' script to create a single file 131 # join_singlestation_obsfiles: Function to join files from 'single-station' 132 # `create_OBSnetcdf.py' script to create a single file 133 # join_sounding_obsfiles: Function to join 1D variables from files generated by 134 # `UWyoming_snd_nc.py' script to create a single file 131 135 # LMDZ_toCF: Function to pass a LMDZ original file to CF-conventions 132 136 # lonlat_polygon: Function to define a lon/lat region giving the coordinates of the vertexs of a given polygon … … 206 210 'grmattr', 'idims', 'ifile', 'igattrs', 'increaseDimvar', 'isgattrs', \ 207 211 'isvattrs', 'itime', 'ivars', 'ivattrs', 'join_singlestation_obsfiles', \ 208 ' LMDZ_toCF', 'lonlat_polygon', 'maskvar',\212 'join_sounding_obsfiles', 'LMDZ_toCF', 'lonlat_polygon', 'maskvar', \ 209 213 'merge_files', 'model_characteristics', \ 210 214 'mthDYNAMICO_toCF', 'ncreplace', 'ncstepdiff', 'netcdf_concatenation', \ … … 269 273 # Operations which file name is not a real file 270 274 NotCheckingFile = ['DatesFiles', 'compute_opersvarsfiles', 'file_creation', \ 271 'join_singlestation_obsfiles', 'list_operations', 'merge_files', \ 275 'join_singlestation_obsfiles', 'join_sounding_obsfiles', 'list_operations', \ 276 'merge_files', \ 272 277 'model_characteristics', 'netcdf_concatenation', 'netcdf_fold_concatenation', \ 273 278 'netcdf_fold_concatenation_HMT'] … … 415 420 elif oper == 'join_singlestation_obsfiles': 416 421 ncvar.join_singlestation_obsfiles(opts.values, opts.varname) 422 elif oper == 'join_sounding_obsfiles': 423 ncvar.join_sounding_obsfiles(opts.values, opts.varname) 417 424 elif oper == 'list_operations': 418 425 # From: http://www.diveintopython.net/power_of_introspection/all_together.html -
trunk/tools/nc_var_tools.py
r2059 r2061 103 103 # ivars: Give all the variable names of a file 104 104 # ivattrs: Give all the attributes of a variable and its type 105 # join_singlestation_obsfiles: Function to join files from 'single-station' `create_OBSnetcdf.py' script to create a single file 105 # join_singlestation_obsfiles: Function to join files from 'single-station' 106 # `create_OBSnetcdf.py' script to create a single file 107 # join_sounding_obsfiles: Function to join 1D variables from files generated by 108 # `UWyoming_snd_nc.py' script to create a single file 106 109 # load_ncvariable_lastdims: Function to load the last [Nlastdims] dimensions of a variable [varn] from a netCDF object at the other dimensions at [prevdims] 107 110 # lonlatProj: Function to compute longitudes and latitudes for a given projection following subroutine calc_resolution_in from ORCHIDEE/src_global/module_InterpWeight … … 24875 24878 return 24876 24879 24880 def join_sounding_obsfiles(values, variable): 24881 """ Function to join 1D variables from files generated by `UWyoming_snd_nc.py' 24882 script to create a single file 24883 values=[namefoldobs]:[headerF] 24884 [namefoldobs]: name of the foler holding all single-stations netCDF files 24885 [headerF]: header of the names of the files with the observations 24886 variable= ',' separated list of variables names ('all' for all variables 24887 encountred within all files) 24888 """ 24889 fname = 'join_sounding_obsfiles' 24890 24891 if values == 'h': 24892 print fname + '_____________________________________________________________' 24893 print join_sounding_obsfiles.__doc__ 24894 quit() 24895 24896 expectargs = '[namefoldobs]:[headerF]' 24897 gen.check_arguments(fname, values, expectargs, ':') 24898 24899 namefoldobs = values.split(':')[0] 24900 headerF = values.split(':')[1] 24901 24902 fobs = gen.files_folder(namefoldobs, headerF) 24903 24904 # Getting number stations 24905 ifile = 0 24906 namevals = [] 24907 refvals = [] 24908 lonvals = [] 24909 latvals = [] 24910 heightvals = [] 24911 timevals = [] 24912 varvals = {} 24913 for fobsn in fobs: 24914 filen = namefoldobs + '/' + fobsn 24915 if not os.path.isfile(filen): 24916 print errormsg 24917 print ' ' + fname + ": file '" + filen + "' does not exist !!" 24918 print " files found in folder '" + namefoldobs + "':", fobs 24919 quit(-1) 24920 24921 onc = NetCDFFile(filen, 'r') 24922 if gen.searchInlist(onc.dimensions,'time'): dimt = onc.dimensions['time'] 24923 elif gen.searchInlist(onc.dimensions,'CFtime'): dimt=onc.dimensions['CFtime'] 24924 else: 24925 print errormsg 24926 print ' ' + fname + ": file '" + filen + "' does not have any 'time'" + \ 24927 "dimension !!" 24928 print " available ones:", onc.dimensions 24929 quit(-1) 24930 24931 if ifile == 0: 24932 varns = [] 24933 if variable == 'all': 24934 allvarns = onc.variables.keys() 24935 allvarns.remove('pres') 24936 for vn in allvarns: 24937 ovn = onc.variables[vn] 24938 # Getting 1D variables 24939 rank = len(ovn.shape) 24940 if rank == 1: 24941 varns.append(vn) 24942 vattrs = ovn.ncattrs() 24943 atv = [] 24944 for atr in vattrs: 24945 atv.append(ovn.getncattr(atr)) 24946 if gen.searchInlist(vattrs, '_FillValue'): 24947 varfill = ovn._FillValue 24948 else: varfill = None 24949 varvals[vn]= [ovn.dtype, vattrs, atv, ovn.dimensions, varfill] 24950 24951 else: 24952 allvarns = gen.str_list(variable, ',') 24953 for vn in allvarns: 24954 if onc.variables.has_key(vn): 24955 ovn = onc.variables[vn] 24956 # Getting 1D variables 24957 rank = len(ovn.shape) 24958 if rank == 1: 24959 varns.append(vn) 24960 vattrs = ovn.ncattrs() 24961 atv = [] 24962 for atr in vattrs: 24963 atv.append(ovn.getncattr(atr)) 24964 if gen.searchInlist(vattrs, '_FillValue'): 24965 varfill = ovn._FillValue 24966 else: varfill = None 24967 varvals[vn]= [ovn.dtype,vattrs,atv,ovn.dimensions,varfill] 24968 24969 if onc.variables.has_key('time'): 24970 timevals = list(onc.variables['time'][:]) 24971 elif onc.variables.has_key('CFtime'): 24972 timevals = list(onc.variables['CFtime'][:]) 24973 else: 24974 print errormsg 24975 print ' '+fname+": file '" + filen + "' does not have any 'time'" + \ 24976 "expected variable ('time', 'CFtime') !!" 24977 varnsorted = onc.variables.has_key(varn) 24978 varnsorted.sort() 24979 print ' available ones:', varnsorted 24980 quit(-1) 24981 else: 24982 if variable == 'all': 24983 allvarns = onc.variables.keys() 24984 allvarns.remove('pres') 24985 else: 24986 allvarns = gen.str_list(variable, ',') 24987 for vn in allvarns: 24988 if not gen.searchInlist(allvarns,vn): 24989 ovn = onc.variables[vn] 24990 rank = len(ovn.shape) 24991 if rank == 1: 24992 varns.append(vn) 24993 vattrs = ovn.ncattrs() 24994 atv = [] 24995 for atr in vattrs: 24996 atv.append(ovn.getncattr(atr)) 24997 if gen.searchInlist(vattrs, '_FillValue'): 24998 varfill = ovn._FillValue 24999 else: varfill = None 25000 varvals[vn]= [ovn.dtype, vattrs, atv, ovn.dimensions, varfill] 25001 25002 if onc.variables.has_key('time'): 25003 tvals = onc.variables['time'][:] 25004 elif onc.variables.has_key('CFtime'): 25005 tvals =onc.variables['CFtime'][:] 25006 else: 25007 print errormsg 25008 print ' ' +fname+ ": file '" +filen+ "' does not have any 'time'" + \ 25009 "expected variable ('time', 'CFtime') !!" 25010 varnsorted = onc.variables.has_key(varn) 25011 varnsorted.sort() 25012 print ' available ones:', varnsorted 25013 quit(-1) 25014 NOtvals, NOits = gen.list_differences(timevals, list(tvals)) 25015 if len(NOtvals) > 0: 25016 print ' ' + fname + ": file '" + filen + "' adds additional times:",\ 25017 NOtvals 25018 for tv in NOtvals: timevals.append(tv) 25019 stn = onc.getncattr('Station_name') 25020 stref = onc.getncattr('Station_ref') 25021 stlon = onc.getncattr('Station_longitude') 25022 stlat = onc.getncattr('Station_latitude') 25023 sthgt = onc.getncattr('Station_elevation') 25024 25025 namevals.append(stn) 25026 refvals.append(stref) 25027 lonvals.append(stlon) 25028 latvals.append(stlat) 25029 heightvals.append(sthgt) 25030 print ' ' + fname + ": found values for station '" + namevals[ifile] + \ 25031 "' at:", lonvals[ifile], ',', latvals[ifile], ' ...' 25032 25033 onc.close() 25034 ifile = ifile + 1 25035 25036 Nfile = ifile 25037 print ' 1D-rank variables to join:', varvals.keys() 25038 25039 # Creation of joined file 25040 ofile = 'joined_soundingstations.nc' 25041 25042 onewnc = NetCDFFile(ofile, 'w') 25043 # dimensions 25044 newdim = onewnc.createDimension('Nstations', Nfile) 25045 newdim = onewnc.createDimension('StrLength', 200) 25046 newdim = onewnc.createDimension('time', None) 25047 25048 # Creation of dimension-variables 25049 newvar = onewnc.createVariable('stsref', 'c', ('Nstations', 'StrLength')) 25050 basicvardef(newvar, 'station_reference', 'Reference of stations', '-') 25051 newvals = writing_str_nc(newvar, refvals, 200) 25052 25053 newvar = onewnc.createVariable('stsname', 'c', ('Nstations', 'StrLength')) 25054 basicvardef(newvar, 'station_name', 'Name of stations', '-') 25055 newvals = writing_str_nc(newvar, namevals, 200) 25056 25057 newvar = onewnc.createVariable('stslon', 'f8', ('Nstations')) 25058 basicvardef(newvar, 'lon', 'Longitude of stations', 'degrees_east') 25059 newvar[:] = lonvals[:] 25060 25061 newvar = onewnc.createVariable('stslat', 'f8', ('Nstations')) 25062 basicvardef(newvar, 'lat', 'Latitude of stations', 'degrees_north') 25063 newvar[:] = latvals[:] 25064 25065 newvar = onewnc.createVariable('stsheight', 'f', ('Nstations')) 25066 basicvardef(newvar, 'height', 'height of stations', 'm') 25067 newvar[:] = heightvals[:] 25068 25069 # Time axis 25070 timevalsort = timevals 25071 timevalsort.sort() 25072 newvart = onewnc.createVariable('ststimes', 'f', ('time')) 25073 newvart[:] = timevalsort[:] 25074 newvart.setncattr('standrad_name','time') 25075 newvart.setncattr('long_name','Time') 25076 25077 ifile = 0 25078 for fobsn in fobs: 25079 filen = namefoldobs + '/' + fobsn 25080 if not os.path.isfile(filen): 25081 print errormsg 25082 print ' ' + fname + ": file '" + filen + "' does not exist !!" 25083 print " files found in folder '" + namefoldobs + "':", fobs 25084 quit(-1) 25085 25086 onc = NetCDFFile(filen, 'r') 25087 ot = onc.variables['time'] 25088 otv = list(ot[:]) 25089 coint, itcoinsort, itcoinotv = gen.list_coincidences(timevalsort, otv) 25090 itcoinsort.sort() 25091 25092 if ifile == 0: 25093 tu = ot.units 25094 newvart.setncattr('units',tu) 25095 for varn in varns: 25096 dicvn = varvals[varn] 25097 vartype = dicvn[0] 25098 attrns = dicvn[1] 25099 attrvs = dicvn[2] 25100 vardims = dicvn[3] 25101 varfill = dicvn[4] 25102 newdims = list(vardims) + ['Nstations'] 25103 for vard in vardims: 25104 if not gen.searchInlist(onewnc.dimensions, vard): 25105 print ' ' + fname + ": adding dimension '" + vard + "' !!" 25106 dobj = onc.dimensions[vard] 25107 if dobj.isunlimited(): 25108 newdim = onewnc.createDimension(vard, None) 25109 else: 25110 lend = len(dobj) 25111 newdim = onewnc.createDimension(vard, lend) 25112 if varfill is not None: 25113 newvar = onewnc.createVariable(varn, vartype, tuple(newdims), \ 25114 fill_value = varfill) 25115 else: 25116 newvar = onewnc.createVariable(varn, vartype, tuple(newdims)) 25117 25118 Nattrs = len(attrns) 25119 for iattr in range(Nattrs): 25120 attrn = attrns[iattr] 25121 if attrn != '_FillValue': 25122 attrv = attrvs[iattr] 25123 newattr = newvar.setncattr(attrn, attrv) 25124 # Global attributes 25125 for attrn in onc.ncattrs(): 25126 attrv = onc.getncattr(attrn) 25127 onewnc.setncattr(attrn, attrv) 25128 newvar = onewnc.variables[varn] 25129 25130 if not onc.variables.has_key(varn): 25131 print warnmsg 25132 print ' ' + fname + ": file '" + filen + "' does not have " + \ 25133 "variable '" + varn + "' filling with missing values !!" 25134 else: 25135 ovar = onc.variables[varn] 25136 varv = ovar[:] 25137 if gen.searchInlist(newvar.dimensions, 'time'): 25138 if len(newvar.shape) == 4: newvar[itcoinsort,:,:,ifile] = varv[:] 25139 elif len(newvar.shape) == 3: newvar[itcoinsort,:,ifile] = varv[:] 25140 elif len(newvar.shape) == 2: newvar[itcoinsort,ifile] = varv[:] 25141 else: 25142 if len(newvar.shape) == 4: newvar[:,:,:,ifile] = varv[:] 25143 elif len(newvar.shape) == 3: newvar[:,:,ifile] = varv[:] 25144 elif len(newvar.shape) == 2: newvar[:,ifile] = varv[:] 25145 else: 25146 for varn in varns: 25147 newvar = onewnc.variables[varn] 25148 25149 dicvn = varvals[varn] 25150 vartype = dicvn[0] 25151 attrns = dicvn[1] 25152 attrvs = dicvn[2] 25153 vardims = dicvn[3] 25154 varfill = dicvn[4] 25155 25156 if not onc.variables.has_key(varn): 25157 print warnmsg 25158 print ' ' + fname + ": file '" + filen + "' does not have " + \ 25159 "variable '" + varn + "' filling with missing values !!" 25160 else: 25161 ovar = onc.variables[varn] 25162 varv = ovar[:] 25163 if gen.searchInlist(newvar.dimensions, 'time'): 25164 if len(newvar.shape) == 4: newvar[itcoinsort,:,:,ifile] = varv[:] 25165 elif len(newvar.shape) == 3: newvar[itcoinsort,:,ifile] = varv[:] 25166 elif len(newvar.shape) == 2: newvar[itcoinsort,ifile] = varv[:] 25167 else: 25168 if len(newvar.shape) == 4: newvar[:,:,:,ifile] = varv[:] 25169 elif len(newvar.shape) == 3: newvar[:,:,ifile] = varv[:] 25170 elif len(newvar.shape) == 2: newvar[:,ifile] = varv[:] 25171 onewnc.sync() 25172 25173 onc.close() 25174 ifile = ifile + 1 25175 25176 # Global attributes 25177 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 25178 onewnc.sync() 25179 onewnc.close() 25180 print fname + ": succesfull creation of file '" + ofile + "' !!" 25181 25182 return 25183 24877 25184 def stats_lonlat_box(vals, lonv, latv, nlon, xlon, nlat, xlat): 24878 25185 """ Function to provide the statistics of a variable from a lon,lat box
Note: See TracChangeset
for help on using the changeset viewer.