import numpy as np
import pandas as pd
from . import add_default
from ......utils.check.errclass import CifError
[docs]
def init_sparse(
self,
trid,
precursor_dict, ref_dict,
tr, transform,
param,
all_transforms,
mapper,
backup_comps,
precursors,
do_pipe_entry=False
):
cmp, prm = trid
precursor_id = tr
successor_id = None
# If precursor is sparse,
# project sparse to array
if (
precursor_dict.get("sparse_data", False)
and not ref_dict.get("sparse_data", False)
and ref_dict.get("domain", None) is not None
):
precursor_id = tr
yml_dict = {
"plugin": {
"name": "sparse2sample",
"version": "std",
"type": "transform",
},
"component": cmp,
"parameter": prm,
"successor": transform,
"precursor": tr,
}
ref_precursor = {(cmp, prm): tr}
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
)
precursor_id = new_id
# Stop here as recursive init_default
# will take care of other dimensions
return precursor_id
# If successor is sampled, should not happen
if ref_dict.get("sampled", False):
raise CifError("Sparse to sample should not happen")
# Sample from complete field to sparse data
# Do it only if successor_id is not None,
# which means that indexing has been computed
if (
not precursor_dict.get("sampled", False)
and not precursor_dict.get("sparse_data", False)
and ref_dict.get("sparse_data", False)
and precursor_dict.get("domain", None) is not None
):
precursor_id = tr
yml_dict = {
"plugin": {
"name": "array2sampled",
"version": "std",
"type": "transform",
},
"component": cmp,
"parameter": prm,
"successor": transform,
"precursor": tr,
}
ref_precursor = {(cmp, prm): tr}
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
)
precursor_id = new_id
# Stop here as recursive init_default
# will take care of other dimensions
return precursor_id
# Reindex dates
precursor_dates = precursor_dict.get("input_dates", {})
if precursor_dates is None:
precursor_dates = {}
target_dates = ref_dict["input_dates"]
if target_dates is None:
target_dates = {}
different_items = \
[k not in target_dates
or not precursor_dates.get(k, pd.DataFrame()).reset_index(drop=True)
.equals(target_dates.get(k, pd.DataFrame()).reset_index(drop=True))
if len(precursor_dates.get(k, pd.DataFrame())) == len(target_dates.get(k, pd.DataFrame())) != 0
else len(precursor_dates.get(k, pd.DataFrame())) != 0 or len(target_dates.get(k, pd.DataFrame())) != 0
for k in precursor_dates]
sparse_in = precursor_dict.get("sparse_data", False)
sparse_out = ref_dict.get("sparse_data", False)
if (np.sum(different_items) > 0) \
and precursor_dates != {}:
# Temporal re-indexing if any
tinterp = getattr(param, "time_interpolation", None)
yml_dict = {
"plugin": {
"name": "time_interpolation",
"version": "std",
"type": "transform",
},
"method": getattr(tinterp, "method", "bilinear"),
"component": [cmp],
"parameter": [prm],
"successor": transform,
"precursor": precursor_id,
"sparse_in": False,
"sampled_in": False,
"sparse_out": False,
"sampled_out": True,
**{attr: getattr(tinterp, attr)
for attr in getattr(tinterp, "attributes", [])
if attr != "plugin"}
}
ref_precursor = {(cmp, prm): precursor_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
)
precursor_id = new_id
if successor_id is None:
successor_id = new_id
# Stop here as recursive init_default
# will take care of other dimensions
return precursor_id
# Vertical interpolation if needed
do_vinterp = (
not precursor_dict.get("sparse_data", False)
and not precursor_dict.get("continuous_vdomain", False)
and not ref_dict.get("continuous_vdomain", False)
and precursor_dict.get("domain", None) is not None
)
if do_vinterp:
vinterp = getattr(param, "vertical_interpolation", None)
yml_dict = {
"plugin": {
"name": "vertical_interpolation",
"version": "std",
"type": "transform",
},
"method": getattr(vinterp, "method", "static-levels"),
"component": cmp,
"parameter": prm,
"successor": transform,
"precursor": precursor_id,
"sparse_in": False,
"sampled_in": False,
"sparse_out": False,
"sampled_out": True,
**{attr: getattr(vinterp, attr)
for attr in getattr(vinterp, "attributes", [])
if attr != "plugin"}
}
ref_precursor = {(cmp, prm): precursor_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
)
precursor_id = new_id
if successor_id is None:
successor_id = new_id
# Stop here as recursive init_default
# will take care of other dimensions
return precursor_id
# Reprojects if domain is different
do_regrid = (
not precursor_dict.get("sparse_data", False)
and not precursor_dict.get("continuous_hdomain", False)
and not ref_dict.get("continuous_hdomain", False)
and precursor_dict.get("domain", None) is not None
)
if do_regrid:
regrid = getattr(
param, "regrid",
getattr(ref_dict.get("tracer", None), "regrid", None))
yml_dict = {
"plugin": {
"name": "regrid",
"version": "std",
"type": "transform",
},
"method": getattr(
regrid, "method",
"gridcell"
),
"component": [cmp],
"parameter": [prm],
"successor": transform,
"precursor": precursor_id,
"sparse_in": False,
"sampled_in": False,
"sparse_out": False,
"sampled_out": True,
**{attr: getattr(regrid, attr)
for attr in getattr(regrid, "attributes", []) if attr != "plugin"}
}
ref_precursor = {(cmp, prm): precursor_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
)
precursor_id = new_id
if successor_id is None:
successor_id = new_id
# Stop here as recursive init_default
# will take care of other dimensions
return precursor_id
# # Project sparse data to array
# if precursor_dict.get("sparse_data", False) \
# and not ref_dict.get("sparse_data", False):
# raise Exception("Is it still used?")
# if cmp == "stratosphere":
# print("HHHHHHHHHHHHHHHHHHHHH")
# print(__file__)
# import code
# code.interact(local=dict(locals(), **globals()))
# yml_dict = {
# "plugin": {
# "name": "sparse2sample",
# "version": "std",
# "type": "transform",
# },
# "component": [cmp],
# "parameter": [prm],
# "successor": transform,
# "precursor": precursor_id,
# }
# ref_precursor = {(cmp, prm): precursor_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
# )
# precursor_id = new_id
# print("FFFFFFFFFFFFFFFF", tr, transform)
# return precursor_id