Changeset 2188 in lmdz_wrf


Ignore:
Timestamp:
Oct 17, 2018, 7:49:15 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `get_right_CFtimeunits': Function to get the right CFtime units from any given format
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2186 r2188  
    111111# fill_Narray: Function to fill a n-dimensional array with an arrary of lesser rank
    112112# get_configuration: Function to get the configuration from an ASCII external file
     113# get_right_CFtimeunits: Function to get the right CFtime units from any given format
    113114# get_specdictionary_HMT: Function to get specific values from a dictionary by selcting that keys with H*M*T
    114115# getting_fixedline: Function to get the values from a line of text with fixed lenght of different values
     
    1463114632    return [ix, iy], mindist
    1463214633
     14634def get_right_CFtimeunits(CFtimeu):
     14635    """ Function to get the right CFtime units from any given format
     14636       CF-time units must be like:
     14637         [time_unit] since [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]
     14638      CFtimeu: CF-time units to look
     14639    >>> get_right_CFtimeunits('minutes since 1949-12-01 00:00:00')
     14640    minutes simnce 1949-12-01 00:00:00
     14641    >>> get_right_CFtimeunits('minutes since 1949-12-01')
     14642    minutes simnce 1949-12-01 00:00:00
     14643    >>> get_right_CFtimeunits('minutes since 19491201 000000')
     14644    minutes simnce 1949-12-01 00:00:00
     14645    >>> get_right_CFtimeunits('days since 1850-1-1')
     14646    days simnce 1850-01-01 00:00:00
     14647    """
     14648    fname = 'get_right_CFtimeunits'
     14649
     14650    if CFtimeu.find('since') == -1:
     14651        print errormsg
     14652        print '  ' + fname + ": provided time-units '" + CFtimeu + "' does not " +   \
     14653          "have even 'since', impossible to proceed !!"
     14654        quit(-1)
     14655
     14656    uvalsecs = CFtimeu.split(' ')
     14657
     14658    # Getting four sections
     14659    if len(uvalsecs) == 4:
     14660        date=uvalsecs[2]
     14661        time=uvalsecs[3]
     14662    else:
     14663        if uvalsecs[2].find('_') == -1:
     14664            print infmsg
     14665            print '  ' + fname + ": CF-time units without time !!"
     14666            print "    adding '00:00:00'"
     14667            date=uvalsecs[2]
     14668            time='00:00:00'
     14669        else:
     14670            date=uvalsecs[2].split('_')[0]
     14671            time=uvalsecs[2].split('_')[1]
     14672
     14673    # Getting four digits
     14674    if date.find('-') == -1:
     14675        if len(date) == 8: date = date[0:4] + '-' + date[4:6] + '-' + date[6:8]
     14676        else: date = date.replace('/','-')
     14677    datesecs=date.split('-')
     14678    date = datesecs[0].zfill(4)+'-'+datesecs[1].zfill(2)+'-'+datesecs[2].zfill(2)
     14679    #print '  ' + fname + ': final date:', date
     14680
     14681    if time.find(':') == -1:
     14682        if len(time) == 6: time = time[0:2] + ':' + time[2:4] + ':' + time[4:6]
     14683        else: date = date.replace('_',':')
     14684    timesecs=time.split(':')
     14685    time = timesecs[0].zfill(2)+':'+timesecs[1].zfill(2)+':'+timesecs[2].zfill(2)
     14686    #print '  ' + fname + ': final time:', time
     14687
     14688    refdateS = date + ' ' + time
     14689
     14690    if int(date.split('-')[0]) > 1900:
     14691        refdate = datetimeStr_datetime(refdateS)
     14692        refdateS = refdate.strftime("%Y-%m-%d %H:%M:%S")
     14693    else:
     14694        print warnmsg
     14695        print '  ' + fname + ": reference date: '" + date + "' below 1900 !!"
     14696
     14697    time_unit = uvalsecs[0]
     14698
     14699    rightCFtimeu = time_unit + ' simnce ' + refdateS
     14700
     14701    return rightCFtimeu
    1463314702
    1463414703#quit()
Note: See TracChangeset for help on using the changeset viewer.