source: lmdz_wrf/trunk/tools/to_OBSstations.py @ 2765

Last change on this file since 2765 was 2752, checked in by lfita, 6 years ago

Adding:

  • `to:_OBSstations.py': Python to transform from a given stations list format to standard 'OBSstations.csv'
File size: 5.5 KB
Line 
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
15from optparse import OptionParser
16import numpy as np
17from netCDF4 import Dataset as NetCDFFile
18import os
19import re
20import subprocess as sub
21import numpy.ma as ma
22import matplotlib as mpl
23mpl.use('Agg')
24from matplotlib.pylab import *
25import matplotlib.pyplot as plt
26from mpl_toolkits.basemap import Basemap
27
28# Importing generic tools file 'generic_tools.py' and the others
29import generic_tools as gen
30import nc_var_tools as ncvar
31import drawing_tools as drw
32import diag_tools as diag
33import geometry_tools as geo
34
35# Importing Fortran modules and the others
36import module_ForDef as fdef
37import module_ForDiag as fdiag
38import module_ForGen as fgen
39import module_ForSci as fsci
40
41parser = OptionParser()
42parser.add_option("-f", "--StFile", dest="Stfile", help="File with the stations values", metavar="FILENAME")
43parser.add_option("-d", "--DescFile", dest="descfile", help="File with the description of the content", metavar="FILENAME")
44(opts, args) = parser.parse_args()
45
46main = 'to_OBSstation.py'
47#######    #######
48## MAIN
49    #######
50# Expected description values
51descstvals = ['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']
54DESCstvals = {'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']}
58directdescstvals = ['station_name', 'WMOid', 'height', 'prov', 'country',            \
59  'nice_name', 'add1', 'add2']
60
61# Non numeric values
62NOnum = ['sepchar', 'spacec']
63
64ofile = 'sub_OBSstations.csv'
65
66# Reading description
67of = open(opts.descfile, 'r')
68vals = {}
69for 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
78of.close()
79
80lvals = list(vals.keys())
81if 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)
87if not gen.searchInlist(lvals, 'Sspacechar'): vals['Ssepchar'] = '!'
88
89spacec = vals['Ssepchar']
90# Getting values
91oof = open(ofile, 'w')
92oif = open(opts.Stfile, 'r')
93for 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
146oif.close()
147oof.close()
148
149print main + ": successfull written of file '" + ofile + "' !!"
150
151
152#quit()
153
Note: See TracBrowser for help on using the repository browser.