Changeset 712 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 25, 2016, 11:22:24 AM (9 years ago)
Author:
lfita
Message:

Imporoving `addvarattrk', by splitting when a string is provided and when a dtype output

Including also the 'unicode' type (assuming RTF-* encoding)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r711 r712  
    20262026    attrname = name of the attribute
    20272027    attrvalue = value of the attribute
    2028     atrtrkind = kind of attribute: 'S', string ('!' as spaces); 'I', integer; 'Inp32', numpy integer 32;
    2029       'R', real; ' npfloat', np.float; 'D', np.float64
     2028    atrtrkind = kind of attribute: 'S', string ('!' as spaces); 'U', unicode ('!' as spaces); 'I', integer;
     2029      'Inp32', numpy integer 32; 'R', real; ' npfloat', np.float; 'D', np.float64
    20302030    """
    2031 
    2032     if attrkind == 'S':
    2033         attrvalue = str(attrval.replace('!', ' '))
    2034     elif attrkind == 'I':
    2035         attrvalue = int(attrval)
    2036     elif attrkind == 'Inp32':
    2037         attrvalue = np.int32(attrval)
    2038     elif attrkind == 'R':
    2039         attrvalue = float(attrval)
    2040     elif attrkind == 'npfloat':
    2041         attrvalue = np.float(attrval)
    2042     elif attrkind == 'D':
    2043         attrvalue = np.float64(attrval)
     2031    fname = 'set_attributek'
     2032    validk = {'S': 'string', 'U': 'unicode', 'I': 'integer',                         \
     2033      'Inp32': 'integer long (np.int32)', 'R': 'float', 'npfloat': 'np.float',       \
     2034      'D': 'float long (np.float64)'}
     2035
     2036    if type(attrkind) == type('s'):
     2037        if attrkind == 'S':
     2038            attrvalue = str(attrval.replace('!', ' '))
     2039        elif attrkind == 'U':
     2040            attrvalue = unicode(attrval.replace('!',' '))
     2041        elif attrkind == 'I':
     2042            attrvalue = np.int(attrval)
     2043        elif attrkind == 'R':
     2044            attrvalue = float(attrval)
     2045        elif attrkind == 'npfloat':
     2046            attrvalue = np.float(attrval)
     2047        elif attrkind == 'D':
     2048            attrvalue = np.float64(attrval)
     2049        else:
     2050            print errormsg
     2051            print '  ' + fname + ": '" + attrkind + "' kind of attribute is not ready!"
     2052            print '  valid values: _______'
     2053            for key in validk.keys():
     2054                print '    ', key,':', validk[key]
     2055            quit(-1)
    20442056    else:
    2045         print errormsg
    2046         print '    set_attributek: "' + attrkind + '" kind of attribute is not ready!'
    2047         quit(-1)
     2057        if attrkind == type(str('a')):
     2058            attrvalue = str(attrval.replace('!', ' '))
     2059        elif attrkind == type(unicode('a')):
     2060            attrvalue = unicode(attrval.replace('!',' '))
     2061        elif attrkind == type(np.int(1)):
     2062            attrvalue = np.int(attrval)
     2063        elif attrkind == np.dtype('i'):
     2064            attrvalue = np.int32(attrval)
     2065        elif attrkind == type(float(1.)):
     2066            attrvalue = float(attrval)
     2067        elif attrkind == type(np.float(1.)):
     2068            attrvalue = np.float(attrval)
     2069        elif attrkind == np.dtype('float32'):
     2070            attrvalue = np.array(attrval, dtype='f')
     2071        elif attrkind == type(np.float32(1.)):
     2072            attrvalue = np.float32(attrval)
     2073        elif attrkind == type(np.float64(1.)):
     2074            attrvalue = np.float64(attrval)
     2075        else:
     2076            print errormsg
     2077            print '  ' + fname + ": '" + attrkind + "' kind of attribute is not ready!"
     2078            print '  valid values: _______'
     2079            for key in validk.keys():
     2080                print '    ', key,':', validk[key]
     2081            quit(-1)
    20482082
    20492083    attvar = ncv.ncattrs()
     
    20522086
    20532087    attr = ncv.setncattr(attrname, attrvalue)
    2054    
     2088       
    20552089    return attr
    20562090
Note: See TracChangeset for help on using the changeset viewer.