Changeset 1918 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Jun 18, 2018, 4:09:24 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • 'ASCIIfile_stats': Function to provide the statistics of a series of values from an ASCII file
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1917 r1918  
    5252# advance_matDate: Function to advance matrix-date with a matrix-increment
    5353# angle_DegMinSec: Function to transform an angle to Degrees Minutes Seconds
     54# ASCIIfile_stats: Function to provide the statistics of a series of values from an ASCII file
    5455# ASCII_HTML: Function to transform from an ASCII line to HTML codification
    5556# ASCII_LaTeX: Function to transform from an ASCII character to LaTeX codification
     
    1326413265    return minNCvalues
    1326513266
     13267def ASCIIfile_stats(filen, comment='#', kind='R'):
     13268    """ Function to provide the statistics of a series of values from an ASCII file
     13269      filen= name of the file
     13270      comment= ':' separated list of comment characters
     13271      kind= kind of values
     13272        'I': integer
     13273        'R': float
     13274        'D': double
     13275    """
     13276    fname = 'ACIIfile_stats'
     13277
     13278    if not os.path.isfile(filen):
     13279        print errormsg
     13280        print '  ' + fname + ": ASCII file '" + filen + "' does not exist !!"
     13281        quit(-1)
     13282
     13283    comments = str_list(comment, ':')
     13284
     13285    objf = open(filen, 'r')
     13286    values = []
     13287    for line in objf:
     13288        linev = line.replace('\n','').replace('\t','').replace('\r','')
     13289        if len(linev) >= 1:
     13290            ch1 = linev[0:1]
     13291            if not searchInlist(comments, ch1):
     13292                print "'" + linev + "'"
     13293                values.append(typemod(linev,kind))
     13294
     13295    objf.close()
     13296
     13297    matv = np.array(values)
     13298    # Statistics
     13299    minv = np.min(matv)
     13300    maxv = np.max(matv)
     13301    meanv = np.mean(matv)
     13302    stdv = np.std(matv)
     13303    quantv = Quantiles(matv, 20)
     13304   
     13305    print "# Statistics of '" + filen + "' _______"
     13306    print '#@# min:', minv
     13307    print '#@# max:', maxv
     13308    print '#@# mean:', meanv
     13309    print '#@# stddev:', stdv
     13310    print '#@# quantiles:', quantv.quantilesv
     13311
     13312    quantS = []
     13313    for iq in quantv.quantilesv: quantS.append(str(iq))
     13314
     13315    print '#@# allvalues; min:' + str(minv) + ';max:' + str(maxv) + ';mean:' +       \
     13316      str(meanv) + ';stddev:' + str(stdv) + ';quant:' + ','.join(quantS)
     13317
     13318    return
     13319
     13320#ASCIIfile_stats('times_NOCDXWRF2.dat')
     13321
    1326613322#quit()
    1326713323
Note: See TracChangeset for help on using the changeset viewer.