Changeset 1857 in lmdz_wrf for trunk


Ignore:
Timestamp:
Mar 25, 2018, 7:51:05 PM (7 years ago)
Author:
lfita
Message:

Working version with 'time_bnds' generation (only tested for WRFtimes)

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1855 r1857  
    1919## e.g. # nc_var.py -o reconstruct_matrix_from_vector -f cruncep_halfdeg_1958.nc -S 'nav_lon:nav_lat:-90.:-7.:-67.:15.:latlon:0.5:0.5:0.05' -v all
    2020## e.g. # nc_var.py -o nc_var.py -o CFmorzization -S 'X|west_east|XLONG,Y|south_north|XLAT,T|Time|WRFtime,Z|bottom_top|ZNU:GlobalAttr1995.inf:proj1995.inf' -f ~/PY/wrfout_d01_1995-01-01_00\:00\:00 -v QFX@instantaneous@None
     21## e.g.# nc_var.py -o CFmorzization -S 'X|west_east|XLONG,Y|south_north|XLAT,T|Time|WRFtime,Z|bottom_top|ZNU:GlobalAttr1995.inf:proj1995.inf' -f ~/PY/wrfout_d01_1995-01-01_00\:00\:00 -v 'RAINC@time:!accumulation@WRFtime_bnds|h|3'
    2122
    2223## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1
  • trunk/tools/nc_var_tools.py

    r1855 r1857  
    4848# compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
    4949# compute_tevolboxtraj_radialsec: Function to compute tevolboxtraj_radialsec: temporal evolution at a given point along a number of radii at a given frequency following a trajectory
     50# compute_time_bnds: Function to copmute CFtime_bnd from time variable and a given period
    5051# compute_WRFtime: function to copmute CFtimes from WRFtime variable
    5152# compute_WRFtime_bnds: Function to copmute CFtime_bnd from WRFtime variable and a given period
     
    2249622497  refdate='19491201000000', tunitsval='minutes'):
    2249722498    """ Function to copmute CFtime_bnd from WRFtime variable and a given period
    22498       refdate= [YYYYMMDDMIHHSS] format of reference date
    22499       tunitsval= CF time units
    2250022499      timewrfv= matrix string values of WRF 'Times' variable
    2250122500      period= [tunit],[quantity] period to compute the bounds as:
     
    2250622505       'begperiod': WRF times represent the beginning of the period
    2250722506       'centperiod': WRF times represent the center of the period
    22508        'endtperiod': WRF times represent the end of the period
     22507       'endperiod': WRF times represent the end of the period
     22508      refdate= [YYYYMMDDMIHHSS] format of reference date
     22509      tunitsval= CF time units
    2250922510    """
    2251022511    import datetime as dt
     
    2265222653              tunitsval)
    2265322654            WRFtime[itb] = gen.realdatetime1_CFcompilant(newTmat, refdate, tunitsval)
     22655    else:
     22656        print errormsg
     22657        print '  ' + fname + ": kind of WRF time variable: '" + kindWRFt +           \
     22658          "' not ready!!"
     22659        print '     available ones:', ['begperiod']
    2265422660           
    2265522661    tunits = tunitsval + ' since ' + refdateS
     
    2267122677
    2267222678#onc.close()
     22679
     22680def compute_time_bnds(timev, tunits, period, kindt='begperiod',                      \
     22681  refdate='19491201000000', tunitsval='minutes'):
     22682    """ Function to copmute CFtime_bnd from time variable and a given period
     22683      timev= values of time variable
     22684      tunits= CF-time units of the input time-values
     22685      period= [tunit],[quantity] period to compute the bounds as:
     22686        [tunit]: unit of time: 'c' century, 'y' year, 'm' month, 'w' week, 'd' day,
     22687          'h' hour, 'i' minute, 's' second
     22688        [quantity] = amount (integer) of [tunit] to cover a period of time_bnds
     22689      kindt: type of time values
     22690       'begperiod': times represent the beginning of the period
     22691       'centperiod': times represent the center of the period
     22692       'endperiod': times represent the end of the period
     22693      refdate= [YYYYMMDDMIHHSS] format of reference date
     22694      tunitsval= CF time units
     22695    """
     22696    import datetime as dt
     22697    fname = 'compute_time_bnds'
     22698
     22699    tunit = period.split(',')[0]
     22700    quantity = int(period.split(',')[1])
     22701
     22702    yrref=refdate[0:4]
     22703    monref=refdate[4:6]
     22704    dayref=refdate[6:8]
     22705    horref=refdate[8:10]
     22706    minref=refdate[10:12]
     22707    secref=refdate[12:14]
     22708
     22709    refdateS = yrref + '-' + monref + '-' + dayref + ' ' + horref + ':' + minref +   \
     22710      ':' + secref
     22711
     22712    dimt = timev.shape[0]
     22713
     22714    # Times period
     22715    begTmat= gen.datetimeStr_conversion(str(timev[0]),'cfTime,'+tunits, 'matYmdHMS')
     22716    endTmat= gen.datetimeStr_conversion(str(timev[dimt-1]),'cfTime,'+tunits,'matYmdHMS')
     22717    CFbeg = gen.realdatetime1_CFcompilant(begTmat, refdate, tunitsval)
     22718    CFend = gen.realdatetime1_CFcompilant(endTmat, refdate, tunitsval)
     22719    begT = dt.datetime(begTmat[0], begTmat[1], begTmat[2], begTmat[3], begTmat[4],   \
     22720      begTmat[5])
     22721    endT = dt.datetime(endTmat[0], endTmat[1], endTmat[2], endTmat[3], endTmat[4],   \
     22722      endTmat[5])
     22723    dFCt = CFend - CFbeg
     22724
     22725    # Units for the variable 'time' to seconds
     22726    availtunitvals = ['centuries', 'years', 'days', 'hours', 'minutes', 'seconds']
     22727    if tunitsval == 'centuries':
     22728        tsecsuv = 100 * 365 * 24 * 3600.
     22729        dFCt = dFCt*100*365*24*3600
     22730    elif tunitsval == 'years':
     22731        tsecsuv = 365 * 24 * 3600.
     22732        dFCt = dFCt*365*24*3600
     22733    elif tunitsval == 'weeks':
     22734        tsecsuv = 7 * 24 * 3600.
     22735        dFCt = dFCt*7*24*3600
     22736    elif tunitsval == 'days':
     22737        tsecsuv = 24 * 3600.
     22738        dFCt = dFCt*24*3600
     22739    elif tunitsval == 'hours':
     22740        tsecsuv = 3600.
     22741        dFCt = dFCt*3600
     22742    elif tunitsval == 'minutes':
     22743        tsecsuv = 60.
     22744        dFCt = dFCt*60
     22745    elif tunitsval == 'seconds':
     22746        tsecsuv = 1.
     22747    else:
     22748        print errormsg
     22749        print '  ' + fname + ":' tunitsvals= '" + tunitsval + "' not ready!!"
     22750        print '    available ones: ', availtunitvals
     22751        quit(-1)
     22752
     22753    # Quantity of time_bnds
     22754    availtunits = ['c', 'y', 'm', 'd', 'h', 'i', 's']
     22755    if tunit == 'c':
     22756        dtbnds = quantity * 100 * 365 * 24 * 3600.
     22757        begTBmat = [int(begTmat[0]/100)*100, 1, 1, 0, 0, 0]
     22758        endTBmat = [int(endTmat[0]/100)*100+100, 1, 1, 0, 0, 0]
     22759        dTBmat = [quantity*100, 0, 0, 0, 0, 0]
     22760    elif tunit == 'y':
     22761        dtbnds = quantity * 365 * 24 * 3600.
     22762        begTBmat = [begTmat[0], 1, 1, 0, 0, 0]
     22763        endTBmat = [endTmat[0]+1, 1, 1, 0, 0, 0]
     22764        dTBmat = [quantity, 0, 0, 0, 0, 0]
     22765    elif tunit == 'm':
     22766        iyr = begTmat[0]
     22767        imo = begTmat[1]
     22768        eyr = endTmat[0]
     22769        emo = endTmat[1]
     22770        begTBmat = [begTmat[0], begTmat[1], 1, 0, 0, 0]
     22771        endTBmat = [endTmat[0], endTmat[1]+1, 1, 0, 0, 0]
     22772        dTBmat = [0, quantity, 0, 0, 0, 0]
     22773    elif tunit == 'w':
     22774        dtbnds = quantity * 7 * 24 * 3600.
     22775        begTBmat = [begTmat[0], begTmat[1], begTmat[2], 0, 0, 0]
     22776        endTBmat = [endTmat[0], endTmat[1], endTmat[2]+7, 0, 0, 0]
     22777        dTBmat = [0, 0, quantity*7, 0, 0, 0]
     22778    elif tunit == 'd':
     22779        dtbnds = quantity * 24 * 3600.
     22780        begTBmat = [begTmat[0], begTmat[1], begTmat[2], 0, 0, 0]
     22781        endTBmat = [endTmat[0], endTmat[1], endTmat[2]+1, 0, 0, 0]
     22782        dTBmat = [0, 0, quantity, 0, 0, 0]
     22783    elif tunit == 'h':
     22784        dtbnds = quantity * 3600.
     22785        begTBmat = [begTmat[0], begTmat[1], begTmat[2], begTmat[3], 0, 0]
     22786        endTBmat = [endTmat[0], endTmat[1], endTmat[2], endTmat[3]+1, 0, 0]
     22787        dTBmat = [0, 0, 0, quantity, 0, 0]
     22788    elif tunit == 'i':
     22789        dtbnds = quantity * 60.
     22790        begTBmat = [begTmat[0], begTmat[1], begTmat[2], begTmat[3], begTmat[4], 0]
     22791        endTBmat = [endTmat[0], endTmat[1], endTmat[2], endTmat[3], endTmat[4]+1, 0]
     22792        dTBmat = [0, 0, 0, 0, quantity, 0]
     22793    elif tunit == 's':
     22794        dtbnds = quantity * 1.
     22795        begTBmat = [begTmat[0], begTmat[1], begTmat[2], begTmat[3], begTmat[4],      \
     22796          begTmat[5]]
     22797        endTBmat = [endTmat[0], endTmat[1], endTmat[2], endTmat[3], endTmat[4],      \
     22798          endTmat[5]+1]
     22799        dTBmat = [0, 0, 0, 0, 0, quantity]
     22800    else:
     22801        print errormsg
     22802        print '  ' + fname + ":' tunit= '" + tunit + "' not ready!!"
     22803        print '    available ones: ', availtunits
     22804        quit(-1)
     22805
     22806    Nbnds = int((dFCt + dtbnds)/ dtbnds)
     22807    if Nbnds < 2:
     22808        print errormsg
     22809        print '  ' + fname + ": from: ", begTmat, 'to', endTmat, 'found', Nbnds,     \
     22810          'time_bnds of', quantity,'*',tunit, '!!'
     22811        print '    revise parameters !!'
     22812        quit(-1)
     22813    print '  ' + fname + ' from: ', begTmat, 'to', endTmat, '(', dFCt, ' seconds )', \
     22814      'found', Nbnds, 'time_bnds of', quantity,'*',tunit, '(', dtbnds, 's)'
     22815
     22816    begTB = dt.datetime(begTBmat[0], begTBmat[1], begTBmat[2], begTBmat[3],          \
     22817      begTBmat[4], begTBmat[5])
     22818    endTB = dt.datetime(endTBmat[0], endTBmat[1], endTBmat[2], endTBmat[3],          \
     22819      endTBmat[4], endTBmat[5])
     22820
     22821    time_bnds = np.zeros((Nbnds,2), dtype=np.float)
     22822    time = np.zeros((Nbnds), dtype=np.float)
     22823    # For the time_bnds
     22824    # In the way that the beginning/ending of the boundaries is computed there is not
     22825    #   distinction among 'beg/cent/end' time-kind values
     22826    for itb in range(Nbnds):
     22827        newTBi = begTB + dt.timedelta(seconds=dtbnds*itb)
     22828        newTBe = begTB + dt.timedelta(seconds=dtbnds*(itb+1))
     22829        newT = begTB + dt.timedelta(seconds=dtbnds*itb+dtbnds/2.)
     22830        newTBmati = [newTBi.year, newTBi.month, newTBi.day, newTBi.hour,             \
     22831          newTBi.minute, newTBi.second]
     22832        newTBmate = [newTBe.year, newTBe.month, newTBe.day, newTBe.hour,             \
     22833          newTBe.minute, newTBe.second]
     22834        newTmat = [newT.year, newT.month, newT.day, newT.hour, newT.minute,          \
     22835          newT.second]
     22836        time_bnds[itb,0] = gen.realdatetime1_CFcompilant(newTBmati, refdate,         \
     22837          tunitsval)
     22838        time_bnds[itb,1] = gen.realdatetime1_CFcompilant(newTBmate, refdate,         \
     22839          tunitsval)
     22840        time[itb] = gen.realdatetime1_CFcompilant(newTmat, refdate, tunitsval)
     22841           
     22842    tunits = tunitsval + ' since ' + refdateS
     22843
     22844    return time_bnds, time, tunits
     22845
     22846#timevals = np.array([23712570, 23712750, 23712930, 23713110, 23713290, 23713470,     \
     22847#    23713650,    \
     22848#    23713830, 23714010, 23714190, 23714370, 23714550, 23714730, 23714910,            \
     22849#    23715090, 23715270, 23715450, 23715630, 23715810, 23715990, 23716170,            \
     22850#    23716350, 23716530, 23716710, 23716890, 23717070, 23717250, 23717430,            \
     22851#    23717610, 23717790, 23717970, 23718150, 23718330, 23718510, 23718690,            \
     22852#    23718870, 23719050, 23719230, 23719410, 23719590, 23719770, 23719950,            \
     22853#    23720130, 23720310, 23720490, 23720670, 23720850, 23721030, 23721210,            \
     22854#    23721390, 23721570, 23721750, 23721930, 23722110, 23722290, 23722470])
     22855#tbnds, tc, tu = compute_time_bnds(timevals, 'minutes since 1949-12-01 00:00:00',     \
     22856#  'd,1', kindt='begperiod', refdate='19491201000000', tunitsval='minutes')
     22857
     22858#itb = gen.netCDFdatetime_realdatetime(tu, 'standard', tbnds[:,0])
     22859#etb = gen.netCDFdatetime_realdatetime(tu, 'standard', tbnds[:,1])
     22860#ctb = gen.netCDFdatetime_realdatetime(tu, 'standard', tc[:])
     22861
     22862#for it in range(len(tc)):
     22863#    print itb[it], ctb[it], etb[it]
     22864#quit()
    2267322865
    2267422866def CFmorzization(values, ncfile, variable):
     
    2320423396                    tbvals, tvals, urefvals = compute_WRFtime_bnds(timewrfv, period, \
    2320523397                      kindWRFt='begperiod', refdate=refT, tunitsval='minutes')
     23398                    # Re-setting time values to the center of the bounds
     23399                    ovtime[:] = tvals[:]
     23400                elif vtb[0:10] == 'Ctime_bnds':
     23401                    vtbvals = vtb.split('|')
     23402                    if len(vtbvals) != 4:
     23403                        print errormsg
     23404                        print '  ' + fname + ": to compute 'time_bnds' from a " +    \
     23405                          "given variable '[tvarn]', one needs to provide the name "+\
     23406                          "of the variable and the period of the bounds as: " +      \
     23407                          "bounds as: 'Ctime_bnds'|[tvarn]|[tunit]|[quantity]"
     23408                        print "    values passed: '" + vtb
     23409                        onc.close()
     23410                        quit(-1)
     23411                    if not gen.searchInlist(onc.variables.keys(), vtbvals[1]):
     23412                        print errormsg
     23413                        print '  ' + fname + ": file does not have variable '" +     \
     23414                          vtbvals[1] + "' to be used to compute 'time_bnds'"
     23415                        print '    available ones:', onc.variables.keys()
     23416
     23417                    period = vtbvals[2] + ',' + vtbvals[3]
     23418                    print infmsg
     23419                    print '      ' + fname + ": creation of variable 'time_bnds' " + \
     23420                      "from variable '" + tvtbvals[1] + "' using period '" + period +\
     23421                      "' !!"
     23422                    odimvar = onc.variables[tvtbvals[1]]
     23423                    timev = odimvar[:]
     23424                    ttu = odimvar.getncattr('units')
     23425
     23426                    refT = gen.datetimeStr_conversion(gattr['basetime'],             \
     23427                      'Y-m-dTH:M:SZ', 'YmdHMS')
     23428                    tbvals, tvals, urefvals = compute_time_bnds(timev, ttu, period,  \
     23429                      kindWRFt='begperiod', refdate=refT, tunitsval='minutes')
     23430                    # Re-setting time values to the center of the bounds
     23431                    ovtime[:] = tvals[:]
    2320623432                else:
    2320723433                    if not onc.variables.has_key(vardimn):
     
    2321323439                        quit(-1)
    2321423440                    tbvals = onc.variables[vardimn]
    23215                 # Re-setting time values to the center of the bounds
    23216                 ovtime[:] = tvals[:]
    23217                 onewnc.sync()
    2321823441
    2321923442                # creation of time_bnds variable
     
    2322223445                newvar = onewnc.createVariable('time_bnds', 'f8', ('time', 'nv'))
    2322323446                newvar[:] = tbvals[:]
     23447                onewnc.sync()
    2322423448
    2322523449        # Adding projection variable
Note: See TracChangeset for help on using the changeset viewer.