You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Normally, xgcm does not keep the original vertical coordinate in the output. But I need all the data variables and pressure as data variable on a vertical altitude coordinate in my output.
I accomplished this by creating a (WIP) function:
importloggingimportnumpyasnpfromxgcmimportGridlogger=logging.getLogger(__name__)
# Set the default target resolution for transforming to altitude.TARGET_RESOLUTION=np.arange(0, 80000, 100)
defxgmc_transformation(
ds: xr.Dataset,
orig_coord: str="pressure",
target_coord: str="altitude",
target_resolution: np.ndarray=TARGET_RESOLUTION,
manual_method: str="auto",
) ->xr.Dataset:
""" Apply a coordinate transformation to the dataset. Parameters ---------- ds : xr.Dataset The input dataset. orig_coord : str, optional The original coordinate to transform from. Default is "pressure". target_coord : str, optional The target coordinate to transform to. Default is "altitude". target_resolution : np.ndarray, optional The target resolution to transform to. Default is np.arange(0, 80000, 100). manual_method : str, optional (WIP!) The chosen manual method for the transformation. Either lin or log. Default is auto. If the original vertical coordinate is pressure, auto chooses log. In this case, pressure will be prepared for the log/lin interpolation by np.log(pressure). It will get antilogged after the transformation. If the original coordinate is altitude, no log is applied. (FIX THIS!) Returns ------- xr.Dataset The transformed dataset. """ifmanual_method!="auto":
method=manual_methodeliforig_coord=="altitude":
method="linear"eliforig_coord=="pressure":
method="log"else:
msg="Method could not be determined"raiseValueError(msg)
ifmethod=="log":
ds[orig_coord] =np.log(ds[orig_coord])
logger.info(
"Tranform Dataset from %s to %s using %s transform",
orig_coord,
target_coord,
method,
)
# Add orig_coord along target_coord in the dataset, as it should be in the outputds[f"{orig_coord}_var"] =ds[orig_coord].broadcast_like(ds[target_coord])
grid=Grid(ds, coords={"Z": {"center": orig_coord}}, periodic=False)
transform_vars= [var_forvar_inds.data_varsifvar_!=target_coord]
transform_list= []
forvar_intransform_vars:
var_transformed=grid.transform(
ds[var_],
"Z",
target_resolution,
target_data=ds[target_coord],
)
transform_list.append(var_transformed)
out=xr.merge(transform_list)
# Rename orig_coord to the original name.out=out.rename({f"{orig_coord}_var": orig_coord})
ifmethod=="log":
out[orig_coord] =np.exp(out[orig_coord])
returnout
I am not sure if this is an especially elegant approach, especially the part where I do ds[f"{orig_coord}_var"] = ds[orig_coord].broadcast_like(ds[target_coord]), to insert pressure again into the ds as data variable along altitude.
Is what I want to accomplish maybe already implemented in some way in xgcm?
The text was updated successfully, but these errors were encountered:
I have an input ds on a vertical pressure coordinate:
I want to transform it to altitude, but also keep the pressure variable in my output. It should look like this then:
Normally,
xgcm
does not keep the original vertical coordinate in the output. But I need all the data variables and pressure as data variable on a vertical altitude coordinate in my output.I accomplished this by creating a (WIP) function:
I am not sure if this is an especially elegant approach, especially the part where I do
ds[f"{orig_coord}_var"] = ds[orig_coord].broadcast_like(ds[target_coord])
, to insert pressure again into the ds as data variable along altitude.Is what I want to accomplish maybe already implemented in some way in
xgcm
?The text was updated successfully, but these errors were encountered: