-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
44 lines (40 loc) · 1.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import numpy as np
import xarray as xr
import pandas as pd
from xclim import ensembles
from xclim.ensembles import create_ensemble
from geopy.geocoders import Nominatim
def get_coords(city:str):
'''
get lat, lon from city
see: https://geopy.readthedocs.io/en/stable/#nominatim
'''
geolocator = Nominatim(user_agent='http')
location = geolocator.geocode(city)
latitude, longitude = location.latitude, location.longitude
print("Location, (lat, lon): ",location, (latitude, longitude))
return (latitude, longitude)
def load_mf_dataset(path, models:list):
'''
load xr files into a dict of a multi-file dataset per model
'''
model_files = {}
for model in models.split(","):
model_filenames=[]
for filename in os.listdir(path):
if model in filename:
model_filenames.append(filename)
model_files[model] = model_filenames
os.chdir(path)
data = {}
for model, files in model_files.items():
print(model, len(files))
data[model] = xr.open_mfdataset(files, engine='netcdf4', chunks={'time': 120})
return data
def multimodel_ensemble(data):
''' given a dict of models i.e. model_name[data]=dataset, create an xclim ensemble
'''
ensemble = create_ensemble([model for model in data.values()]).load()
ensemble.close()
return ensemble