Changeset 905 in lmdz_wrf for trunk/tools
- Timestamp:
- Jun 19, 2016, 9:46:06 PM (8 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r900 r905 40 40 'ivattrs', 'maskvar', 'model_characteristics', \ 41 41 'ncreplace', 'ncstepdiff', 'netcdf_concatenation', 'netcdf_fold_concatenation', \ 42 'Partialmap_Entiremap', 'Partialmap_EntiremapFor', 'Partialmap_EntiremapForExact', \ 42 'netcdf_fold_concatenation_HMT', 'Partialmap_Entiremap', \ 43 'Partialmap_EntiremapFor', 'Partialmap_EntiremapForExact', \ 43 44 'pinterp', 'remapnn', \ 44 45 'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues', \ … … 97 98 # Operations which file name is not a real file 98 99 NotCheckingFile = ['DatesFiles', 'file_creation', 'list_operations', \ 99 'model_characteristics', 'netcdf_concatenation', 'netcdf_fold_concatenation'] 100 'model_characteristics', 'netcdf_concatenation', 'netcdf_fold_concatenation', \ 101 'netcdf_fold_concatenation_HMT'] 100 102 101 103 ####### ###### ##### #### ### ## # … … 216 218 elif oper == 'netcdf_fold_concatenation': 217 219 ncvar.netcdf_fold_concatenation(opts.values, opts.ncfile, opts.varname) 220 elif oper == 'netcdf_fold_concatenation_HMT': 221 ncvar.netcdf_fold_concatenation_HMT(opts.values, opts.ncfile, opts.varname) 218 222 elif oper == 'opersvarsfiles': 219 223 ncvar.compute_opersvarsfiles(opts.values, opts.varname) -
trunk/tools/nc_var_tools.py
r903 r905 75 75 # netcdf_concatenation: Function to concatenate netCDF files for a given set of variable 76 76 # netcdf_fold_concatenation: Function to concatenate netCDF files in a given folder for a given set of variables 77 # netcdf_fold_concatenation_HMT: Function to concatenate netCDF files in a given folder for a given set of variables giving Header, Middle, Tail for the name files 77 78 # operation_alongdims: Function to operate along different dimensions of a variable 78 79 # operdim: Function to operate along a series of dimensions … … 10066 10067 #dimVar_creation('bottom_top', 'wrfout_d01_1979-12-01_00:00:00_south_north_B3-E3-I1_west_east_B26-E26-I1.nc') 10067 10068 10069 def netcdf_fold_concatenation_HMT(values, ncfile, varn): 10070 """ Function to concatenate netCDF files in a given folder for a given set of variables 10071 giving Header, Middle, Tail for the name files 10072 netcdf_fold_concateation(values, ncfile, varn) 10073 [values]= [fold],[dimname] 10074 [fold]: folder with the location of the netCDF files 10075 [dimname]: dimension along which files should be concatenated 10076 [tail]: tail of the files to concatenate (optional) 10077 [ncfile]= [header],[middle],[tail] 10078 [header] = header of the name of the files to concatenate [ncfile]*[middle]*[tail] 10079 [middle] = middle of the name of the files to concatenate [ncfile]*[middle]*[tail] 10080 [tail] = tail of the name of the files to concatenate [ncfile]*[middle]*[tail] 10081 10082 [varn]= ',' separated list of variables to concatenate 10083 [var1],[var2],[...[varN]] or 'all' for all variables 10084 """ 10085 import subprocess as sub 10086 fname='netcdf_fold_concatenate_HMT' 10087 10088 ofile = 'netcdf_fold_concatenated_HMT.nc' 10089 10090 if values == 'h': 10091 print fname + '_____________________________________________________________' 10092 print netcdf_fold_concatenation_HMT.__doc__ 10093 quit() 10094 10095 fold = values.split(',')[0] 10096 condim = values.split(',')[1] 10097 10098 confiles = gen.files_folder_HMT(fold,ncfile.split(',') 10099 Nfiles = len(confiles) 10100 10101 print ' ' + fname +': concatenating:', Nfiles, 'files' 10102 if Nfiles == 0: 10103 print errormsg 10104 print ' ' + fname + ": there are no files as '" + fold + '/' + ncfile + "*' !!" 10105 quit(-1) 10106 print confiles 10107 10108 # Opening all files 10109 ncobjs = [] 10110 for filen in confiles: 10111 print 'charging: ',filen 10112 ncobjs.append(NetCDFFile(fold + '/' + filen, 'r')) 10113 10114 # Looking for the new length of the concatenation dimension 10115 if not ncobjs[0].dimensions.has_key(condim): 10116 print errormsg 10117 print ' ' + fname + ": files do not have dimensions '" + condim + "' !!!" 10118 quit(-1) 10119 10120 totcondim = 0 10121 for ifile in range(Nfiles): 10122 ncobj = ncobjs[ifile] 10123 totcondim = totcondim + len(ncobjs[ifile].dimensions[condim]) 10124 10125 print "total concatenated dimension '" + condim + "': ", totcondim 10126 10127 # concatenated file creation 10128 ## 10129 newnc = NetCDFFile(ofile, 'w') 10130 10131 # dimension creation 10132 ## 10133 if ncobjs[0].dimensions[condim].isunlimited(): 10134 newnc.createDimension(condim, None) 10135 else: 10136 newnc.createDimension(condim, totcondim) 10137 # Fake variable for the dimension 10138 print 'condim:',condim 10139 dims = [] 10140 dims.append(condim) 10141 newvar = newnc.createVariable('int' + condim, 'i4', tuple(dims)) 10142 newvar[:] = range(totcondim) 10143 10144 # Looping variables 10145 ## 10146 if varn == 'all': 10147 desvars = ncobjs[0].variables 10148 else: 10149 desvars = varn.split(',') 10150 10151 for dvar in desvars: 10152 if not ncobjs[0].variables.has_key(dvar): 10153 print errormsg 10154 print ' ' + fname + ": files do not have variable '" + dvar + "' !!!" 10155 quit(-1) 10156 10157 for dvar in desvars: 10158 print ' ' + fname + ": concatenating '" + dvar + "'..." 10159 objvar = ncobjs[0].variables[dvar] 10160 # creation of dimensions 10161 vardims = objvar.dimensions 10162 for ndim in vardims: 10163 if not newnc.dimensions.has_key(ndim): 10164 vardimo = ncobjs[0].dimensions[ndim] 10165 if vardimo.isunlimited(): 10166 newnc.createDimension(ndim, None) 10167 else: 10168 newnc.createDimension(ndim, len(vardimo)) 10169 # creation of variable 10170 kvar = objvar.dtype 10171 newvar = newnc.createVariable(dvar, kvar, objvar.dimensions) 10172 varattrs = objvar.ncattrs() 10173 for atr in varattrs: 10174 attrv = objvar.getncattr(atr) 10175 newattr = set_attribute(newvar, atr, attrv) 10176 10177 # variable values 10178 # vartotv = np.zeros((newvar.shape), dtype=kvar) 10179 objvar = ncobjs[0].variables[dvar] 10180 10181 if gen.searchInlist(list(objvar.dimensions),condim): 10182 begslicetot = 0 10183 for ifile in range(Nfiles): 10184 slicevartot = [] 10185 if ncobjs[ifile].variables.has_key(dvar): 10186 objvar = ncobjs[ifile].variables[dvar] 10187 for dimn in objvar.dimensions: 10188 ldimfile = len(ncobjs[ifile].dimensions[dimn]) 10189 if dimn == condim: 10190 slicevartot.append(slice(begslicetot,begslicetot+ldimfile)) 10191 begslicetot = begslicetot + ldimfile 10192 else: 10193 slicevartot.append(slice(0,ldimfile)) 10194 newvar[tuple(slicevartot)] = objvar[:] 10195 newnc.sync() 10196 else: 10197 print errormsg 10198 print ' ' + fname + ": file '" + fold + '/' + confiles[ifile] + \ 10199 "' does not have variable '" + dvar + "' !!" 10200 quit(-1) 10201 else: 10202 newvar[:] = objvar[:] 10203 newnc.sync() 10204 10205 # newvar[:] = vartotv 10206 10207 gattrs = ncobjs[0].ncattrs() 10208 for attr in gattrs: 10209 attrv = ncobjs[0].getncattr(attr) 10210 newattr = set_attribute(newnc, attr, attrv) 10211 10212 for ifile in range(Nfiles): 10213 ncobjs[ifile].close() 10214 10215 newnc.sync() 10216 newnc.close() 10217 10218 print "Successfull creation of concatenated file '" + ofile + "' !!!" 10219 10220 return 10221 10068 10222 def netcdf_fold_concatenation(values, ncfile, varn): 10069 10223 """ Function to concatenate netCDF files in a given folder for a given set of variables 10070 10224 netcdf_fold_concateation(values, ncfile, varn) 10071 [values]= [fold],[dimname] folder with the location of the netCDF files10225 [values]= [fold],[dimname] 10072 10226 [fold]: folder with the location of the netCDF files 10073 10227 [dimname]: dimension along which files should be concatenated 10228 [tail]: tail of the files to concatenate (optional) 10074 10229 [ncfile]= header of the name of the files to concatenate [ncfile]* 10075 10230 [varn]= ',' separated list of variables to concatenate
Note: See TracChangeset
for help on using the changeset viewer.