Changeset 1101 in lmdz_wrf for trunk/tools/generic_tools.py
- Timestamp:
- Sep 12, 2016, 3:51:09 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1100 r1101 33 33 # chainSnum_levnext: Function to provide the next value for a given level from a chainStrnum hirerarchy of numbers 34 34 # chainSnum_num: Function to pass a `ChainStrNum' string to a number 35 # changedate360: Class to change a date on a 360 days/yr (or 12 30-days months) calendar 35 36 # coincident_CFtimes: Function to make coincident times for two different sets of CFtimes 36 37 # coldec_hex: Function to pas a decimal ([r,g,b]; [0.,1.]) color to hexadecimal (#[RR][GG][BB], 00-64, 0A-FF) … … 42 43 # dictKeysVals_stringList: Function to provide strings from a dictionary with keys which contain values of lists 43 44 # dictvar_listS: Function to provide a Dc string separated list of DVs separated [key] [value] couples following a list of keys 45 # diff_dates360: Function to provide the number of seconds of difference between two dates (dateA - dateB) on a 46 # 360 days/yr (or 12 30-days months) calendar 47 # dtsec360dyr: Class to operate a number of seconds to a date in a 360 days/yr (or 12 30-days months) calendar 44 48 # files_folder_HMT: Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail] 45 49 # get_configuration: Function to get the configuration from an ASCII external file … … 58 62 # num_split: Function to split a string at each numeric value keeping the number as the last character of each cut 59 63 # oper_submatrix: Function to perform an operation of a given matrix along a sub-set of values along its dimensions 64 # period_information_360d: Function to provide the information of a given period idate, edate (dates in 65 # [YYYY][MM][DD][HH][MI][SS] format) in a 360 years calendar 60 66 # printing_dictionary: Function to print the content of a dictionary 61 67 # PolyArea: Function to compute the area of the polygon following 'Shoelace formula' … … 281 287 282 288 return newlist 289 290 def datetimeStr_conversion(StringDT,typeSi,typeSo): 291 """ Function to transform a string date to an another date object 292 StringDT= string with the date and time 293 typeSi= type of datetime string input 294 typeSo= type of datetime string output 295 [typeSi/o] 296 'cfTime': [time],[units]; ]time in CF-convention format [units] = [tunits] since [refdate] 297 'matYmdHMS': numerical vector with [[YYYY], [MM], [DD], [HH], [MI], [SS]] 298 'YmdHMS': [YYYY][MM][DD][HH][MI][SS] format 299 'Y-m-d_H:M:S': [YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format 300 'Y-m-d H:M:S': [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] format 301 'Y/m/d H-M-S': [YYYY]/[MM]/[DD] [HH]-[MI]-[SS] format 302 'WRFdatetime': [Y], [Y], [Y], [Y], '-', [M], [M], '-', [D], [D], '_', [H], 303 [H], ':', [M], [M], ':', [S], [S] 304 >>> datetimeStr_conversion('1976-02-17_08:32:05','Y-m-d_H:M:S','matYmdHMS') 305 [1976 2 17 8 32 5] 306 >>> datetimeStr_conversion(str(137880)+',minutes since 1979-12-01_00:00:00','cfTime','Y/m/d H-M-S') 307 1980/03/05 18-00-00 308 """ 309 import datetime as dt 310 311 fname = 'datetimeStr_conversion' 312 313 if StringDT[0:1] == 'h': 314 print fname + '_____________________________________________________________' 315 print datetimeStr_conversion.__doc__ 316 quit() 317 318 if typeSi == 'cfTime': 319 timeval = np.float(StringDT.split(',')[0]) 320 tunits = StringDT.split(',')[1].split(' ')[0] 321 Srefdate = StringDT.split(',')[1].split(' ')[2] 322 323 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 324 ## 325 yrref=Srefdate[0:4] 326 monref=Srefdate[5:7] 327 dayref=Srefdate[8:10] 328 329 trefT = Srefdate.find(':') 330 if not trefT == -1: 331 # print ' ' + fname + ': refdate with time!' 332 horref=Srefdate[11:13] 333 minref=Srefdate[14:16] 334 secref=Srefdate[17:19] 335 refdate = datetimeStr_datetime( yrref + '-' + monref + '-' + dayref + \ 336 '_' + horref + ':' + minref + ':' + secref) 337 else: 338 refdate = datetimeStr_datetime( yrref + '-' + monref + '-' + dayref + \ 339 + '_00:00:00') 340 341 if tunits == 'weeks': 342 newdate = refdate + dt.timedelta(weeks=float(timeval)) 343 elif tunits == 'days': 344 newdate = refdate + dt.timedelta(days=float(timeval)) 345 elif tunits == 'hours': 346 newdate = refdate + dt.timedelta(hours=float(timeval)) 347 elif tunits == 'minutes': 348 newdate = refdate + dt.timedelta(minutes=float(timeval)) 349 elif tunits == 'seconds': 350 newdate = refdate + dt.timedelta(seconds=float(timeval)) 351 elif tunits == 'milliseconds': 352 newdate = refdate + dt.timedelta(milliseconds=float(timeval)) 353 else: 354 print errormsg 355 print ' timeref_datetime: time units "' + tunits + '" not ready!!!!' 356 quit(-1) 357 358 yr = newdate.year 359 mo = newdate.month 360 da = newdate.day 361 ho = newdate.hour 362 mi = newdate.minute 363 se = newdate.second 364 elif typeSi == 'matYmdHMS': 365 yr = StringDT[0] 366 mo = StringDT[1] 367 da = StringDT[2] 368 ho = StringDT[3] 369 mi = StringDT[4] 370 se = StringDT[5] 371 elif typeSi == 'YmdHMS': 372 yr = int(StringDT[0:4]) 373 mo = int(StringDT[4:6]) 374 da = int(StringDT[6:8]) 375 ho = int(StringDT[8:10]) 376 mi = int(StringDT[10:12]) 377 se = int(StringDT[12:14]) 378 elif typeSi == 'Y-m-d_H:M:S': 379 dateDT = StringDT.split('_') 380 dateD = dateDT[0].split('-') 381 timeT = dateDT[1].split(':') 382 yr = int(dateD[0]) 383 mo = int(dateD[1]) 384 da = int(dateD[2]) 385 ho = int(timeT[0]) 386 mi = int(timeT[1]) 387 se = int(timeT[2]) 388 elif typeSi == 'Y-m-d H:M:S': 389 dateDT = StringDT.split(' ') 390 dateD = dateDT[0].split('-') 391 timeT = dateDT[1].split(':') 392 yr = int(dateD[0]) 393 mo = int(dateD[1]) 394 da = int(dateD[2]) 395 ho = int(timeT[0]) 396 mi = int(timeT[1]) 397 se = int(timeT[2]) 398 elif typeSi == 'Y/m/d H-M-S': 399 dateDT = StringDT.split(' ') 400 dateD = dateDT[0].split('/') 401 timeT = dateDT[1].split('-') 402 yr = int(dateD[0]) 403 mo = int(dateD[1]) 404 da = int(dateD[2]) 405 ho = int(timeT[0]) 406 mi = int(timeT[1]) 407 se = int(timeT[2]) 408 elif typeSi == 'WRFdatetime': 409 yr = int(StringDT[0])*1000 + int(StringDT[1])*100 + int(StringDT[2])*10 + \ 410 int(StringDT[3]) 411 mo = int(StringDT[5])*10 + int(StringDT[6]) 412 da = int(StringDT[8])*10 + int(StringDT[9]) 413 ho = int(StringDT[11])*10 + int(StringDT[12]) 414 mi = int(StringDT[14])*10 + int(StringDT[15]) 415 se = int(StringDT[17])*10 + int(StringDT[18]) 416 else: 417 print errormsg 418 print ' ' + fname + ': type of String input date "' + typeSi + \ 419 '" not ready !!!!' 420 quit(-1) 421 422 if typeSo == 'matYmdHMS': 423 dateYmdHMS = np.zeros((6), dtype=int) 424 dateYmdHMS[0] = yr 425 dateYmdHMS[1] = mo 426 dateYmdHMS[2] = da 427 dateYmdHMS[3] = ho 428 dateYmdHMS[4] = mi 429 dateYmdHMS[5] = se 430 elif typeSo == 'YmdHMS': 431 dateYmdHMS = str(yr).zfill(4) + str(mo).zfill(2) + str(da).zfill(2) + \ 432 str(ho).zfill(2) + str(mi).zfill(2) + str(se).zfill(2) 433 elif typeSo == 'Y-m-d_H:M:S': 434 dateYmdHMS = str(yr).zfill(4) + '-' + str(mo).zfill(2) + '-' + \ 435 str(da).zfill(2) + '_' + str(ho).zfill(2) + ':' + str(mi).zfill(2) + ':' + \ 436 str(se).zfill(2) 437 elif typeSo == 'Y-m-d H:M:S': 438 dateYmdHMS = str(yr).zfill(4) + '-' + str(mo).zfill(2) + '-' + \ 439 str(da).zfill(2) + ' ' + str(ho).zfill(2) + ':' + str(mi).zfill(2) + ':' + \ 440 str(se).zfill(2) 441 elif typeSo == 'Y/m/d H-M-S': 442 dateYmdHMS = str(yr).zfill(4) + '/' + str(mo).zfill(2) + '/' + \ 443 str(da).zfill(2) + ' ' + str(ho).zfill(2) + '-' + str(mi).zfill(2) + '-' + \ 444 str(se).zfill(2) 445 elif typeSo == 'WRFdatetime': 446 dateYmdHMS = [] 447 yM = yr/1000 448 yC = (yr-yM*1000)/100 449 yD = (yr-yM*1000-yC*100)/10 450 yU = yr-yM*1000-yC*100-yD*10 451 452 mD = mo/10 453 mU = mo-mD*10 454 455 dD = da/10 456 dU = da-dD*10 457 458 hD = ho/10 459 hU = ho-hD*10 460 461 miD = mi/10 462 miU = mi-miD*10 463 464 sD = se/10 465 sU = se-sD*10 466 467 dateYmdHMS.append(str(yM)) 468 dateYmdHMS.append(str(yC)) 469 dateYmdHMS.append(str(yD)) 470 dateYmdHMS.append(str(yU)) 471 dateYmdHMS.append('-') 472 dateYmdHMS.append(str(mD)) 473 dateYmdHMS.append(str(mU)) 474 dateYmdHMS.append('-') 475 dateYmdHMS.append(str(dD)) 476 dateYmdHMS.append(str(dU)) 477 dateYmdHMS.append('_') 478 dateYmdHMS.append(str(hD)) 479 dateYmdHMS.append(str(hU)) 480 dateYmdHMS.append(':') 481 dateYmdHMS.append(str(miD)) 482 dateYmdHMS.append(str(miU)) 483 dateYmdHMS.append(':') 484 dateYmdHMS.append(str(sD)) 485 dateYmdHMS.append(str(sU)) 486 else: 487 print errormsg 488 print ' ' + fname + ': type of output date "' + typeSo + '" not ready !!!!' 489 quit(-1) 490 491 return dateYmdHMS 283 492 284 493 def period_information(idate, edate, totunits): … … 462 671 etime = edateT 463 672 464 N days = 1673 Nweeks = 1 465 674 ExactDays = itime.strftime("%Y%m%d%H%M%S") 466 675 it = itime 467 676 while it + dt.timedelta(days=7) <= etime: 468 677 it = it + dt.timedelta(days=7) 469 N days = Ndays + 7678 Nweeks = Nweeks + 1 470 679 ExactDays = ExactDays + '@' + it.strftime("%Y%m%d%H%M%S") 471 680 472 oinf = oinf + ',' + str(N days) + ',' + ExactDays681 oinf = oinf + ',' + str(Nweeks) + ',' + ExactDays 473 682 474 683 # Looking for number of days tacking exact beginning of the units [iYYYY][iMM][iDD]000000, [eYYYY][eMM][eDD+1]000000 … … 546 755 Nseconds = Nseconds + 1 547 756 ExactSeconds = ExactSeconds + '@' + it.strftime("%Y%m%d%H%M%S") 757 758 oinf = oinf + ',' + str(Nseconds) + ',' + ExactSeconds 759 760 else: 761 print errormsg 762 print ' '+ fname + ": totalunits '" + totunits + "' not ready !!" 763 print ' total time units ready:', readyT 764 quit(-1) 765 766 return oinf 767 768 def diff_dates360(dateA,dateB): 769 """ Function to provide the number of seconds of difference between two dates (dateA - dateB) on a 770 360 days/yr (or 12 30-days months) calendar 771 dateA= date in [Y, m, d, h, mi, s] 772 dateB= date in [Y, m, d, h, mi, s] 773 >>> diff_dates360([1976,2,17,8,29,30], [1976,2,27,8,29,30]) 774 -864000 775 """ 776 fname = 'diff_dates360' 777 778 # Reference date January first 1 779 refdate = np.array([1,1,1,0,0,0]) 780 781 diffdatesA = np.array(dateA) - np.array(refdate) 782 diffdatesB = np.array(dateB) - np.array(refdate) 783 784 # seconds by time-units 785 minsec = 60 786 hoursec = 60 * minsec 787 daysec = 24 * hoursec 788 monsec = 30 * daysec 789 yrsec = 12 * monsec 790 secs = [yrsec,monsec,daysec,hoursec,minsec,1] 791 792 dsecsA = np.sum(diffdatesA*secs) 793 dsecsB = np.sum(diffdatesB*secs) 794 795 diffsecs = dsecsA - dsecsB 796 797 return diffsecs 798 799 def changedate360(idate,val,unit): 800 """ Class to change a date on a 360 days/yr (or 12 30-days months) calendar 801 idate = date to change [Y, m, d, h, mi, s] 802 val = value to change 803 unit= units to change [0: year; 1: month; 2: day; 3: hour; 4: minute; 5: second] 804 >>> changedate360([1976,2,17,8,29,15],55,4) 805 [1976, 2, 17, 9, 24, 15] 806 >>> changedate360([1976,2,17,8,29,15],-55,5) 807 [1976, 2, 17, 8, 28, 20] 808 changedate360([1976,12,29,23,59,15],55,5) 809 [1977, 1, 1, 0, 0, 10] 810 """ 811 fname = 'changedate360' 812 813 sgn = np.abs(val)/val 814 815 newdate = list(idate) 816 817 Sunits = ['year', 'month', 'day', 'hour', 'minute', 'second'] 818 # Maximum values for the time-unit 819 tumaxs = [1e9, 13, 31, 24, 60, 60] 820 # Minimum values for the time-unit 821 tumins = [1, 1, 1, 0, 0, 0] 822 823 if val > tumaxs[unit]: 824 print errormsg 825 print ' ' + fname + ': value to change:', val," for unit: '"+Sunits[unit]+ \ 826 "' bigger than its limit:", tumaxs[unit] 827 quit(-1) 828 829 newval = idate[unit] + val 830 for itu in range(unit,-1,-1): 831 if newval >= tumaxs[itu]: 832 if itu > 2: 833 newdate[itu] = newval - tumaxs[itu] 834 else: 835 newdate[itu] = newval - tumaxs[itu] + 1 836 if newdate[itu] < tumins[itu]: newdate[itu] = tumins[itu] 837 newdate[itu-1] = newdate[itu-1] + 1 838 elif newval < tumins[itu]: 839 newdate[itu] = newdate[itu] + val + tumaxs[itu] + tumins[itu] 840 newdate[itu-1] = newdate[itu-1] - 1 841 else: 842 newdate[itu] = newval 843 break 844 newval = newdate[itu-1] 845 846 return newdate 847 848 class dtsec360dyr(object): 849 """ Class to operate a number of seconds to a date in a 360 days/yr (or 12 30-days months) calendar 850 date = initial date [Y, m, d, h, mi, s] 851 delatsec = diferential of seconds to use 852 self.year= year new date 853 self.month= month new date 854 self.day= day new date 855 self.hour= hour new date 856 self.minute= minute new date 857 self.second= second new date 858 self.dyear= increment in years 859 self.dmonth= increment in months 860 self.dday= increment in days 861 self.dhour= increment in hours 862 self.dminute= increment in minutes 863 self.dsecond= increment in seconds 864 self.newdate= resultant date as [Y, m, d, h, mi, s] 865 >>> dtsec360dyr([1976,2,17,8,27,0], 3680) 866 1976 2 17 9 28 20 867 0 0 0 1 1 20 868 >>> dtsec360dyr([1976,2,17,8,27,0], -3680) 869 1976 2 17 7 25 40 870 0 0 0 -1 -1 -20 871 """ 872 def __init__(self, date, deltasec): 873 fname = 'dtsec360dyr' 874 minsec = 60 875 hoursec = 60 * minsec 876 daysec = 24 * hoursec 877 monsec = 30 * daysec 878 yrsec = 12 * monsec 879 secs = [yrsec,monsec,daysec,hoursec,minsec,1] 880 881 self.year = None 882 self.month = None 883 self.day = None 884 self.hour = None 885 self.minute = None 886 self.second = None 887 self.dyear = None 888 self.dmonth = None 889 self.dday = None 890 self.dhour = None 891 self.dminute = None 892 self.dsecond = None 893 self.newdate = None 894 if date is not None and deltasec is not None: 895 absdelta = np.abs(deltasec) 896 sign = deltasec/absdelta 897 newdate = list(date) 898 Ndeltas = np.zeros((6), dtype=int) 899 for idt in range(6): 900 Ndeltas[idt] = (absdelta-np.sum(Ndeltas[0:idt]*secs[0:idt]))/secs[idt] 901 if Ndeltas[idt] != 0: 902 newdate = changedate360(newdate,sign*Ndeltas[idt],idt) 903 904 self.year = newdate[0] 905 self.month = newdate[1] 906 self.day = newdate[2] 907 self.hour = newdate[3] 908 self.minute = newdate[4] 909 self.second = newdate[5] 910 self.dyear = sign*Ndeltas[0] 911 self.dmonth = sign*Ndeltas[1] 912 self.dday = sign*Ndeltas[2] 913 self.dhour = sign*Ndeltas[3] 914 self.dminute = sign*Ndeltas[4] 915 self.dsecond = sign*Ndeltas[5] 916 self.newdate = newdate 917 918 def period_information_360d(idate, edate, totunits): 919 """ Function to provide the information of a given period idate, edate (dates in [YYYY][MM][DD][HH][MI][SS] format) in a 920 360 years calendar 921 [idate]= initial date of the period (in [YYYY][MM][DD][HH][MI][SS] format) 922 [edate]= end date of the period (in [YYYY][MM][DD][HH][MI][SS] format) 923 [totunits]= latest number of entire temporal units to compute: 'year', 'month', 'day', 'hour', 'minute', 'second' 924 925 resultant information given as ',' list of: 926 [dT]: sign of the temporal increment ('pos', 'neg') 927 [idate]: initial date as [YYYY]:[MM]:[DD]:[HH]:[MI]:[SS] 928 [edate]: end date as [YYYY]:[MM]:[DD]:[HH]:[MI]:[SS] 929 [Nsecs]: total seconds between dates 930 [Nyears]: Number of years taken as entire units ([iYYYY]0101000000 to [eYYYY+1]0101000000) 931 [ExactYears]: Exact year-dates of the period as a '@' separated list of [YYYY]0101000000 932 [Nmonths]: Number of months taken as entire units ([iYYYY][iMM]01000000 to [eYYYY][eMM+1]01000000) 933 [ExactMonths]: Exact mon-dates of the period as a '@' separated list of [YYYY][MM]01000000 934 [Ndays]: Number of days taken as entire units ([iYYYY][iMM][iDD]000000 to [eYYYY][eMM][eDD+1]000000) 935 [ExactDays]: Exact day-dates of the period as a '@' separated list of [YYYY][MM][DD]000000 936 [Nhours]: Number of hours taken as entire units ([iYYYY][iMM][iDD][iHH]0000 to [eYYYY][eMM][eDD][eHH+1]0000) 937 [ExactHours]: Exact hour-dates of the period as a '@' separated list of [YYYY][MM][DD][HH]0000 938 [Nminutes]: Number of minutes taken as entire units ([iYYYY][iMM][iDD][iHH][iMI]00 to [eYYYY][eMM][eDD][eHH][eMI+1]00) 939 [ExactMinutes]: Exact minutes-dates of the period as a '@' separated list of [YYYY][MM][DD][HH][MI]00 940 [Nsecs]: Number of minutes taken as entire units ([iYYYY][iMM][iDD][iHH][iMI][iS] to [eYYYY][eMM][eDD][eHH][eMI][eSE+1]) 941 [ExactSeconds]: Exact seconds-dates of the period as a '@' separated list of [YYYY][MM][DD][HH][MI][SS] 942 943 >>> period_information_360d( '19760217083110', '19770128000000', 'week') 944 pos,1976:02:17:08:31:10,1977:01:28:00:00:00,29863730.0,49,19760217000000@19760224000000@19760301000000@19760308000000@ 945 19760315000000@19760322000000@19760329000000@19760406000000@19760413000000@19760420000000@19760427000000@ 946 19760504000000@19760511000000@19760518000000@19760525000000@19760602000000@19760609000000@19760616000000@ 947 19760623000000@19760630000000@19760707000000@19760714000000@19760721000000@19760728000000@19760805000000@ 948 19760812000000@19760819000000@19760826000000@19760903000000@19760910000000@19760917000000@19760924000000@ 949 19761001000000@19761008000000@19761015000000@19761022000000@19761029000000@19761106000000@19761113000000@ 950 19761120000000@19761127000000@19761204000000@19761211000000@19761218000000@19761225000000@19770102000000@ 951 19770109000000@19770116000000@19770123000000 952 """ 953 import datetime as dt 954 955 fname = 'period_information_360d' 956 957 if idate == 'h': 958 print fname + '_____________________________________________________________' 959 print period_information_360d.__doc__ 960 quit() 961 962 expectargs = '[idate],[edate],[totunits]' 963 964 check_arguments(fname,str(idate)+','+str(edate)+','+str(totunits),expectargs,',') 965 966 readyT = ['year','month','week','day','hour','minute','second'] 967 968 # Seconds time-units 969 minsec = 60 970 hoursec = 60 * minsec 971 daysec = 24 * hoursec 972 monsec = 30 * daysec 973 yrsec = 12 * monsec 974 secs = [yrsec,monsec,daysec,hoursec,minsec,1] 975 976 # Checking length 977 Lidate = len(idate) 978 Ledate = len(edate) 979 980 if Lidate != Ledate: 981 print errormsg 982 print ' ' + fname + ': string dates of the period have different length !!' 983 print " idate: '" + idate + "' (L=", Lidate, ") edate: '" + edate + \ 984 "' (L=", Ledate, ')' 985 quit(-1) 986 else: 987 if Lidate != 14: 988 print errormsg 989 print ' ' + fname + ": wrong initial date: '" + idate + "' must " + \ 990 'have 14 characters!!' 991 print ' and it has:', Lidate 992 quit(-1) 993 if Ledate != 14: 994 print errormsg 995 print ' ' + fname + ": wrong end date: '" + edate + "' must " + \ 996 'have 14 characters!!' 997 print ' and it has:', Ledate 998 quit(-1) 999 1000 # Checking for right order of dates 1001 if int(idate) > int(edate): 1002 print warnmsg 1003 print ' ' + fname + ": initial date '" + idate + "' after end date '" + \ 1004 edate + "' !!" 1005 print " re-sorting them!" 1006 i1 = edate 1007 e1 = idate 1008 idate = i1 1009 edate = e1 1010 oinf = 'neg' 1011 else: 1012 oinf = 'pos' 1013 1014 # Year, month, day, hour, minute, second initial date 1015 iyrS = idate[0:4] 1016 imoS = idate[4:6] 1017 idaS = idate[6:8] 1018 ihoS = idate[8:10] 1019 imiS = idate[10:12] 1020 iseS = idate[12:14] 1021 1022 oinf = oinf + ',' + iyrS+':'+imoS+':'+idaS+':'+ihoS+':'+imiS+':'+iseS 1023 1024 # Year, month, day, hour, minute, second end date 1025 eyrS = edate[0:4] 1026 emoS = edate[4:6] 1027 edaS = edate[6:8] 1028 ehoS = edate[8:10] 1029 emiS = edate[10:12] 1030 eseS = edate[12:14] 1031 1032 oinf = oinf + ',' + eyrS+':'+emoS+':'+edaS+':'+ehoS+':'+emiS+':'+eseS 1033 1034 idateT = dt.datetime(int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),int(iseS)) 1035 edateT = dt.datetime(int(eyrS),int(emoS),int(edaS),int(ehoS),int(emiS),int(eseS)) 1036 1037 # Seconds between dates 1038 DT = edateT - idateT 1039 Nsecs = DT.total_seconds() 1040 1041 oinf = oinf + ',' + str(Nsecs) 1042 1043 daysYr = 360 1044 months = np.ones((12), dtype=int)*1 1045 1046 # Looking for number of years tacking exact beginning of the units [iYYYY]0101000000, [eYYYY+1]0101000000 1047 ## 1048 if totunits == 'year': 1049 itime = int(iyrS) 1050 if edate[4:15] != '0101000000': 1051 etime = int(eyrS)+1 1052 else: 1053 etime = int(eyrS) 1054 1055 itimeT = dt.datetime(itime,1,1,0,0,0) 1056 1057 Nyears = 1 1058 ExactYears = itimeT.strftime("%Y%m%d%H%M%S") 1059 it = int(iyrS) 1060 while it + 1 <= etime: 1061 it = it + 1 1062 Nyears = Nyears + 1 1063 itT = dt.datetime(it,1,1,0,0,0) 1064 ExactYears = ExactYears + '@' + itT.strftime("%Y%m%d%H%M%S") 1065 1066 oinf = oinf + ',' + str(Nyears) + ',' + ExactYears 1067 1068 # Looking for number of months tacking exact beginning of the units [iYYYY][iMM]01000000, [eYYYY][eMM+1]01000000 1069 ## 1070 elif totunits == 'month': 1071 itime = int(iyrS)*100 + int(imoS) 1072 if edate[6:15] != '01000000': 1073 emo1 = int(emoS)+1 1074 if emo1 > 13: 1075 eyr1 = str(int(eyrS) + 1) 1076 emo1 = 01 1077 else: 1078 eyr1 = eyrS 1079 1080 else: 1081 eyr1 = eyrS 1082 emo1 = emoS 1083 1084 etime = int(eyr1)*100 + int(emo1) 1085 itimeT = dt.datetime(int(iyrS),int(imoS),1,0,0,0) 1086 1087 Nmonths = 1 1088 ExactMonths = itimeT.strftime("%Y%m%d%H%M%S") 1089 it = itime 1090 while it + 1 <= etime: 1091 it = it + 1 1092 Nmonths = Nmonths + 1 1093 ityr = it/100 1094 itmo = it - ityr*100 1095 if itmo > 12: 1096 ityr = ityr + 1 1097 itmo = 1 1098 it = ityr * 100 + itmo 1099 1100 itT = dt.datetime(ityr,itmo,1,0,0,0) 1101 ExactMonths = ExactMonths + '@' + itT.strftime("%Y%m%d%H%M%S") 1102 1103 oinf = oinf + ',' + str(Nmonths) + ',' + ExactMonths 1104 1105 # Looking for number of weeks tacking exact beginning of the units [iYYYY][iMM][iDD]000000, [eYYYY][eMM][eDD+7]000000 1106 ## 1107 elif totunits == 'week': 1108 itime = [int(iyrS),int(imoS),int(idaS),0,0,0] 1109 if edate[8:15] != '000000': 1110 etime = [int(eyrS), int(emoS), int(edaS)+7,0,0,0] 1111 else: 1112 etime = [int(eyrS),int(emoS),int(edaS),int(ehoS),int(emiS),int(eseS)] 1113 1114 ExactDays = datetimeStr_conversion(itime,'matYmdHMS','YmdHMS') 1115 1116 erefsecs = diff_dates360(etime,[1,1,1,0,0,0]) 1117 1118 Nweeks = 1 1119 it = itime 1120 refsecs = diff_dates360(it,[1,1,1,0,0,0]) 1121 while refsecs + 7*daysec <= erefsecs: 1122 refsecs = refsecs + 7*daysec 1123 dateincr = dtsec360dyr(it,7*daysec) 1124 it = dateincr.newdate 1125 Nweeks = Nweeks + 1 1126 ExactDays = ExactDays + '@' + datetimeStr_conversion(it,'matYmdHMS','YmdHMS') 1127 1128 oinf = oinf + ',' + str(Nweeks) + ',' + ExactDays 1129 1130 # Looking for number of days tacking exact beginning of the units [iYYYY][iMM][iDD]000000, [eYYYY][eMM][eDD+1]000000 1131 ## 1132 elif totunits == 'day': 1133 itime = [int(iyrS),int(imoS),int(idaS),0,0,0] 1134 if edate[8:15] != '000000': 1135 etime = [int(eyrS), int(emoS), int(edaS)+1,0,0,0] 1136 else: 1137 etime = [int(eyrS),int(emoS),int(edaS),int(ehoS),int(emiS),int(eseS)] 1138 1139 ExactDays = datetimeStr_conversion(itime,'matYmdHMS','YmdHMS') 1140 1141 erefsecs = diff_dates360(etime,[1,1,1,0,0,0]) 1142 1143 Ndays = 1 1144 it = itime 1145 refsecs = diff_dates360(it,[1,1,1,0,0,0]) 1146 while refsecs + 1*daysec <= erefsecs: 1147 refsecs = refsecs + 1*daysec 1148 dateincr = dtsec360dyr(it,1*daysec) 1149 it = dateincr.newdate 1150 Ndays = Ndayss + 1 1151 ExactDays = ExactDays + '@' + datetimeStr_conversion(it,'matYmdHMS','YmdHMS') 1152 1153 oinf = oinf + ',' + str(Ndays) + ',' + ExactDays 1154 1155 # Looking for number of hours tacking exact beginning of the units [iYYYY][iMM][iDD][iHH]0000, [eYYYY][eMM][eDD][iHH+1]0000 1156 ## 1157 elif totunits == 'hour': 1158 itime = [int(iyrS),int(imoS),int(idaS),int(ihoS),0,0] 1159 if edate[10:15] != '0000': 1160 etime = [int(eyrS), int(emoS), int(edaS), int(ehoS)+1,0,0] 1161 else: 1162 etime = [int(eyrS),int(emoS),int(edaS),int(ehoS),int(emiS),int(eseS)] 1163 1164 Nhours = 1 1165 ExactHours = datetimeStr_conversion(itime,'matYmdHMS','YmdHMS') 1166 erefsecs = diff_dates360(etime,[1,1,1,0,0,0]) 1167 it = itime 1168 refsecs = diff_dates360(it,[1,1,1,0,0,0]) 1169 while refsecs + 1*daysec <= erefsecs: 1170 refsecs = refsecs + 1*hoursec 1171 dateincr = dtsec360dyr(it,1*hoursec) 1172 it = dateincr.newdate 1173 Nhours = Nhours + 1 1174 ExactHours = ExactHours + '@' + datetimeStr_conversion(it,'matYmdHMS','YmdHMS') 1175 1176 oinf = oinf + ',' + str(Nhours) + ',' + ExactHours 1177 1178 # Looking for number of minutes tacking exact beginning of the units [iYYYY][iMM][iDD][iHH][iMI]00, [eYYYY][eMM][eDD][iHH][iMI+1]00 1179 ## 1180 elif totunits == 'minute': 1181 itime = [int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),0] 1182 if edate[12:15] != '00': 1183 etime = [int(eyrS), int(emoS), int(edaS), int(ehoS), int(emiS)+1,0] 1184 else: 1185 etime = [int(eyrS),int(emoS),int(edaS),int(ehoS),int(emiS),int(eseS)] 1186 1187 Nminutes = 1 1188 ExactMinutes = datetimeStr_conversion(itime,'matYmdHMS','YmdHMS') 1189 erefsecs = diff_dates360(etime,[1,1,1,0,0,0]) 1190 it = itime 1191 refsecs = diff_dates360(it,[1,1,1,0,0,0]) 1192 while refsecs + 1*minsec <= erefsecs: 1193 refsecs = refsecs + 1*minsec 1194 dateincr = dtsec360dyr(it,1*hoursec) 1195 Nminutes = Nminutes + 1 1196 ExactMinutes = ExactMinutes +'@' + datetimeStr_conversion(it,'matYmdHMS',\ 1197 'YmdHMS') 1198 1199 oinf = oinf + ',' + str(Nminutes) + ',' + ExactMinutes 1200 1201 # Looking for number of seconds tacking exact beginning of the units [iYYYY][iMM][iDD][iHH][iMI][iSE], 1202 # [eYYYY][eMM][eDD][iHH][iMI][iSE+1] 1203 ## 1204 elif totunits == 'second': 1205 itime = [int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),int(iseS)] 1206 if edate[12:15] != '00': 1207 etime = [int(eyrS), int(emoS), int(edaS), int(ehoS), int(emiS), int(eseS)+1] 1208 else: 1209 etime = [int(iyrS),int(imoS),int(idaS),int(ihoS),int(imiS),int(iseS)] 1210 1211 Nseconds = 1 1212 ExactSeconds = datetimeStr_conversion(itime,'matYmdHMS','YmdHMS') 1213 erefsecs = diff_dates360(etime,[1,1,1,0,0,0]) 1214 it = itime 1215 refsecs = diff_dates360(it,[1,1,1,0,0,0]) 1216 while refsecs + 1 <= erefsecs: 1217 refsecs = refsecs + 1 1218 dateincr = dtsec360dyr(it,1*hoursec) 1219 Nseconds = Nseconds + 1 1220 ExactSeconds = ExactSeconds + '@'+ datetimeStr_conversion(it,'matYmdHMS',\ 1221 'YmdHMS') 548 1222 549 1223 oinf = oinf + ',' + str(Nseconds) + ',' + ExactSeconds … … 4600 5274 return nlines 4601 5275 4602 def datetimeStr_conversion(StringDT,typeSi,typeSo):4603 """ Function to transform a string date to an another date object4604 StringDT= string with the date and time4605 typeSi= type of datetime string input4606 typeSo= type of datetime string output4607 [typeSi/o]4608 'cfTime': [time],[units]; ]time in CF-convention format [units] = [tunits] since [refdate]4609 'matYmdHMS': numerical vector with [[YYYY], [MM], [DD], [HH], [MI], [SS]]4610 'YmdHMS': [YYYY][MM][DD][HH][MI][SS] format4611 'Y-m-d_H:M:S': [YYYY]-[MM]-[DD]_[HH]:[MI]:[SS] format4612 'Y-m-d H:M:S': [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] format4613 'Y/m/d H-M-S': [YYYY]/[MM]/[DD] [HH]-[MI]-[SS] format4614 'WRFdatetime': [Y], [Y], [Y], [Y], '-', [M], [M], '-', [D], [D], '_', [H],4615 [H], ':', [M], [M], ':', [S], [S]4616 >>> datetimeStr_conversion('1976-02-17_08:32:05','Y-m-d_H:M:S','matYmdHMS')4617 [1976 2 17 8 32 5]4618 >>> datetimeStr_conversion(str(137880)+',minutes since 1979-12-01_00:00:00','cfTime','Y/m/d H-M-S')4619 1980/03/05 18-00-004620 """4621 import datetime as dt4622 4623 fname = 'datetimeStr_conversion'4624 4625 if StringDT[0:1] == 'h':4626 print fname + '_____________________________________________________________'4627 print datetimeStr_conversion.__doc__4628 quit()4629 4630 if typeSi == 'cfTime':4631 timeval = np.float(StringDT.split(',')[0])4632 tunits = StringDT.split(',')[1].split(' ')[0]4633 Srefdate = StringDT.split(',')[1].split(' ')[2]4634 4635 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS]4636 ##4637 yrref=Srefdate[0:4]4638 monref=Srefdate[5:7]4639 dayref=Srefdate[8:10]4640 4641 trefT = Srefdate.find(':')4642 if not trefT == -1:4643 # print ' ' + fname + ': refdate with time!'4644 horref=Srefdate[11:13]4645 minref=Srefdate[14:16]4646 secref=Srefdate[17:19]4647 refdate = datetimeStr_datetime( yrref + '-' + monref + '-' + dayref + \4648 '_' + horref + ':' + minref + ':' + secref)4649 else:4650 refdate = datetimeStr_datetime( yrref + '-' + monref + '-' + dayref + \4651 + '_00:00:00')4652 4653 if tunits == 'weeks':4654 newdate = refdate + dt.timedelta(weeks=float(timeval))4655 elif tunits == 'days':4656 newdate = refdate + dt.timedelta(days=float(timeval))4657 elif tunits == 'hours':4658 newdate = refdate + dt.timedelta(hours=float(timeval))4659 elif tunits == 'minutes':4660 newdate = refdate + dt.timedelta(minutes=float(timeval))4661 elif tunits == 'seconds':4662 newdate = refdate + dt.timedelta(seconds=float(timeval))4663 elif tunits == 'milliseconds':4664 newdate = refdate + dt.timedelta(milliseconds=float(timeval))4665 else:4666 print errormsg4667 print ' timeref_datetime: time units "' + tunits + '" not ready!!!!'4668 quit(-1)4669 4670 yr = newdate.year4671 mo = newdate.month4672 da = newdate.day4673 ho = newdate.hour4674 mi = newdate.minute4675 se = newdate.second4676 elif typeSi == 'matYmdHMS':4677 yr = StringDT[0]4678 mo = StringDT[1]4679 da = StringDT[2]4680 ho = StringDT[3]4681 mi = StringDT[4]4682 se = StringDT[5]4683 elif typeSi == 'YmdHMS':4684 yr = int(StringDT[0:4])4685 mo = int(StringDT[4:6])4686 da = int(StringDT[6:8])4687 ho = int(StringDT[8:10])4688 mi = int(StringDT[10:12])4689 se = int(StringDT[12:14])4690 elif typeSi == 'Y-m-d_H:M:S':4691 dateDT = StringDT.split('_')4692 dateD = dateDT[0].split('-')4693 timeT = dateDT[1].split(':')4694 yr = int(dateD[0])4695 mo = int(dateD[1])4696 da = int(dateD[2])4697 ho = int(timeT[0])4698 mi = int(timeT[1])4699 se = int(timeT[2])4700 elif typeSi == 'Y-m-d H:M:S':4701 dateDT = StringDT.split(' ')4702 dateD = dateDT[0].split('-')4703 timeT = dateDT[1].split(':')4704 yr = int(dateD[0])4705 mo = int(dateD[1])4706 da = int(dateD[2])4707 ho = int(timeT[0])4708 mi = int(timeT[1])4709 se = int(timeT[2])4710 elif typeSi == 'Y/m/d H-M-S':4711 dateDT = StringDT.split(' ')4712 dateD = dateDT[0].split('/')4713 timeT = dateDT[1].split('-')4714 yr = int(dateD[0])4715 mo = int(dateD[1])4716 da = int(dateD[2])4717 ho = int(timeT[0])4718 mi = int(timeT[1])4719 se = int(timeT[2])4720 elif typeSi == 'WRFdatetime':4721 yr = int(StringDT[0])*1000 + int(StringDT[1])*100 + int(StringDT[2])*10 + \4722 int(StringDT[3])4723 mo = int(StringDT[5])*10 + int(StringDT[6])4724 da = int(StringDT[8])*10 + int(StringDT[9])4725 ho = int(StringDT[11])*10 + int(StringDT[12])4726 mi = int(StringDT[14])*10 + int(StringDT[15])4727 se = int(StringDT[17])*10 + int(StringDT[18])4728 else:4729 print errormsg4730 print ' ' + fname + ': type of String input date "' + typeSi + \4731 '" not ready !!!!'4732 quit(-1)4733 4734 if typeSo == 'matYmdHMS':4735 dateYmdHMS = np.zeros((6), dtype=int)4736 dateYmdHMS[0] = yr4737 dateYmdHMS[1] = mo4738 dateYmdHMS[2] = da4739 dateYmdHMS[3] = ho4740 dateYmdHMS[4] = mi4741 dateYmdHMS[5] = se4742 elif typeSo == 'YmdHMS':4743 dateYmdHMS = str(yr).zfill(4) + str(mo).zfill(2) + str(da).zfill(2) + \4744 str(ho).zfill(2) + str(mi).zfill(2) + str(se).zfill(2)4745 elif typeSo == 'Y-m-d_H:M:S':4746 dateYmdHMS = str(yr).zfill(4) + '-' + str(mo).zfill(2) + '-' + \4747 str(da).zfill(2) + '_' + str(ho).zfill(2) + ':' + str(mi).zfill(2) + ':' + \4748 str(se).zfill(2)4749 elif typeSo == 'Y-m-d H:M:S':4750 dateYmdHMS = str(yr).zfill(4) + '-' + str(mo).zfill(2) + '-' + \4751 str(da).zfill(2) + ' ' + str(ho).zfill(2) + ':' + str(mi).zfill(2) + ':' + \4752 str(se).zfill(2)4753 elif typeSo == 'Y/m/d H-M-S':4754 dateYmdHMS = str(yr).zfill(4) + '/' + str(mo).zfill(2) + '/' + \4755 str(da).zfill(2) + ' ' + str(ho).zfill(2) + '-' + str(mi).zfill(2) + '-' + \4756 str(se).zfill(2)4757 elif typeSo == 'WRFdatetime':4758 dateYmdHMS = []4759 yM = yr/10004760 yC = (yr-yM*1000)/1004761 yD = (yr-yM*1000-yC*100)/104762 yU = yr-yM*1000-yC*100-yD*104763 4764 mD = mo/104765 mU = mo-mD*104766 4767 dD = da/104768 dU = da-dD*104769 4770 hD = ho/104771 hU = ho-hD*104772 4773 miD = mi/104774 miU = mi-miD*104775 4776 sD = se/104777 sU = se-sD*104778 4779 dateYmdHMS.append(str(yM))4780 dateYmdHMS.append(str(yC))4781 dateYmdHMS.append(str(yD))4782 dateYmdHMS.append(str(yU))4783 dateYmdHMS.append('-')4784 dateYmdHMS.append(str(mD))4785 dateYmdHMS.append(str(mU))4786 dateYmdHMS.append('-')4787 dateYmdHMS.append(str(dD))4788 dateYmdHMS.append(str(dU))4789 dateYmdHMS.append('_')4790 dateYmdHMS.append(str(hD))4791 dateYmdHMS.append(str(hU))4792 dateYmdHMS.append(':')4793 dateYmdHMS.append(str(miD))4794 dateYmdHMS.append(str(miU))4795 dateYmdHMS.append(':')4796 dateYmdHMS.append(str(sD))4797 dateYmdHMS.append(str(sU))4798 else:4799 print errormsg4800 print ' ' + fname + ': type of output date "' + typeSo + '" not ready !!!!'4801 quit(-1)4802 4803 return dateYmdHMS4804 4805 5276 def table_tex(tablevals, colnames, rownames, of): 4806 5277 """ Function to write into a LaTeX tabular from a table of values
Note: See TracChangeset
for help on using the changeset viewer.