[2752] | 1 | # PyNCplot |
---|
| 2 | ## e.g. # to_OBSstations.py -d bases_antartida.desc -f bases_antartida.dat |
---|
| 3 | # Python to transform from a given stations list format to standard 'OBSstations.csv' |
---|
| 4 | # From L. Fita work in different places: CCRC (Australia), LMD (France) |
---|
| 5 | # More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot |
---|
| 6 | # |
---|
| 7 | # pyNCplot and its component nc_var.py comes with ABSOLUTELY NO WARRANTY. |
---|
| 8 | # This work is licendes under a Creative Commons |
---|
| 9 | # Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0) |
---|
| 10 | # |
---|
| 11 | ## Python script to indepdententy test different components |
---|
| 12 | ## L. Fita, CIMA. |
---|
| 13 | ####### ####### ##### ##### #### ### ## # |
---|
| 14 | |
---|
| 15 | from optparse import OptionParser |
---|
| 16 | import numpy as np |
---|
| 17 | from netCDF4 import Dataset as NetCDFFile |
---|
| 18 | import os |
---|
| 19 | import re |
---|
| 20 | import subprocess as sub |
---|
| 21 | import numpy.ma as ma |
---|
| 22 | import matplotlib as mpl |
---|
| 23 | mpl.use('Agg') |
---|
| 24 | from matplotlib.pylab import * |
---|
| 25 | import matplotlib.pyplot as plt |
---|
| 26 | from mpl_toolkits.basemap import Basemap |
---|
| 27 | |
---|
| 28 | # Importing generic tools file 'generic_tools.py' and the others |
---|
| 29 | import generic_tools as gen |
---|
| 30 | import nc_var_tools as ncvar |
---|
| 31 | import drawing_tools as drw |
---|
| 32 | import diag_tools as diag |
---|
| 33 | import geometry_tools as geo |
---|
| 34 | |
---|
| 35 | # Importing Fortran modules and the others |
---|
| 36 | import module_ForDef as fdef |
---|
| 37 | import module_ForDiag as fdiag |
---|
| 38 | import module_ForGen as fgen |
---|
| 39 | import module_ForSci as fsci |
---|
| 40 | |
---|
| 41 | parser = OptionParser() |
---|
| 42 | parser.add_option("-f", "--StFile", dest="Stfile", help="File with the stations values", metavar="FILENAME") |
---|
[2813] | 43 | parser.add_option("-g", "--Debug", dest="debug", help="Activate debug (Not passed, assumed no)", metavar="VALUE") |
---|
[2752] | 44 | parser.add_option("-d", "--DescFile", dest="descfile", help="File with the description of the content", metavar="FILENAME") |
---|
| 45 | (opts, args) = parser.parse_args() |
---|
| 46 | |
---|
| 47 | main = 'to_OBSstation.py' |
---|
| 48 | ####### ####### |
---|
| 49 | ## MAIN |
---|
| 50 | ####### |
---|
[2813] | 51 | if opts.debug is None: |
---|
| 52 | debug = False |
---|
| 53 | else: |
---|
| 54 | debug = True |
---|
| 55 | |
---|
[2752] | 56 | # Expected description values |
---|
| 57 | descstvals = ['station_name', 'WMOid', 'longitude', 'lon_deg', 'lon_min', 'lon_sec', \ |
---|
| 58 | 'latitude', 'lat_deg', 'lat_min', 'lat_sec', 'height', 'prov', 'country', \ |
---|
| 59 | 'nice_name', 'add1', 'add2'] |
---|
| 60 | DESCstvals = {'station_name': ['S'], 'WMOid': ['I'], 'longitude': ['F'], \ |
---|
| 61 | 'lon_deg': ['I'], 'lon_min': ['I'], 'lon_sec': ['F'], 'latitude': ['F'], \ |
---|
| 62 | 'lat_deg': ['I'], 'lat_min': ['I'], 'lat_sec': ['F'], 'height': ['F'], \ |
---|
| 63 | 'prov': ['S'], 'country': ['S'], 'nice_name': ['S'], 'add1': ['S'], 'add2': ['S']} |
---|
| 64 | directdescstvals = ['station_name', 'WMOid', 'height', 'prov', 'country', \ |
---|
| 65 | 'nice_name', 'add1', 'add2'] |
---|
| 66 | |
---|
| 67 | # Non numeric values |
---|
| 68 | NOnum = ['sepchar', 'spacec'] |
---|
| 69 | |
---|
| 70 | ofile = 'sub_OBSstations.csv' |
---|
| 71 | |
---|
| 72 | # Reading description |
---|
| 73 | of = open(opts.descfile, 'r') |
---|
| 74 | vals = {} |
---|
| 75 | for line in of: |
---|
| 76 | if len(line) > 2 and line[0:1] != '#': |
---|
| 77 | linevals = line.replace('\n','').replace('\t','').replace('\r', \ |
---|
| 78 | '').replace(' ','').split('=') |
---|
| 79 | if not gen.searchInlist(NOnum, linevals[0]): |
---|
| 80 | vals[linevals[0]] = int(linevals[1]) |
---|
| 81 | else: |
---|
| 82 | vals[linevals[0]] = linevals[1] |
---|
| 83 | |
---|
| 84 | of.close() |
---|
| 85 | |
---|
[2813] | 86 | if debug: |
---|
| 87 | print ' Values to search _______' |
---|
| 88 | gen.printing_dictionary(vals) |
---|
| 89 | |
---|
[2752] | 90 | lvals = list(vals.keys()) |
---|
| 91 | if not gen.searchInlist(lvals, 'sepchar'): |
---|
| 92 | print gen.errormsg |
---|
| 93 | print ' ' + main + ": no split character provided !!" |
---|
| 94 | print " description file '" + opts.descfile + "' must content one entry as "+ \ |
---|
| 95 | "sepchar = [char] !!" |
---|
| 96 | quit(-1) |
---|
| 97 | if not gen.searchInlist(lvals, 'Sspacechar'): vals['Ssepchar'] = '!' |
---|
| 98 | |
---|
| 99 | spacec = vals['Ssepchar'] |
---|
| 100 | # Getting values |
---|
| 101 | oof = open(ofile, 'w') |
---|
| 102 | oif = open(opts.Stfile, 'r') |
---|
| 103 | for line in oif: |
---|
| 104 | stval = {} |
---|
| 105 | if len(line) > 2 and line[0:1] != '#': |
---|
| 106 | linevals = line.replace('\n','').replace('\t','').replace('\r', \ |
---|
| 107 | '') |
---|
| 108 | linev = linevals.split(vals['sepchar']) |
---|
| 109 | |
---|
[2813] | 110 | if debug: |
---|
| 111 | print 'line values _______' |
---|
| 112 | Nvals = len(linev) |
---|
| 113 | for iv in range(Nvals): |
---|
| 114 | if gen.searchInlist(vals.values(),iv): |
---|
| 115 | obsval = gen.dictionary_key(vals, iv) |
---|
| 116 | print ' ', iv, ':', linev[iv], '<>', obsval |
---|
| 117 | else: |
---|
| 118 | print ' ', iv, ':', linev[iv] |
---|
| 119 | |
---|
[2752] | 120 | if vals.has_key(descstvals[2]) and not vals.has_key(descstvals[3]): |
---|
| 121 | ic = vals[descstvals[2]] |
---|
| 122 | av = np.float(linev[ic]) |
---|
| 123 | DMS = gen.angle_DegMinSec(av) |
---|
| 124 | stval[descstvals[2]] = av |
---|
| 125 | for i in range(3): stval[descstvals[3+i]] = DMS[i] |
---|
| 126 | |
---|
| 127 | if vals.has_key(descstvals[3]) and not vals.has_key(descstvals[2]): |
---|
| 128 | ic = vals[descstvals[3]] |
---|
| 129 | DMS = [int(linev[ic]), int(linev[ic+1]), np.float(linev[ic+2])] |
---|
| 130 | av = gen.DegMinSec_angle(DMS[0], DMS[1], DMS[2]) |
---|
| 131 | stval[descstvals[2]] = av |
---|
| 132 | for i in range(3): stval[descstvals[3+i]] = DMS[i] |
---|
| 133 | |
---|
| 134 | if vals.has_key(descstvals[6]) and not vals.has_key(descstvals[7]): |
---|
| 135 | ic = vals[descstvals[6]] |
---|
| 136 | av = np.float(linev[ic]) |
---|
| 137 | DMS = gen.angle_DegMinSec(av) |
---|
[2799] | 138 | stval[descstvals[6]] = av |
---|
[2752] | 139 | for i in range(3): stval[descstvals[7+i]] = DMS[i] |
---|
| 140 | |
---|
| 141 | if vals.has_key(descstvals[7]) and not vals.has_key(descstvals[6]): |
---|
| 142 | ic = vals[descstvals[7]] |
---|
| 143 | DMS = [int(linev[ic]), int(linev[ic+1]), np.float(linev[ic+2])] |
---|
| 144 | av = gen.DegMinSec_angle(DMS[0], DMS[1], DMS[2]) |
---|
| 145 | stval[descstvals[6]] = av |
---|
| 146 | for i in range(3): stval[descstvals[7+i]] = DMS[i] |
---|
| 147 | |
---|
| 148 | for Sd in directdescstvals: |
---|
| 149 | Ddescv = DESCstvals[Sd] |
---|
| 150 | if vals.has_key(Sd): |
---|
| 151 | ic = vals[Sd] |
---|
| 152 | stval[Sd] = gen.typemod(linev[ic].replace(spacec, '!'), Ddescv[0]) |
---|
| 153 | else: |
---|
| 154 | if Ddescv[0] == 'S': missval = '-' |
---|
| 155 | elif Ddescv[0] == 'I': missval = gen.fillValueI |
---|
| 156 | elif Ddescv[0] == 'F': missval = gen.fillValueF |
---|
| 157 | stval[Sd] = missval |
---|
| 158 | |
---|
| 159 | Sline = '' |
---|
| 160 | for Sd in descstvals: |
---|
| 161 | if Sd == descstvals[0]: Sline = stval[Sd] |
---|
| 162 | else: Sline = Sline + ',' + str(stval[Sd]) |
---|
| 163 | |
---|
| 164 | oof.write(Sline+'\n') |
---|
| 165 | |
---|
| 166 | oif.close() |
---|
| 167 | oof.close() |
---|
| 168 | |
---|
| 169 | print main + ": successfull written of file '" + ofile + "' !!" |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | #quit() |
---|
| 173 | |
---|