Changeset 1491 in lmdz_wrf


Ignore:
Timestamp:
Apr 4, 2017, 3:11:30 PM (8 years ago)
Author:
lfita
Message:

Adding in `setvar_asciivalues':

  • Abailability to read a 3D field
  • '#' As commentary character
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1490 r1491  
    76057605    """ Function to set de values of a variable with an ASCII file (common
    76067606          Fortran-like format)
    7607         values= asciifile: ASCII file with a Fortran-like structure
     7607        values= asciifile: ASCII file with a Fortran-like structure ('#' for commentaries)
    76087608          [dim1]_1 [[dim2]_1 ... [dim2]_M]
    76097609          ...
     
    76517651        valfinal = np.zeros((1), dtype=vartype)
    76527652        for line in objasciif:
    7653             val = (line.replace('\n','').replace(' ','').replace('\t',''))
    7654             if vartype != '|S1':
    7655                 valfinal[0] = gen.retype(val, vartype)
    7656                 if it <= values.shape[0]-1:
    7657                     values[it] = gen.retype(valfinal[0], vartype)
    7658                     it = it +1
    7659             else:
    7660                 for i1 in range(len(val)):
    7661                     values[it] = val.replace('!',' ')
     7653            if line[0:1] != '#':
     7654                val = (line.replace('\n','').replace(' ','').replace('\t',''))
     7655                if vartype != '|S1':
     7656                    valfinal[0] = gen.retype(val, vartype)
     7657                    if it <= values.shape[0]-1:
     7658                        values[it] = gen.retype(valfinal[0], vartype)
     7659                        it = it +1
     7660                else:
     7661                    for i1 in range(len(val)):
     7662                        values[it] = val.replace('!',' ')
    76627663    elif Ndims == 2:
    76637664        iline=0
    76647665        for line in objasciif:
    7665             vals = line.replace('\n','').replace('\t','').split(' ')
    7666             if vartype != '|S1':
    7667                 if len(vals) != values.shape[1]:
    7668                     print errormsg
    7669                     print '  ' + fname + ': given: ',len(vals),' but variable ' +    \
    7670                       'requires: ',values.shape[1],'!!!'
    7671                     exit(-1)
    7672                 for i1 in range(len(vals)):
    7673                     values[iline,i1] = gen.retype(vals[i1], vartype)
    7674             else:
    7675                 for i1 in range(len(vals)):
    7676                     values[iline,i1] = vals[i1].replace('!',' ')
    7677             iline=iline+1
     7666            if line[0:1] != '#':
     7667                vals = line.replace('\n','').replace('\t','').split(' ')
     7668                if vartype != '|S1':
     7669                    if len(vals) != values.shape[1]:
     7670                        print errormsg
     7671                        print '  ' + fname + ': given: ',len(vals),' but variable '+ \
     7672                          'requires: ',values.shape[1],'!!!'
     7673                        exit(-1)
     7674                    for i1 in range(len(vals)):
     7675                        values[iline,i1] = gen.retype(vals[i1], vartype)
     7676                else:
     7677                    for i1 in range(len(vals)):
     7678                        values[iline,i1] = vals[i1].replace('!',' ')
     7679                iline=iline+1
     7680    elif Ndims == 3:
     7681        d0 = objvar.shape[0]
     7682        d1 = objvar.shape[1]
     7683        d2 = objvar.shape[2]
     7684        iline=0
     7685        icol=0
     7686        for line in objasciif:
     7687            if line[0:1] != '#' and len(line) > 2:
     7688                vals = line.replace('\n','').replace('\t','').split(' ')
     7689                if vals.count('') != 0: vals.remove('')
     7690                if vartype != '|S1':
     7691                    if len(vals) != values.shape[2]:
     7692                        print vals
     7693                        print errormsg
     7694                        print '  ' + fname + ': given: ',len(vals),' but variable '+ \
     7695                          'requires: ',values.shape[2],'!!!'
     7696                        exit(-1)
     7697                    for i1 in range(len(vals)):
     7698                        values[icol,iline,i1] = gen.retype(vals[i1], vartype)
     7699                else:
     7700                    for i1 in range(len(vals)):
     7701                        values[icol,iline,i1] = vals[i1].replace('!',' ')
     7702                iline=iline+1
     7703                if iline > d1-1:
     7704                    iline = 0
     7705                    icol = icol + 1
    76787706    else:
    76797707        print errormsg
    7680         print '  ' + fname + ': number of dimensions',Ndims,'not ready !!!!'
     7708        print '  ' + fname + ': number of dimensions', Ndims, 'not ready !!!!'
    76817709        quit(-1)
    76827710
Note: See TracChangeset for help on using the changeset viewer.