Changeset 1867 in lmdz_wrf for trunk/tools/nc_var_tools.py


Ignore:
Timestamp:
Mar 26, 2018, 8:09:39 PM (7 years ago)
Author:
lfita
Message:

Adding and using:

  • `getvar_listonc': Function to get a variable object from a list of netCDF file object
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1866 r1867  
    7777# get_1str_nc: Function to get 1 string value in a netCDF variable as a chain of 1char values
    7878# get_attribute: Function to get an attribute from a netCDF file
     79# getvar_listonc: Function to get a variable object from a list of netCDF file object
    7980# get_namelist_vars: Function to get namelist-like  values ([varname] = [value])
    8081# get_str_nc: Function to get string values in a netCDF variable as a chains of 1char values
     
    2286622867#quit()
    2286722868
     22869def getvar_listonc(varn, listoc):
     22870    """ Function to get a variable object from a list of netCDF file object
     22871      varn= name of the variable to be found
     22872      listoc= list of NetCDF file objects
     22873    """
     22874    fname = 'getvar_listonc'
     22875
     22876    allfs = {}
     22877    fns = []
     22878    for ioc in range(len(listoc)):
     22879        oc = listoc[ioc]
     22880        allfs[oc.filepath] = [oc.dimensions, oc.variables.keys()]
     22881        fns.append(oc.filepath)
     22882
     22883    # Looping for all files
     22884    found = False
     22885    for ioc in range(len(listoc)):
     22886        oc = listoc[ioc]
     22887        oncvars = allfs[ioc]
     22888        oncvars.sort()
     22889        if oc.variables.has_key(varn):
     22890            ov = oc.variables[varn]
     22891            found = True
     22892            break
     22893
     22894    if not found:
     22895        print errormsg
     22896        print '  ' + fname + "': any netcdf file '" + allfs.keys() + "' contain " + \
     22897          "variable '" + varn + "' !!"
     22898        print '    available ones _______'
     22899        ioc = 0
     22900        for fn in fns:
     22901            print fn, ':', allfs[fn][1]
     22902            listoc[ioc].close()
     22903            ioc = ioc + 1
     22904            quit(-1)
     22905
     22906    return ov
     22907
    2286822908def projection2D_information(PROJn, AXinf, projinf, dx, dy, oncs):
    2286922909    """ Function to get the basic information of the 2D-projection: new dimensions, variables, ...
     
    2287222912        AXinf: dictionary with the informatino of the axes 'X', 'Y', 'Z', 'T'
    2287322913        projinf: dictionary with all the information of the projection
    22874         oncs= netCDF objects from which retrieve the information
     22914        oncs= list of netCDF objects from which retrieve the information
    2287522915      Returns:
    2287622916        newdim: dictionary new dimensions to be added {dimn1: dimlength1, ..., dimnN: dimlengthN}
     
    2298123021        newvar['rlon'] = ['grid_longitude', 'longitude in rotated pole grid',        \
    2298223022          'degrees']
    22983         found = Flase
    22984         fileNs = ''
    22985         for ionc in range(len(oncs)):
    22986             onc = oncs[ionc]
    22987             fileNs = fileNs + onc.filepath + ', '
    22988             if gen.searchInlist(onc.variables.keys(),projinf['fileXrefvals'][0]):
    22989                 oXvar = onc.variables[projinf['fileXvals'][0]]
    22990                 break
    22991         if not found:
    22992             print errormsg
    22993             print '  ' + fname + ": files: " + fileNs + " do not have variable " +   \
    22994               "with X ref values '" + projinf['fileXrefvals'][0] + "' !!"
    22995             print '    available ones:', onc.variables.keys()
    22996             quit(-1)
     23023
     23024        ovar = getvar_listonc(projinf['fileXvals'][0], oncs)
     23025        oXvar = ovar[0]
    2299723026        if len(oXvar.shape) == 3:
    2299823027            newvarv['rlon'] = oXvar[0,0,:]
     
    2301423043        newdim['rlat'] = dy
    2301523044        newvar['rlat'] = ['grid_latitude', 'latitude in rotated pole grid', 'degrees']
    23016         if not gen.searchInlist(onc.variables.keys(),projinf['fileYrefvals'][0]):
    23017             print errormsg
    23018             print '  ' + fname + ": file does not have variable with Y ref values '"+\
    23019               projinf['fileYrefvals'][0] + "' !!"
    23020             print '    available ones:', onc.variables.keys()
    23021             quit(-1)
    23022         oYvar = onc.variables[projinf['fileYvals'][0]]
     23045        ovar = getvar_listonc(projinf['fileYvals'][0], oncs)
     23046        oYvar = ovar[0]
    2302323047        if len(oYvar.shape) == 3:
    2302423048            newvarv['rlat'] = oYvar[0,0,:]
     
    2341123435        Varns = gen.str_list(variable, ',')
    2341223436
     23437    # Joining all netCDF files
     23438    allonc = exonc + [onc]
     23439    allfileNs = {}
     23440    for ionc in range(len(allonc)):
     23441        onc = allonc[ionc]
     23442        allfileNs[onc.filepath] = [onc.dimensions.keys(), onc.variables.keys()]
     23443
    2341323444    # Creation of new file name
    2341423445    # This will done following CORDEX rules (getting information from the ASCII
     
    2343023461    axes = ['X', 'Y', 'Z', 'T']
    2343123462
    23432     oncvars = onc.variables.keys()
    23433     oncvars.sort()
    23434 
    2343523463    # Axes information: dimensions and variables dependency
    2343623464    # CF-dimensions of a given axis
     
    2344023468    # CD-fimensions of the CF-variables for each axis
    2344123469    CFaxisvardimvals = {}
     23470
    2344223471    for axn in axes:
    2344323472        axisv = axisinf[axn]
     
    2344723476        # Axis values from file
    2344823477        if vardimn != 'WRFtime':
    23449             if not onc.variables.has_key(vardimn):
    23450                 print errormsg
    23451                 print '  '+fname+"': netcdf file '"+ ncfile + "' does not contain "+ \
    23452                   "variable '" + vardimn + "' !!"
    23453                 print '    available ones:', oncvars
    23454                 onc.close()
    23455                 quit(-1)
    23456             oaxisv = onc.variables[vardimn]
     23478            oaxisv = getvar_listonc(vardimn, allloncs)
    2345723479        else:
    2345823480            print infmsg
    2345923481            print '    ' + fname + ": creation of variable 'time' from WRF 'Times' !!"
    23460             odimvar = onc.variables['Times']
     23482            odimvar = getvar_listonc('Times', allloncs)
    2346123483            timewrfv = odimvar[:]
    2346223484            refT = gen.datetimeStr_conversion(gattr['basetime'], 'Y-m-dTH:M:SZ',     \
     
    2357723599        newDim, newVar, newVARv, nonCFproj = projection2D_information(gattr['grid'], \
    2357823600          CFdimvals, pattr, filedimvals['X'].shape[1], filedimvals['X'].shape[0],    \
    23579           exonc + [onc])
     23601          alloncs)
    2358023602
    2358123603    if len(newDim.keys()) > 0:
Note: See TracChangeset for help on using the changeset viewer.