Changeset 2445 in lmdz_wrf
- Timestamp:
- Apr 16, 2019, 8:24:14 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2443 r2445 662 662 'Standard', 'noleap', '365d', '360d'] 663 663 664 if StringDT[0:1] == 'h':664 if type(StringDT) == type('S') and StringDT[0:1] == 'h': 665 665 print fname + '_____________________________________________________________' 666 666 print datetimeStr_conversion.__doc__ … … 1089 1089 return dateYmdHMS 1090 1090 1091 1092 def impose_gregorian(timev, Tunits, cal):1093 """ Function to impose gregorian calendar to a series of times with a1094 non-standard calendar1095 timev: list of values1096 Tunits: CF units of time [Tunits] since [DateRef]1097 cal: non standard calendar1098 >>> impose_gregorian(np.range(800,10000,100), 'days since 1949-12-01 00:00:00',1099 'noleap')1100 [ 800. 901.]1101 """1102 import datetime as dt1103 fname = 'impose_gregorian'1104 1105 availTu = ['weeks', 'days', 'hours', 'minutes', 'seconds']1106 availcalendar = ['noleap', '365d']1107 1108 lTunits = Tunits.split(' ')1109 NTunits = len(lTunits)1110 Tu = lTunits[0]1111 if NTunits < 4:1112 print infmsg1113 print ' ' + fname + ": CF-time units without time, adding '00:00:00' !!"1114 Srefdate = lTunits[2] + ' 00:00:00'1115 refdate = dt.datetime.combine(dateStr_date(lTunits[2]), \1116 timeStr_time('00:00:00'))1117 else:1118 Srefdate = lTunits[2] + ' ' + lTunits[3]1119 refdate = dt.datetime.combine(dateStr_date(lTunits[2]), \1120 timeStr_time(lTunits[3]))1121 1122 if type(timev) == type(range(2)):1123 dimt = len(timev)1124 timev = np.array(timev)1125 elif type(timev) == type(np.arange(2)):1126 dimt = timev.shape[0]1127 elif type(timev) == type(int(2)) or type(timev) == type(np.float(2.)) or \1128 type(timev) == type(float(2.)) or type(timev) == type(np.float32(2.)) or \1129 type(timev) == type(np.float64(2.)):1130 dimt = 11131 timev = np.ones(dimt, dtype=type(timev))*timev1132 else:1133 print errormsg1134 print ' ' + fname + ": type of time values: '", type(timev), "' not ready !!"1135 print ' available ones:', type(range(2)), type(np.arange(2)),type(int(2)),\1136 type(np.float(2.)), type(float(2.)), type(np.float32(2.)), \1137 type(np.float64(2.))1138 quit(-1)1139 1140 if Tu == 'weeks':1141 timevsecs = timev * 24. * 7 * 3600.1142 elif Tu == 'days':1143 timevsecs = timev * 24. * 3600.1144 elif Tu == 'hours':1145 timevsecs = timev * 3600.1146 elif Tu == 'minutes':1147 timevsecs = timev * 60.1148 elif Tu == 'seconds':1149 timevsecs = timev * 1.1150 else:1151 print errormsg1152 print ' ' + fname + ": CF time units '" + Tu + "' not ready !!"1153 print ' available ones:', availTu1154 quit(-1)1155 1156 newtimes = np.zeros((dimt), dtype=np.float64)1157 1158 yrref = refdate.year1159 if cal == 'noleap' or cal == '365d':1160 for it in range(dimt):1161 # Some issues in implementations/machines ...1162 # FROM: https://stackoverflow.com/questions/33566939/timedelta-error-with-numpy-longdouble-dtype1163 tv = int(timevsecs[it])1164 deltaT = dt.timedelta(seconds=tv)1165 datetime = refdate + deltaT1166 yr = datetime.year1167 Nleapd = leapdays(yrref, yr)1168 if datetime.month <= 2 and days_month(yr, 2) > 28: Nleapd = Nleapd - 11169 newtimes[it] = timevsecs[it] + Nleapd*24*3600.1170 else:1171 print errormsg1172 print ' ' + fname + ": calendar '" + cal + "' not ready !!"1173 print ' available ones:', availcalendar1174 quit(-1)1175 1176 # Re-transforming to the original units1177 if Tu == 'weeks':1178 newtimes = newtimes/(24.*7*3600.)1179 elif Tu == 'days':1180 newtimes = newtimes/(24.*3600.)1181 elif Tu == 'hours':1182 newtimes = newtimes/(3600.)1183 elif Tu == 'minutes':1184 newtimes = newtimes/(60.)1185 1186 return newtimes1187 1188 1091 def period_information(idate, edate, totunits): 1189 1092 """ Function to provide the information of a given period idate, edate (dates in [YYYY][MM][DD][HH][MI][SS] format) … … 1945 1848 1946 1849 return oinf 1850 1851 1852 def dateStr_date(StringDate): 1853 """ Function to transform a string date ([YYYY]-[MM]-[DD] format) to a date object 1854 >>> dateStr_date('1976-02-17') 1855 1976-02-17 1856 """ 1857 import datetime as dt 1858 1859 dateD = StringDate.split('-') 1860 if int(dateD[0]) == 0: 1861 print warnmsg 1862 print ' dateStr_date: 0 reference year!! changing to 1' 1863 dateD[0] = 1 1864 newdate = dt.date(int(dateD[0]), int(dateD[1]), int(dateD[2])) 1865 return newdate 1866 1867 def timeStr_time(StringDate): 1868 """ Function to transform a string date ([HH]:[MI]:[SS] format) to a time object 1869 >>> timeStr_datetime('04:32:54') 1870 04:32:54 1871 """ 1872 import datetime as dt 1873 1874 timeT = StringDate.split(':') 1875 if len(timeT) == 3: 1876 newtime = dt.time(int(timeT[0]), int(timeT[1]), int(timeT[2])) 1877 else: 1878 newtime = dt.time(int(timeT[0]), int(timeT[1]), 0) 1879 1880 return newtime 1881 1882 def DateTimeStr_date(StringDate): 1883 """ Function to transform a string date-time ([YYYY][MM][DD][HH][MI][SS] format) to a date object 1884 >>> DateTimeStr_date('19760217083205') 1885 1976-02-17 08:32:05 1886 """ 1887 import datetime as dt 1888 1889 yr = int(StringDate[0:4]) 1890 mo = int(StringDate[4:6]) 1891 da = int(StringDate[6:8]) 1892 ho = int(StringDate[8:10]) 1893 mi = int(StringDate[10:12]) 1894 se = int(StringDate[12:14]) 1895 1896 if yr == 0: 1897 print warnmsg 1898 print ' ' + fname + ': 0 reference year!! changing to 1' 1899 yr = 1 1900 newdate = dt.datetime(yr, mo, da, ho, mi, se) 1901 1902 return newdate 1903 1904 1905 def impose_gregorian(timev, Tunits, cal): 1906 """ Function to impose gregorian calendar to a series of times with a 1907 non-standard calendar 1908 timev: list of values 1909 Tunits: CF units of time [Tunits] since [DateRef] 1910 cal: non standard calendar 1911 >>> impose_gregorian(np.range(800,10000,100), 'days since 1949-12-01 00:00:00', 1912 'noleap') 1913 [ 800. 901.] 1914 """ 1915 import datetime as dt 1916 fname = 'impose_gregorian' 1917 1918 availTu = ['weeks', 'days', 'hours', 'minutes', 'seconds'] 1919 availcalendar = ['noleap', '365d'] 1920 1921 lTunits = Tunits.split(' ') 1922 NTunits = len(lTunits) 1923 Tu = lTunits[0] 1924 if NTunits < 4: 1925 print infmsg 1926 print ' ' + fname + ": CF-time units without time, adding '00:00:00' !!" 1927 Srefdate = lTunits[2] + ' 00:00:00' 1928 refdate = dt.datetime.combine(dateStr_date(lTunits[2]), \ 1929 timeStr_time('00:00:00')) 1930 else: 1931 Srefdate = lTunits[2] + ' ' + lTunits[3] 1932 refdate = dt.datetime.combine(dateStr_date(lTunits[2]), \ 1933 timeStr_time(lTunits[3])) 1934 1935 if type(timev) == type(range(2)): 1936 dimt = len(timev) 1937 timev = np.array(timev) 1938 elif type(timev) == type(np.arange(2)): 1939 dimt = timev.shape[0] 1940 elif type(timev) == type(int(2)) or type(timev) == type(np.float(2.)) or \ 1941 type(timev) == type(float(2.)) or type(timev) == type(np.float32(2.)) or \ 1942 type(timev) == type(np.float64(2.)): 1943 dimt = 1 1944 timev = np.ones(dimt, dtype=type(timev))*timev 1945 else: 1946 print errormsg 1947 print ' ' + fname + ": type of time values: '", type(timev), "' not ready !!" 1948 print ' available ones:', type(range(2)), type(np.arange(2)),type(int(2)),\ 1949 type(np.float(2.)), type(float(2.)), type(np.float32(2.)), \ 1950 type(np.float64(2.)) 1951 quit(-1) 1952 1953 if Tu == 'weeks': 1954 timevsecs = timev * 24. * 7 * 3600. 1955 elif Tu == 'days': 1956 timevsecs = timev * 24. * 3600. 1957 elif Tu == 'hours': 1958 timevsecs = timev * 3600. 1959 elif Tu == 'minutes': 1960 timevsecs = timev * 60. 1961 elif Tu == 'seconds': 1962 timevsecs = timev * 1. 1963 else: 1964 print errormsg 1965 print ' ' + fname + ": CF time units '" + Tu + "' not ready !!" 1966 print ' available ones:', availTu 1967 quit(-1) 1968 1969 newtimes = np.zeros((dimt), dtype=np.float64) 1970 1971 yrref = refdate.year 1972 if cal == 'noleap' or cal == '365d': 1973 for it in range(dimt): 1974 # Some issues in implementations/machines ... 1975 # FROM: https://stackoverflow.com/questions/33566939/timedelta-error-with-numpy-longdouble-dtype 1976 tv = int(timevsecs[it]) 1977 deltaT = dt.timedelta(seconds=tv) 1978 datetime = refdate + deltaT 1979 yr = datetime.year 1980 Nleapd = leapdays(yrref, yr) 1981 if datetime.month <= 2 and days_month(yr, 2) > 28: Nleapd = Nleapd - 1 1982 newtimes[it] = timevsecs[it] + Nleapd*24*3600. 1983 else: 1984 print errormsg 1985 print ' ' + fname + ": calendar '" + cal + "' not ready !!" 1986 print ' available ones:', availcalendar 1987 quit(-1) 1988 1989 # Re-transforming to the original units 1990 if Tu == 'weeks': 1991 newtimes = newtimes/(24.*7*3600.) 1992 elif Tu == 'days': 1993 newtimes = newtimes/(24.*3600.) 1994 elif Tu == 'hours': 1995 newtimes = newtimes/(3600.) 1996 elif Tu == 'minutes': 1997 newtimes = newtimes/(60.) 1998 1999 return newtimes 2000 2001 def timeref_datetime(refd, timeval, tu, cal='gregorian'): 2002 """ Function to transform from a [timeval] in [tu] units from the time referece [tref] to datetime object 2003 refd: time of reference (as datetime object) 2004 timeval: time value (as [tu] from [tref]) 2005 tu: time units 2006 cal: calendar 2007 >>> timeref_datetime(dt.datetime(1949,12,1,0,0,0), 229784.36, 'hours') 2008 1976-02-17 08:21:36 2009 >>> timeref_datetime(dt.datetime(1949,12,1,0,0,0), 230784.36, 'hours') 2010 1976-03-30 00:21:36 2011 >>> timeref_datetime(dt.datetime(1949,12,1,0,0,0), 230784.36, 'hours', 'noleap') 2012 1976-04-06 00:21:36 2013 """ 2014 import datetime as dt 2015 import numpy as np 2016 2017 availtu = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds'] 2018 2019 daterefS = str(refd.year).zfill(4) + '-' + str(refd.month).zfill(2) + '-' + \ 2020 str(refd.day).zfill(2) + ' ' + str(refd.hour).zfill(2) + ':'+ \ 2021 str(refd.minute).zfill(2) + ':' + str(refd.second).zfill(2) 2022 2023 # For non-gregorian calendars, we impose gregorian (since, we are using python) 2024 if cal == 'noleap' or cal == '365d': 2025 timev = impose_gregorian(timeval, tu + ' since ' + daterefS, cal) 2026 else: 2027 timev = timeval + 0. 2028 2029 ## Not in timedelta 2030 # if tu == 'years': 2031 # realdate = refdate + dt.timedelta(years=float(timeval)) 2032 # elif tu == 'months': 2033 # realdate = refdate + dt.timedelta(months=float(timeval)) 2034 if tu == 'weeks': 2035 realdate = refd + dt.timedelta(weeks=float(timev)) 2036 elif tu == 'days': 2037 realdate = refd + dt.timedelta(days=float(timev)) 2038 elif tu == 'hours': 2039 realdate = refd + dt.timedelta(hours=float(timev)) 2040 elif tu == 'minutes': 2041 realdate = refd + dt.timedelta(minutes=float(timev)) 2042 elif tu == 'seconds': 2043 realdate = refd + dt.timedelta(seconds=float(timev)) 2044 elif tu == 'milliseconds': 2045 realdate = refd + dt.timedelta(milliseconds=float(timev)) 2046 else: 2047 print errormsg 2048 print ' ' + fname + ": time units '" + tu + "' not ready!!!!" 2049 print ' available ones:', availtu 2050 quit(-1) 2051 2052 return realdate 2053 2054 def timeref_datetime_mat(refd, timeval, tu): 2055 """ Function to transform from a [timeval] in [tu] units from the time referece [tref] to matrix with: year, day, month, hour, minute, second 2056 refd: time of reference (as datetime object) 2057 timeval: time value (as [tu] from [tref]) 2058 tu: time units 2059 >>> timeref = date(1949,12,1,0,0,0) 2060 >>> timeref_datetime(timeref, 229784.36, hours) 2061 [1976 2 17 8 36 21] 2062 """ 2063 import datetime as dt 2064 import numpy as np 2065 2066 2067 realdates = np.zeros(6, dtype=int) 2068 ## Not in timedelta 2069 # if tu == 'years': 2070 # realdate = refdate + dt.timedelta(years=float(timeval)) 2071 # elif tu == 'months': 2072 # realdate = refdate + dt.timedelta(months=float(timeval)) 2073 if tu == 'weeks': 2074 realdate = refd + dt.timedelta(weeks=float(timeval)) 2075 elif tu == 'days': 2076 realdate = refd + dt.timedelta(days=float(timeval)) 2077 elif tu == 'hours': 2078 realdate = refd + dt.timedelta(hours=float(timeval)) 2079 elif tu == 'minutes': 2080 realdate = refd + dt.timedelta(minutes=float(timeval)) 2081 elif tunits == 'seconds': 2082 realdate = refd + dt.timedelta(seconds=float(timeval)) 2083 elif tunits == 'milliseconds': 2084 realdate = refd + dt.timedelta(milliseconds=float(timeval)) 2085 else: 2086 print errormsg 2087 print ' timeref_datetime: time units "' + tu + '" not ready!!!!' 2088 quit(-1) 2089 2090 realdates[0] = int(realdate.year) 2091 realdates[1] = int(realdate.month) 2092 realdates[2] = int(realdate.day) 2093 realdates[3] = int(realdate.hour) 2094 realdates[4] = int(realdate.second) 2095 realdates[5] = int(realdate.minute) 2096 2097 return realdates 2098 2099 def realdatetime_CFcompilant(times, Srefdate, tunits): 2100 """ Function to transform a matrix with real time values ([year, month, day, hour, minute, second]) to a netCDF one 2101 times= matrix with times 2102 Srefdate= reference date ([YYYY][MM][DD][HH][MI][SS] format) 2103 tunits= units of time respect to Srefdate 2104 >>> realdatetime_CFcompilant(np.array([ [1976, 2, 17, 8, 20, 0], [1976, 2, 18, 8, 20, 0]], dtype=int), '19491201000000', 'hours') 2105 [ 229784.33333333 229808.33333333] 2106 """ 2107 2108 import datetime as dt 2109 yrref=int(Srefdate[0:4]) 2110 monref=int(Srefdate[4:6]) 2111 dayref=int(Srefdate[6:8]) 2112 horref=int(Srefdate[8:10]) 2113 minref=int(Srefdate[10:12]) 2114 secref=int(Srefdate[12:14]) 2115 2116 refdate=dt.datetime(yrref, monref, dayref, horref, minref, secref) 2117 2118 dimt=times.shape[0] 2119 2120 cfdates = np.zeros((dimt), dtype=np.float64) 2121 if tunits == 'weeks': 2122 for it in range(dimt): 2123 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2124 cfdates[it] = (cfdate.days + cfdate.seconds/(3600.*24.))/7. 2125 elif tunits == 'days': 2126 for it in range(dimt): 2127 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2128 cfdates[it] = cfdate.days + cfdate.seconds/(3600.*24.) 2129 elif tunits == 'hours': 2130 for it in range(dimt): 2131 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2132 cfdates[it] = cfdate.days*24. + cfdate.seconds/3600. 2133 elif tunits == 'minutes': 2134 for it in range(dimt): 2135 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2136 cfdates[it] = cfdate.days*24.*60. + cfdate.seconds/60. 2137 elif tunits == 'seconds': 2138 for it in range(dimt): 2139 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2140 cfdates[it] = cfdate.days*24.*3600. + cfdate.seconds 2141 elif tunits == 'milliseconds': 2142 for it in range(dimt): 2143 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2144 cfdates[it] = cfdate.days*1000.*24.*3600. + cfdate.seconds*1000. 2145 elif tunits == 'microseconds': 2146 for it in range(dimt): 2147 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate 2148 cfdates[it] = cfdate.days*1000000.*24.*3600. + cfdate.seconds*1000000. 2149 else: 2150 print errormsg 2151 print ' ' + fname + ': time units "' + tunits + '" is not ready!!!' 2152 quit(-1) 2153 2154 return cfdates 2155 2156 def netCDFdatetime_realdatetime(units, tcalendar, times): 2157 """ Function to transfrom from netCDF CF-compilant times to real time 2158 """ 2159 import datetime as dt 2160 2161 txtunits = units.split(' ') 2162 tunits = txtunits[0] 2163 Srefdate = txtunits[len(txtunits) - 1] 2164 2165 # Calendar type 2166 ## 2167 is360 = False 2168 if tcalendar is not None: 2169 print ' netCDFdatetime_realdatetime: There is a calendar attribute' 2170 if tcalendar == '365_day' or tcalendar == 'noleap': 2171 print ' netCDFdatetime_realdatetime: No leap years!' 2172 isleapcal = False 2173 elif tcalendar == 'proleptic_gregorian' or tcalendar == 'standard' or tcalendar == 'gregorian': 2174 isleapcal = True 2175 elif tcalendar == '360_day': 2176 is360 = True 2177 isleapcal = False 2178 else: 2179 print errormsg 2180 print ' netCDFdatetime_realdatetime: Calendar "' + tcalendar + '" not prepared!' 2181 quit(-1) 2182 2183 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 2184 ## 2185 timeval = Srefdate.find(':') 2186 2187 if not timeval == -1: 2188 print ' netCDFdatetime_realdatetime: refdate with time!' 2189 refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate) 2190 else: 2191 refdate = dateStr_date(Srefdate) 2192 2193 dimt = len(times) 2194 # datetype = type(dt.datetime(1972,02,01)) 2195 # realdates = np.array(dimt, datetype) 2196 # print realdates 2197 2198 ## Not in timedelta 2199 # if tunits == 'years': 2200 # for it in range(dimt): 2201 # realdate = refdate + dt.timedelta(years=float(times[it])) 2202 # realdates[it] = int(realdate.year) 2203 # elif tunits == 'months': 2204 # for it in range(dimt): 2205 # realdate = refdate + dt.timedelta(months=float(times[it])) 2206 # realdates[it] = int(realdate.year) 2207 # realdates = [] 2208 realdates = np.zeros((dimt, 6), dtype=int) 2209 if tunits == 'weeks': 2210 for it in range(dimt): 2211 realdate = refdate + dt.timedelta(weeks=float(times[it])) 2212 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2213 elif tunits == 'days': 2214 for it in range(dimt): 2215 realdate = refdate + dt.timedelta(days=float(times[it])) 2216 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2217 elif tunits == 'hours': 2218 for it in range(dimt): 2219 realdate = refdate + dt.timedelta(hours=float(times[it])) 2220 # if not isleapcal: 2221 # Nleapdays = cal.leapdays(int(refdate.year), int(realdate.year)) 2222 # realdate = realdate - dt.timedelta(days=Nleapdays) 2223 # if is360: 2224 # Nyears360 = int(realdate.year) - int(refdate.year) + 1 2225 # realdate = realdate -dt.timedelta(days=Nyears360*5) 2226 # realdates[it] = realdate 2227 # realdates = refdate + dt.timedelta(hours=float(times)) 2228 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2229 elif tunits == 'minutes': 2230 for it in range(dimt): 2231 realdate = refdate + dt.timedelta(minutes=float(times[it])) 2232 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2233 elif tunits == 'seconds': 2234 for it in range(dimt): 2235 realdate = refdate + dt.timedelta(seconds=float(times[it])) 2236 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2237 elif tunits == 'milliseconds': 2238 for it in range(dimt): 2239 realdate = refdate + dt.timedelta(milliseconds=float(times[it])) 2240 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2241 elif tunits == 'microseconds': 2242 for it in range(dimt): 2243 realdate = refdate + dt.timedelta(microseconds=float(times[it])) 2244 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second] 2245 else: 2246 print errormsg 2247 print ' netCDFdatetime_realdatetime: time units "' + tunits + '" is not ready!!!' 2248 quit(-1) 2249 2250 return realdates 2251 2252 def date_juliandate(refyr, TOTsecs): 2253 """ Function to transform from a total quantity of seconds since the 2254 beginning of a year to 360 days / year calendar 2255 TOTsecs= total number of seconds 2256 """ 2257 fname = 'date_juliandate' 2258 2259 secsYear = 3600*24*30*12. 2260 secsMonth = 3600*24*30. 2261 secsDay = 3600*24. 2262 secsHour = 3600. 2263 secsMinute = 60. 2264 2265 rldate = np.zeros((6), dtype=int) 2266 rldate[0] = refyr 2267 if TOTsecs > secsYear: 2268 rldate[0] = refyr + int(TOTsecs/secsYear) + 1 2269 TOTsecs = TOTsecs - int(TOTsecs/secsYear)*secsYear 2270 2271 if np.mod(TOTsecs,secsMonth) == 0: 2272 rldate[1] = int(TOTsecs/secsMonth) 2273 else: 2274 rldate[1] = int(TOTsecs/secsMonth) + 1 2275 TOTsecs = TOTsecs - int(TOTsecs/secsMonth)*secsMonth 2276 2277 if np.mod(TOTsecs,secsDay) == 0: 2278 rldate[2] = int(TOTsecs/secsDay) 2279 else: 2280 rldate[2] = int(TOTsecs/secsDay) + 1 2281 TOTsecs = TOTsecs - int(TOTsecs/secsDay)*secsDay 2282 2283 if np.mod(TOTsecs,secsHour) == 0: 2284 rldate[3] = int(TOTsecs/secsHour) 2285 else: 2286 rldate[3] = int(TOTsecs/secsHour) + 1 2287 TOTsecs = TOTsecs - int(TOTsecs/secsHour)*secsHour 2288 2289 if np.mod(TOTsecs,secsMinute) == 0: 2290 rldate[4] = int(TOTsecs/secsMinute) 2291 else: 2292 rldate[4] = int(TOTsecs/secsMinute) + 1 2293 TOTsecs = TOTsecs - int(TOTsecs/secsMinute)*secsMinute 2294 2295 rldate[5] = TOTsecs 2296 2297 # print refyr,TOTsecs,':',rldate 2298 # quit() 2299 2300 return rldate 2301 2302 def CFtimes_datetime(ncfile, tname): 2303 """ Provide date/time array from a file with a series of netCDF CF-compilant time variable 2304 ncfile = netCDF file name 2305 tname = name of the variable time in [ncfile] 2306 output: 2307 array(dimt, 0) = year 2308 array(dimt, 1) = month 2309 array(dimt, 2) = day 2310 array(dimt, 3) = hour 2311 array(dimt, 4) = minute 2312 array(dimt, 5) = second 2313 """ 2314 import datetime as dt 2315 fname = 'CFtimes_datetime' 2316 2317 availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds'] 2318 2319 times = ncfile.variables[tname] 2320 timevals = times[:] 2321 2322 attvar = times.ncattrs() 2323 if not searchInlist(attvar, 'units'): 2324 print errormsg 2325 print ' ' + fname + ": '" + tname + "' does not have attribute: 'units'" 2326 quit(-1) 2327 else: 2328 units = times.getncattr('units') 2329 2330 txtunits = units.split(' ') 2331 tunits = txtunits[0] 2332 Srefdate = txtunits[len(txtunits) - 1] 2333 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 2334 ## 2335 timeval = Srefdate.find(':') 2336 2337 if not timeval == -1: 2338 # print ' refdate with time!' 2339 refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate) 2340 else: 2341 refdate = dateStr_date(Srefdate) 2342 2343 dimt = len(timevals) 2344 realdates = np.zeros((dimt, 6), dtype=int) 2345 2346 secsDay=3600*24. 2347 2348 # Checking calendar! 2349 ## 2350 y360 = False 2351 daycal360 = ['earth_360d', '360d', '360days', '360_day'] 2352 if searchInlist(attvar, 'calendar'): 2353 calendar = times.getncattr('calendar') 2354 if searchInlist(daycal360,calendar): 2355 print warnmsg 2356 print ' ' + fname + ': calendar of 12 months of 30 days !!' 2357 y360 = True 2358 2359 ## Not in timedelta 2360 # if tunits == 'years': 2361 # for it in range(dimt): 2362 # realdate = refdate + dt.timedelta(years=float(times[it])) 2363 # realdates[it] = int(realdate.year) 2364 # elif tunits == 'months': 2365 # for it in range(dimt): 2366 # realdate = refdate + dt.timedelta(months=float(times[it])) 2367 # realdates[it] = int(realdate.year) 2368 if y360: 2369 if tunits == 'weeks': 2370 for it in range(dimt): 2371 deltat = dt.timedelta(weeks=float(times[it])) 2372 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2373 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2374 elif tunits == 'days': 2375 for it in range(dimt): 2376 deltat = dt.timedelta(days=float(times[it])) 2377 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2378 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2379 elif tunits == 'hours': 2380 for it in range(dimt): 2381 realdate = dt.timedelta(hours=float(times[it])) 2382 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2383 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2384 elif tunits == 'minutes': 2385 for it in range(dimt): 2386 realdate = dt.timedelta(minutes=float(times[it])) 2387 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2388 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2389 elif tunits == 'seconds': 2390 for it in range(dimt): 2391 realdate = dt.timedelta(seconds=float(times[it])) 2392 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2393 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2394 elif tunits == 'miliseconds': 2395 for it in range(dimt): 2396 realdate = dt.timedelta(miliseconds=float(times[it])) 2397 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2398 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2399 else: 2400 print errormsg 2401 print fname + ' : time units "' + tunits + '" not ready!!!!' 2402 print ' avaialable ones:', availtunits 2403 quit(-1) 2404 else: 2405 if tunits == 'weeks': 2406 for it in range(dimt): 2407 realdate = refdate + dt.timedelta(weeks=float(times[it])) 2408 realdates[it,0] = int(realdate.year) 2409 realdates[it,1] = int(realdate.month) 2410 realdates[it,2] = int(realdate.day) 2411 realdates[it,3] = int(realdate.hour) 2412 realdates[it,4] = int(realdate.second) 2413 realdates[it,5] = int(realdate.minute) 2414 elif tunits == 'days': 2415 for it in range(dimt): 2416 realdate = refdate + dt.timedelta(days=float(times[it])) 2417 realdates[it,0] = int(realdate.year) 2418 realdates[it,1] = int(realdate.month) 2419 realdates[it,2] = int(realdate.day) 2420 realdates[it,3] = int(realdate.hour) 2421 realdates[it,4] = int(realdate.second) 2422 realdates[it,5] = int(realdate.minute) 2423 elif tunits == 'hours': 2424 for it in range(dimt): 2425 realdate = refdate + dt.timedelta(hours=float(times[it])) 2426 realdates[it,0] = int(realdate.year) 2427 realdates[it,1] = int(realdate.month) 2428 realdates[it,2] = int(realdate.day) 2429 realdates[it,3] = int(realdate.hour) 2430 realdates[it,4] = int(realdate.second) 2431 realdates[it,5] = int(realdate.minute) 2432 elif tunits == 'minutes': 2433 for it in range(dimt): 2434 realdate = refdate + dt.timedelta(minutes=float(times[it])) 2435 realdates[it,0] = int(realdate.year) 2436 realdates[it,1] = int(realdate.month) 2437 realdates[it,2] = int(realdate.day) 2438 realdates[it,3] = int(realdate.hour) 2439 realdates[it,4] = int(realdate.second) 2440 realdates[it,5] = int(realdate.minute) 2441 elif tunits == 'seconds': 2442 for it in range(dimt): 2443 realdate = refdate + dt.timedelta(seconds=float(times[it])) 2444 realdates[it,0] = int(realdate.year) 2445 realdates[it,1] = int(realdate.month) 2446 realdates[it,2] = int(realdate.day) 2447 realdates[it,3] = int(realdate.hour) 2448 realdates[it,4] = int(realdate.second) 2449 realdates[it,5] = int(realdate.minute) 2450 elif tunits == 'milliseconds': 2451 for it in range(dimt): 2452 realdate = refdate + dt.timedelta(milliseconds=float(times[it])) 2453 realdates[it,0] = int(realdate.year) 2454 realdates[it,1] = int(realdate.month) 2455 realdates[it,2] = int(realdate.day) 2456 realdates[it,3] = int(realdate.hour) 2457 realdates[it,4] = int(realdate.second) 2458 realdates[it,5] = int(realdate.minute) 2459 else: 2460 print errormsg 2461 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!' 2462 quit(-1) 2463 2464 return realdates 2465 2466 def CFtimesvar_datetime(times, units, calendar): 2467 """ Provide date/time array from a CF-compilant time variable 2468 timevals = CF values of the variable time 2469 units = CF-compilant units ([Tunit] since [date]) of timevals 2470 calendar= calendar of the time values 2471 'earth_360d', '360d', '360days', '360_day' any of them for years of 360 days 2472 output: 2473 array(dimt, 0) = year 2474 array(dimt, 1) = month 2475 array(dimt, 2) = day 2476 array(dimt, 3) = hour 2477 array(dimt, 4) = minute 2478 array(dimt, 5) = second 2479 """ 2480 import datetime as dt 2481 fname = 'CFtimesvar_datetime' 2482 2483 availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds'] 2484 2485 txtunits = units.split(' ') 2486 tunits = txtunits[0] 2487 Srefdate = txtunits[len(txtunits) - 1] 2488 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 2489 ## 2490 timeval = Srefdate.find(':') 2491 if not timeval == -1: 2492 # print ' refdate with time!' 2493 refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate) 2494 else: 2495 refdate = dateStr_date(Srefdate) 2496 2497 dimt = len(times) 2498 realdates = np.zeros((dimt, 6), dtype=int) 2499 2500 secsDay=3600*24. 2501 2502 # Checking calendar! 2503 ## 2504 y360 = False 2505 daycal360 = ['earth_360d', '360d', '360days', '360_day'] 2506 if searchInlist(daycal360,calendar): 2507 print warnmsg 2508 print ' ' + fname + ': calendar of 12 months of 30 days !!' 2509 y360 = True 2510 2511 ## Not in timedelta 2512 # if tunits == 'years': 2513 # for it in range(dimt): 2514 # realdate = refdate + dt.timedelta(years=float(times[it])) 2515 # realdates[it] = int(realdate.year) 2516 # elif tunits == 'months': 2517 # for it in range(dimt): 2518 # realdate = refdate + dt.timedelta(months=float(times[it])) 2519 # realdates[it] = int(realdate.year) 2520 if y360: 2521 if tunits == 'weeks': 2522 for it in range(dimt): 2523 deltat = dt.timedelta(weeks=float(times[it])) 2524 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2525 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2526 elif tunits == 'days': 2527 for it in range(dimt): 2528 deltat = dt.timedelta(days=float(times[it])) 2529 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2530 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2531 elif tunits == 'hours': 2532 for it in range(dimt): 2533 realdate = dt.timedelta(hours=float(times[it])) 2534 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2535 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2536 elif tunits == 'minutes': 2537 for it in range(dimt): 2538 realdate = dt.timedelta(minutes=float(times[it])) 2539 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2540 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2541 elif tunits == 'seconds': 2542 for it in range(dimt): 2543 realdate = dt.timedelta(seconds=float(times[it])) 2544 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2545 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2546 elif tunits == 'miliseconds': 2547 for it in range(dimt): 2548 realdate = dt.timedelta(miliseconds=float(times[it])) 2549 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000. 2550 realdates[it,:] = date_juliandate(refdate.year,Tsecs) 2551 else: 2552 print errormsg 2553 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!' 2554 print ' available ones:', availtunits 2555 quit(-1) 2556 else: 2557 if tunits == 'weeks': 2558 for it in range(dimt): 2559 realdate = refdate + dt.timedelta(weeks=float(times[it])) 2560 realdates[it,0] = int(realdate.year) 2561 realdates[it,1] = int(realdate.month) 2562 realdates[it,2] = int(realdate.day) 2563 realdates[it,3] = int(realdate.hour) 2564 realdates[it,4] = int(realdate.second) 2565 realdates[it,5] = int(realdate.minute) 2566 elif tunits == 'days': 2567 for it in range(dimt): 2568 realdate = refdate + dt.timedelta(days=float(times[it])) 2569 realdates[it,0] = int(realdate.year) 2570 realdates[it,1] = int(realdate.month) 2571 realdates[it,2] = int(realdate.day) 2572 realdates[it,3] = int(realdate.hour) 2573 realdates[it,4] = int(realdate.second) 2574 realdates[it,5] = int(realdate.minute) 2575 elif tunits == 'hours': 2576 for it in range(dimt): 2577 realdate = refdate + dt.timedelta(hours=float(times[it])) 2578 realdates[it,0] = int(realdate.year) 2579 realdates[it,1] = int(realdate.month) 2580 realdates[it,2] = int(realdate.day) 2581 realdates[it,3] = int(realdate.hour) 2582 realdates[it,4] = int(realdate.second) 2583 realdates[it,5] = int(realdate.minute) 2584 elif tunits == 'minutes': 2585 for it in range(dimt): 2586 realdate = refdate + dt.timedelta(minutes=float(times[it])) 2587 realdates[it,0] = int(realdate.year) 2588 realdates[it,1] = int(realdate.month) 2589 realdates[it,2] = int(realdate.day) 2590 realdates[it,3] = int(realdate.hour) 2591 realdates[it,4] = int(realdate.second) 2592 realdates[it,5] = int(realdate.minute) 2593 elif tunits == 'seconds': 2594 for it in range(dimt): 2595 realdate = refdate + dt.timedelta(seconds=float(times[it])) 2596 realdates[it,0] = int(realdate.year) 2597 realdates[it,1] = int(realdate.month) 2598 realdates[it,2] = int(realdate.day) 2599 realdates[it,3] = int(realdate.hour) 2600 realdates[it,4] = int(realdate.second) 2601 realdates[it,5] = int(realdate.minute) 2602 elif tunits == 'milliseconds': 2603 for it in range(dimt): 2604 realdate = refdate + dt.timedelta(milliseconds=float(times[it])) 2605 realdates[it,0] = int(realdate.year) 2606 realdates[it,1] = int(realdate.month) 2607 realdates[it,2] = int(realdate.day) 2608 realdates[it,3] = int(realdate.hour) 2609 realdates[it,4] = int(realdate.second) 2610 realdates[it,5] = int(realdate.minute) 2611 else: 2612 print errormsg 2613 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!' 2614 print ' available ones:', availtunits 2615 2616 quit(-1) 2617 2618 return realdates 1947 2619 1948 2620 def variables_values(varName): … … 3104 3776 return end 3105 3777 3106 def dateStr_date(StringDate):3107 """ Function to transform a string date ([YYYY]-[MM]-[DD] format) to a date object3108 >>> dateStr_date('1976-02-17')3109 1976-02-173110 """3111 import datetime as dt3112 3113 dateD = StringDate.split('-')3114 if int(dateD[0]) == 0:3115 print warnmsg3116 print ' dateStr_date: 0 reference year!! changing to 1'3117 dateD[0] = 13118 newdate = dt.date(int(dateD[0]), int(dateD[1]), int(dateD[2]))3119 return newdate3120 3121 def timeStr_time(StringDate):3122 """ Function to transform a string date ([HH]:[MI]:[SS] format) to a time object3123 >>> timeStr_datetime('04:32:54')3124 04:32:543125 """3126 import datetime as dt3127 3128 timeT = StringDate.split(':')3129 if len(timeT) == 3:3130 newtime = dt.time(int(timeT[0]), int(timeT[1]), int(timeT[2]))3131 else:3132 newtime = dt.time(int(timeT[0]), int(timeT[1]), 0)3133 3134 return newtime3135 3136 def DateTimeStr_date(StringDate):3137 """ Function to transform a string date-time ([YYYY][MM][DD][HH][MI][SS] format) to a date object3138 >>> DateTimeStr_date('19760217083205')3139 1976-02-17 08:32:053140 """3141 import datetime as dt3142 3143 yr = int(StringDate[0:4])3144 mo = int(StringDate[4:6])3145 da = int(StringDate[6:8])3146 ho = int(StringDate[8:10])3147 mi = int(StringDate[10:12])3148 se = int(StringDate[12:14])3149 3150 if yr == 0:3151 print warnmsg3152 print ' ' + fname + ': 0 reference year!! changing to 1'3153 yr = 13154 newdate = dt.datetime(yr, mo, da, ho, mi, se)3155 3156 return newdate3157 3158 def timeref_datetime(refd, timeval, tu, cal='gregorian'):3159 """ Function to transform from a [timeval] in [tu] units from the time referece [tref] to datetime object3160 refd: time of reference (as datetime object)3161 timeval: time value (as [tu] from [tref])3162 tu: time units3163 cal: calendar3164 >>> timeref_datetime(dt.datetime(1949,12,1,0,0,0), 229784.36, 'hours')3165 1976-02-17 08:21:363166 >>> timeref_datetime(dt.datetime(1949,12,1,0,0,0), 230784.36, 'hours')3167 1976-03-30 00:21:363168 >>> timeref_datetime(dt.datetime(1949,12,1,0,0,0), 230784.36, 'hours', 'noleap')3169 1976-04-06 00:21:363170 """3171 import datetime as dt3172 import numpy as np3173 3174 availtu = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds']3175 3176 daterefS = str(refd.year).zfill(4) + '-' + str(refd.month).zfill(2) + '-' + \3177 str(refd.day).zfill(2) + ' ' + str(refd.hour).zfill(2) + ':'+ \3178 str(refd.minute).zfill(2) + ':' + str(refd.second).zfill(2)3179 3180 # For non-gregorian calendars, we impose gregorian (since, we are using python)3181 if cal == 'noleap' or cal == '365d':3182 timev = impose_gregorian(timeval, tu + ' since ' + daterefS, cal)3183 else:3184 timev = timeval + 0.3185 3186 ## Not in timedelta3187 # if tu == 'years':3188 # realdate = refdate + dt.timedelta(years=float(timeval))3189 # elif tu == 'months':3190 # realdate = refdate + dt.timedelta(months=float(timeval))3191 if tu == 'weeks':3192 realdate = refd + dt.timedelta(weeks=float(timev))3193 elif tu == 'days':3194 realdate = refd + dt.timedelta(days=float(timev))3195 elif tu == 'hours':3196 realdate = refd + dt.timedelta(hours=float(timev))3197 elif tu == 'minutes':3198 realdate = refd + dt.timedelta(minutes=float(timev))3199 elif tu == 'seconds':3200 realdate = refd + dt.timedelta(seconds=float(timev))3201 elif tu == 'milliseconds':3202 realdate = refd + dt.timedelta(milliseconds=float(timev))3203 else:3204 print errormsg3205 print ' ' + fname + ": time units '" + tu + "' not ready!!!!"3206 print ' available ones:', availtu3207 quit(-1)3208 3209 return realdate3210 3211 def timeref_datetime_mat(refd, timeval, tu):3212 """ Function to transform from a [timeval] in [tu] units from the time referece [tref] to matrix with: year, day, month, hour, minute, second3213 refd: time of reference (as datetime object)3214 timeval: time value (as [tu] from [tref])3215 tu: time units3216 >>> timeref = date(1949,12,1,0,0,0)3217 >>> timeref_datetime(timeref, 229784.36, hours)3218 [1976 2 17 8 36 21]3219 """3220 import datetime as dt3221 import numpy as np3222 3223 3224 realdates = np.zeros(6, dtype=int)3225 ## Not in timedelta3226 # if tu == 'years':3227 # realdate = refdate + dt.timedelta(years=float(timeval))3228 # elif tu == 'months':3229 # realdate = refdate + dt.timedelta(months=float(timeval))3230 if tu == 'weeks':3231 realdate = refd + dt.timedelta(weeks=float(timeval))3232 elif tu == 'days':3233 realdate = refd + dt.timedelta(days=float(timeval))3234 elif tu == 'hours':3235 realdate = refd + dt.timedelta(hours=float(timeval))3236 elif tu == 'minutes':3237 realdate = refd + dt.timedelta(minutes=float(timeval))3238 elif tunits == 'seconds':3239 realdate = refd + dt.timedelta(seconds=float(timeval))3240 elif tunits == 'milliseconds':3241 realdate = refd + dt.timedelta(milliseconds=float(timeval))3242 else:3243 print errormsg3244 print ' timeref_datetime: time units "' + tu + '" not ready!!!!'3245 quit(-1)3246 3247 realdates[0] = int(realdate.year)3248 realdates[1] = int(realdate.month)3249 realdates[2] = int(realdate.day)3250 realdates[3] = int(realdate.hour)3251 realdates[4] = int(realdate.second)3252 realdates[5] = int(realdate.minute)3253 3254 return realdates3255 3256 def realdatetime_CFcompilant(times, Srefdate, tunits):3257 """ Function to transform a matrix with real time values ([year, month, day, hour, minute, second]) to a netCDF one3258 times= matrix with times3259 Srefdate= reference date ([YYYY][MM][DD][HH][MI][SS] format)3260 tunits= units of time respect to Srefdate3261 >>> realdatetime_CFcompilant(np.array([ [1976, 2, 17, 8, 20, 0], [1976, 2, 18, 8, 20, 0]], dtype=int), '19491201000000', 'hours')3262 [ 229784.33333333 229808.33333333]3263 """3264 3265 import datetime as dt3266 yrref=int(Srefdate[0:4])3267 monref=int(Srefdate[4:6])3268 dayref=int(Srefdate[6:8])3269 horref=int(Srefdate[8:10])3270 minref=int(Srefdate[10:12])3271 secref=int(Srefdate[12:14])3272 3273 refdate=dt.datetime(yrref, monref, dayref, horref, minref, secref)3274 3275 dimt=times.shape[0]3276 3277 cfdates = np.zeros((dimt), dtype=np.float64)3278 if tunits == 'weeks':3279 for it in range(dimt):3280 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3281 cfdates[it] = (cfdate.days + cfdate.seconds/(3600.*24.))/7.3282 elif tunits == 'days':3283 for it in range(dimt):3284 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3285 cfdates[it] = cfdate.days + cfdate.seconds/(3600.*24.)3286 elif tunits == 'hours':3287 for it in range(dimt):3288 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3289 cfdates[it] = cfdate.days*24. + cfdate.seconds/3600.3290 elif tunits == 'minutes':3291 for it in range(dimt):3292 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3293 cfdates[it] = cfdate.days*24.*60. + cfdate.seconds/60.3294 elif tunits == 'seconds':3295 for it in range(dimt):3296 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3297 cfdates[it] = cfdate.days*24.*3600. + cfdate.seconds3298 elif tunits == 'milliseconds':3299 for it in range(dimt):3300 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3301 cfdates[it] = cfdate.days*1000.*24.*3600. + cfdate.seconds*1000.3302 elif tunits == 'microseconds':3303 for it in range(dimt):3304 cfdate = dt.datetime(times[it,0], times[it,1], times[it,2], times[it,3], times[it,4], times[it,5]) - refdate3305 cfdates[it] = cfdate.days*1000000.*24.*3600. + cfdate.seconds*1000000.3306 else:3307 print errormsg3308 print ' ' + fname + ': time units "' + tunits + '" is not ready!!!'3309 quit(-1)3310 3311 return cfdates3312 3313 def netCDFdatetime_realdatetime(units, tcalendar, times):3314 """ Function to transfrom from netCDF CF-compilant times to real time3315 """3316 import datetime as dt3317 3318 txtunits = units.split(' ')3319 tunits = txtunits[0]3320 Srefdate = txtunits[len(txtunits) - 1]3321 3322 # Calendar type3323 ##3324 is360 = False3325 if tcalendar is not None:3326 print ' netCDFdatetime_realdatetime: There is a calendar attribute'3327 if tcalendar == '365_day' or tcalendar == 'noleap':3328 print ' netCDFdatetime_realdatetime: No leap years!'3329 isleapcal = False3330 elif tcalendar == 'proleptic_gregorian' or tcalendar == 'standard' or tcalendar == 'gregorian':3331 isleapcal = True3332 elif tcalendar == '360_day':3333 is360 = True3334 isleapcal = False3335 else:3336 print errormsg3337 print ' netCDFdatetime_realdatetime: Calendar "' + tcalendar + '" not prepared!'3338 quit(-1)3339 3340 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]3341 ##3342 timeval = Srefdate.find(':')3343 3344 if not timeval == -1:3345 print ' netCDFdatetime_realdatetime: refdate with time!'3346 refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate)3347 else:3348 refdate = dateStr_date(Srefdate)3349 3350 dimt = len(times)3351 # datetype = type(dt.datetime(1972,02,01))3352 # realdates = np.array(dimt, datetype)3353 # print realdates3354 3355 ## Not in timedelta3356 # if tunits == 'years':3357 # for it in range(dimt):3358 # realdate = refdate + dt.timedelta(years=float(times[it]))3359 # realdates[it] = int(realdate.year)3360 # elif tunits == 'months':3361 # for it in range(dimt):3362 # realdate = refdate + dt.timedelta(months=float(times[it]))3363 # realdates[it] = int(realdate.year)3364 # realdates = []3365 realdates = np.zeros((dimt, 6), dtype=int)3366 if tunits == 'weeks':3367 for it in range(dimt):3368 realdate = refdate + dt.timedelta(weeks=float(times[it]))3369 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3370 elif tunits == 'days':3371 for it in range(dimt):3372 realdate = refdate + dt.timedelta(days=float(times[it]))3373 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3374 elif tunits == 'hours':3375 for it in range(dimt):3376 realdate = refdate + dt.timedelta(hours=float(times[it]))3377 # if not isleapcal:3378 # Nleapdays = cal.leapdays(int(refdate.year), int(realdate.year))3379 # realdate = realdate - dt.timedelta(days=Nleapdays)3380 # if is360:3381 # Nyears360 = int(realdate.year) - int(refdate.year) + 13382 # realdate = realdate -dt.timedelta(days=Nyears360*5)3383 # realdates[it] = realdate3384 # realdates = refdate + dt.timedelta(hours=float(times))3385 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3386 elif tunits == 'minutes':3387 for it in range(dimt):3388 realdate = refdate + dt.timedelta(minutes=float(times[it]))3389 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3390 elif tunits == 'seconds':3391 for it in range(dimt):3392 realdate = refdate + dt.timedelta(seconds=float(times[it]))3393 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3394 elif tunits == 'milliseconds':3395 for it in range(dimt):3396 realdate = refdate + dt.timedelta(milliseconds=float(times[it]))3397 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3398 elif tunits == 'microseconds':3399 for it in range(dimt):3400 realdate = refdate + dt.timedelta(microseconds=float(times[it]))3401 realdates[it,:]=[realdate.year, realdate.month, realdate.day, realdate.hour, realdate.minute, realdate.second]3402 else:3403 print errormsg3404 print ' netCDFdatetime_realdatetime: time units "' + tunits + '" is not ready!!!'3405 quit(-1)3406 3407 return realdates3408 3409 def date_juliandate(refyr, TOTsecs):3410 """ Function to transform from a total quantity of seconds since the3411 beginning of a year to 360 days / year calendar3412 TOTsecs= total number of seconds3413 """3414 fname = 'date_juliandate'3415 3416 secsYear = 3600*24*30*12.3417 secsMonth = 3600*24*30.3418 secsDay = 3600*24.3419 secsHour = 3600.3420 secsMinute = 60.3421 3422 rldate = np.zeros((6), dtype=int)3423 rldate[0] = refyr3424 if TOTsecs > secsYear:3425 rldate[0] = refyr + int(TOTsecs/secsYear) + 13426 TOTsecs = TOTsecs - int(TOTsecs/secsYear)*secsYear3427 3428 if np.mod(TOTsecs,secsMonth) == 0:3429 rldate[1] = int(TOTsecs/secsMonth)3430 else:3431 rldate[1] = int(TOTsecs/secsMonth) + 13432 TOTsecs = TOTsecs - int(TOTsecs/secsMonth)*secsMonth3433 3434 if np.mod(TOTsecs,secsDay) == 0:3435 rldate[2] = int(TOTsecs/secsDay)3436 else:3437 rldate[2] = int(TOTsecs/secsDay) + 13438 TOTsecs = TOTsecs - int(TOTsecs/secsDay)*secsDay3439 3440 if np.mod(TOTsecs,secsHour) == 0:3441 rldate[3] = int(TOTsecs/secsHour)3442 else:3443 rldate[3] = int(TOTsecs/secsHour) + 13444 TOTsecs = TOTsecs - int(TOTsecs/secsHour)*secsHour3445 3446 if np.mod(TOTsecs,secsMinute) == 0:3447 rldate[4] = int(TOTsecs/secsMinute)3448 else:3449 rldate[4] = int(TOTsecs/secsMinute) + 13450 TOTsecs = TOTsecs - int(TOTsecs/secsMinute)*secsMinute3451 3452 rldate[5] = TOTsecs3453 3454 # print refyr,TOTsecs,':',rldate3455 # quit()3456 3457 return rldate3458 3459 def CFtimes_datetime(ncfile, tname):3460 """ Provide date/time array from a file with a series of netCDF CF-compilant time variable3461 ncfile = netCDF file name3462 tname = name of the variable time in [ncfile]3463 output:3464 array(dimt, 0) = year3465 array(dimt, 1) = month3466 array(dimt, 2) = day3467 array(dimt, 3) = hour3468 array(dimt, 4) = minute3469 array(dimt, 5) = second3470 """3471 import datetime as dt3472 fname = 'CFtimes_datetime'3473 3474 availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds']3475 3476 times = ncfile.variables[tname]3477 timevals = times[:]3478 3479 attvar = times.ncattrs()3480 if not searchInlist(attvar, 'units'):3481 print errormsg3482 print ' ' + fname + ": '" + tname + "' does not have attribute: 'units'"3483 quit(-1)3484 else:3485 units = times.getncattr('units')3486 3487 txtunits = units.split(' ')3488 tunits = txtunits[0]3489 Srefdate = txtunits[len(txtunits) - 1]3490 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]3491 ##3492 timeval = Srefdate.find(':')3493 3494 if not timeval == -1:3495 # print ' refdate with time!'3496 refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate)3497 else:3498 refdate = dateStr_date(Srefdate)3499 3500 dimt = len(timevals)3501 realdates = np.zeros((dimt, 6), dtype=int)3502 3503 secsDay=3600*24.3504 3505 # Checking calendar!3506 ##3507 y360 = False3508 daycal360 = ['earth_360d', '360d', '360days', '360_day']3509 if searchInlist(attvar, 'calendar'):3510 calendar = times.getncattr('calendar')3511 if searchInlist(daycal360,calendar):3512 print warnmsg3513 print ' ' + fname + ': calendar of 12 months of 30 days !!'3514 y360 = True3515 3516 ## Not in timedelta3517 # if tunits == 'years':3518 # for it in range(dimt):3519 # realdate = refdate + dt.timedelta(years=float(times[it]))3520 # realdates[it] = int(realdate.year)3521 # elif tunits == 'months':3522 # for it in range(dimt):3523 # realdate = refdate + dt.timedelta(months=float(times[it]))3524 # realdates[it] = int(realdate.year)3525 if y360:3526 if tunits == 'weeks':3527 for it in range(dimt):3528 deltat = dt.timedelta(weeks=float(times[it]))3529 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3530 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3531 elif tunits == 'days':3532 for it in range(dimt):3533 deltat = dt.timedelta(days=float(times[it]))3534 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3535 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3536 elif tunits == 'hours':3537 for it in range(dimt):3538 realdate = dt.timedelta(hours=float(times[it]))3539 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3540 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3541 elif tunits == 'minutes':3542 for it in range(dimt):3543 realdate = dt.timedelta(minutes=float(times[it]))3544 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3545 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3546 elif tunits == 'seconds':3547 for it in range(dimt):3548 realdate = dt.timedelta(seconds=float(times[it]))3549 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3550 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3551 elif tunits == 'miliseconds':3552 for it in range(dimt):3553 realdate = dt.timedelta(miliseconds=float(times[it]))3554 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3555 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3556 else:3557 print errormsg3558 print fname + ' : time units "' + tunits + '" not ready!!!!'3559 print ' avaialable ones:', availtunits3560 quit(-1)3561 else:3562 if tunits == 'weeks':3563 for it in range(dimt):3564 realdate = refdate + dt.timedelta(weeks=float(times[it]))3565 realdates[it,0] = int(realdate.year)3566 realdates[it,1] = int(realdate.month)3567 realdates[it,2] = int(realdate.day)3568 realdates[it,3] = int(realdate.hour)3569 realdates[it,4] = int(realdate.second)3570 realdates[it,5] = int(realdate.minute)3571 elif tunits == 'days':3572 for it in range(dimt):3573 realdate = refdate + dt.timedelta(days=float(times[it]))3574 realdates[it,0] = int(realdate.year)3575 realdates[it,1] = int(realdate.month)3576 realdates[it,2] = int(realdate.day)3577 realdates[it,3] = int(realdate.hour)3578 realdates[it,4] = int(realdate.second)3579 realdates[it,5] = int(realdate.minute)3580 elif tunits == 'hours':3581 for it in range(dimt):3582 realdate = refdate + dt.timedelta(hours=float(times[it]))3583 realdates[it,0] = int(realdate.year)3584 realdates[it,1] = int(realdate.month)3585 realdates[it,2] = int(realdate.day)3586 realdates[it,3] = int(realdate.hour)3587 realdates[it,4] = int(realdate.second)3588 realdates[it,5] = int(realdate.minute)3589 elif tunits == 'minutes':3590 for it in range(dimt):3591 realdate = refdate + dt.timedelta(minutes=float(times[it]))3592 realdates[it,0] = int(realdate.year)3593 realdates[it,1] = int(realdate.month)3594 realdates[it,2] = int(realdate.day)3595 realdates[it,3] = int(realdate.hour)3596 realdates[it,4] = int(realdate.second)3597 realdates[it,5] = int(realdate.minute)3598 elif tunits == 'seconds':3599 for it in range(dimt):3600 realdate = refdate + dt.timedelta(seconds=float(times[it]))3601 realdates[it,0] = int(realdate.year)3602 realdates[it,1] = int(realdate.month)3603 realdates[it,2] = int(realdate.day)3604 realdates[it,3] = int(realdate.hour)3605 realdates[it,4] = int(realdate.second)3606 realdates[it,5] = int(realdate.minute)3607 elif tunits == 'milliseconds':3608 for it in range(dimt):3609 realdate = refdate + dt.timedelta(milliseconds=float(times[it]))3610 realdates[it,0] = int(realdate.year)3611 realdates[it,1] = int(realdate.month)3612 realdates[it,2] = int(realdate.day)3613 realdates[it,3] = int(realdate.hour)3614 realdates[it,4] = int(realdate.second)3615 realdates[it,5] = int(realdate.minute)3616 else:3617 print errormsg3618 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!'3619 quit(-1)3620 3621 return realdates3622 3623 def CFtimesvar_datetime(times, units, calendar):3624 """ Provide date/time array from a CF-compilant time variable3625 timevals = CF values of the variable time3626 units = CF-compilant units ([Tunit] since [date]) of timevals3627 calendar= calendar of the time values3628 'earth_360d', '360d', '360days', '360_day' any of them for years of 360 days3629 output:3630 array(dimt, 0) = year3631 array(dimt, 1) = month3632 array(dimt, 2) = day3633 array(dimt, 3) = hour3634 array(dimt, 4) = minute3635 array(dimt, 5) = second3636 """3637 import datetime as dt3638 fname = 'CFtimesvar_datetime'3639 3640 availtunits = ['weeks', 'days', 'hours', 'minutes', 'seconds', 'miliseconds']3641 3642 txtunits = units.split(' ')3643 tunits = txtunits[0]3644 Srefdate = txtunits[len(txtunits) - 1]3645 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]3646 ##3647 timeval = Srefdate.find(':')3648 if not timeval == -1:3649 # print ' refdate with time!'3650 refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate)3651 else:3652 refdate = dateStr_date(Srefdate)3653 3654 dimt = len(times)3655 realdates = np.zeros((dimt, 6), dtype=int)3656 3657 secsDay=3600*24.3658 3659 # Checking calendar!3660 ##3661 y360 = False3662 daycal360 = ['earth_360d', '360d', '360days', '360_day']3663 if searchInlist(daycal360,calendar):3664 print warnmsg3665 print ' ' + fname + ': calendar of 12 months of 30 days !!'3666 y360 = True3667 3668 ## Not in timedelta3669 # if tunits == 'years':3670 # for it in range(dimt):3671 # realdate = refdate + dt.timedelta(years=float(times[it]))3672 # realdates[it] = int(realdate.year)3673 # elif tunits == 'months':3674 # for it in range(dimt):3675 # realdate = refdate + dt.timedelta(months=float(times[it]))3676 # realdates[it] = int(realdate.year)3677 if y360:3678 if tunits == 'weeks':3679 for it in range(dimt):3680 deltat = dt.timedelta(weeks=float(times[it]))3681 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3682 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3683 elif tunits == 'days':3684 for it in range(dimt):3685 deltat = dt.timedelta(days=float(times[it]))3686 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3687 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3688 elif tunits == 'hours':3689 for it in range(dimt):3690 realdate = dt.timedelta(hours=float(times[it]))3691 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3692 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3693 elif tunits == 'minutes':3694 for it in range(dimt):3695 realdate = dt.timedelta(minutes=float(times[it]))3696 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3697 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3698 elif tunits == 'seconds':3699 for it in range(dimt):3700 realdate = dt.timedelta(seconds=float(times[it]))3701 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3702 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3703 elif tunits == 'miliseconds':3704 for it in range(dimt):3705 realdate = dt.timedelta(miliseconds=float(times[it]))3706 Tsecs = deltat.days*secsDay + deltat.seconds + deltat.microseconds/1000.3707 realdates[it,:] = date_juliandate(refdate.year,Tsecs)3708 else:3709 print errormsg3710 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!'3711 print ' available ones:', availtunits3712 quit(-1)3713 else:3714 if tunits == 'weeks':3715 for it in range(dimt):3716 realdate = refdate + dt.timedelta(weeks=float(times[it]))3717 realdates[it,0] = int(realdate.year)3718 realdates[it,1] = int(realdate.month)3719 realdates[it,2] = int(realdate.day)3720 realdates[it,3] = int(realdate.hour)3721 realdates[it,4] = int(realdate.second)3722 realdates[it,5] = int(realdate.minute)3723 elif tunits == 'days':3724 for it in range(dimt):3725 realdate = refdate + dt.timedelta(days=float(times[it]))3726 realdates[it,0] = int(realdate.year)3727 realdates[it,1] = int(realdate.month)3728 realdates[it,2] = int(realdate.day)3729 realdates[it,3] = int(realdate.hour)3730 realdates[it,4] = int(realdate.second)3731 realdates[it,5] = int(realdate.minute)3732 elif tunits == 'hours':3733 for it in range(dimt):3734 realdate = refdate + dt.timedelta(hours=float(times[it]))3735 realdates[it,0] = int(realdate.year)3736 realdates[it,1] = int(realdate.month)3737 realdates[it,2] = int(realdate.day)3738 realdates[it,3] = int(realdate.hour)3739 realdates[it,4] = int(realdate.second)3740 realdates[it,5] = int(realdate.minute)3741 elif tunits == 'minutes':3742 for it in range(dimt):3743 realdate = refdate + dt.timedelta(minutes=float(times[it]))3744 realdates[it,0] = int(realdate.year)3745 realdates[it,1] = int(realdate.month)3746 realdates[it,2] = int(realdate.day)3747 realdates[it,3] = int(realdate.hour)3748 realdates[it,4] = int(realdate.second)3749 realdates[it,5] = int(realdate.minute)3750 elif tunits == 'seconds':3751 for it in range(dimt):3752 realdate = refdate + dt.timedelta(seconds=float(times[it]))3753 realdates[it,0] = int(realdate.year)3754 realdates[it,1] = int(realdate.month)3755 realdates[it,2] = int(realdate.day)3756 realdates[it,3] = int(realdate.hour)3757 realdates[it,4] = int(realdate.second)3758 realdates[it,5] = int(realdate.minute)3759 elif tunits == 'milliseconds':3760 for it in range(dimt):3761 realdate = refdate + dt.timedelta(milliseconds=float(times[it]))3762 realdates[it,0] = int(realdate.year)3763 realdates[it,1] = int(realdate.month)3764 realdates[it,2] = int(realdate.day)3765 realdates[it,3] = int(realdate.hour)3766 realdates[it,4] = int(realdate.second)3767 realdates[it,5] = int(realdate.minute)3768 else:3769 print errormsg3770 print ' ' + fname + ': time units "' + tunits + '" not ready!!!!'3771 print ' available ones:', availtunits3772 3773 quit(-1)3774 3775 return realdates3776 3777 3778 class statsValWeigthed(object): 3778 3779 """Weigthed Statistics class providing: -
trunk/tools/nc_var_tools.py
r2444 r2445 2582 2582 self.dt = timev[1]-timev[0] 2583 2583 self.firstTu = timev[0] 2584 print 'Lluis:', self.refdate, self.firstTu, self.units, cal 2584 2585 self.firstTt = gen.timeref_datetime(self.refdate, self.firstTu, \ 2585 2586 self.units, cal)
Note: See TracChangeset
for help on using the changeset viewer.