Changeset 1883 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 3, 2018, 10:21:23 PM (7 years ago)
Author:
lfita
Message:

Workiing version of 'splitfile_dim'

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1882 r1883  
    153153# spacemean: Function to retrieve a space mean series from a multidimensional variable of a file
    154154# 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)
    155156# statcompare_files: Python script to statistically compare two different files
    156157# subbasin: Function to retrieve the subbasin (all the sub-flows untila a given lon,lat)
     
    204205  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
    205206  'setvar_nc', 'sorttimesmat', 'spacemean', 'SpatialWeightedMean',                   \
    206   'statcompare_files',                                                               \
     207  'splitfile_dim', 'statcompare_files',                                              \
    207208  'subbasin', 'submns', 'subyrs', 'TimeInf', 'time_reset',                           \
    208209  'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr',    \
     
    461462elif oper == 'SpatialWeightedMean':
    462463    ncvar.SpatialWeightedMean(opts.values, opts.ncfile, opts.varname)
     464elif oper == 'splitfile_dim':
     465    ncvar.splitfile_dim(opts.values, opts.ncfile, opts.varname)
    463466elif oper == 'statcompare_files':
    464467    ncvar.statcompare_files(opts.values)
  • trunk/tools/nc_var_tools.py

    r1882 r1883  
    142142# spacemean: Function to retrieve a space mean series from a multidimensional variable of a file
    143143# 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)
    144145# statcompare_files: Python script to statistically compare two different files
    145146# submns: Function to retrieve a series of months from a file
     
    2406024061    return
    2406124062
    24062 
     24063def 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
    2406324196#quit()
Note: See TracChangeset for help on using the changeset viewer.