[177] | 1 | # Python script to transform from LMDZ output to regular output |
---|
| 2 | # L. Fita, LMD. Jussieu, September 2013 |
---|
| 3 | # On de coupling of WRF and LMDZ, matrices of (1,dimx*dimy,dimz) sizre are passed and therefore outputted with this shape. |
---|
| 4 | # This script transform the LMDZ output to a regular (dimx,dimy,dimz) output |
---|
| 5 | # |
---|
| 6 | ## export PATH=/u/lflmd/bin/gcc_Python-2.7.5/bin:${PATH} |
---|
| 7 | ## e.g.: # WRFLMDZ_regularout.py -d 31,31 -f /d4/lflmd/etudes/WRF_LMDZ/test_phylmd/run/histmth.nc -o histins_reg.nc |
---|
| 8 | import numpy as np |
---|
| 9 | from netCDF4 import Dataset as NetCDFFile |
---|
| 10 | import os |
---|
| 11 | import re |
---|
| 12 | import nc_var_tools as ncvar |
---|
| 13 | from optparse import OptionParser |
---|
| 14 | |
---|
| 15 | main='WRFLMDZout_regularout.py' |
---|
| 16 | errormsg='ERROR -- error -- ERROR -- error' |
---|
| 17 | warnmsg='WARNING -- warning -- WARNING -- warning' |
---|
| 18 | |
---|
| 19 | ####### ###### ##### #### #### ## # |
---|
| 20 | |
---|
| 21 | parser = OptionParser() |
---|
| 22 | parser.add_option("-d", "--dimensions", dest="dims", |
---|
| 23 | help="dimx,dimy", metavar="VALUES") |
---|
| 24 | parser.add_option("-f", "--LMDZ_file", dest="lfile", |
---|
| 25 | help="LMDZ file to use", metavar="FILE") |
---|
| 26 | parser.add_option("-o", "--output_file", dest="ofile", |
---|
| 27 | help="output file name to use", metavar="FILE") |
---|
| 28 | |
---|
| 29 | (opts, args) = parser.parse_args() |
---|
| 30 | |
---|
| 31 | ####### ####### |
---|
| 32 | ## MAIN |
---|
| 33 | ####### |
---|
| 34 | |
---|
| 35 | ofile=opts.ofile |
---|
| 36 | |
---|
| 37 | dimx=int(opts.dims.split(',')[0]) |
---|
| 38 | dimy=int(opts.dims.split(',')[1]) |
---|
| 39 | |
---|
| 40 | if not os.path.isfile(opts.lfile): |
---|
| 41 | print errormsg |
---|
| 42 | print ' ' + main + ' LMDZ file: "' + opts.lfile + '" does not exist !!' |
---|
| 43 | print errormsg |
---|
| 44 | quit(-1) |
---|
| 45 | |
---|
| 46 | objlfile = NetCDFFile(opts.lfile, 'r') |
---|
| 47 | |
---|
| 48 | # Checking dimensions. Remeber lat=(dimx*dimy) |
---|
| 49 | ## |
---|
| 50 | varobj=objlfile.variables['lat'] |
---|
| 51 | varinf = ncvar.variable_inf(varobj) |
---|
| 52 | if dimx*dimy != varinf.dims[0]: |
---|
| 53 | print errormsg |
---|
| 54 | print ' ' + main + ': given dimensions', dimx,',', dimy, \ |
---|
| 55 | 'does not coincide with lat size: ',varinf.dims [0],'!!!' |
---|
| 56 | quit(-1) |
---|
| 57 | |
---|
| 58 | # Checking dimensions. time |
---|
| 59 | ## |
---|
| 60 | varobj=objlfile.variables['time_counter'] |
---|
| 61 | varinf = ncvar.variable_inf(varobj) |
---|
| 62 | if varinf.dims[0] == 0: |
---|
| 63 | print errormsg |
---|
| 64 | print ' ' + main + ': variable "time_counter" does not have values!!!!' |
---|
| 65 | quit(-1) |
---|
| 66 | |
---|
| 67 | objofile = NetCDFFile(opts.ofile, 'w') |
---|
| 68 | |
---|
| 69 | lfilevars = objlfile.variables |
---|
| 70 | for varn in lfilevars: |
---|
| 71 | print ' Transforming "' + varn + '"...' |
---|
| 72 | varobj=objlfile.variables[varn] |
---|
| 73 | |
---|
| 74 | varinf = ncvar.variable_inf(varobj) |
---|
| 75 | |
---|
| 76 | vardims = varinf.dims |
---|
| 77 | vardimns = varinf.dimns |
---|
| 78 | vartype = varinf.dtype |
---|
| 79 | varattr = varinf.attributes |
---|
| 80 | |
---|
| 81 | # Checking dimensions |
---|
| 82 | ## |
---|
| 83 | newdimns = [] |
---|
| 84 | for vdim in vardimns: |
---|
| 85 | objofiledimns = objofile.dimensions.keys() |
---|
| 86 | if vdim == 'time_counter': |
---|
| 87 | newvdim = 'time' |
---|
| 88 | elif vdim == 'tbnds': |
---|
| 89 | newvdim = 'bnds' |
---|
| 90 | else: |
---|
| 91 | newvdim = vdim |
---|
| 92 | |
---|
| 93 | if not ncvar.searchInlist(objofiledimns, newvdim): |
---|
| 94 | objvdim = objlfile.dimensions[vdim] |
---|
| 95 | if vdim == 'lon': |
---|
| 96 | dimsize = dimx |
---|
| 97 | elif vdim == 'lat': |
---|
| 98 | dimsize = dimy |
---|
| 99 | else: |
---|
| 100 | if objvdim.isunlimited(): |
---|
| 101 | dimsize=None |
---|
| 102 | else: |
---|
| 103 | dimsize = len(objvdim) |
---|
| 104 | |
---|
| 105 | print ' Adding dimension "' + newvdim + '" size:', dimsize |
---|
| 106 | dim = objofile.createDimension(newvdim, dimsize) |
---|
| 107 | newdimns.append(newvdim) |
---|
| 108 | |
---|
| 109 | newvardimns=tuple(newdimns) |
---|
| 110 | |
---|
| 111 | # Checking fill value |
---|
| 112 | ## |
---|
| 113 | if ncvar.searchInlist(varattr, '_FillValue'): |
---|
| 114 | varfil = varobj._FillValue |
---|
| 115 | else: |
---|
| 116 | varfil = False |
---|
| 117 | |
---|
| 118 | if varn == 'time_counter': |
---|
| 119 | varn = 'time' |
---|
| 120 | elif varn == 'time_counter_bnds': |
---|
| 121 | varn='time_bnds' |
---|
| 122 | |
---|
| 123 | varvalues = varobj[:] |
---|
| 124 | |
---|
| 125 | varshape = varvalues.shape |
---|
| 126 | Ndims = len(varshape) |
---|
| 127 | |
---|
| 128 | regvardims = list(vardims) |
---|
| 129 | for idim in range(varinf.Ndims): |
---|
| 130 | if vardimns[idim] == 'lon': |
---|
| 131 | regvardims[idim] = dimx |
---|
| 132 | elif vardimns[idim] == 'lat': |
---|
| 133 | regvardims[idim] = dimy |
---|
| 134 | else: |
---|
| 135 | regvardims[idim] = vardims[idim] |
---|
| 136 | |
---|
| 137 | if varinf.Ndims != 0: |
---|
| 138 | print ' Adding variable: "' + varn + '" shape: ', regvardims[0:varinf.Ndims] |
---|
| 139 | newvar = objofile.createVariable(varn, vartype, newvardimns, fill_value=varfil) |
---|
| 140 | |
---|
| 141 | if varinf.Ndims == 1: |
---|
| 142 | varvals = np.zeros(regvardims[0], dtype=vartype) |
---|
| 143 | if varn == 'lon': |
---|
| 144 | dlon=360./(dimx*1.) |
---|
| 145 | print 'Computing the lon values centered at 180.!' |
---|
| 146 | varvals=dlon/2. + dlon*np.array(range(dimx)) |
---|
| 147 | print varvals |
---|
| 148 | elif varn == 'lat': |
---|
| 149 | varvals = varvalues[dimx*np.array(range(dimy))] |
---|
| 150 | elif varn == 'presnivs': |
---|
| 151 | print varvalues |
---|
| 152 | dimz=regvardims[0] |
---|
| 153 | varvals = varvalues |
---|
| 154 | # varvals = dimz*1.-np.array(range(dimz))*1. |
---|
| 155 | else: |
---|
| 156 | varvals = varvalues |
---|
| 157 | elif varinf.Ndims == 2: |
---|
| 158 | varvals = np.zeros((regvardims[0], regvardims[1]), dtype=vartype) |
---|
| 159 | varvals = varvalues.reshape(regvardims[0], regvardims[1]) |
---|
| 160 | elif varinf.Ndims == 3: |
---|
| 161 | varvals = np.zeros((regvardims[0], regvardims[1], regvardims[2]), \ |
---|
| 162 | dtype=vartype) |
---|
| 163 | varvals = varvalues.reshape(regvardims[0], regvardims[1], regvardims[2]) |
---|
| 164 | elif varinf.Ndims == 4: |
---|
| 165 | varvals = np.zeros((regvardims[0], regvardims[1], regvardims[2], \ |
---|
| 166 | regvardims[3]), dtype=vartype) |
---|
| 167 | varvals = varvalues.reshape(regvardims[0], regvardims[1], regvardims[2], \ |
---|
| 168 | regvardims[3]) |
---|
| 169 | elif varinf.Ndims == 5: |
---|
| 170 | varvals = np.zeros((regvardims[0], regvardims[1], regvardims[2], \ |
---|
| 171 | regvardims[3], regvardims[4]), dtype=vartype) |
---|
| 172 | varvals = varvalues.reshape(regvardims[0], regvardims[1], regvardims[2], \ |
---|
| 173 | regvardims[3], regvardims[4]) |
---|
| 174 | elif varinf.Ndims == 6: |
---|
| 175 | varvals = np.zeros((regvardims[0], regvardims[1], regvardims[2], \ |
---|
| 176 | regvardims[3], regvardims[4], regvardims[5]), dtype=vartype) |
---|
| 177 | varvals = varvalues.reshape(regvardims[0], regvardims[1], regvardims[2], \ |
---|
| 178 | regvardims[3], regvardims[4], regvardims[5]) |
---|
| 179 | else: |
---|
| 180 | print errormsg |
---|
| 181 | print ' variable size ',varinf.Ndims,' is not ready!!!!' |
---|
| 182 | |
---|
| 183 | for attr in varattr: |
---|
| 184 | newvarattrs = newvar.ncattrs() |
---|
| 185 | attrv = varobj.getncattr(attr) |
---|
| 186 | if not ncvar.searchInlist(newvarattrs, attr): |
---|
| 187 | if attr == 'coordinates': |
---|
| 188 | newattrv = attrv.replace('time_counter','time') |
---|
| 189 | attrv = newattrv.replace('time_counter_bnds','time_bnds') |
---|
| 190 | newattrv = attrv.replace('tbnds','bnds') |
---|
| 191 | attrv = newattrv |
---|
| 192 | |
---|
| 193 | newvar.setncattr(attr, attrv) |
---|
| 194 | |
---|
| 195 | newvar[:] = varvals |
---|
| 196 | objofile.sync() |
---|
| 197 | |
---|
| 198 | objofile.sync() |
---|
| 199 | lfilegattrs = objlfile.ncattrs() |
---|
| 200 | Nattrs = len(lfilegattrs) |
---|
| 201 | print ' Adding ', Nattrs,' global atributes' |
---|
| 202 | |
---|
| 203 | for attr in lfilegattrs: |
---|
| 204 | attrv = objlfile.getncattr(attr) |
---|
| 205 | atvar = ncvar.set_attribute(objofile, attr, attrv) |
---|
| 206 | |
---|
| 207 | objlfile.close() |
---|
| 208 | objofile.sync() |
---|
| 209 | objofile.close() |
---|
| 210 | |
---|
| 211 | print 'File "' + opts.ofile + '" succesffully created!' |
---|