Changeset 534 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 29, 2015, 3:30:23 PM (9 years ago)
Author:
lfita
Message:

Adding `date_juliandate' in order to obtain matrix-like dates on a 360day calendar (12 months of 30 days)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r533 r534  
    29582958    return timeinf
    29592959
     2960def date_juliandate(rdate):
     2961    """ Function to transform from a standard date to a matrix julian day based one
     2962      rdate= `datetime' object to transform
     2963    """
     2964    import datetime as dt
     2965
     2966    fname = 'date_juliandate'
     2967
     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!!'
     2980
     2981    return rldate
     2982
    29602983def CFtimes_datetime(ncfile, tname):
    29612984    """ Provide date/time array from a file with a series of netCDF CF-compilant time variable
     
    29993022    realdates = np.zeros((dimt, 6), dtype=int)
    30003023
     3024# Checking calendar!
     3025##
     3026    360daycal = ['earth_360d', '360d', '360days']
     3027    if searchInlist(attvar, 'calendar'):
     3028        calendar = times.getncattr('calendar')
     3029        if searchInlist(360daycal,calendar):
     3030            print warnmsg
     3031            print '  ' + fname + ': calendar of 12 months of 30 days !!'
     3032            360daycal = True
     3033
    30013034## Not in timedelta
    30023035#    if tunits == 'years':
     
    30083041#            realdate = refdate + dt.timedelta(months=float(times[it]))
    30093042#            realdates[it] = int(realdate.year)
    3010     if tunits == 'weeks':
    3011         for it in range(dimt):
    3012             realdate = refdate + dt.timedelta(weeks=float(times[it]))
    3013             realdates[it,0] = int(realdate.year)
    3014             realdates[it,1] = int(realdate.month)
    3015             realdates[it,2] = int(realdate.day)
    3016             realdates[it,3] = int(realdate.hour)
    3017             realdates[it,4] = int(realdate.second)
    3018             realdates[it,5] = int(realdate.minute)
    3019     elif tunits == 'days':
    3020         for it in range(dimt):
    3021             realdate = refdate + dt.timedelta(days=float(times[it]))
    3022             realdates[it,0] = int(realdate.year)
    3023             realdates[it,1] = int(realdate.month)
    3024             realdates[it,2] = int(realdate.day)
    3025             realdates[it,3] = int(realdate.hour)
    3026             realdates[it,4] = int(realdate.second)
    3027             realdates[it,5] = int(realdate.minute)
    3028     elif tunits == 'hours':
    3029        for it in range(dimt):
    3030             realdate = refdate + dt.timedelta(hours=float(times[it]))
    3031             realdates[it,0] = int(realdate.year)
    3032             realdates[it,1] = int(realdate.month)
    3033             realdates[it,2] = int(realdate.day)
    3034             realdates[it,3] = int(realdate.hour)
    3035             realdates[it,4] = int(realdate.second)
    3036             realdates[it,5] = int(realdate.minute)
    3037     elif tunits == 'minutes':
    3038        for it in range(dimt):
    3039             realdate = refdate + dt.timedelta(minutes=float(times[it]))
    3040             realdates[it,0] = int(realdate.year)
    3041             realdates[it,1] = int(realdate.month)
    3042             realdates[it,2] = int(realdate.day)
    3043             realdates[it,3] = int(realdate.hour)
    3044             realdates[it,4] = int(realdate.second)
    3045             realdates[it,5] = int(realdate.minute)
    3046     elif tunits == 'seconds':
    3047        for it in range(dimt):
    3048             realdate = refdate + dt.timedelta(seconds=float(times[it]))
    3049             realdates[it,0] = int(realdate.year)
    3050             realdates[it,1] = int(realdate.month)
    3051             realdates[it,2] = int(realdate.day)
    3052             realdates[it,3] = int(realdate.hour)
    3053             realdates[it,4] = int(realdate.second)
    3054             realdates[it,5] = int(realdate.minute)
    3055     elif tunits == 'milliseconds':
    3056        for it in range(dimt):
    3057             realdate = refdate + dt.timedelta(milliseconds=float(times[it]))
    3058             realdates[it,0] = int(realdate.year)
    3059             realdates[it,1] = int(realdate.month)
    3060             realdates[it,2] = int(realdate.day)
    3061             realdates[it,3] = int(realdate.hour)
    3062             realdates[it,4] = int(realdate.second)
    3063             realdates[it,5] = int(realdate.minute)
     3043    if 360daycal:
     3044        if tunits == 'weeks':
     3045            for it in range(dimt):
     3046                realdate = refdate + dt.timedelta(weeks=float(times[it]))
     3047                realdates[it,:] = date_juliandate(realdate)
     3048        elif tunits == 'days':
     3049            for it in range(dimt):
     3050                realdate = refdate + dt.timedelta(days=float(times[it]))
     3051                realdates[it,:] = date_juliandate(realdate)
     3052        elif tunits == 'hours':
     3053           for it in range(dimt):
     3054                realdate = refdate + dt.timedelta(hours=float(times[it]))
     3055                realdates[it,:] = date_juliandate(realdate)
     3056        elif tunits == 'minutes':
     3057           for it in range(dimt):
     3058                realdate = refdate + dt.timedelta(minutes=float(times[it]))
     3059                realdates[it,:] = date_juliandate(realdate)
     3060        elif tunits == 'seconds':
     3061           for it in range(dimt):
     3062                realdate = refdate + dt.timedelta(seconds=float(times[it]))
     3063                realdates[it,:] = date_juliandate(realdate)
     3064        elif tunits == 'miliseconds':
     3065           for it in range(dimt):
     3066                realdate = refdate + dt.timedelta(miliseconds=float(times[it]))
     3067                realdates[it,:] = date_juliandate(realdate)
     3068        else:
     3069              print errormsg
     3070              print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
     3071              quit(-1)
    30643072    else:
    3065           print errormsg
    3066           print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
    3067           quit(-1)
    3068 
     3073        if tunits == 'weeks':
     3074            for it in range(dimt):
     3075                realdate = refdate + dt.timedelta(weeks=float(times[it]))
     3076                realdates[it,0] = int(realdate.year)
     3077                realdates[it,1] = int(realdate.month)
     3078                realdates[it,2] = int(realdate.day)
     3079                realdates[it,3] = int(realdate.hour)
     3080                realdates[it,4] = int(realdate.second)
     3081                realdates[it,5] = int(realdate.minute)
     3082        elif tunits == 'days':
     3083            for it in range(dimt):
     3084                realdate = refdate + dt.timedelta(days=float(times[it]))
     3085                realdates[it,0] = int(realdate.year)
     3086                realdates[it,1] = int(realdate.month)
     3087                realdates[it,2] = int(realdate.day)
     3088                realdates[it,3] = int(realdate.hour)
     3089                realdates[it,4] = int(realdate.second)
     3090                realdates[it,5] = int(realdate.minute)
     3091        elif tunits == 'hours':
     3092           for it in range(dimt):
     3093                realdate = refdate + dt.timedelta(hours=float(times[it]))
     3094                realdates[it,0] = int(realdate.year)
     3095                realdates[it,1] = int(realdate.month)
     3096                realdates[it,2] = int(realdate.day)
     3097                realdates[it,3] = int(realdate.hour)
     3098                realdates[it,4] = int(realdate.second)
     3099                realdates[it,5] = int(realdate.minute)
     3100        elif tunits == 'minutes':
     3101           for it in range(dimt):
     3102                realdate = refdate + dt.timedelta(minutes=float(times[it]))
     3103                realdates[it,0] = int(realdate.year)
     3104                realdates[it,1] = int(realdate.month)
     3105                realdates[it,2] = int(realdate.day)
     3106                realdates[it,3] = int(realdate.hour)
     3107                realdates[it,4] = int(realdate.second)
     3108                realdates[it,5] = int(realdate.minute)
     3109        elif tunits == 'seconds':
     3110           for it in range(dimt):
     3111                realdate = refdate + dt.timedelta(seconds=float(times[it]))
     3112                realdates[it,0] = int(realdate.year)
     3113                realdates[it,1] = int(realdate.month)
     3114                realdates[it,2] = int(realdate.day)
     3115                realdates[it,3] = int(realdate.hour)
     3116                realdates[it,4] = int(realdate.second)
     3117                realdates[it,5] = int(realdate.minute)
     3118        elif tunits == 'milliseconds':
     3119           for it in range(dimt):
     3120                realdate = refdate + dt.timedelta(milliseconds=float(times[it]))
     3121                realdates[it,0] = int(realdate.year)
     3122                realdates[it,1] = int(realdate.month)
     3123                realdates[it,2] = int(realdate.day)
     3124                realdates[it,3] = int(realdate.hour)
     3125                realdates[it,4] = int(realdate.second)
     3126                realdates[it,5] = int(realdate.minute)
     3127        else:
     3128              print errormsg
     3129              print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
     3130              quit(-1)
     3131   
    30693132    return realdates
    30703133
Note: See TracChangeset for help on using the changeset viewer.