- Timestamp:
- Apr 3, 2018, 10:21:23 PM (7 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r1882 r1883 153 153 # spacemean: Function to retrieve a space mean series from a multidimensional variable of a file 154 154 # SpatialWeightedMean: Function to compute the spatial mean using weights from a netCDF file 155 # splitfile_dim: Function to split a file along a given dimension with a new file for each different value along the dimension (assuming resultant vardim of rank-1) 155 156 # statcompare_files: Python script to statistically compare two different files 156 157 # subbasin: Function to retrieve the subbasin (all the sub-flows untila a given lon,lat) … … 204 205 'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues', \ 205 206 'setvar_nc', 'sorttimesmat', 'spacemean', 'SpatialWeightedMean', \ 206 's tatcompare_files',\207 'splitfile_dim', 'statcompare_files', \ 207 208 'subbasin', 'submns', 'subyrs', 'TimeInf', 'time_reset', \ 208 209 'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', \ … … 461 462 elif oper == 'SpatialWeightedMean': 462 463 ncvar.SpatialWeightedMean(opts.values, opts.ncfile, opts.varname) 464 elif oper == 'splitfile_dim': 465 ncvar.splitfile_dim(opts.values, opts.ncfile, opts.varname) 463 466 elif oper == 'statcompare_files': 464 467 ncvar.statcompare_files(opts.values) -
trunk/tools/nc_var_tools.py
r1882 r1883 142 142 # spacemean: Function to retrieve a space mean series from a multidimensional variable of a file 143 143 # SpatialWeightedMean: Function to compute the spatial mean using weights from a netCDF file 144 # splitfile_dim: Function to split a file along a given dimension with a new file for each different value along the dimension (assuming resultant vardim of rank-1) 144 145 # statcompare_files: Python script to statistically compare two different files 145 146 # submns: Function to retrieve a series of months from a file … … 24060 24061 return 24061 24062 24062 24063 def splitfile_dim(values, ncfile, variable): 24064 """ Function to split a file along a given dimension with a new file for each different value along the dimension (assuming resultant vardim of rank-1) 24065 values= [dimname]:[vardimname]:[headfile]:[fmtdim] 24066 [dimname]: name of the dimension along which split the file 24067 [vardimname]: variable with the values of the dimension 24068 [headfile]: beginning of the name of the new files 24069 [fmtdim]: format of the labels on the new files ('auto' for d, following python rules) 24070 ncfile= file to split 24071 variable= ',' list of variables to split ('all' for all variables) 24072 """ 24073 fname = 'splitfile_dim' 24074 24075 if values == 'h': 24076 print fname + '_____________________________________________________________' 24077 print splitfile_dim.__doc__ 24078 quit() 24079 24080 expectargs = '[dimname]:[vardimname]:[headfile]:[fmtdim]' 24081 gen.check_arguments(fname, values, expectargs, ':') 24082 24083 dimname = values.split(':')[0] 24084 vardimname = values.split(':')[1] 24085 headfile = values.split(':')[2] 24086 fmtdim = gen.auto_val(values.split(':')[3], 'd') 24087 24088 onc = NetCDFFile(ncfile, 'r') 24089 dimns = list(onc.dimensions) 24090 dimns.sort() 24091 if not gen.searchInlist(dimns,dimname): 24092 print errormsg 24093 print ' ' + fname + ": file '" + onc + "' does not have dimension '" + \ 24094 dimname + "' !!" 24095 print ' available ones:', dimns 24096 24097 varns = list(onc.variables.keys()) 24098 varns.sort() 24099 if not gen.searchInlist(varns,vardimname): 24100 print errormsg 24101 print ' ' + fname + ": file '" + onc + "' does not have variable '" + \ 24102 vardimname + "' !!" 24103 print ' available ones:', varns 24104 24105 ovar = onc.variables[vardimname] 24106 dimns = list(ovar.dimensions) 24107 if not gen.searchInlist(dimns,dimname): 24108 print errormsg 24109 print ' '+fname+": variable '" + vardimname + "' does not have dimension '"+\ 24110 dimname + "' !!" 24111 print ' available ones:', dimns 24112 24113 # getting values to split with 24114 slicevar = [] 24115 for idim in range(len(dimns)): 24116 if dimns[idim] == dimname: 24117 slicevar.append(slice(0,len(onc.dimensions[dimns[idim]]))) 24118 else: 24119 slicevar.append(0) 24120 splitvals = ovar[tuple(slicevar)] 24121 24122 # Variables to split 24123 if variable == 'all': 24124 splitvarns = varns 24125 else: 24126 splitvarns = gen.str_list(variable,',') 24127 24128 # splitting for each value along the dimension 24129 for idim in range(splitvals.shape[0]): 24130 fmtS = "{:"+fmtdim+"}" 24131 idimS = fmtS.format(splitvals[idim]) 24132 ofile = headfile + idimS + '.nc' 24133 onewnc = NetCDFFile(ofile, 'w') 24134 for vn in splitvarns: 24135 if not gen.searchInlist(varns,vardimname): 24136 print errormsg 24137 print ' ' + fname + ": file '" + onc + "' does not have variable '"+\ 24138 vardimname + "' !!" 24139 print ' available ones:', varns 24140 ovn = onc.variables[vn] 24141 dvs = ovn.dimensions 24142 slicevar = [] 24143 for dv in dvs: 24144 if dv != dimname: 24145 slicevar.append(slice(0,len(onc.dimensions[dv]))) 24146 else: 24147 slicevar.append(idim) 24148 if not gen.searchInlist(onewnc.dimensions, dv): 24149 if onc.dimensions[dv].isunlimited(): 24150 newdim = onewnc.createDimension(dv, None) 24151 else: 24152 if dv != dimname: 24153 newdim=onewnc.createDimension(dv,len(onc.dimensions[dv])) 24154 else: 24155 newdim=onewnc.createDimension(dv,1) 24156 24157 newvar = onewnc.createVariable(vn, nctype(ovn.dtype), dvs) 24158 newvar[:] = ovn[tuple(slicevar)] 24159 ovattr = ovn.ncattrs() 24160 for nattr in ovattr: 24161 attrv = ovn.getncattr(nattr) 24162 set_attribute(newvar,nattr,attrv) 24163 onewnc.sync() 24164 24165 # Adding vardimname 24166 if not onewnc.variables.has_key(vardimname): 24167 ovn = onc.variables[vardimname] 24168 dvs = ovn.dimensions 24169 slicevar = [] 24170 for dv in dvs: 24171 if dv != dimname: 24172 slicevar.append(0) 24173 else: 24174 slicevar.append(idim) 24175 if not gen.searchInlist(onewnc.dimensions, dv): 24176 newdim=onewnc.createDimension(dv,1) 24177 newvar = onewnc.createVariable(vardimname, nctype(ovn.dtype), tuple([dimname])) 24178 newvar[:] = ovn[tuple(slicevar)] 24179 ovattr = ovn.ncattrs() 24180 for nattr in ovattr: 24181 attrv = ovn.getncattr(nattr) 24182 set_attribute(newvar,nattr,attrv) 24183 onewnc.sync() 24184 24185 # Global attributes 24186 for attrn in onc.ncattrs(): 24187 attrv = onc.getncattr(attrn) 24188 set_attribute(onewnc, attrn, attrv) 24189 add_global_PyNCplot(onewnc, main, fname, '0.1') 24190 onewnc.sync() 24191 onewnc.close() 24192 24193 print fname + ": successful writting of '" + ofile + "' !!" 24194 24195 return 24063 24196 #quit()
Note: See TracChangeset
for help on using the changeset viewer.