1 | # Python script to transfomr ASCII LIDAR outputs to netCDF |
---|
2 | ## g.e. # TS_ASCII_netCDF.py -f //media/ExtDiskD/bkup/ciclad/etudes/WL_HyMeX/iop15/wrf/run/control/stations_20121018000000-20121022000000/h0001.d01.TS -s 20121018000000 |
---|
3 | |
---|
4 | import os |
---|
5 | from optparse import OptionParser |
---|
6 | import numpy as np |
---|
7 | from netCDF4 import Dataset as NetCDFFile |
---|
8 | import nc_var_tools as ncvar |
---|
9 | |
---|
10 | main = 'TS_ASCII_netCDF.py' |
---|
11 | errormsg='ERROR -- error -- ERROR -- error' |
---|
12 | warnmsg='WARNING -- warning -- WARNING -- warning' |
---|
13 | |
---|
14 | fillValue = 1.e20 |
---|
15 | |
---|
16 | ####### ###### ##### #### ### ## # |
---|
17 | |
---|
18 | parser = OptionParser() |
---|
19 | parser.add_option("-f", "--TS_file", dest="lfile", |
---|
20 | help="Time Series ASCII text file to use", metavar="FILE") |
---|
21 | parser.add_option("-s", "--SimulationStartTime", dest="stime", |
---|
22 | help="Starting time of the simulation ([YYYY][MM][DD][HH][MI][SS] format)", metavar="DATE") |
---|
23 | |
---|
24 | (opts, args) = parser.parse_args() |
---|
25 | |
---|
26 | |
---|
27 | tsvn = ['t', 'q', 'u', 'v', 'psfc', 'glw', 'gsw', 'hfx', 'lh', 'tsk', 'tslb1', 'rainc', 'rainnc', 'clw'] |
---|
28 | |
---|
29 | tsvln = ['2 m Temperature', '2 m vapor mixing ratio', '10 m U wind (earth-relative)', '10 m V wind (earth-relative)', 'surface pressure', 'downward longwave radiation flux at the ground (downward is positive)', 'net shortwave radiation flux at the ground (downward is positive)', 'surface sensible heat flux (upward is positive)', 'surface latent heat flux (upward is positive)', 'skin temperature', 'top soil layer temperature', 'rainfall from a cumulus scheme', 'rainfall from an explicit scheme', 'total column-integrated water vapor and cloud variables'] |
---|
30 | |
---|
31 | tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', '1'] |
---|
32 | |
---|
33 | ####### ####### |
---|
34 | ## MAIN |
---|
35 | ####### |
---|
36 | |
---|
37 | ofile = 'ts.nc' |
---|
38 | Ntsvariables = len(tsvn) |
---|
39 | |
---|
40 | if not os.path.isfile(opts.lfile): |
---|
41 | print errormsg |
---|
42 | print ' ' + main + ': Time-Series ASCII text file "' + opts.lfile + \ |
---|
43 | '" does not exist !!' |
---|
44 | print errormsg |
---|
45 | quit() |
---|
46 | |
---|
47 | if opts.stime is None: |
---|
48 | print errormsg |
---|
49 | print ' ' + main + ': No initial date/time of the simulation is provided!' |
---|
50 | quit(-1) |
---|
51 | else: |
---|
52 | stime = opts.stime |
---|
53 | refdate = stime[0:4] + '-' + stime[4:6] + '-' + stime[6:8] + ' ' + stime[8:10] + \ |
---|
54 | ':' + stime[10:12] + ':' + stime[12:14] |
---|
55 | |
---|
56 | objlfile = open(opts.lfile, 'r') |
---|
57 | |
---|
58 | objofile = NetCDFFile(ofile, 'w') |
---|
59 | |
---|
60 | # Creation of dimensions |
---|
61 | ## |
---|
62 | objofile.createDimension('time',None) |
---|
63 | |
---|
64 | ncvar.set_attribute(objofile, 'author', 'Lluis Fita Borrell') |
---|
65 | ncvar.set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique') |
---|
66 | ncvar.set_attribute(objofile, 'university', 'University Pierre et Marie Curie') |
---|
67 | ncvar.set_attribute(objofile, 'center', 'Centre national de la recherche scientifique') |
---|
68 | ncvar.set_attribute(objofile, 'country', 'France') |
---|
69 | ncvar.set_attribute(objofile, 'city', 'Paris') |
---|
70 | ncvar.set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py') |
---|
71 | ncvar.set_attribute(objofile, 'version', '1.0') |
---|
72 | |
---|
73 | time_step = [] |
---|
74 | psfc = [] |
---|
75 | rainc = [] |
---|
76 | rainnc = [] |
---|
77 | drydens = [] |
---|
78 | |
---|
79 | tsvals = {} |
---|
80 | |
---|
81 | iline=0 |
---|
82 | itz = 0 |
---|
83 | for line in objlfile: |
---|
84 | values = ncvar.reduce_spaces(line) |
---|
85 | # print iline, values[0], dimz, Searchdimz |
---|
86 | # Writting general information |
---|
87 | if iline == 0: |
---|
88 | newvar = objofile.createVariable('station','c') |
---|
89 | ncvar.set_attribute(newvar, 'name',values[0]) |
---|
90 | ncvar.set_attribute(newvar, 'acronym',values[3]) |
---|
91 | ncvar.set_attribute(newvar, 'real_lon', \ |
---|
92 | np.float(values[6].replace(',','').replace('(','').replace(')','')) ) |
---|
93 | ncvar.set_attribute(newvar, 'real_lat', \ |
---|
94 | np.float(values[5].replace(',','').replace('(','').replace(')','')) ) |
---|
95 | ncvar.set_attribute(newvar, 'x_grid_point', \ |
---|
96 | int(values[8].replace(',','').replace('(','').replace(')','')) ) |
---|
97 | ncvar.set_attribute(newvar, 'y_grid_point', \ |
---|
98 | int(values[9].replace(',','').replace('(','').replace(')','')) ) |
---|
99 | ncvar.set_attribute(newvar, 'model_lon', \ |
---|
100 | np.float(values[12].replace(',','').replace('(','').replace(')','')) ) |
---|
101 | ncvar.set_attribute(newvar, 'model_lat', \ |
---|
102 | np.float(values[11].replace(',','').replace('(','').replace(')','')) ) |
---|
103 | ncvar.set_attribute(newvar, 'model_height', \ |
---|
104 | np.float(values[13].replace(',','').replace('(','').replace(')','')) ) |
---|
105 | simstarttime = refdate |
---|
106 | else: |
---|
107 | tsvals[itz] = values |
---|
108 | time_step.append(np.float(values[1])) |
---|
109 | itz = itz + 1 |
---|
110 | iline = iline + 1 |
---|
111 | |
---|
112 | dimt = len(time_step) |
---|
113 | |
---|
114 | print ' Found:',dimt,'time steps' |
---|
115 | objlfile.close() |
---|
116 | |
---|
117 | time_stepv = np.zeros((dimt), dtype=np.float) |
---|
118 | tsvaluesv = np.zeros( (dimt,Ntsvariables), dtype= np.float) |
---|
119 | |
---|
120 | pracc = np.zeros((dimt), dtype=np.float) |
---|
121 | |
---|
122 | itz = 0 |
---|
123 | for it in range(dimt): |
---|
124 | time_stepv[it] = np.float(time_step[it]) |
---|
125 | |
---|
126 | for iv in range(Ntsvariables): |
---|
127 | tsvaluesv[it,iv] = np.float(tsvals[itz][iv+5]) |
---|
128 | |
---|
129 | pracc[it] = np.float(tsvals[it][16]) + np.float(tsvals[it][17]) |
---|
130 | itz = itz + 1 |
---|
131 | |
---|
132 | # time |
---|
133 | newvar = objofile.createVariable('time','f8',('time')) |
---|
134 | newvar[:] = time_stepv |
---|
135 | newattr = ncvar.basicvardef(newvar, 'time', 'time', 'hours since ' + \ |
---|
136 | simstarttime.replace('_',' ')) |
---|
137 | ncvar.set_attribute(newvar, 'calendar', 'standard') |
---|
138 | |
---|
139 | # time-series variables |
---|
140 | for iv in range(Ntsvariables): |
---|
141 | newvar = objofile.createVariable(tsvn[iv], 'f4', ('time')) |
---|
142 | newvar[:] = tsvaluesv[:,iv] |
---|
143 | newattr = ncvar.basicvardef(newvar, tsvn[iv], tsvln[iv], tsvu[iv] ) |
---|
144 | |
---|
145 | # Extra vars |
---|
146 | |
---|
147 | # pr |
---|
148 | varvals = np.zeros((dimt), dtype=np.float) |
---|
149 | varvals[1:dimt] = pracc[1:dimt] - pracc[0:dimt-1] |
---|
150 | |
---|
151 | newvar = objofile.createVariable('pr', 'f4', ('time')) |
---|
152 | newvar[:] = varvals |
---|
153 | newattr = ncvar.basicvardef(newvar, 'pr', 'precipitation', 'mm' ) |
---|
154 | |
---|
155 | objofile.sync() |
---|
156 | objofile.close() |
---|
157 | |
---|
158 | print 'Successfull generation of Time-Series netCDF file "' + ofile + '" !!!!!' |
---|