Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
start_date = end_date - timedelta(hours=days_from_today*24)
logger.info('get_weather_history_for_station: using default start_date %s', start_date)
given_start = False
# Get last update datetime
if not given_start:
try:
last_record = WeatherHistory.objects.filter(weather_station=weather_station).order_by('-as_of_datetime').first()
if last_record:
start_date = last_record.as_of_datetime + timedelta(minutes=1)
logger.info('get_weather_history_for_station: last record start_date %s', start_date)
except Exception as e:
logging.warning(e)
# Get weather station
station = eeweather.ISDStation(weather_station.weather_station_code)
# Make sure all dates are UTC
start_date = start_date.replace(tzinfo=pytz.UTC)
end_date = end_date.replace(tzinfo=pytz.UTC)
logger.info('get_weather_history_for_station: from %s to %s',
start_date, end_date)
temp_degC, warnings = station.load_isd_hourly_temp_data(
start_date,
end_date,
read_from_cache=False)
logger.info('get_weather_history_for_station: got %s / %s', temp_degC, warnings)
WeatherHistory.objects.filter(as_of_datetime__gte=start_date).filter(as_of_datetime__lte=end_date).delete()
for dt, deg_c in temp_degC.iteritems():
if dt < start_date:
continue
elif numpy.isnan(deg_c):
def __init__(self, usaf_id, normalized, use_cz2010):
self.usaf_id = usaf_id
self.station = eeweather.ISDStation(usaf_id)
self.normalized = normalized
self.use_cz2010 = use_cz2010
self.resource_loc = None
self._assign_station_type_and_loc(normalized, use_cz2010)
self.tempC = pd.Series(dtype=float)
self._load_station_index()
if station not in self.station_index:
message = (
"Station {} is not a CZ2010 station."
" See eemeter/resources/supported_cz2010_stations.json for a"
" complete list of stations."
.format(station)
)
raise ValueError(message)
index = pd.date_range("1900-01-01 00:00", "1900-12-31 23:00",
freq='H', tz=pytz.UTC)
return eeweather.load_cz2010_hourly_temp_data(
station, index[0], index[-1])
'''
# NOTE: This URL is hardcoded but the data may not always be available
def _get_temperature_data_eeweather(usaf_id, start,
end, normalized, use_cz2010):
if normalized:
if use_cz2010:
tempC = eeweather.load_cz2010_hourly_temp_data(usaf_id, start, end)
else:
tempC = eeweather.load_tmy3_hourly_temp_data(usaf_id, start, end)
else:
tempC = eeweather.load_isd_hourly_temp_data(usaf_id, start, end)
return tempC
def load_isd_data_eeweather(self, station, start, end):
return eeweather.load_isd_hourly_temp_data(
station, start, end)
def _get_temperature_data_eeweather(usaf_id, start,
end, normalized, use_cz2010):
if normalized:
if use_cz2010:
tempC = eeweather.load_cz2010_hourly_temp_data(usaf_id, start, end)
else:
tempC = eeweather.load_tmy3_hourly_temp_data(usaf_id, start, end)
else:
tempC = eeweather.load_isd_hourly_temp_data(usaf_id, start, end)
return tempC
def _get_temperature_data_eeweather(usaf_id, start,
end, normalized, use_cz2010):
if normalized:
if use_cz2010:
tempC = eeweather.load_cz2010_hourly_temp_data(usaf_id, start, end)
else:
tempC = eeweather.load_tmy3_hourly_temp_data(usaf_id, start, end)
else:
tempC = eeweather.load_isd_hourly_temp_data(usaf_id, start, end)
return tempC
def get_hourly_weather_normal_data(self, station):
self._load_station_index()
if station not in self.station_index:
message = (
"Station {} is not a TMY3 station."
" See eemeter/resources/supported_tmy3_stations.json for a"
" complete list of stations."
.format(station)
)
raise ValueError(message)
index = pd.date_range("1900-01-01 00:00", "1900-12-31 23:00",
freq='H', tz=pytz.UTC)
return eeweather.load_tmy3_hourly_temp_data(
station, index[0], index[-1])
'''
url = (
station_name = row[2]
country = row[3]
state = row[4]
icao = row[5]
latitude = parse_float(row[6])
longitude = parse_float(row[7])
elevation = parse_float(row[8])
elevation_uom_id = 'length_m'
weather_station_id = 'USAF_' + usaf_id
weather_station_code = usaf_id
# note ensure eemeter has metadata for it
# else fetching data would fail anyway ..
try:
get_isd_station_metadata(usaf_id)
except Exception:
print('-- IGNORE station without eemeter metadata: ', usaf_id)
try:
c.execute("""DELETE FROM core_weather_station where weather_station_code = %s""",
[weather_station_code]
)
except Exception:
pass
continue
try:
c.execute("""INSERT INTO core_weather_station (weather_station_id, weather_station_code,
station_name, country, state, call, latitude, longitude, elevation, elevation_uom_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
[weather_station_id, weather_station_code,
station_name, country, state, icao, latitude, longitude, elevation, elevation_uom_id]
def get_weather_station_for_location(latitude, longitude, as_object=True):
ranked_stations = eeweather.rank_stations(latitude, longitude)
for i in range(len(ranked_stations)):
try:
ranked_station = ranked_stations.iloc[i]
if as_object:
return WeatherStation.objects.get(weather_station_code=ranked_station.name)
else:
return ranked_station.name
except Exception as e:
logging.error(e)