source: lmdz_wrf/trunk/tools/join_histstations.py @ 1361

Last change on this file since 1361 was 400, checked in by lfita, 10 years ago

Script to join 'histstations_[nnnn].nc' from LMDZ

File size: 2.5 KB
Line 
1# Python script to join histstations files from LMDZ
2# L. Fita, LMD. Jussieu, April 2015
3
4from optparse import OptionParser
5import numpy as np
6from netCDF4 import Dataset as NetCDFFile
7import os
8import re
9import nc_var_tools as ncvar
10
11main = 'join_histstations.py'
12errormsg = 'ERROR -- error -- ERROR -- error'
13warnmsg = 'WARNING -- warning -- WARNING -- warning'
14
15####### ###### ##### #### ### ## #
16parser = OptionParser()
17parser.add_option("-f", "--folder", dest="histfold", 
18  help="folder with the histstations_[nnnn].nc files", metavar="FILE")
19parser.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    #######
27stdim = 'x'
28ofile = 'histstations.nc'
29
30if 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)
35else:
36    print errormsg
37    print '  ' + main + 'No folder with histstations_[nnnn].nc is provided!!'
38    quit(-1)
39
40stsorted = True
41if 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)
46else:
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
52lsthistst = files_folder(histfolder, 'histstations_')
53Nhistst = len(lsthistst)
54
55print 'Found',Nhistst,"On folder '" + histfolder + "'"
56
57# Getting values and creating file
58Nsthiststf = {}
59lochiststf = {}
60Ntotst = 0
61
62for 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
72Ntotst = np.sum(Nsthiststf.values())
73print 'There are',Ntotst,'stations'
74allsto = NetCDFFile(ofile, 'w')
75
76# Dimensions
77ncobj == NetCDFFile(lsthistst[0], 'r')
78dims = ncobj.dimensions.keys()
79
80for 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)
89ncobj.close()
90allsto.sync()
91
92# Getting values
93for histstf in lsthistst:
94    ncobj = NetcDDFile(histstf, 'r')
95    varns = ncobj.variables.keys()
96    for var in 
97
98
Note: See TracBrowser for help on using the repository browser.