コード例 #1
0
    def station_invalid(el_list, vX_list, time_range, stn, contraints):
        #Check if constraints are met for element list and date range
        if constraints in ['any_any', 'any_all']:
            flag_invalid_station = True
        elif constraints in ['all_all', 'all_any']:
            flag_invalid_station = False

        for el_idx, el_vX in enumerate(el_list):
            #Find correct index in vX_list
            try:
                idx = vX_list.index(el_vX)
            except:
                if constraints in ['all_all', 'all_any']:
                    flag_invalid_station = True
                    break
                elif constraints in ['any_any', 'any_all']:
                    continue
            #Sanity Check
            if not stn['valid_daterange'][idx] and (constraints == 'all_all'  or constraints == 'all_any' or constraints is None):
                #data for this element does not exist at station
                flag_invalid_station = True
                break
            elif not stn['valid_daterange'][idx] and (constraints == 'any_any' or constraints == 'any_all'):
                continue

            #Find period of record for this element and station
            por_start = WRCCUtils.date_to_datetime(stn['valid_daterange'][idx][0])
            por_end = WRCCUtils.date_to_datetime(stn['valid_daterange'][idx][1])
            if time_range[0].lower() != 'por':
                user_start = WRCCUtils.date_to_datetime(time_range[0])
            else:
                user_start = por_start
            if time_range[1].lower() != 'por':
                user_end = WRCCUtils.date_to_datetime(time_range[1])
            else:
                user_end = por_end
            #Check constraints logic for this element and station
            if constraints == 'all_all' or constraints is None:
                #all  elements have data records for all dates within start and end date given by user
                if user_start < por_start or user_end > por_end:
                    flag_invalid_station =  True
                    break
            elif constraints == 'any_any':
                #At least one element has one data record within user given time_range
                if (user_end >= por_start and user_start <= por_end) or (user_start <= por_end and user_end >=por_start):
                    flag_invalid_station = False
                    break
            elif constraints == 'all_any':
                #All elements have at least one data record within user given time_range
                if (user_end >= por_start and user_start <= por_end) or (user_start <= por_end and user_end >=por_start):
                    continue
                else:
                    flag_invalid_station =  True
                    break
            elif constraints == 'any_all':
                #At least one elements has data records for all dates within given date_range
                if user_start >= por_start and user_end <= por_end:
                    flag_invalid_station = False
                    break
        return flag_invalid_station
コード例 #2
0
def check_end_date(form):
    err = None
    s_date = form['start_date']
    e_date = form['end_date']
    #Check valid daterange error
    if e_date.lower() == 'por':
        if 'station_id' in form.keys():
            return err
        else:
            return '%s is not a valid Start Date for a multi-station or grid request!' %str(form['end_date'])
    s_date = WRCCUtils.date_to_eight(s_date)
    e_date = WRCCUtils.date_to_eight(e_date)
    if len(e_date)!=8:
        return '%s is not a valid date.' %str(form['end_date'])

    try:
        int(e_date)
    except:
        return '%s is not a valid date.' %str(form['end_date'])

    #Check month
    if int(e_date[4:6]) < 1 or int(e_date[4:6]) > 12:
        return '%s is not a valid date.' %str(form['end_date'])
    #Check day
    if int(e_date[6:8]) < 1 or int(e_date[4:6]) > 31:
        return '%s is not a valid date.' %str(form['end_date'])


    #Ceck for month length
    ml = WRCCData.MONTH_LENGTHS[int(e_date[4:6]) - 1]
    if int(e_date[6:8]) > ml:
        if str(e_date[4:6]) == '02' or str(e_date[4:6]) == '2':
            if int(e_date[6:8]) == 29 and  WRCCUtils.is_leap_year(e_date[0:4]):
                return None
                #return '%s only has %s days. You entered: %s' %(WRCCData.NUMBER_TO_MONTH_NAME[str(e_date[4:6])],'29',str(e_date[6:8]))
            else:
                return '%s only has %s days. You entered: %s' %(WRCCData.NUMBER_TO_MONTH_NAME[str(e_date[4:6])],str(ml),str(e_date[6:8]))
        else:
            return '%s only has %s days. You entered: %s' %(WRCCData.NUMBER_TO_MONTH_NAME[str(e_date[4:6])],str(ml),str(e_date[6:8]))

    #Check that start date is ealier than end date
    if s_date.lower() == 'por':
        return err
    try:
        sd = datetime.datetime(int(s_date[0:4]), int(s_date[4:6].lstrip('0')), int(s_date[6:8].lstrip('0')))
    except:
        pass
    try:
        ed = datetime.datetime(int(e_date[0:4]), int(e_date[4:6].lstrip('0')), int(e_date[6:8].lstrip('0')))
    except:
        return '%s is not a valid date.' %form['end_date']
    try:
        if ed < sd:
            return 'Start Date is later then End Year.'
    except:pass

    #Check grid data dates
    if 'location' in form.keys() or ('data_type' in form.keys() and form['data_type'] == 'grid'):
        flag = False
        #Set grid date range
        #Set grid date range
        if int(form['grid']) in range(22,42):
            grid_dr = [['19500101','20991231']]
        else:
            grid_dr = WRCCData.GRID_CHOICES[str(form['grid'])][3]
        #For Prism we need to check if monthy/yearly resolution
        #and pick proper daterange
        if 'temporal_resolution' in form.keys() and form['temporal_resolution'] in ['mly','yly'] and str(form['grid']) == '21':
            grid_dr = WRCCData.PRISM_MLY_YLY[str(form['grid'])][3]
        for dr in grid_dr:
            grid_s = WRCCUtils.date_to_datetime(dr[0])
            grid_e = WRCCUtils.date_to_datetime(dr[1])
            if grid_s <= sd and ed <= grid_e:
                flag = False
                break
            else:
                if str(dr[1]) == str(grid_dr[-1][1]):
                    flag = True
                continue
        if flag:
            grid_s = WRCCUtils.datetime_to_date(grid_s,'-')
            grid_e = WRCCUtils.datetime_to_date(grid_e,'-')
            return 'Valid date range for this grid is: ' + grid_s + ' - ' + grid_e
        '''
        #Limit grid requests to max 10 years for multi point requests
        if not 'location' in form.keys() and (ed - sd).days > 10 * 366:
            err = 'Request for more than one grid point are limited to ten years or less! ' +\
            'Please adjust your dates accordingly.'
            return err
        '''
    #Check for unreasonable start and end dates
    #for station data requests
    data_type = WRCCUtils.get_data_type(form)
    unreasonable = False
    if data_type == 'station' and form['app_name'] == 'multi_lister':
        #Limit multi station requests to 75 years
        if not 'station_id' in form.keys() and (ed - sd).days > 75 * 366:
            err = 'Request for more than one station are limited to 75 years or less! ' +\
            'Please adjust your dates accordingly.'
    #Check that station data end date is today or earlier
    if 'station_id' in form.keys() or ('data_type' in form.keys() and form['data_type'] == 'station'):
        if e_date.lower() != 'por':
            today = WRCCUtils.format_date_string(WRCCUtils.set_back_date(0),'-')
            if WRCCUtils.date_to_datetime(e_date) >  WRCCUtils.date_to_datetime(today):
                return 'Not a valid End Date. Date should be ' + today +' or ealier.'

    return err
