|
| 1 | +### This module contains date handling routines used in the other ecco_access modules. |
| 2 | + |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def date_adjustment(ShortName,StartDate,EndDate,CMR_query=True): |
| 8 | + """ |
| 9 | + Adjusts StartDate and EndDate, augmenting where day or month may be missing. |
| 10 | + Returns either strings ready for NASA Earthdata CMR query (CMR_query = True), |
| 11 | + or numpy.datetime64 values. |
| 12 | + """ |
| 13 | + |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | + # # Adjust StartDate and EndDate |
| 18 | + |
| 19 | + if StartDate=='yesterday': |
| 20 | + StartDate = yesterday() |
| 21 | + if EndDate==-1: |
| 22 | + EndDate = StartDate |
| 23 | + elif StartDate=='yesterday': |
| 24 | + StartDate = yesterday() |
| 25 | + elif EndDate=='today': |
| 26 | + EndDate = today() |
| 27 | + |
| 28 | + if len(StartDate) == 4: |
| 29 | + StartDate += '-01-01' |
| 30 | + elif len(StartDate) == 7: |
| 31 | + StartDate += '-01' |
| 32 | + elif len(StartDate) != 10: |
| 33 | + sys.exit('\nStart date should be in format ''YYYY'', ''YYYY-MM'', or ''YYYY-MM-DD''!\n'\ |
| 34 | + +'Program will exit now !\n') |
| 35 | + |
| 36 | + if len(EndDate) == 4: |
| 37 | + EndDate += '-12-31' |
| 38 | + elif len(EndDate) == 7: |
| 39 | + EndDate = str(np.datetime64(str(np.datetime64(EndDate,'M')+np.timedelta64(1,'M'))+'-01','D')\ |
| 40 | + -np.timedelta64(1,'D')) |
| 41 | + elif len(EndDate) != 10: |
| 42 | + sys.exit('\nEnd date should be in format ''YYYY'', ''YYYY-MM'', or ''YYYY-MM-DD''!\n'\ |
| 43 | + +'Program will exit now !\n') |
| 44 | + |
| 45 | + # for snapshot datasets, move EndDate one day later |
| 46 | + if 'SNAPSHOT' in ShortName: |
| 47 | + EndDate = str(np.datetime64(EndDate,'D') + np.timedelta64(1,'D')) |
| 48 | + |
| 49 | + # CMR request adjustments |
| 50 | + if CMR_query: |
| 51 | + SingleDay_flag = False |
| 52 | + if (('MONTHLY' in ShortName) or ('DAILY' in ShortName)): |
| 53 | + if np.datetime64(EndDate,'D') - np.datetime64(StartDate,'D') \ |
| 54 | + > np.timedelta64(1,'D'): |
| 55 | + # for monthly and daily datasets, do not include the month or day before |
| 56 | + StartDate = str(np.datetime64(StartDate,'D') + np.timedelta64(1,'D')) |
| 57 | + else: |
| 58 | + # for single day ranges we need to make the adjustment |
| 59 | + # after the CMR request |
| 60 | + SingleDay_flag = True |
| 61 | + |
| 62 | + return StartDate,EndDate,SingleDay_flag |
| 63 | + |
| 64 | + else: |
| 65 | + StartDate = np.datetime64(StartDate,'D') |
| 66 | + EndDate = np.datetime64(EndDate,'D') |
| 67 | + |
| 68 | + return StartDate,EndDate |
0 commit comments