Changeset 905 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 19, 2016, 9:46:06 PM (8 years ago)
Author:
lfita
Message:

Adding `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

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r900 r905  
    4040  'ivattrs', 'maskvar', 'model_characteristics',                                     \
    4141  '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',                         \
    4344  'pinterp', 'remapnn',                                                              \
    4445  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
     
    9798# Operations which file name is not a real file
    9899NotCheckingFile = ['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']
    100102
    101103####### ###### ##### #### ### ## #
     
    216218elif oper == 'netcdf_fold_concatenation':
    217219    ncvar.netcdf_fold_concatenation(opts.values, opts.ncfile, opts.varname)
     220elif oper == 'netcdf_fold_concatenation_HMT':
     221    ncvar.netcdf_fold_concatenation_HMT(opts.values, opts.ncfile, opts.varname)
    218222elif oper == 'opersvarsfiles':
    219223    ncvar.compute_opersvarsfiles(opts.values, opts.varname)
  • trunk/tools/nc_var_tools.py

    r903 r905  
    7575# netcdf_concatenation: Function to concatenate netCDF files for a given set of variable
    7676# 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
    7778# operation_alongdims: Function to operate along different dimensions of a variable
    7879# operdim: Function to operate along a series of dimensions
     
    1006610067#dimVar_creation('bottom_top', 'wrfout_d01_1979-12-01_00:00:00_south_north_B3-E3-I1_west_east_B26-E26-I1.nc')
    1006710068
     10069def 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
    1006810222def netcdf_fold_concatenation(values, ncfile, varn):
    1006910223    """ Function to concatenate netCDF files in a given folder for a given set of variables
    1007010224    netcdf_fold_concateation(values, ncfile, varn)
    10071       [values]= [fold],[dimname] folder with the location of the netCDF files
     10225      [values]= [fold],[dimname]
    1007210226        [fold]: folder with the location of the netCDF files
    1007310227        [dimname]: dimension along which files should be concatenated
     10228        [tail]: tail of the files to concatenate (optional)
    1007410229      [ncfile]= header of the name of the files to concatenate [ncfile]*
    1007510230      [varn]= ',' separated list of variables to concatenate
Note: See TracChangeset for help on using the changeset viewer.