1 | # Python script to join histstations files from LMDZ |
---|
2 | # L. Fita, LMD. Jussieu, April 2015 |
---|
3 | |
---|
4 | from optparse import OptionParser |
---|
5 | import numpy as np |
---|
6 | from netCDF4 import Dataset as NetCDFFile |
---|
7 | import os |
---|
8 | import re |
---|
9 | import nc_var_tools as ncvar |
---|
10 | |
---|
11 | main = 'join_histstations.py' |
---|
12 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
13 | warnmsg = 'WARNING -- warning -- WARNING -- warning' |
---|
14 | |
---|
15 | ####### ###### ##### #### ### ## # |
---|
16 | parser = OptionParser() |
---|
17 | parser.add_option("-f", "--folder", dest="histfold", |
---|
18 | help="folder with the histstations_[nnnn].nc files", metavar="FILE") |
---|
19 | parser.add_option("-l", "--lfile", dest="lfile", |
---|
20 | help="file with the standard output of the LMDZ", metavar="FILE") |
---|
21 | |
---|
22 | (opts, args) = parser.parse_args() |
---|
23 | |
---|
24 | ####### ####### |
---|
25 | ## MAIN |
---|
26 | ####### |
---|
27 | stdim = 'x' |
---|
28 | ofile = 'histstations.nc' |
---|
29 | |
---|
30 | if histfold is not None |
---|
31 | if not os.path.isdir(histfold): |
---|
32 | print errormsg |
---|
33 | print ' ' + main + ": folder '" + histfold + "' does not exist!!" |
---|
34 | quit(-1) |
---|
35 | else: |
---|
36 | print errormsg |
---|
37 | print ' ' + main + 'No folder with histstations_[nnnn].nc is provided!!' |
---|
38 | quit(-1) |
---|
39 | |
---|
40 | stsorted = True |
---|
41 | if lfile is not None and not os.path.isfile(lfile): |
---|
42 | print errormsg |
---|
43 | print ' ' + main + ": LMDZ standard output file '" + lfile + \ |
---|
44 | "' does not exist!!" |
---|
45 | quit(-1) |
---|
46 | else: |
---|
47 | print errormsg |
---|
48 | print ' ' + main + ": no LMDZ standard output file provided!!" |
---|
49 | print ' getting stations as they come from the files' |
---|
50 | stsorted = False |
---|
51 | |
---|
52 | lsthistst = files_folder(histfolder, 'histstations_') |
---|
53 | Nhistst = len(lsthistst) |
---|
54 | |
---|
55 | print 'Found',Nhistst,"On folder '" + histfolder + "'" |
---|
56 | |
---|
57 | # Getting values and creating file |
---|
58 | Nsthiststf = {} |
---|
59 | lochiststf = {} |
---|
60 | Ntotst = 0 |
---|
61 | |
---|
62 | for histstf in lsthistst: |
---|
63 | ncobj = NetcDDFile(histstf, 'r') |
---|
64 | Nsthiststf[histstf] = len(ncobj.dimensions[stdim]) |
---|
65 | if Nsthiststf[histstf] != 0: |
---|
66 | loc |
---|
67 | lochiststf[histstf] = ncobj.variables[lonst] |
---|
68 | |
---|
69 | ncobj.close() |
---|
70 | |
---|
71 | # All stations |
---|
72 | Ntotst = np.sum(Nsthiststf.values()) |
---|
73 | print 'There are',Ntotst,'stations' |
---|
74 | allsto = NetCDFFile(ofile, 'w') |
---|
75 | |
---|
76 | # Dimensions |
---|
77 | ncobj == NetCDFFile(lsthistst[0], 'r') |
---|
78 | dims = ncobj.dimensions.keys() |
---|
79 | |
---|
80 | for ds in dims: |
---|
81 | dobj = ncobj.dimensions[ds] |
---|
82 | if ds != stdim: |
---|
83 | if dobj.isunlimited(): |
---|
84 | allsto.createDimension(ds, None) |
---|
85 | else: |
---|
86 | allsto.createDimension(ds, len(dobj)) |
---|
87 | else: |
---|
88 | allsto.createDimension(ds, Ntotst) |
---|
89 | ncobj.close() |
---|
90 | allsto.sync() |
---|
91 | |
---|
92 | # Getting values |
---|
93 | for histstf in lsthistst: |
---|
94 | ncobj = NetcDDFile(histstf, 'r') |
---|
95 | varns = ncobj.variables.keys() |
---|
96 | for var in |
---|
97 | |
---|
98 | |
---|