コード例 #3
0
def check_start_date(form):
    err = None
    s_date = form['start_date']
    e_date = form['end_date']
    if s_date.lower() == 'por':
        if 'station_id' in form.keys():
            return err
        else:
            return '%s is not a valid option for a multi-station or grid request.' %form['start_date']
    s_date = WRCCUtils.date_to_eight(s_date)
    e_date = WRCCUtils.date_to_eight(e_date)
    #Valid daterange check
    if len(s_date)!=8:
        return '%s is not a valid date.' %str(form['start_date'])
    try:
        int(s_date)
    except:
        return '%s is not a valid date.' %str(form['start_date'])

    #Check month
    if int(s_date[4:6]) < 1 or int(s_date[4:6]) > 12:
        return '%s is not a valid date.' %str(form['start_date'])
    #Check day
    if int(s_date[6:8]) < 1 or int(s_date[4:6]) > 31:
        return '%s is not a valid date.' %str(form['start_date'])

    #Check for month lengths
    ml = WRCCData.MONTH_LENGTHS[int(s_date[4:6]) - 1]
    if int(s_date[6:8]) > ml:
        if str(s_date[4:6]) == '02' or str(s_date[4:6]) == '2':
            if int(s_date[6:8]) == 29 and  WRCCUtils.is_leap_year(s_date[0:4]):
                return None
            else:
                return '%s only has %s days. You entered: %s' %(WRCCData.NUMBER_TO_MONTH_NAME[str(s_date[4:6])],str(ml),str(s_date[6:8]))
        else:
            return '%s only has %s days. You entered: %s' %(WRCCData.NUMBER_TO_MONTH_NAME[str(s_date[4:6])],str(ml),str(s_date[6:8]))

    #Check for leap year issue
    if not WRCCUtils.is_leap_year(s_date[0:4]) and s_date[4:6] == '02' and s_date[6:8] == '29':
        return '%s is not a leap year. Change start date to February 28.' %str(s_date[0:4])

    #Check that start date is earlier than end date
    if e_date.lower() == 'por':
        return err
    sd = '9999-99-99'
    ed = '9999-99-99'
    try:
        sd = datetime.datetime(int(s_date[0:4]), int(s_date[4:6]), int(s_date[6:8]))
    except:
        return '%s is not a valid date.' %str(form['start_date'])

    try:
        ed = datetime.datetime(int(e_date[0:4]), int(e_date[4:6]), int(e_date[6:8]))
        if ed < sd:
            return 'Start Date is later then End Year.'
    except:
        #return 'End date %s is not a valid date.' %str(form['end_date'])
        pass


    #Check grid data dates
    if 'location' in form.keys() or ('data_type' in form.keys() and form['data_type'] == 'grid'):
        flag = False
        #Set grid date range
        #Set grid date range
        if int(form['grid']) in range(22,42):
            grid_dr = [['19500101','20991231']]
        else:
            grid_dr = WRCCData.GRID_CHOICES[str(form['grid'])][3]
        #For Prism we need to check if monthy/yearly resolution
        #and pick proper daterange
        if 'temporal_resolution' in form.keys() and form['temporal_resolution'] in ['mly','yly'] and str(form['grid']) == '21':
            grid_dr = WRCCData.PRISM_MLY_YLY[str(form['grid'])][3]
        for dr in grid_dr:
            grid_s = WRCCUtils.date_to_datetime(dr[0])
            grid_e = WRCCUtils.date_to_datetime(dr[1])
            if grid_s <= sd and ed <= grid_e:
                flag = False
                break
            else:
                if str(dr[1]) == str(grid_dr[-1][1]):
                    flag = True
                continue
        if flag:
            grid_s = WRCCUtils.datetime_to_date(grid_s,'-')
            grid_e = WRCCUtils.datetime_to_date(grid_e,'-')
            return 'Valid date range for this grid is: ' +str(grid_s) + ' - ' + str(grid_e)

        '''
        #Limit grid requests to max 10 years for multi point requests
        if not 'location' in form.keys() and (ed - sd).days > 10 * 366:
            err = 'Request for more than one grid point are limited to ten years or less! ' +\
            'Please adjust your dates accordingly.'
            return err
        '''

    #Check for unreasonable start and end dates
    #for station data requests
    data_type = WRCCUtils.get_data_type(form)
    unreasonable = False
    if data_type == 'station':
        #Limit multi station requests to 75 years
        if not 'station_id' in form.keys() and (ed - sd).days > 75 * 366:
            err = 'Request for more than one station are limited to 75 years or less! ' +\
            'Please adjust your dates accordingly.'
            return err
        unreasonable = False
        if s_date.lower() !='por' and int(s_date[0:4]) <= 1850:
            unreasonable = True
        if unreasonable:
            meta_params = {
                WRCCData.FORM_TO_META_PARAMS[form['area_type']]: form[form['area_type']],
                'elems':','.join(form['variables']),
                'meta':'valid_daterange'
            }
            if 'pet' in form['variables'] or 'dtr' in form['variables']:
                meta_params['elems'].replace('pet','maxt,mint')
                meta_params['elems'].replace('dtr','maxt,mint')
            #meta_data = AcisWS.StnMeta(meta_params)
            try:
                meta_data = AcisWS.StnMeta(meta_params)
            except:
                meta_data = {'meta':[]}
            start_dts = [];end_dts = []
            if meta_data and 'meta' in meta_data.keys():
                for stn_meta in meta_data['meta']:
                    for el_vd in stn_meta['valid_daterange']:
                        if el_vd and len(el_vd) == 2:
                            start_dts.append(WRCCUtils.date_to_datetime(el_vd[0]))
                            end_dts.append(WRCCUtils.date_to_datetime(el_vd[1]))

            if start_dts and end_dts:
                start = min(start_dts)
                end = max(end_dts)
                if start > WRCCUtils.date_to_datetime(s_date) and  end < WRCCUtils.date_to_datetime(e_date):
                    s = WRCCUtils.datetime_to_date(start,'-')
                    e = WRCCUtils.datetime_to_date(end,'-')
                    err = 'Please change Start and End Date to earliest/latest record found: ' +\
                     s + ', ' + e
                    return err
            if start_dts:
                start = min(start_dts)
                if start > WRCCUtils.date_to_datetime(s_date):
                    s = WRCCUtils.datetime_to_date(start,'-')
                    err = 'Please change Start Date to earliest record found: ' + s
                    return err

    #Check station data start date for single station requesrs
    if 'station_id' in form.keys():
        if int(s_date[0:4]) < int(stn_earliest[0:4]):
            return 'Not a valid Start Date. Year must be later than %s.' %(stn_earliest[0:4])


    return err
コード例 #4
0
#!/usr/bin/python

'''
Module WRCCFormCheck

Checks input form parameters
'''
import datetime
import re
import WRCCData, WRCCUtils, AcisWS

today = datetime.datetime.today()
stn_earliest = '18250101'
stn_earliest_dt = WRCCUtils.date_to_datetime(stn_earliest)

def check_start_year(form):
    err = None
    yr = form['start_year']
    #Check for valid daterange error
    if yr == '':
        return 'No valid start year could be found for this station!'
    if yr.lower() == 'por':
        if 'location' in form.keys():
            return 'POR is not a valid year for gridded data.'
        return err
    else:
        return err
    if len(yr)!=4:
        return 'Year should be of form yyyy. You entered %s' %yr
    try:
        int(yr)