Exemplo n.º 1
0
    def weatherFileTimespan(self, year, month, timezone='UTC'):
        """
        Returns tuple with start hour and end hour for the monthly file.
        If timezone is specified, times will be adjust to that timezone.
        Otherwise, times are in UTC timezones (same as times in file).
        """
        start_time = datetime.datetime(year, month, 1, 0)
        start_time = tzutils.tzaDatetime(start_time, 'UTC')
        last_day = lastDayOfMonth(year, month)
        end_time = datetime.datetime(year, month, last_day, 23)
        end_time = tzutils.tzaDatetime(end_time, 'UTC')

        if timezone != 'UTC':
            start_time = tzutils.asLocalTime(start_time, timezone)
            end_time = tzutils.asLocalTime(end_time, timezone)

        return start_time, end_time
Exemplo n.º 2
0
    def slices(self, slice_start_time, slice_end_time):

        slices = [ ]
        start_time = tzutils.tzaDatetime(slice_start_time, self.tzinfo)
        end_time = tzutils.tzaDatetime(slice_end_time, self.tzinfo)

        if start_time.month == end_time.month:
            slices.append((start_time, end_time))
        else:
            last_day = lastDayOfMonth(start_time)
            month_end = start_time.replace(day=last_day, hour=23)
            slices.append((start_time, month_end))
            
            month = start_time.month + 1
            while month < end_time.month:
                month_start = start_time.replace(month=month, day=1, hour=0)
                last_day = lastDayOfMonth(month_start)
                month_end = month_start.replace(day=last_day, hour=23)
                slices.append((month_start, month_end))
            
            slices.append((end_time.replace(day=1, hour=0), end_time))

        return tuple(slices)
Exemplo n.º 3
0
    def timeSlice(self, variable, slice_start_time, slice_end_time, **kwargs):
        debug = kwargs.get('debug', False)
        failed = []

        if self.grib_indexes is None: self._initStaticResources_()

        region = kwargs.get('region', self.grib_region)

        grib_start_time = tzutils.tzaDatetime(slice_start_time, self.tzinfo)
        if slice_end_time > slice_start_time:
            grib_end_time = tzutils.tzaDatetime(slice_end_time, self.tzinfo)

            # a requested end time is not necessarily available
            # so strip off missing hours from end of time span
            while grib_end_time >= grib_start_time:
                filepath = self.gribFilepath(grib_end_time, variable, region)
                if os.path.exists(filepath): break
                grib_end_time -= ONE_HOUR

            num_hours = tzutils.hoursInTimespan(grib_start_time, grib_end_time)
            data = N.empty((num_hours, ) + self.grid_dimensions, dtype=float)
            data.fill(N.nan)

            units = None
            date_indx = 0
            grib_time = grib_start_time
            while units is None and grib_time <= grib_end_time:
                success, package = self.dataFromGrib(variable,
                                                     grib_time,
                                                     return_units=True,
                                                     **kwargs)
                if success:
                    units, data_for_hour = package
                    data[date_indx, :, :] = data_for_hour
                else:
                    failed.append(package)

                grib_time += ONE_HOUR
                date_indx += 1

            while grib_time <= grib_end_time:
                OK, package = self.dataFromGrib(variable, grib_time, **kwargs)
                if OK: data[date_indx, :, :] = package
                else: failed.append(package)

                grib_time += ONE_HOUR
                date_indx += 1

        else:
            success, package = self.dataFromGrib(variable,
                                                 grib_start_time,
                                                 return_units=True,
                                                 **kwargs)
            if not success:
                data = N.empty(self.grid_dimensions, dtype=float)
                units = None
                failed.append(package)
            else:
                units, data = package

        return units, data, tuple(failed)