Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Projections can be given as strings or instances of pyproj.Proj.
Special care is taken for the case where the final projection is
of type rotated pole as handled by RotProj.
"""
if p1 == p2:
return shapes
if isinstance(p1, RotProj):
if p2 == 'latlong':
reproject_points = lambda x,y: p1(x, y, inverse=True)
else:
raise NotImplementedError("`p1` can only be a RotProj if `p2` is latlong!")
if isinstance(p2, RotProj):
shapes = reproject(shapes, p1, 'latlong')
reproject_points = p2
else:
reproject_points = partial(pyproj.transform, as_projection(p1), as_projection(p2))
def _reproject_shape(shape):
return transform(reproject_points, shape)
if isinstance(shapes, pd.Series):
return shapes.map(_reproject_shape)
elif isinstance(shapes, dict):
return OrderedDict((k, _reproject_shape(v)) for k, v in iteritems(shapes))
else:
return list(map(_reproject_shape, shapes))
def __call__(self, x, y, inverse=False, **kw):
if inverse:
gx, gy = super(RotProj, self).__call__(x, y,
inverse=False, **kw)
return np.rad2deg(gx), np.rad2deg(gy)
else:
return super(RotProj, self).__call__(np.deg2rad(x),
np.deg2rad(y),
inverse=True, **kw)
import pandas as pd
import numpy as np
import xarray as xr
import pyproj
from six import iteritems
from itertools import groupby
from operator import itemgetter
import os
import glob
from ..config import cordex_dir
from ..gis import RotProj
# Model and Projection Settings
model = 'MPI-M-MPI-ESM-LR'
projection = RotProj(dict(proj='ob_tran', o_proj='latlong', lon_0=180,
o_lon_p=-162, o_lat_p=39.25))
def rename_and_clean_coords(ds):
ds = ds.rename({'rlon': 'x', 'rlat': 'y'})
# drop some coordinates and variables we do not use
ds = ds.drop((set(ds.coords) | set(ds.data_vars))
& {'bnds', 'height', 'rotated_pole'})
return ds
def prepare_data_cordex(fn, year, months, oldname, newname, xs, ys):
with xr.open_dataset(fn) as ds:
ds = rename_and_clean_coords(ds)
ds = ds.rename({oldname: newname})
ds = ds.sel(x=xs, y=ys)
if newname in {'influx', 'outflux'}: