Index: /trunk/tools/generic_tools.py
===================================================================
--- /trunk/tools/generic_tools.py	(revision 2157)
+++ /trunk/tools/generic_tools.py	(revision 2158)
@@ -70,4 +70,6 @@
 # CFmonthU_daysU: Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'
 # CFtime_freq: Function to provide the frequency of CF times (in seconds)
+# CFtimes_datetime: Provide date/time array from a file with a series of netCDF CF-compilant time variable
+# CFtimesvar_datetime: Provide date/time array from a CF-compilant time variable
 # CFvar_DIAGvar: Function to provide which model diagnostic values can provide a CF-variable from ASCII file
 # CFvar_MODvar: Function to provide which model values can provide a CF-variable from ASCII file
@@ -3145,4 +3147,153 @@
     return realdates
 
+def CFtimesvar_datetime(times, units, calendar):
+    """ Provide date/time array from a CF-compilant time variable
+    timevals = CF values of the variable time
+    units = CF-compilant units ([Tunit] since [date]) of timevals
+    calendar= calendar of the time values
+      'earth_360d', '360d', '360days', '360_day' any of them for years of 360 days
+    output:
+      array(dimt, 0) = year
+      array(dimt, 1) = month
+      array(dimt, 2) = day
+      array(dimt, 3) = hour
+      array(dimt, 4) = minute
+      array(dimt, 5) = second
+    """
+    import datetime as dt
+    fname = 'CFtimesvar_datetime'
+
+    txtunits = units.split(' ')
+    tunits = txtunits[0]
+    Srefdate = txtunits[len(txtunits) - 1]
+# Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]
+##
+    timeval = Srefdate.find(':')
+    if not timeval == -1:
+#        print '  refdate with time!'
+        refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate)
+    else:
+        refdate = dateStr_date(Srefdate)
+
+    dimt = len(times)
+    realdates = np.zeros((dimt, 6), dtype=int)
+
+    secsDay=3600*24.
+
+# Checking calendar! 
+## 
+    y360 = False
+    daycal360 = ['earth_360d', '360d', '360days', '360_day']
+    if searchInlist(daycal360,calendar):
+        print warnmsg
+        print '  ' + fname + ': calendar of 12 months of 30 days !!'
+        y360 = True
+
+## Not in timedelta
+#    if tunits == 'years':
+#        for it in range(dimt):
+#            realdate = refdate + dt.timedelta(years=float(times[it]))
+#            realdates[it] = int(realdate.year)
+#    elif tunits == 'months':
+#        for it in range(dimt):
+#            realdate = refdate + dt.timedelta(months=float(times[it]))
+#            realdates[it] = int(realdate.year)
+    if y360:
+        if tunits == 'weeks':
+            for it in range(dimt):
+                deltat = dt.timedelta(weeks=float(times[it]))
+                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
+                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
+        elif tunits == 'days':
+            for it in range(dimt):
+                deltat = dt.timedelta(days=float(times[it]))
+                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
+                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
+        elif tunits == 'hours':
+           for it in range(dimt):
+                realdate = dt.timedelta(hours=float(times[it]))
+                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
+                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
+        elif tunits == 'minutes':
+           for it in range(dimt):
+                realdate = dt.timedelta(minutes=float(times[it]))
+                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
+                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
+        elif tunits == 'seconds':
+           for it in range(dimt):
+                realdate = dt.timedelta(seconds=float(times[it]))
+                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
+                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
+        elif tunits == 'miliseconds':
+           for it in range(dimt):
+                realdate = dt.timedelta(miliseconds=float(times[it]))
+                Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.
+                realdates[it,:] = date_juliandate(refdate.year,Tsecs)
+        else:
+              print errormsg
+              print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
+              quit(-1)
+    else:
+        if tunits == 'weeks':
+            for it in range(dimt):
+                realdate = refdate + dt.timedelta(weeks=float(times[it]))
+                realdates[it,0] = int(realdate.year)
+                realdates[it,1] = int(realdate.month)
+                realdates[it,2] = int(realdate.day)
+                realdates[it,3] = int(realdate.hour)
+                realdates[it,4] = int(realdate.second)
+                realdates[it,5] = int(realdate.minute)
+        elif tunits == 'days':
+            for it in range(dimt):
+                realdate = refdate + dt.timedelta(days=float(times[it]))
+                realdates[it,0] = int(realdate.year)
+                realdates[it,1] = int(realdate.month)
+                realdates[it,2] = int(realdate.day)
+                realdates[it,3] = int(realdate.hour)
+                realdates[it,4] = int(realdate.second)
+                realdates[it,5] = int(realdate.minute)
+        elif tunits == 'hours':
+           for it in range(dimt):
+                realdate = refdate + dt.timedelta(hours=float(times[it]))
+                realdates[it,0] = int(realdate.year)
+                realdates[it,1] = int(realdate.month)
+                realdates[it,2] = int(realdate.day)
+                realdates[it,3] = int(realdate.hour)
+                realdates[it,4] = int(realdate.second)
+                realdates[it,5] = int(realdate.minute)
+        elif tunits == 'minutes':
+           for it in range(dimt):
+                realdate = refdate + dt.timedelta(minutes=float(times[it]))
+                realdates[it,0] = int(realdate.year)
+                realdates[it,1] = int(realdate.month)
+                realdates[it,2] = int(realdate.day)
+                realdates[it,3] = int(realdate.hour)
+                realdates[it,4] = int(realdate.second)
+                realdates[it,5] = int(realdate.minute)
+        elif tunits == 'seconds':
+           for it in range(dimt):
+                realdate = refdate + dt.timedelta(seconds=float(times[it]))
+                realdates[it,0] = int(realdate.year)
+                realdates[it,1] = int(realdate.month)
+                realdates[it,2] = int(realdate.day)
+                realdates[it,3] = int(realdate.hour)
+                realdates[it,4] = int(realdate.second)
+                realdates[it,5] = int(realdate.minute)
+        elif tunits == 'milliseconds':
+           for it in range(dimt):
+                realdate = refdate + dt.timedelta(milliseconds=float(times[it]))
+                realdates[it,0] = int(realdate.year)
+                realdates[it,1] = int(realdate.month)
+                realdates[it,2] = int(realdate.day)
+                realdates[it,3] = int(realdate.hour)
+                realdates[it,4] = int(realdate.second)
+                realdates[it,5] = int(realdate.minute)
+        else:
+              print errormsg
+              print '    CFtimes_datetime: time units "' + tunits + '" not ready!!!!'
+              quit(-1)
+    
+    return realdates
+
 class statsValWeigthed(object):
   """Weigthed Statistics class providing:
@@ -13876,4 +14027,83 @@
 #print WRF_percenlevels(38)
 
+def time_slices(tv, tu, cal, per, amount):
+    """ Function to return temporal slices of a series of times for a given amount 
+        of periods
+      tv: time vallues (CF format)
+      tu: CF time-units
+      cal: calendar
+      per: period
+        'year': full year [01/01 00:00:00 - 12/31 23:59:59]
+        'month': full months [01 00:00:00 - [31,30,28/29] 23:59:59]
+        'day': full days [00:00:00 - 23:59:59]
+        'hour': full hours [00:00 - 59:59]
+        'minute': full minutes [00 - 59]
+      amount: amount of periods
+    """
+    import datetime as dt
+    fname = 'time_slices'
+
+    mattimes = CFtimesvar_datetime(tv, tu, cal)
+    print 'CF times _______'
+    print mattimes
+
+    if type(tv) == type(np.arange(2)):
+        dimt = tv.shape[0]
+    elif type(tv) == type(range(2)):
+        dimt = len(tv)
+    else:
+        print errormsg
+        print '  ' + fname + ': time values type ', type(tv), 'not ready !!'
+        print '    available ones:', type(np.arange(1)), type(range(2))
+        quit(-1)
+
+    slices = []
+    if per == 'year':
+        imat = 0
+        # Getting unique values
+        years = list(mattimes[:,imat])
+        setvar = set(years)
+        utimes = list(setvar)
+        Nutimes = len(utimes)
+
+        # introducing amounts
+        print utimes, ':', Nutimes
+        atimes = np.arange(utimes[0], utimes[Nutimes-1], amount)
+        Ntimes = len(atimes)
+        
+        itt = 0
+        for iat in range(Ntimes-1):
+            # timeslice: [ini_slice, end_slice, freq_slice]
+            timeslice = [itt,itt,1]
+            ip = atimes[iat]
+            ep = atimes[iat+1]
+
+            for it in range(itt,dimt):
+                if mattimes[it,imat] >= ip and mattimes[it,imat] < ep: 
+                    timeslice[1] = it
+                else:
+                    slices.append(timeslice)
+                    itt = it + 0
+                    break
+
+    Nslices = len(slices)
+    for isl in range(Nslices):
+        print atimes[isl], atimes[isl+1], ':', slices[isl]
+
+    return slices
+
+tv = []
+for it in range(0,10):
+    tv.append(it*365)
+    tv.append(it*365+180.)
+    tv.append(it*365+360.)
+
+tu = 'days since 1949-12-01 00:00:00'
+per = 'year'
+calend = 'standard'
+amount = 1
+
+time_slices(tv, tu, calend, per, amount)
+
 #quit()
 
