Changeset 1849 in lmdz_wrf


Ignore:
Timestamp:
Mar 25, 2018, 3:00:18 PM (7 years ago)
Author:
lfita
Message:

Working version with projection information added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1848 r1849  
    2268922689                given axis
    2269022690          [globattrfilen]: name of ASCII file with the given metadata to be added as global attributes
    22691             with a line per attribute [attrname] [value] it has to have at least (underparenthesis for description)
    22692             use '#' for comments (use '[unknown]' for that values set-up according to content from file:
    22693               institute_id [value, use '!' for spaces] (CV)
     22691            with a line per attribute [attrname] [value](use '!' for spaces]  (underparenthesis for description)
     22692            use '#' for comments
     22693            File has to have at least:
     22694              institute_id [value] (CV)
    2269422695              institution  [value]
    2269522696              model_id [value] (CORDEX starts with <institute_id>-)
     
    2271522716              basetime [value] (reference time "1949-12-01T00:00:00Z" formatted acording to UNIDATA conventions)
    2271622717              calendar [value] (CF conventions as in driving model)
    22717               grid [value] (according to CF conventions
     22718              grid [value] (according to CF conventions)
    2271822719          [projectfile]: ASCII file with all the CF-mandatory projection information. A line for each attribute. Name of
    2271922720            the variable to be used to refer the projection inside the file will be the same as global attribute `grid'
     
    2278522786        quit(-1)
    2278622787
    22787     pinf = {}
     22788    pattr = {}
    2278822789    opinff = open(projfilen, 'r')
    2278922790    for line in opinff:
    2279022791        if line[0:1] != '#' and len(line) > 1:
    2279122792            linevals = line.replace('\n','').replace('\t','').split(' ')
    22792             pinf[linevals[0]] = [linevals[1].replace('!',' '), linevals[2]]
     22793            if len(linevals) != 3:
     22794                print errormsg
     22795                print '  ' + fname + ": wrong values in file '" + projfilen + "' !!"
     22796                print '    each line has to have three words (space separated values)'
     22797                print "    from line: '" + line + "'"
     22798                print "    values found:", linevals
     22799                print "      remember to use '!' for spaces"
     22800                quit(-1)
     22801            pattr[linevals[0]] = [linevals[1].replace('!',' '), linevals[2]]
    2279322802    opinff.close()
    2279422803
     
    2285822867              tunitsval='minutes')
    2285922868            oaxisv = tvals
    22860      
     22869
    2286122870        # CF values of axis
    2286222871        CFdimvalues = gen.CFcorValues(axn)
     
    2289422903                    CFvardimvalues.append(dn)
    2289522904                    varslice.append(slice(0,len(onc.dimensions[dn])))
    22896                     if dn == dimn and CFdimvalues['length'] != -1: 
     22905                    if dn == dimn and CFdimvalues['length'] != -1:
    2289722906                        CFdimvalues['length'] = len(onc.dimensions[dn])
    2289822907            axsiv = oaxisv[tuple(varslice)]
     
    2292722936              'Y-m-dTH:M:SZ')
    2292822937            secfreq = gen.CFtime_freq(axisv[0], axisv[1], tunits)
    22929             Sfreq = -1
    2293022938            # Labelling frequency
    22931             if np.mod(secfreq, 100 * 365 * 24 * 3600.) == 0.:
    22932                 Sfreq = str(int(secfreq/(100 * 365 * 24 * 3600.))) + 'C'
    22933             elif Sfreq == -1 and np.mod(secfreq, 365 * 24 * 3600.) == 0.:
    22934                 Sfreq = str(int(secfreq/(365 * 24 * 3600.))) + 'Y'
    22935             elif Sfreq == -1 and np.mod(secfreq, 7 * 24 * 3600.) == 0.:
    22936                 Sfreq = str(int(secfreq/(7 * 24 * 3600.))) + 'W'
    22937             elif Sfreq == -1 and np.mod(secfreq, 24 * 3600.) == 0.:
    22938                 Sfreq = str(int(secfreq/(24.*3600.))) + 'D'
    22939             elif Sfreq == -1 and np.mod(secfreq,3600.) == 0.:
    22940                 Sfreq = str(int(secfreq/3600.)) + 'H'
    22941             elif Sfreq == -1 and np.mod(secfreq,60.) == 0.:
    22942                 Sfreq = str(int(secfreq/60.)) + 'M'
    22943             else:
    22944                 # Getting frequency from the global attributes
     22939            if gattr['frequency'] == '[unknown]':
     22940                if np.mod(secfreq, 100 * 365 * 24 * 3600.) == 0.:
     22941                    Sfreq = str(int(secfreq/(100 * 365 * 24 * 3600.))) + 'C'
     22942                elif np.mod(secfreq, 365 * 24 * 3600.) == 0.:
     22943                    Sfreq = str(int(secfreq/(365 * 24 * 3600.))) + 'Y'
     22944                elif np.mod(secfreq, 7 * 24 * 3600.) == 0.:
     22945                    Sfreq = str(int(secfreq/(7 * 24 * 3600.))) + 'W'
     22946                elif np.mod(secfreq, 24 * 3600.) == 0.:
     22947                    Sfreq = str(int(secfreq/(24.*3600.))) + 'D'
     22948                elif np.mod(secfreq,3600.) == 0.:
     22949                    Sfreq = str(int(secfreq/3600.)) + 'H'
     22950                elif np.mod(secfreq,60.) == 0.:
     22951                    Sfreq = str(int(secfreq/60.)) + 'M'
     22952                else:
     22953                    print errormsg
     22954                    print '  ' + fname + ": no frequency has been provided '" +      \
     22955                      gattr['frequency'] + "' but, data from file does not conform "+\
     22956                      "any know time-units!!"
     22957                    print '    frequency from file:', secfreq, ' seconds _______'
     22958                    print '      century:', secfreq/(100 * 365 * 24 * 3600.)
     22959                    print '      year (365d):', secfreq/(365 * 24 * 3600.)
     22960                    print '      week:', secfreq/(7 * 24 * 3600.)
     22961                    print '      day:', secfreq/(24 * 3600.)
     22962                    print '      hour:', secfreq/(3600.)
     22963                    print '      minute:', secfreq/(60.)
     22964                    print '      second:', secfreq
     22965                    print "  provide a frequency value in the global attributes " + \
     22966                      "file '" + globalattrfilen + "' as frequency [value]"
     22967                    quit(-1)
     22968                gattr['frequency'] = Sfreq
     22969            else:
    2294522970                Sfreq = gattr['frequency']
    22946             if gattr['frequency'] == '[unknown]': gattr['frequency'] = Sfreq
    22947             print '    frquency in file:', secfreq, 's', Sfreq
    2294822971
    2294922972    # Renaming dimensions for 2D lon,lat variable-dimensions
     
    2297823001                axinf = axisinf[axn1]
    2297923002                cfaxinf = CFdimvals[axn1]
    22980                 if axinf[0] == fdn: 
     23003                if axinf[0] == fdn:
    2298123004                    cfdimaxes.append(cfaxinf['dimn'])
    2298223005                    if filecfdimn[fdn] is None: filecfdimn[fdn] = cfaxinf['dimn']
     
    2299023013
    2299123014        CFaxisvardimvals[axn] = cfdimaxes
    22992  
     23015
    2299323016    print 'file --> CF equivalencies of dimensions _______'
    2299423017    print filecfdimn
     
    2303623059          '_' + gattr['rcm_version_id'] + '_' + Sfreq + '_' + Sinit + '-' + Sendt +  \
    2303723060          '.nc'
     23061
    2303823062        onewnc = NetCDFFile(filen,'w')
    2303923063        now = dt.datetime.now()
Note: See TracChangeset for help on using the changeset viewer.