Changeset 538 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 29, 2015, 7:43:26 PM (10 years ago)
Author:
lfita
Message:

Adding the right '360dqy' calendar calculation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r535 r538  
    29582958    return timeinf
    29592959
    2960 def date_juliandate(rdate):
    2961     """ Function to transform from a standard date to a matrix julian day based one
    2962       rdate= `datetime' object to transform
     2960def date_juliandate(refyr, TOTsecs):
     2961    """ Function to transform from a total quantity of seconds since the
     2962      beginning of a year to 360 days / year calendar
     2963      TOTsecs= total number of seconds
    29632964    """
    2964     import datetime as dt
    2965 
    29662965    fname = 'date_juliandate'
    29672966
    2968     jday = int(rdate.timetuple().tm_yday)
    2969     rldate[0] = int(rdate.year)
    2970     rldate[1] = jday/30 + 1
    2971     rldate[2] = jday - (rldate[1]-1)*30
    2972     rldate[3] = int(rdate.hour)
    2973     rldate[4] = int(rdate.second)
    2974     rldate[5] = int(rdate.minute)
    2975 
    2976     if rldate[2] < 0:
    2977         print errormsg
    2978         print '  ' + fname + ': wrong date', rdate, 'it gives a negative',           \
    2979           rldate[2],'day!!'
     2967    secsYear = 3600*24*30*12.
     2968    secsMonth = 3600*24*30.
     2969    secsDay = 3600*24.
     2970    secsHour = 3600.
     2971    secsMinute = 60.
     2972
     2973    rldate = np.zeros((6), dtype=int)
     2974    rldate[0] = refyr
     2975    if TOTsecs > secsYear:
     2976        rldate[0] = refyr + int(TOTsecs/secsYear) + 1
     2977        TOTsecs = TOTsecs - int(TOTsecs/secsYear)*secsYear
     2978
     2979    if np.mod(TOTsecs,secsMonth) == 0:
     2980        rldate[1] = int(TOTsecs/secsMonth)
     2981    else:
     2982        rldate[1] = int(TOTsecs/secsMonth) + 1
     2983    TOTsecs = TOTsecs - int(TOTsecs/secsMonth)*secsMonth
     2984
     2985    if np.mod(TOTsecs,secsDay) == 0:
     2986        rldate[2] = int(TOTsecs/secsDay)
     2987    else:
     2988        rldate[2] = int(TOTsecs/secsDay) + 1
     2989    TOTsecs = TOTsecs - int(TOTsecs/secsDay)*secsDay
     2990
     2991    if np.mod(TOTsecs,secsHour) == 0:
     2992        rldate[3] = int(TOTsecs/secsHour)
     2993    else:
     2994        rldate[3] = int(TOTsecs/secsHour) + 1
     2995    TOTsecs = TOTsecs - int(TOTsecs/secsHour)*secsHour
     2996
     2997    if np.mod(TOTsecs,secsMinute) == 0:
     2998        rldate[4] = int(TOTsecs/secsMinute)
     2999    else:
     3000        rldate[4] = int(TOTsecs/secsMinute) + 1
     3001    TOTsecs = TOTsecs - int(TOTsecs/secsMinute)*secsMinute
     3002
     3003    rldate[5] = TOTsecs
     3004
     3005#    print refyr,TOTsecs,':',rldate
     3006#    quit()
    29803007
    29813008    return rldate
     
    29943021    """
    29953022    import datetime as dt
     3023    fname = 'CFtimes_datetime'
    29963024
    29973025    times = ncfile.variables[tname]
     
    30013029    if not searchInlist(attvar, 'units'):
    30023030        print errormsg
    3003         print '    CFtimes_datetime: "time" does not have attribute: "units"'
     3031        print '  ' + fname + ": '" + tname + "' does not have attribute: 'units'"
    30043032        quit(-1)
    30053033    else:
     
    30223050    realdates = np.zeros((dimt, 6), dtype=int)
    30233051
     3052    secsDay=3600*24.
     3053
    30243054# Checking calendar!
    30253055##
    30263056    y360 = False
    3027     daycal360 = ['earth_360d', '360d', '360days']
     3057    daycal360 = ['earth_360d', '360d', '360days', '360_day']
    30283058    if searchInlist(attvar, 'calendar'):
    30293059        calendar = times.getncattr('calendar')
     
    30453075        if tunits == 'weeks':
    30463076            for it in range(dimt):
    3047                 realdate = refdate + dt.timedelta(weeks=float(times[it]))
    3048                 realdates[it,:] = date_juliandate(realdate)
     3077                deltat = dt.timedelta(weeks=float(times[it]))
     3078                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
     3079                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
    30493080        elif tunits == 'days':
    30503081            for it in range(dimt):
    3051                 realdate = refdate + dt.timedelta(days=float(times[it]))
    3052                 realdates[it,:] = date_juliandate(realdate)
     3082                deltat = dt.timedelta(days=float(times[it]))
     3083                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
     3084                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
    30533085        elif tunits == 'hours':
    30543086           for it in range(dimt):
    3055                 realdate = refdate + dt.timedelta(hours=float(times[it]))
    3056                 realdates[it,:] = date_juliandate(realdate)
     3087                realdate = dt.timedelta(hours=float(times[it]))
     3088                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
     3089                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
    30573090        elif tunits == 'minutes':
    30583091           for it in range(dimt):
    3059                 realdate = refdate + dt.timedelta(minutes=float(times[it]))
    3060                 realdates[it,:] = date_juliandate(realdate)
     3092                realdate = dt.timedelta(minutes=float(times[it]))
     3093                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
     3094                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
    30613095        elif tunits == 'seconds':
    30623096           for it in range(dimt):
    3063                 realdate = refdate + dt.timedelta(seconds=float(times[it]))
    3064                 realdates[it,:] = date_juliandate(realdate)
     3097                realdate = dt.timedelta(seconds=float(times[it]))
     3098                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
     3099                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
    30653100        elif tunits == 'miliseconds':
    30663101           for it in range(dimt):
    3067                 realdate = refdate + dt.timedelta(miliseconds=float(times[it]))
    3068                 realdates[it,:] = date_juliandate(realdate)
     3102                realdate = dt.timedelta(miliseconds=float(times[it]))
     3103                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
     3104                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
    30693105        else:
    30703106              print errormsg
     
    1620216238                        slicevar.append(ddt)
    1620316239
    16204 #                print ddt,d1,d2,mattimes[ddt,:],'var:',slicevar,'new:',slicenewvar
     16240                print ddt,d1,d2,mattimes[ddt,:],'var:',slicevar,'new:',slicenewvar
    1620516241
    1620616242                newvarv[tuple(slicenewvar)] = oVn[tuple(slicevar)]
Note: See TracChangeset for help on using the changeset viewer.