Changeset 2471 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Apr 24, 2019, 8:43:23 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `fromanydate_CFYmd': Function to transform from any string format of date to Y-m-d used in CF-conventions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2469 r2471  
    122122# fill_Narray: Function to fill a n-dimensional array with an arrary of lesser rank
    123123# fix_CFdates: Fixing CF-time values with wrong setting
     124# fromanydate_CFYmd: Function to transform from any string format of date to Y-m-d used in
     125#   CF-conventions
    124126# gen_impose_gregorian: Function to impose gregorian calendar to a series of times with a
    125127#   non-standard calendar independently from datetime (imposes yr > 1900)
     
    1623916241#  'days since 1949-12-01 00:00:00')
    1624016242
     16243def fromanydate_CFYmd(date):
     16244    """ Function to transform from any string format of date to Y-m-d used in
     16245        CF-conventions
     16246      date: date to transform
     16247    >>> fromanydate_Ymd('0001-01-01')
     16248    '0001-01-01'
     16249    >>> fromanydate_Ymd('1850-1-1')
     16250    '1850-01-01'
     16251    >>> fromanydate_Ymd('1850/1/1')
     16252    '1850-01-01'
     16253    >>> fromanydate_Ymd('1976-02-17_08:32:27')
     16254    '1976-02-17'
     16255    """
     16256    fname = 'fromanydate_CFYmd'
     16257
     16258    if date.count('-') + date.count('/') == 0:
     16259        print errormsg
     16260        print '  ' + fname + ": date without either '-' or '/' not ready !!"
     16261        print "    passed date '" + date + "' not manegeable"
     16262        quit(-1)
     16263
     16264    # Replacing '/' by '-'
     16265    if date.count('/') != 0: date = date.replace('/','-')
     16266
     16267    # Removing possible [datre]_[time]
     16268    if date.count('_') != 0: date = date.split('_')[0]
     16269
     16270    ldate = date.split('-')
     16271    if len(ldate) != 3:
     16272        print errormsg
     16273        print '  ' + fname + ": date '" + date + "' not ready !!"
     16274        print "     it requires 3 '-' sections '[yr]-[mo]-[dd]'"
     16275        quit(-1)
     16276
     16277    yr = str(int(ldate[0])).zfill(4)
     16278    mo = str(int(ldate[1])).zfill(2)
     16279    dd = str(int(ldate[2])).zfill(2)
     16280
     16281    newdate = yr + '-' + mo + '-' + dd
     16282
     16283    return newdate
     16284
    1624116285def change_CFRefdate(timevals, origcftimeu, newSrefdate='1949-12-01 00:00:00'):
    1624216286    """ Change CF-time values with a new reference date
     
    1625816302    origtv = origcftimeu.split(' ')
    1625916303    origtunits = origtv[0]
    16260     origSrefdate = origtv[2]
     16304    origSrefdate0 = origtv[2]
     16305
     16306    origSrefdate = fromanydate_Ymd(origSrefdate)
    1626116307
    1626216308    yrref = int(origSrefdate[0:4])
     
    1636816414    origtunits = origtv[0]
    1636916415    if len(origtv) == 4:
    16370         origSrefdate = origtv[2] + ' ' + origtv[3]
     16416        origSrefdate = fromanydate_Ymd(origtv[2]) + ' ' + origtv[3]
    1637116417    else:
    1637216418        print warnmsg
    1637316419        print '  ' + fname + ": original time units wihout time !!"
    1637416420        print "   adding it as '00:00:00'"
    16375         origSrefdate = origtv[2] + ' 00:00:00'
     16421        origSrefdate = fromanydate_Ymd(origtv[2]) + ' 00:00:00'
    1637616422
    1637716423    # From original to days
     
    1643716483    """
    1643816484    fname = 'change_CFcalendar'
    16439     availorigcal = ['noleap', '365d']
     16485    availorigcal = ['noleap', '365d', '365_day']
    1644016486    availnewcal = ['gregorian', 'standard']
    1644116487
    1644216488    # If the calendar is not gregorian, it needs to be fixed from origSrefdate!
    16443     if origcal == 'noleap' or origcal == '365d':
     16489    if origcal == 'noleap' or origcal == '365d' or origcal == '365_day' :
    1644416490        print '  ' + fname + ": found non-standard calendar '" + origcal + "' !!"
    1644516491        if newcal == 'gregorian' or newcal == 'standard':
Note: See TracChangeset for help on using the changeset viewer.