import numpy as np
from . import add_default
[docs]
def init_regrid(
self,
trid,
tmp_dict, trid_dict,
precursor_id, transform,
param,
all_transforms,
mapper,
backup_comps,
precursors,
return_last=True,
do_pipe_entry=False
):
cmp, prm = trid
local_pipe = [precursor_id, transform]
did_nothing = True
# Reprojects if domain is different
tmp_dom = tmp_dict["domain"]
trid_dom = trid_dict["domain"]
same_hdomain = (
np.all(tmp_dom.zlon == trid_dom.zlon)
and np.all(tmp_dom.zlat == trid_dom.zlat)
) if tmp_dom.zlon.shape == trid_dom.zlon.shape \
and tmp_dom.zlat.shape == trid_dom.zlat.shape \
else False
same_is_lbc = (
trid_dict.get("is_lbc", False) == tmp_dict.get("is_lbc", False)
)
new_id = precursor_id
local_pipe_index = local_pipe.index(transform)
if not same_hdomain:
regrid = getattr(param, "regrid", None)
yml_dict = {
"plugin": {
"name": "regrid",
"version": "std",
"type": "transform",
},
"method": getattr(regrid, "method", "bilinear"),
"target_lbc": trid_dict.get("is_lbc", False),
"component": [cmp],
"parameter": [prm],
"successor": transform,
"precursor": new_id,
**{attr: getattr(regrid, attr)
for attr in getattr(regrid, "attributes", []) if attr != "plugin"}
}
ref_precursor = {(cmp, prm): new_id}
ref_successor = {(cmp, prm): transform}
new_transf, new_id = add_default.add_default(
self,
all_transforms,
yml_dict,
position="index",
index=all_transforms.attributes.index(transform),
mapper=mapper,
init=True,
backup_comps=backup_comps,
precursor=ref_precursor,
successor=ref_successor,
do_pipe_entry=do_pipe_entry
)
local_pipe.insert(local_pipe_index, new_id)
did_nothing = False
return local_pipe, did_nothing
# Vertical interpolation if needed
has_vertical_in = \
hasattr(tmp_dom, "sigma_a") \
or hasattr(tmp_dom, "heights")
has_vertical_out = \
hasattr(trid_dom, "sigma_a") \
or hasattr(trid_dom, "heights")
same_is_top = trid_dict.get(
"is_top", False) == tmp_dict.get("is_top", False)
if has_vertical_in and has_vertical_out \
and not trid_dict.get("vdomain_from_previous", False):
same_vdomain = False
if hasattr(tmp_dom, "sigma_a") \
and hasattr(trid_dom, "sigma_a"):
same_vdomain = len(tmp_dom.sigma_a) \
== len(trid_dom.sigma_a)
if same_vdomain:
same_vdomain = np.allclose(tmp_dom.sigma_a,
trid_dom.sigma_a)
elif hasattr(tmp_dom, "heights") \
and hasattr(trid_dom, "heights"):
same_vdomain = len(tmp_dom.heights) \
== len(trid_dom.heights)
if same_vdomain:
same_vdomain = np.allclose(
tmp_dom.heights, trid_dom.heights)
if same_vdomain:
return local_pipe, did_nothing
vinterp = getattr(param, "vertical_interpolation", None)
yml_dict = {
"plugin": {
"name": "vertical_interpolation",
"version": "std",
"type": "transform",
},
"method": getattr(vinterp, "method", "linear"),
"target_top": trid_dict.get("is_top", False),
"component": cmp,
"parameter": prm,
"successor": transform,
"precursor": new_id,
**{attr: getattr(vinterp, attr)
for attr in getattr(vinterp, "attributes", []) if attr != "plugin"}
}
ref_precursor = {(cmp, prm): new_id}
ref_successor = {(cmp, prm): transform}
local_pipe_index = local_pipe.index(transform)
new_transf, new_id = add_default.add_default(
self,
all_transforms,
yml_dict,
position="index",
index=all_transforms.attributes.index(transform),
mapper=mapper,
init=True,
backup_comps=backup_comps,
precursor=ref_precursor,
successor=ref_successor,
do_pipe_entry=do_pipe_entry
)
local_pipe.insert(local_pipe_index, new_id)
did_nothing = False
return local_pipe, did_nothing