Changeset 2457 in lmdz_wrf
- Timestamp:
- Apr 22, 2019, 8:53:32 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2448 r2457 727 727 if not searchInlist(availcalendar , calendar): 728 728 print errormsg 729 print ' ' + fname + ": calendar '" + + "' not ready !!"729 print ' ' + fname + ": calendar '" + calendar + "' not ready !!" 730 730 print ' avaialable ones:', availcalendar 731 731 quit(-1) … … 754 754 '_00:00:00') 755 755 756 removeleap = False 756 757 if calendar == 'noleap' or calendar == '365d': 757 removeleap = False758 758 newdate = advance_cfDate(refdate, timeval, tunits) 759 759 if int(yrref) <= newdate.year: … … 15803 15803 # print ' ' , '(', inds[i,j,0], ',', inds[i,j,1], ')' 15804 15804 15805 def fix_CFdates(timevals, origcftimeu, origcal, \ 15806 newcftimeu= 'hours since 1949-12-01 00:00:00', newcal='gregorian'): 15807 """ Fixing CF-time values with wrong setting 15808 NOTE: CF-times does not authorize timeu = 'month' 15809 python datetime package does not work for dates < 1900 15810 timevals: actual CF time-values 15811 origcftimeu: current original CF-time values [timeu] since [Refdate] 15812 newcftimeu: new correct CF and python time units 15813 >>> times = [15.5, 45, 74.5, 105, 135.5, 166, 196.5, 227.5, 258, 288.5, 319] 15814 >>> fix_CFdates(times, 'days since 1850-01-01 00:00:00', 'noleap', 15815 'days since 1949-12-01 00:00:00') 15816 [-36477.5 -36448. -36418.5 -36388. -36357.5 -36327. -36296.5 -36265.5 15817 -36235. -36204.5 -36174. ] 15818 """ 15819 import datetime as dt 15820 import subprocess as sub 15821 15822 fname = 'fix_CFdates' 15823 15824 availtu = ['years', 'weeks', 'days', 'hours', 'minutes', 'seconds'] 15825 15826 # original 15827 origtv = origcftimeu.split(' ') 15828 origtunits = origtv[0] 15829 origSrefdate = origtv[2] 15830 15831 yrref=origSrefdate[0:4] 15832 monref=origSrefdate[5:7] 15833 dayref=origSrefdate[8:10] 15834 15835 if int(yrref) < 1970: origsing = -1. 15836 else: origsing = 1. 15837 15838 # Does original reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 15839 ## 15840 trefT = origcftimeu.find(':') 15841 if not trefT == -1: 15842 if len(origtv) == 3: 15843 horref=int(origSrefdate[11:13]) 15844 minref=int(origSrefdate[14:16]) 15845 secref=int(origSrefdate[17:19]) 15846 else: 15847 origSreftime = origtv[3] 15848 horref=int(origSreftime[0:2]) 15849 minref=int(origSreftime[3:5]) 15850 secref=int(origSreftime[6:8]) 15851 else: 15852 horref = 0 15853 minref = 0 15854 secref = 0 15855 15856 addsecs = horref*3600 + minref*60 + secref 15857 addsS = str(addsecs) 15858 15859 # Using shell date `coreutil' 15860 seconds = sub.Popen('/bin/date +%s -u -d"' + origSrefdate[0:10] + ' ' + addsS + \ 15861 ' seconds"', shell=True, stdout=sub.PIPE) 15862 origsecondsdiff = np.float64(seconds.communicate()[0].replace('\n','')) 15863 15864 # new 15865 newtv = newcftimeu.split(' ') 15866 newtunits = newtv[0] 15867 newSrefdate = newtv[2] 15868 15869 yrref=newSrefdate[0:4] 15870 monref=newSrefdate[5:7] 15871 dayref=newSrefdate[8:10] 15872 15873 if int(yrref) < 1970: newsing = -1. 15874 else: newsing = 1. 15875 15876 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 15877 ## 15878 trefT = newcftimeu.find(':') 15879 if not trefT == -1: 15880 if len(newtv) == 3: 15881 horref=int(newSrefdate[11:13]) 15882 minref=int(newSrefdate[14:16]) 15883 secref=int(newSrefdate[17:19]) 15884 else: 15885 newSreftime = newtv[3] 15886 horref=int(newSreftime[0:2]) 15887 minref=int(newSreftime[3:5]) 15888 secref=int(newSreftime[6:8]) 15889 else: 15890 horref = 0 15891 minref = 0 15892 secref = 0 15893 15894 addsecs = horref*3600 + minref*60 + secref 15895 addsS = str(addsecs) 15896 15897 # Using shell date `coreutil' 15898 seconds = sub.Popen('/bin/date +%s -u -d"' + newSrefdate[0:10] + ' ' + addsS + \ 15899 ' seconds"', shell=True, stdout=sub.PIPE) 15900 newsecondsdiff = np.float64(seconds.communicate()[0].replace('\n','')) 15901 15902 # Seconds difference respect new date 15903 secsdiff = origsecondsdiff - newsecondsdiff 15904 15905 if origtunits == 'years': 15906 secsdtr = 365 * 24 * 3600. 15907 elif origtunits == 'weeks': 15908 secsdtr = 7 * 24 * 3600. 15909 elif origtunits == 'days': 15910 secsdtr = 24 * 3600. 15911 elif origtunits == 'hours': 15912 secsdtr = 3600. 15913 elif origtunits == 'minutes': 15914 secsdtr = 60. 15915 elif origtunits == 'seconds': 15916 secsdtr = 1. 15917 else: 15918 print errormsg 15919 print ' ' + fname + ": original time-units '" + origtunits + "' not ready !!" 15920 print ' available ones: ', availtu 15921 quit(-1) 15922 15923 if type(timevals) != type(np.ones((2), dtype=int)): 15924 newtimevals = np.array(timevals, dtype=np.float64) 15925 newtimevals = newtimevals*secsdtr 15926 else: 15927 newtimevals = timevals*secsdtr 15928 newtimevals = newtimevals + origsecondsdiff 15929 newtimevals = newtimevals - newsecondsdiff 15930 15931 # Re-scaling to the new time units 15932 if newtunits == 'years': 15933 secsdtr = 365 * 24 * 3600. 15934 elif newtunits == 'weeks': 15935 secsdtr = 7 * 24 * 3600. 15936 elif newtunits == 'days': 15937 secsdtr = 24 * 3600. 15938 elif newtunits == 'hours': 15939 secsdtr = 3600. 15940 elif newtunits == 'minutes': 15941 secsdtr = 60. 15942 elif newtunits == 'seconds': 15943 secsdtr = 1. 15944 else: 15945 print errormsg 15946 print ' ' + fname + ": new time-units '" + newtunits + "' not ready !!" 15947 print ' available ones: ', availtu 15948 quit(-1) 15949 newtimevals = newtimevals / secsdtr 15950 15951 if origcal == 'noleap3' or origcal == '365d': 15952 print ' ' + fname + ": found non-standard calendar '" + origcal + "' !!" 15953 # Imposing to gregorian calendar 15954 newtimevals = impose_gregorian(newtimevals, newcftimeu, origcal) 15955 15956 return newtimevals 15957 15958 times = [15.5, 45, 74.5, 105, 135.5, 166, 196.5, 227.5, 258, 288.5, 319] 15959 times = [56649.5, 56680, 56710.5, 56741, 56771.5, 56802.5, 56833, 56863.5, 56894, 56924.5] 15960 print fix_CFdates(times, 'days since 1850-01-01 00:00:00', 'noleap', \ 15961 'days since 1949-12-01 00:00:00') 15805 15962 15806 15963 #quit()
Note: See TracChangeset
for help on using the changeset viewer.