Changeset 1753 in lmdz_wrf


Ignore:
Timestamp:
Dec 21, 2017, 3:57:15 PM (8 years ago)
Author:
lfita
Message:

Adding:

  • `getting_fixedline': Function to get the values from a line of text with fixed lenght of different values
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1752 r1753  
    2727fillValueD = 1.e20
    2828fillValueC = '-'
     29fillValueS = '-'
     30fillValueB = 'None'
    2931fillValueI = -99999
    3032fillValueI16 = -99999
     
    8284# get_configuration: Function to get the configuration from an ASCII external file
    8385# get_specdictionary_HMT: Function to get specific values from a dictionary by selcting that keys with H*M*T
     86# getting_fixedline: Function to get the values from a line of text with fixed lenght of different values
    8487# grib_CFequiv: Function to provide the CF name of a GRIB variable code number
    8588# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
     
    46894692    fname='typemod'
    46904693
    4691     if typeval == 'int':
     4694    if typeval == 'int' or typeval == 'I':
    46924695        return int(value)
    46934696    elif typeval == 'long':
     
    47014704    elif typeval == 'bool':
    47024705        return bool(value)
     4706    elif typeval == 'B':
     4707        return Str_Bool(value)
    47034708    elif typeval == 'list':
    47044709        newval = []
     
    47384743    elif typeval == 'float32':
    47394744        return np.float32(value)
    4740     elif typeval == 'np.float64':
     4745    elif typeval == 'np.float64' or typeval == 'D':
    47414746        return np.float64(value)
    47424747    elif typeval == 'float64':
     
    1254612551    return narray
    1254712552
     12553def getting_fixedline(line, cuts, types):
     12554    """ Function to get the values from a line of text with fixed lenght of different values
     12555      line: line with values
     12556      cuts: character number where a value ends
     12557      types: consecutive type of values
     12558        'I': integer
     12559        'R': real
     12560        'D': float64
     12561        'S': string
     12562        'B': boolean
     12563    >>> Sline='   87007 03012015  25.6   6.4             9.4    5   15'
     12564    >>> getting_fixedline(Sline, [8, 17, 23, 29, 36, 40, 45, 50], ['I', 'R', 'R', 'R', 'I', 'R', 'R', 'R'])
     12565    [87007, 3012015.0, 25.6, 6.4, -99999, -99999, 9.4, 5.0, 15.0]
     12566    """
     12567    fname = 'getting_fixedline'
     12568
     12569    values = []
     12570    val = line[0:cuts[0]+1]
     12571    if len(val.replace(' ','')) >= 1:
     12572        values.append(typemod(val, types[0]))
     12573    else:
     12574        if types[0] == 'I': values.append(fillValueI)
     12575        elif types[0] == 'R': values.append(fillValueF)
     12576        elif types[0] == 'D': values.append(fillValueF)
     12577        elif types[0] == 'S': values.append(fillValueS)
     12578        elif types[0] == 'B': values.append(fillValueB)
     12579
     12580    Ncuts = len(cuts)
     12581    for ic in range(1,Ncuts):
     12582        val = line[cuts[ic-1]:cuts[ic]+1]
     12583        print 'Lluis val:', val
     12584        if len(val.replace(' ','')) >= 1:
     12585            values.append(typemod(val, types[ic]))
     12586        else:
     12587            if types[0] == 'I': values.append(fillValueI)
     12588            elif types[0] == 'R': values.append(fillValueF)
     12589            elif types[0] == 'D': values.append(fillValueF)
     12590            elif types[0] == 'S': values.append(fillValueS)
     12591            elif types[0] == 'B': values.append(fillValueB)
     12592
     12593    # Last value
     12594    Lline = len(line)
     12595    val = line[cuts[Ncuts-1]:Lline]
     12596    if len(val.replace(' ','')) >= 1:
     12597        values.append(typemod(val, types[Ncuts-1]))
     12598    else:
     12599        if types[0] == 'I': values.append(fillValueI)
     12600        elif types[0] == 'R': values.append(fillValueF)
     12601        elif types[0] == 'D': values.append(fillValueF)
     12602        elif types[0] == 'S': values.append(fillValueS)
     12603        elif types[0] == 'B': values.append(fillValueB)
     12604
     12605    return values
     12606
    1254812607#quit()
    1254912608
Note: See TracChangeset for help on using the changeset viewer.