# Python script to join histstations files from LMDZ # L. Fita, LMD. Jussieu, April 2015 from optparse import OptionParser import numpy as np from netCDF4 import Dataset as NetCDFFile import os import re import nc_var_tools as ncvar main = 'join_histstations.py' errormsg = 'ERROR -- error -- ERROR -- error' warnmsg = 'WARNING -- warning -- WARNING -- warning' ####### ###### ##### #### ### ## # parser = OptionParser() parser.add_option("-f", "--folder", dest="histfold", help="folder with the histstations_[nnnn].nc files", metavar="FILE") parser.add_option("-l", "--lfile", dest="lfile", help="file with the standard output of the LMDZ", metavar="FILE") (opts, args) = parser.parse_args() ####### ####### ## MAIN ####### stdim = 'x' ofile = 'histstations.nc' if histfold is not None if not os.path.isdir(histfold): print errormsg print ' ' + main + ": folder '" + histfold + "' does not exist!!" quit(-1) else: print errormsg print ' ' + main + 'No folder with histstations_[nnnn].nc is provided!!' quit(-1) stsorted = True if lfile is not None and not os.path.isfile(lfile): print errormsg print ' ' + main + ": LMDZ standard output file '" + lfile + \ "' does not exist!!" quit(-1) else: print errormsg print ' ' + main + ": no LMDZ standard output file provided!!" print ' getting stations as they come from the files' stsorted = False lsthistst = files_folder(histfolder, 'histstations_') Nhistst = len(lsthistst) print 'Found',Nhistst,"On folder '" + histfolder + "'" # Getting values and creating file Nsthiststf = {} lochiststf = {} Ntotst = 0 for histstf in lsthistst: ncobj = NetcDDFile(histstf, 'r') Nsthiststf[histstf] = len(ncobj.dimensions[stdim]) if Nsthiststf[histstf] != 0: loc lochiststf[histstf] = ncobj.variables[lonst] ncobj.close() # All stations Ntotst = np.sum(Nsthiststf.values()) print 'There are',Ntotst,'stations' allsto = NetCDFFile(ofile, 'w') # Dimensions ncobj == NetCDFFile(lsthistst[0], 'r') dims = ncobj.dimensions.keys() for ds in dims: dobj = ncobj.dimensions[ds] if ds != stdim: if dobj.isunlimited(): allsto.createDimension(ds, None) else: allsto.createDimension(ds, len(dobj)) else: allsto.createDimension(ds, Ntotst) ncobj.close() allsto.sync() # Getting values for histstf in lsthistst: ncobj = NetcDDFile(histstf, 'r') varns = ncobj.variables.keys() for var in