Created
November 10, 2020 12:02
-
-
Save kclosu/c394d3652ab57ed02dd1d4a5a4790089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import datetime | |
import logging | |
import requests | |
import xarray as xr | |
logging.basicConfig(level=logging.DEBUG) | |
_variables = ['air', 'uwnd', 'vwnd', 'rhum'] | |
_year = datetime.datetime.now().year | |
_filenamepattern = os.path.join(os.path.dirname(__file__), 'datasets', '{variable}.{year}.nc') | |
def lastmodified(file): | |
lastmodified = os.stat(file).st_mtime | |
return datetime.date.fromtimestamp(lastmodified) | |
def update_file(variable, year=_year): | |
filename = _filenamepattern.format(variable=variable, year=year) | |
base_url = 'https://www.esrl.noaa.gov/psd/thredds/fileServer/Datasets/ncep' | |
if 'gauss' in variable: | |
base_url = 'https://www.esrl.noaa.gov/psd/thredds/fileServer/Datasets/ncep.reanalysis/surface_gauss' | |
url = '{base_url}/{variable}.{year}.nc'.format(base_url=base_url, variable=variable, year=year) | |
r = requests.get(url) | |
r.raise_for_status() | |
open(filename , 'wb').write(r.content) | |
def update_files(variables=_variables, year=_year): | |
for v in variables: | |
update_file(v, year) | |
def load_weather(variables=_variables, year=_year): | |
wds = None | |
for v in variables: | |
logging.debug(v) | |
f = _filenamepattern.format(variable=v, year=year) | |
logging.debug(f) | |
if not os.path.isfile(f) or (year==_year and lastmodified(f) != datetime.date.today()): | |
update_file(v, year) | |
ds = xr.open_dataset(f) | |
ds = ds.sel(time=~ds.indexes['time'].duplicated()) | |
if not wds: | |
wds = ds | |
else: | |
wds = xr.merge([wds, ds]) | |
return wds | |
def point_time_weather(wds, lat, lon, time): | |
point = wds.sel(lon=lon, lat=lat, level=1000, method='nearest') | |
p1w = point.rolling(time=7).mean() | |
p2w = point.rolling(time=14).mean() | |
p3w = point.rolling(time=21).mean() | |
v = point.sel(time=time) | |
v1w = p1w.sel(time=time) | |
v2w = p2w.sel(time=time) | |
v3w = p3w.sel(time=time) | |
return { | |
'temperature': v.air.values.item(0), | |
'humidity': v.rhum.values.item(0), | |
'vwind': v.vwnd.values.item(0), | |
'uwind': v.uwnd.values.item(0), | |
't1w': v1w.air.values.item(0), | |
't2w': v2w.air.values.item(0), | |
't3w': v3w.air.values.item(0), | |
'h1w': v1w.rhum.values.item(0), | |
'h2w': v2w.rhum.values.item(0), | |
'h3w': v3w.rhum.values.item(0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
sys.path.insert(1, os.path.join(sys.path[0], '..')) | |
import datetime | |
import pickle | |
import xarray as xr | |
import getncepreanalisys | |
from config import Config | |
configuration = Config() | |
variables = configuration.get('weather.variables') | |
def main(): | |
""" | |
Скачивает файлы погоды NCEP Reanalisys II | |
""" | |
for year in [1998]: | |
for v in variables: | |
f = getncepreanalisys._filenamepattern.format(variable=v, year=year) | |
if not os.path.isfile(f): | |
getncepreanalisys.update_file(v, year) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment