[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") |
---|
| 43 | parser.add_option("-d", "--DescFile", dest="descfile", help="File with the description of the content", metavar="FILENAME") |
---|
| 44 | (opts, args) = parser.parse_args() |
---|
| 45 | |
---|
| 46 | main = 'to_OBSstation.py' |
---|
| 47 | ####### ####### |
---|
| 48 | ## MAIN |
---|
| 49 | ####### |
---|
| 50 | # Expected description values |
---|
| 51 | descstvals = ['station_name', 'WMOid', 'longitude', 'lon_deg', 'lon_min', 'lon_sec', \ |
---|
| 52 | 'latitude', 'lat_deg', 'lat_min', 'lat_sec', 'height', 'prov', 'country', \ |
---|
| 53 | 'nice_name', 'add1', 'add2'] |
---|
| 54 | DESCstvals = {'station_name': ['S'], 'WMOid': ['I'], 'longitude': ['F'], \ |
---|
| 55 | 'lon_deg': ['I'], 'lon_min': ['I'], 'lon_sec': ['F'], 'latitude': ['F'], \ |
---|
| 56 | 'lat_deg': ['I'], 'lat_min': ['I'], 'lat_sec': ['F'], 'height': ['F'], \ |
---|
| 57 | 'prov': ['S'], 'country': ['S'], 'nice_name': ['S'], 'add1': ['S'], 'add2': ['S']} |
---|
| 58 | directdescstvals = ['station_name', 'WMOid', 'height', 'prov', 'country', \ |
---|
| 59 | 'nice_name', 'add1', 'add2'] |
---|
| 60 | |
---|
| 61 | # Non numeric values |
---|
| 62 | NOnum = ['sepchar', 'spacec'] |
---|
| 63 | |
---|
| 64 | ofile = 'sub_OBSstations.csv' |
---|
| 65 | |
---|
| 66 | # Reading description |
---|
| 67 | of = open(opts.descfile, 'r') |
---|
| 68 | vals = {} |
---|
| 69 | for line in of: |
---|
| 70 | if len(line) > 2 and line[0:1] != '#': |
---|
| 71 | linevals = line.replace('\n','').replace('\t','').replace('\r', \ |
---|
| 72 | '').replace(' ','').split('=') |
---|
| 73 | if not gen.searchInlist(NOnum, linevals[0]): |
---|
| 74 | vals[linevals[0]] = int(linevals[1]) |
---|
| 75 | else: |
---|
| 76 | vals[linevals[0]] = linevals[1] |
---|
| 77 | |
---|
| 78 | of.close() |
---|
| 79 | |
---|
| 80 | lvals = list(vals.keys()) |
---|
| 81 | if not gen.searchInlist(lvals, 'sepchar'): |
---|
| 82 | print gen.errormsg |
---|
| 83 | print ' ' + main + ": no split character provided !!" |
---|
| 84 | print " description file '" + opts.descfile + "' must content one entry as "+ \ |
---|
| 85 | "sepchar = [char] !!" |
---|
| 86 | quit(-1) |
---|
| 87 | if not gen.searchInlist(lvals, 'Sspacechar'): vals['Ssepchar'] = '!' |
---|
| 88 | |
---|
| 89 | spacec = vals['Ssepchar'] |
---|
| 90 | # Getting values |
---|
| 91 | oof = open(ofile, 'w') |
---|
| 92 | oif = open(opts.Stfile, 'r') |
---|
| 93 | for line in oif: |
---|
| 94 | stval = {} |
---|
| 95 | if len(line) > 2 and line[0:1] != '#': |
---|
| 96 | linevals = line.replace('\n','').replace('\t','').replace('\r', \ |
---|
| 97 | '') |
---|
| 98 | linev = linevals.split(vals['sepchar']) |
---|
| 99 | |
---|
| 100 | if vals.has_key(descstvals[2]) and not vals.has_key(descstvals[3]): |
---|
| 101 | ic = vals[descstvals[2]] |
---|
| 102 | av = np.float(linev[ic]) |
---|
| 103 | DMS = gen.angle_DegMinSec(av) |
---|
| 104 | stval[descstvals[2]] = av |
---|
| 105 | for i in range(3): stval[descstvals[3+i]] = DMS[i] |
---|
| 106 | |
---|
| 107 | if vals.has_key(descstvals[3]) and not vals.has_key(descstvals[2]): |
---|
| 108 | ic = vals[descstvals[3]] |
---|
| 109 | DMS = [int(linev[ic]), int(linev[ic+1]), np.float(linev[ic+2])] |
---|
| 110 | av = gen.DegMinSec_angle(DMS[0], DMS[1], DMS[2]) |
---|
| 111 | stval[descstvals[2]] = av |
---|
| 112 | for i in range(3): stval[descstvals[3+i]] = DMS[i] |
---|
| 113 | |
---|
| 114 | if vals.has_key(descstvals[6]) and not vals.has_key(descstvals[7]): |
---|
| 115 | ic = vals[descstvals[6]] |
---|
| 116 | av = np.float(linev[ic]) |
---|
| 117 | DMS = gen.angle_DegMinSec(av) |
---|
| 118 | stval[descstvals[6]] = lonv |
---|
| 119 | for i in range(3): stval[descstvals[7+i]] = DMS[i] |
---|
| 120 | |
---|
| 121 | if vals.has_key(descstvals[7]) and not vals.has_key(descstvals[6]): |
---|
| 122 | ic = vals[descstvals[7]] |
---|
| 123 | DMS = [int(linev[ic]), int(linev[ic+1]), np.float(linev[ic+2])] |
---|
| 124 | av = gen.DegMinSec_angle(DMS[0], DMS[1], DMS[2]) |
---|
| 125 | stval[descstvals[6]] = av |
---|
| 126 | for i in range(3): stval[descstvals[7+i]] = DMS[i] |
---|
| 127 | |
---|
| 128 | for Sd in directdescstvals: |
---|
| 129 | Ddescv = DESCstvals[Sd] |
---|
| 130 | if vals.has_key(Sd): |
---|
| 131 | ic = vals[Sd] |
---|
| 132 | stval[Sd] = gen.typemod(linev[ic].replace(spacec, '!'), Ddescv[0]) |
---|
| 133 | else: |
---|
| 134 | if Ddescv[0] == 'S': missval = '-' |
---|
| 135 | elif Ddescv[0] == 'I': missval = gen.fillValueI |
---|
| 136 | elif Ddescv[0] == 'F': missval = gen.fillValueF |
---|
| 137 | stval[Sd] = missval |
---|
| 138 | |
---|
| 139 | Sline = '' |
---|
| 140 | for Sd in descstvals: |
---|
| 141 | if Sd == descstvals[0]: Sline = stval[Sd] |
---|
| 142 | else: Sline = Sline + ',' + str(stval[Sd]) |
---|
| 143 | |
---|
| 144 | oof.write(Sline+'\n') |
---|
| 145 | |
---|
| 146 | oif.close() |
---|
| 147 | oof.close() |
---|
| 148 | |
---|
| 149 | print main + ": successfull written of file '" + ofile + "' !!" |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | #quit() |
---|
| 153 | |
---|