def __init__(self, id, created_at, updated_at, external_id, name, lon, lat, alt, rank): assert id is not None assert external_id is not None assert lon is not None assert lat is not None if lon < -180.0 or lon > 180.0: raise ValueError("'lon' value must be between -180 and 180") self._lon = float(lon) if lat < -90.0 or lat > 90.0: raise ValueError("'lat' value must be between -90 and 90") if alt is not None: if alt < 0.0: raise ValueError("'alt' value must not be negative") self.id = id self.created_at = created_at if self.created_at is not None: padded_created_at = self._format_micros(created_at) t = dt.strptime(padded_created_at, '%Y-%m-%dT%H:%M:%S.%fZ').replace( tzinfo=timeformatutils.UTC()) self.created_at = timeformatutils.timeformat(t, 'unix') self.updated_at = updated_at if self.updated_at is not None: padded_updated_at = self._format_micros(updated_at) t = dt.strptime(padded_updated_at, '%Y-%m-%dT%H:%M:%S.%fZ').replace( tzinfo=timeformatutils.UTC()) self.updated_at = timeformatutils.timeformat(t, 'unix') self.external_id = external_id self.name = name self.lon = lon self.lat = lat self.alt = alt self.rank = rank
def to_entity(self): """ Generates an Ozone object out of the current model :return: a pyowm.webapi25.ozone.Ozone instance """ return OzoneEntity( timeformatutils.timeformat(self.reference_time, 'unix'), Location.to_entity(self.location), self.interval, self.du_value, timeformatutils.timeformat(self.reception_time, 'unix'))
def to_entity(self): """ Generates a UVIndex object out of the current model :return: a pyowm.webapi25.uvindex.UVIndex instance """ return UVIndexEntity( timeformatutils.timeformat(self.reference_time, 'unix'), Location.to_entity(self.location), self.interval, self.value, timeformatutils.timeformat(self.reception_time, 'unix'))
def to_entity(self): """ Generates a COIndex object out of the current model :return: a pyowm.webapi25.coindex.COIndex instance """ return COIndexEntity( timeformatutils.timeformat(self.reference_time, 'unix'), Location.to_entity(self.location), self.interval, json.loads(self.co_samples), timeformatutils.timeformat(self.reception_time, 'unix'))
def to_entity(self): """ Generates a Weather object out of the current model :return: a pyowm.webapi25.weather.Weather instance """ return WeatherEntity( timeformatutils.timeformat(self.reference_time, 'unix'), timeformatutils.timeformat(self.sunset_time, 'unix'), timeformatutils.timeformat(self.sunrise_time, 'unix'), self.clouds, json.loads(self.rain), json.loads(self.snow), json.loads(self.wind), self.humidity, json.loads(self.pressure), json.loads(self.temperature), self.status, self.detailed_status, self.weather_code, self.weather_icon_name, self.visibility_distance, self.dewpoint, self.humidex, self.heat_index)
def test_timeformat(self): unixtime = 1378459200 iso = "2013-09-06 09:20:00+00" date = datetime(2013, 9, 6, 9, 20, 0) self.assertEqual(unixtime, timeformatutils.timeformat(unixtime, "unix")) self.assertEqual(iso, timeformatutils.timeformat(unixtime, "iso")) self.assertEqual(date, timeformatutils.timeformat(unixtime, "date")) self.assertEqual(unixtime, timeformatutils.timeformat(iso, "unix")) self.assertEqual(iso, timeformatutils.timeformat(iso, "iso")) self.assertEqual(date, timeformatutils.timeformat(iso, "date")) self.assertEqual(unixtime, timeformatutils.timeformat(date, "unix")) self.assertEqual(iso, timeformatutils.timeformat(date, "iso")) self.assertEqual(date, timeformatutils.timeformat(date, "date"))
def to_entity(self): """ Generates an Observation object out of the current model :return: a pyowm.webapi25.observation.Observation instance """ return ObservationEntity( timeformatutils.timeformat(self.reception_time, 'unix'), self.location.to_entity(), self.weather.to_entity())
def to_entity(self): """ Generates a StationHistory object out of the current model :return: a pyowm.webapi25.stationhistory.StationHistory instance """ data = {int(k): v for k, v in json.loads(self.measurements).items()} return StationHistoryEntity( self.station_id, self.interval, timeformatutils.timeformat(self.reception_time, 'unix'), data)
def to_entity(self): """ Generates a Forecast object out of the current model :return: a pyowm.webapi25.forecast.Forecast instance """ return ForecastEntity( self.interval, timeformatutils.timeformat(self.reception_time, 'unix'), self.location.to_entity(), [Weather.to_entity(w) for w in self.weathers.all()])
def acquisition_time(self, timeformat='unix'): """Returns the UTC time telling when the image data was acquired by the satellite :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` '*date* for ``datetime.datetime`` object instance :type timeformat: str :returns: an int or a str """ return timeformatutils.timeformat(self._acquisition_time, timeformat)
def downloaded_on(self, timeformat='unix'): """Returns the UTC time telling when the satellite image was downloaded from the OWM Agro API :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` '*date* for ``datetime.datetime`` object instance :type timeformat: str :returns: an int or a str """ return timeformatutils.timeformat(self._downloaded_on, timeformat)
def issued_on(self, timeformat='unix'): """Returns the UTC time telling when the query was performed against the OWM Agro API :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` '*date* for ``datetime.datetime`` object instance :type timeformat: str :returns: an int or a str """ return timeformatutils.timeformat(self.query_timestamp, timeformat)
def get_reference_time(self, timeformat='unix'): """Returns the GMT time telling when the weather was measured :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time or '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` :type timeformat: str :returns: an int or a str :raises: ValueError when negative values are provided """ return timeformatutils.timeformat(self._reference_time, timeformat)
def get_sunrise_time(self, timeformat='unix'): """Returns the GMT time of sunrise :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time or '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` :type timeformat: str :returns: an int or a str :raises: ValueError """ return timeformatutils.timeformat(self._sunrise_time, timeformat)
def get_reception_time(self, timeformat='unix'): """Returns the GMT time telling when the meteostation history data was received from the OWM web API :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time or '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` :type timeformat: str :returns: an int or a str :raises: ValueError """ return timeformatutils.timeformat(self._reception_time, timeformat)
def now(timeformat='date'): """ Returns the current time in the specified timeformat. :param timeformat: the target format for the time conversion. May be: '*date*' (default - outputs a ``datetime.datetime`` object), '*unix*' (outputs a long UNIXtime) or '*iso*' (outputs an ISO8601-formatted string with pattern ``YYYY-MM-DD HH:MM:SS+00``) :type timeformat: str :returns: the current time value :raises: ValueError when unknown timeformat switches are provided or when negative time values are provided """ return timeformatutils.timeformat(datetime.now(), timeformat)
def get_reference_time(self, timeformat='unix'): """ Returns the GMT time telling when the CO samples have been measured :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` '*date* for ``datetime.datetime`` object instance :type timeformat: str :returns: an int or a str :raises: ValueError when negative values are provided """ return timeformatutils.timeformat(self._reference_time, timeformat)
def get_reception_time(self, timeformat='unix'): """Returns the GMT time telling when the forecast was received from the OWM Weather API :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` '*date* for ``datetime.datetime`` object instance :type timeformat: str :returns: an int or a str :raises: ValueError """ return timeformatutils.timeformat(self._reception_time, timeformat)
def last_update_time(self, timeformat='unix'): """Returns the UTC time of the last update on this station's metadata :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time, '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` or `date` for a ``datetime.datetime`` object :type timeformat: str :returns: an int or a str or a ``datetime.datetime`` object or None :raises: ValueError """ if self.updated_at is None: return None return timeformatutils.timeformat(self.updated_at, timeformat)
def creation_time(self, timeformat='unix'): """Returns the UTC time of creation of this aggregated measurement :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time, '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` or `date` for a ``datetime.datetime`` object :type timeformat: str :returns: an int or a str or a ``datetime.datetime`` object or None :raises: ValueError """ if self.timestamp is None: return None return timeformatutils.timeformat(self.timestamp, timeformat)
def when_ends(self, timeformat='unix'): """ Returns the GMT time of the end of the forecast coverage, which is the time of the most recent *Weather* item in the forecast :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time or '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` :type timeformat: str :returns: a long or a str :raises: *ValueError* when invalid time format values are provided """ end_coverage = max([item.get_reference_time() \ for item in self._forecast]) return timeformatutils.timeformat(end_coverage, timeformat)
def when_starts(self, timeformat='unix'): """ Returns the GMT time of the start of the forecast coverage, which is the time of the most ancient *Weather* item in the forecast :param timeformat: the format for the time value. May be: '*unix*' (default) for UNIX time '*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` '*date* for ``datetime.datetime`` object instance :type timeformat: str :returns: a long or a str :raises: *ValueError* when invalid time format values are provided """ start_coverage = min([item.get_reference_time() \ for item in self._forecast]) return timeformatutils.timeformat(start_coverage, timeformat)