Changeset 417


Ignore:
Timestamp:
Nov 23, 2011, 1:09:21 PM (13 years ago)
Author:
acolaitis
Message:

PYTHON
######

mcs.py script is pretty finished now. Usage:

mcs.py -f diagfi16.nc -m /san0/acolmd/DATA/MCS/MCS_processeddata/MCSdata_binned_MY29.nc -i -x --var temp --override

where -i specify that you want to interpolate your file on MCS pressure levels, -x get rids of aps and bps in the output, --override forces zrecast to work even if a z-recasted file is already present.

Location:
trunk/UTIL/PYTHON
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UTIL/PYTHON/mcs.py

    r409 r417  
    1616   from optparse import OptionParser    ### to be replaced by argparse
    1717   from netCDF4 import Dataset
    18    from os import system
     18   from os import system,path
    1919   from times import sol2ls
    2020   import numpy as np
     
    3030   parser.add_option('-m', '--mfile',  action='store',dest='mcsfile',     type="string",  default=None,  help='[NEEDED] filename for MCS comparison.')
    3131   parser.add_option('-v', '--var',    action='append',dest='var',      type="string",  default=None,  help='[NEEDED] Variables to process. (coma-separated list. aps and bps are always included.)')
     32   parser.add_option('-x', action='store_true',dest='recast',     default=False,  help='Force aps and bps to be ommited in output file (usefull if your file is already recasted along z) [False]')
     33   parser.add_option('-i', '--zrecast', action='store_true', dest='zrecast', default=False, help='Cast zrecast.e on diagfi file with MCS pressure levels. Will pass this operation is recasted file is already present, unless --override is specified. [False]')
     34   parser.add_option('--override', action='store_true', dest='override', default=False, help='Force zrecast.e to act even if recasted file is already present(will erase previous recasted file) [False]')
    3235
    3336   #############################
     
    3841   ### Load and check data
    3942
     43   if opt.var is None:
     44      print "You must specify at least a field to process with -v."
     45      exit()
     46
     47   # Zrecast
     48
     49   varznames=separatenames(opt.var[0])
     50
     51   if opt.zrecast:
     52      if (path.exists(opt.file[0:len(opt.file)-3]+"_P.nc") and (not opt.override)):
     53         print "--> "+opt.file[0:len(opt.file)-3]+"_P.nc"
     54         print "Recasted file is already there, skipping interpolation. [use --override to force interpolation]"
     55         filename=opt.file[0:len(opt.file)-3]+"_P.nc"
     56      else:
     57         print "--> "+opt.file[0:len(opt.file)-3]+"_P.nc"
     58         filename=call_zrecast (  interp_mode   = 2, \
     59                    input_name      = [opt.file], \
     60                    fields  = varznames, \
     61                    predifined = 'mcs')[0]
     62
    4063   # Files
    4164
    42    nc=Dataset(opt.file)
     65   print "--> Loading diagfi dataset."
     66
     67   nc=Dataset(filename)
    4368   ncmcs=Dataset(opt.mcsfile)
    4469
     
    4974   alt=nc.variables["altitude"][:]
    5075   time=nc.variables["Time"][:] # in fraction of sols
    51    controle=nc.variables["controle"][:]
    52    day_ini=controle[3]%669
     76   if "controle" in nc.variables:
     77      controle=nc.variables["controle"][:]
     78      day_ini=controle[3]%669
     79   else:
     80      if opt.zrecast:
     81         nccontrol=Dataset(opt.file)
     82         if "controle" in nccontrol.variables:
     83            controle=nccontrol.variables["controle"][:]
     84            day_ini=controle[3]%669
     85         else:
     86            print "Error: could not find controle variable in diagfi."
     87            day_ini=input("Please type initial sol number:")%669
    5388   time[:]=time[:]+day_ini
    5489   nx=len(lon)
     
    5994
    6095   # MCS
     96
     97   print "--> Loading and preparing MCS dataset."
    6198
    6299   dtimemintmp=ncmcs.variables["dtimemin"][:,:,:]
     
    79116   # Variables to treat
    80117
     118   print "--> Preparing diagfi dataset."
     119
    81120   varz=[]
    82    varznames=separatenames(opt.var[0])
    83121   n=0
    84122   for zn in varznames:
    85        varz.append(getfield(nc,zn))
     123       load=getfield(nc,zn)
     124       if opt.zrecast:
     125          load=np.ma.masked_where(load < -1.e-30,load)
     126          load.set_fill_value([np.NaN])
     127          load=load.filled()
     128       varz.append(load)
    86129       print "Found: "+zn+" with dimensions: "
    87130       print np.array(varz[n]).shape
     
    109152       fullnames.append("n"+name)
    110153   print "Will output: "
    111    print fullnames
     154   if opt.recast: print fullnames[2:]
     155   else: print fullnames
    112156   #############################
    113157   ### Building
     
    234278          all.append(varnightout[vv,:,:,:,:].filled())
    235279
     280   if opt.recast:
     281      all=all[2:]
     282      fullnames=fullnames[2:]
     283
    236284   make_gcm_netcdf (zfilename="diagfi_MCS.nc", \
    237285                        zdescription="Temperatures from diagfi reworked to match MCS format", \
  • trunk/UTIL/PYTHON/zrecast_wrapper.py

    r409 r417  
    1111    from os import system
    1212    pressure_axis_tes=[1658.152,1291.37,1005.72,783.2555,610.,475.0685,369.9837,288.1436,224.4065,174.7679,136.1094,106.0021,82.55452,64.29353,50.07185,38.99599,30.37011,23.65227,18.4204,14.34583,11.17254]
    13     pressure_axis_mcs=[1878.9, 1658.2, 1463.3, 1291.4, 1139.6, 1005.7, 887.54, 783.26,691.22, 610, 538.32, 475.07, 419.25, 369.98, 326.51, 288.14, 254.29,224.41, 198.04, 174.77, 154.23, 136.11, 120.12, 106, 93.547, 82.555,72.854, 64.294, 56.739, 50.072, 44.188, 38.996, 34.414, 30.37, 26.802,23.652, 20.873, 18.42, 16.256, 14.346, 12.66, 11.173, 9.8597, 8.7012,7.6788, 6.7765, 5.9802, 5.2775, 4.6574, 4.1101, 3.6272, 3.201, 2.8249,2.4929, 2.2, 1.9415, 1.7134, 1.512, 1.3344, 1.1776, 1.0392, 0.9171,0.80934, 0.71424, 0.63031, 0.55625, 0.49089, 0.43321, 0.3823, 0.33738,0.29774, 0.26275, 0.23188, 0.20463, 0.18059, 0.15937, 0.14064, 0.12412,0.10953, 0.096661, 0.085303, 0.07528, 0.066434, 0.058628, 0.051739,0.04566, 0.040294, 0.03556, 0.031381, 0.027694, 0.02444, 0.021568,0.019034, 0.016797, 0.014824, 0.013082, 0.011545, 0.010188, 0.0089909,0.0079345, 0.0070021, 0.0061794, 0.0054533, 0.0048125, 0.004247]
     13    pressure_axis_mcs=[1878.9, 1658.2, 1463.3, 1291.4, 1139.6, 1005.7, 887.54, 783.26,691.22, 610., 538.32, 475.07, 419.25, 369.98, 326.51, 288.14, 254.29,224.41, 198.04, 174.77, 154.23, 136.11, 120.12, 106., 93.547, 82.555,72.854, 64.294, 56.739, 50.072, 44.188, 38.996, 34.414, 30.37, 26.802,23.652, 20.873, 18.42, 16.256, 14.346, 12.66, 11.173, 9.8597, 8.7012,7.6788, 6.7765, 5.9802, 5.2775, 4.6574, 4.1101, 3.6272, 3.201, 2.8249,2.4929, 2.2, 1.9415, 1.7134, 1.512, 1.3344, 1.1776, 1.0392, 0.9171,0.80934, 0.71424, 0.63031, 0.55625, 0.49089, 0.43321, 0.3823, 0.33738,0.29774, 0.26275, 0.23188, 0.20463, 0.18059, 0.15937, 0.14064, 0.12412,0.10953, 0.096661, 0.085303, 0.07528, 0.066434, 0.058628, 0.051739,0.04566, 0.040294, 0.03556, 0.031381, 0.027694, 0.02444, 0.021568,0.019034, 0.016797, 0.014824, 0.013082, 0.011545, 0.010188, 0.0089909,0.0079345, 0.0070021, 0.0061794, 0.0054533, 0.0048125, 0.004247]
    1414    system("rm -f zrecast.auto.def")
    1515    system("touch zrecast.auto.def")
    1616    indicefile=0
    1717    outputfilename=""
     18    f = open('zrecast.auto.def', 'w')
    1819    for zfile in input_name:
    19         system("echo "+zfile+" >> zrecast.auto.def")
     20        f.write(zfile+"\n")
    2021        for zvar in fields:
    21             system("echo "+zvar+" >> zrecast.auto.def")
    22         system("echo >> zrecast.auto.def")
     22             f.write(zvar+"\n")
     23        f.write("\n")
    2324        if interp_mode == 4:
    2425             append="_S"
    25              system("echo 3 >> zrecast.auto.def")
    26              system("echo yes >> zrecast.auto.def")
    27              system("echo 0 150000 >> zrecast.auto.def")
     26             f.write("3"+"\n")
     27             f.write("yes"+"\n")
     28             f.write("0 150000"+"\n")
    2829        elif interp_mode == 2:
    2930             append="_P"
    30              if predifined is "TES":
    31                 system("echo 1 >> zrecast.auto.def")
    32                 system("echo no >> zrecast.auto.def")
    33                 system("echo "+len(pressure_axis_tes)+" >> zrecast.auto.def")
     31             if predifined in ["TES","tes"]:
     32                print "Using TES pressure levels"
     33                f.write("1"+"\n")
     34                f.write("no"+"\n")
     35                f.write(str(len(pressure_axis_tes))+"\n")
    3436                for zp in pressure_axis_tes:
    35                    system("echo "+zp+" >> zrecast.auto.def")
    36              elif predifined is "MCS":
    37                 system("echo no >> zrecast.auto.def")
    38                 system("echo "+len(pressure_axis_mcs)+" >> zrecast.auto.def")
     37                    f.write(str(zp)+"\n")
     38                f.write("\n")
     39             elif predifined in ["MCS","mcs"]:
     40                print "Using MCS pressure levels"
     41                f.write("1"+"\n")
     42                f.write("no"+"\n")
     43                f.write(str(len(pressure_axis_mcs))+"\n")
    3944                for zp in pressure_axis_mcs:
    40                    system("echo "+zp+" >> zrecast.auto.def")
     45                    f.write(str(zp)+"\n")
     46                f.write("\n")
    4147             else:
    42                 system("echo 1 >> zrecast.auto.def")
    43                 system("echo yes >> zrecast.auto.def")
    44                 system("echo 370 0.1 >> zrecast.auto.def")  #I put that randomly! (a.c.)
    45                 system("echo 20 >> zrecast.auto.def")
     48                f.write("1"+"\n")
     49                f.write("yes"+"\n")
     50                f.write("370 0.1"+"\n")
     51                f.write("20"+"\n")
    4652        else:
    4753             print "zrecast interp option unsupported for now. Exiting."
    4854             exit()
     55        f.close()
    4956        system("zrecast.e < zrecast.auto.def")
    5057
Note: See TracChangeset for help on using the changeset viewer.