# PyNCplot ## e.g. # to_OBSstations.py -d bases_antartida.desc -f bases_antartida.dat # Python to transform from a given stations list format to standard 'OBSstations.csv' # From L. Fita work in different places: CCRC (Australia), LMD (France) # More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot # # pyNCplot and its component nc_var.py comes with ABSOLUTELY NO WARRANTY. # This work is licendes under a Creative Commons # Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0) # ## Python script to indepdententy test different components ## L. Fita, CIMA. ####### ####### ##### ##### #### ### ## # from optparse import OptionParser import numpy as np from netCDF4 import Dataset as NetCDFFile import os import re import subprocess as sub import numpy.ma as ma import matplotlib as mpl mpl.use('Agg') from matplotlib.pylab import * import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap # Importing generic tools file 'generic_tools.py' and the others import generic_tools as gen import nc_var_tools as ncvar import drawing_tools as drw import diag_tools as diag import geometry_tools as geo # Importing Fortran modules and the others import module_ForDef as fdef import module_ForDiag as fdiag import module_ForGen as fgen import module_ForSci as fsci parser = OptionParser() parser.add_option("-f", "--StFile", dest="Stfile", help="File with the stations values", metavar="FILENAME") parser.add_option("-g", "--Debug", dest="debug", help="Activate debug (Not passed, assumed no)", metavar="VALUE") parser.add_option("-d", "--DescFile", dest="descfile", help="File with the description of the content", metavar="FILENAME") (opts, args) = parser.parse_args() main = 'to_OBSstation.py' ####### ####### ## MAIN ####### if opts.debug is None: debug = False else: debug = True # Expected description values descstvals = ['station_name', 'WMOid', 'longitude', 'lon_deg', 'lon_min', 'lon_sec', \ 'latitude', 'lat_deg', 'lat_min', 'lat_sec', 'height', 'prov', 'country', \ 'nice_name', 'add1', 'add2'] DESCstvals = {'station_name': ['S'], 'WMOid': ['I'], 'longitude': ['F'], \ 'lon_deg': ['I'], 'lon_min': ['I'], 'lon_sec': ['F'], 'latitude': ['F'], \ 'lat_deg': ['I'], 'lat_min': ['I'], 'lat_sec': ['F'], 'height': ['F'], \ 'prov': ['S'], 'country': ['S'], 'nice_name': ['S'], 'add1': ['S'], 'add2': ['S']} directdescstvals = ['station_name', 'WMOid', 'height', 'prov', 'country', \ 'nice_name', 'add1', 'add2'] # Non numeric values NOnum = ['sepchar', 'spacec'] ofile = 'sub_OBSstations.csv' # Reading description of = open(opts.descfile, 'r') vals = {} for line in of: if len(line) > 2 and line[0:1] != '#': linevals = line.replace('\n','').replace('\t','').replace('\r', \ '').replace(' ','').split('=') if not gen.searchInlist(NOnum, linevals[0]): vals[linevals[0]] = int(linevals[1]) else: vals[linevals[0]] = linevals[1] of.close() if debug: print ' Values to search _______' gen.printing_dictionary(vals) lvals = list(vals.keys()) if not gen.searchInlist(lvals, 'sepchar'): print gen.errormsg print ' ' + main + ": no split character provided !!" print " description file '" + opts.descfile + "' must content one entry as "+ \ "sepchar = [char] !!" quit(-1) if not gen.searchInlist(lvals, 'Sspacechar'): vals['Ssepchar'] = '!' spacec = vals['Ssepchar'] # Getting values oof = open(ofile, 'w') oif = open(opts.Stfile, 'r') for line in oif: stval = {} if len(line) > 2 and line[0:1] != '#': linevals = line.replace('\n','').replace('\t','').replace('\r', \ '') linev = linevals.split(vals['sepchar']) if debug: print 'line values _______' Nvals = len(linev) for iv in range(Nvals): if gen.searchInlist(vals.values(),iv): obsval = gen.dictionary_key(vals, iv) print ' ', iv, ':', linev[iv], '<>', obsval else: print ' ', iv, ':', linev[iv] if vals.has_key(descstvals[2]) and not vals.has_key(descstvals[3]): ic = vals[descstvals[2]] av = np.float(linev[ic]) DMS = gen.angle_DegMinSec(av) stval[descstvals[2]] = av for i in range(3): stval[descstvals[3+i]] = DMS[i] if vals.has_key(descstvals[3]) and not vals.has_key(descstvals[2]): ic = vals[descstvals[3]] DMS = [int(linev[ic]), int(linev[ic+1]), np.float(linev[ic+2])] av = gen.DegMinSec_angle(DMS[0], DMS[1], DMS[2]) stval[descstvals[2]] = av for i in range(3): stval[descstvals[3+i]] = DMS[i] if vals.has_key(descstvals[6]) and not vals.has_key(descstvals[7]): ic = vals[descstvals[6]] av = np.float(linev[ic]) DMS = gen.angle_DegMinSec(av) stval[descstvals[6]] = av for i in range(3): stval[descstvals[7+i]] = DMS[i] if vals.has_key(descstvals[7]) and not vals.has_key(descstvals[6]): ic = vals[descstvals[7]] DMS = [int(linev[ic]), int(linev[ic+1]), np.float(linev[ic+2])] av = gen.DegMinSec_angle(DMS[0], DMS[1], DMS[2]) stval[descstvals[6]] = av for i in range(3): stval[descstvals[7+i]] = DMS[i] for Sd in directdescstvals: Ddescv = DESCstvals[Sd] if vals.has_key(Sd): ic = vals[Sd] stval[Sd] = gen.typemod(linev[ic].replace(spacec, '!'), Ddescv[0]) else: if Ddescv[0] == 'S': missval = '-' elif Ddescv[0] == 'I': missval = gen.fillValueI elif Ddescv[0] == 'F': missval = gen.fillValueF stval[Sd] = missval Sline = '' for Sd in descstvals: if Sd == descstvals[0]: Sline = stval[Sd] else: Sline = Sline + ',' + str(stval[Sd]) oof.write(Sline+'\n') oif.close() oof.close() print main + ": successfull written of file '" + ofile + "' !!" #quit()