Beispiel #1
0
def parse_gridded_request(request):
    """
    Parses a request for a gridded forecast.
    """
    # takes the first 4 '|' separated fields, if fewer than
    # k exist the missing fields are replaced with None
    model_domain, grid_str, hours_str, variables = split_fields(request, 4)
    model, domain_str = model_domain.split(':')
    # parse the domain and make sure its ok
    domain = utils.parse_domain(domain_str)
    # parse the grid_string
    resol = utils.parse_resolution(grid_str)
    # parse the hours string
    hours = utils.parse_hours(hours_str)
    # check the variables
    if variables is None:
        variables = []
    else:
        variables = variables.split(',')
    variables = utils.validate_variables(variables)
    return {'type': 'gridded',
            'model': model.lower().strip(),
            'domain': domain,
            'resolution': resol,
            'hours': hours,
            'variables': variables}
Beispiel #2
0
    def test_parse_weekdays(self):
        """
        Tests if weekdays are parsed accordingly from user friendly
        formatted string.
        """
        input_str = [
            "Mon-Sun 11:00 am - 10 pm", "Mon-Fri, Sat 11 am - 12 pm ",
            "Sun 11 am - 4:30 am"
        ]
        expected_result = [
            (time(11, 0, 0), time(22, 0, 0)),
            (time(11, 0, 0), time(12, 0, 0)),
            (time(11, 0, 0), time(4, 30, 0)),
        ]

        output = []
        for i in input_str:
            output.append(parse_hours(i))

        for o, er in zip(output, expected_result):
            self.assertEqual(o, er)
Beispiel #3
0
def parse_spot_request(request):
    """
    parses a request for a spot forecast
    """
    model_domain, time_str, variables, image = split_fields(request, 4)
    spot, location_str = model_domain.split(':', 1)
    assert spot.lower() == 'spot'
    if ':' in location_str:
        model, location_str = location_str.split(':', 1)
        model = model.lower()
    else:
        model = 'gefs'
    location = utils.parse_location(location_str)
    # default to 4 days every three hours
    time_str = time_str or '5,6'
    # First try parsing the format `days,period`
    try:
      hours = utils.parse_times(time_str)
    except ValueError, e:
      # if that fails try the gridded format `hour0,hour1,...,hourn`
      hours = utils.parse_hours(time_str)