Click here to download this notebook.

How to add custom data to NadirData

NadirData.data attribute allows to access and enrich the database used by this object [doc].

The following example illustrates how it can be used.

Consider you want to work on a customized selection flag not yet computed in your data source.

[1]:
import os

import xarray as xr

from casys import NadirData, CasysPlot, DataParams, Field

NadirData.enable_loginfo()

Create and/or load your data

[2]:
# Using the model's computed NadirData object.
ad = NadirData.load(os.environ["DOC_MODEL"])

ds = ad.data
ds
[2]:
<xarray.Dataset> Size: 33MB
Dimensions:         (time: 582789)
Coordinates:
  * time            (time) datetime64[ns] 5MB 2019-06-01T05:30:29.807497 ... ...
Data variables:
    LONGITUDE       (time) float64 5MB 17.01 17.14 17.27 ... -123.5 -123.3
    LATITUDE        (time) float64 5MB -66.15 -66.15 -66.15 ... 66.15 66.15
    wind            (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    SLA             (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    SIGMA0.ALTI     (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    EDITING.CUSTOM  (time) int64 5MB 1 1 1 1 1 1 2 1 1 1 ... 1 1 1 1 1 1 1 1 1 1

Compute a new field

You can use any algorithm/custom function here using any external or existing data as parameters.

The only constraint is to be able to make the result fit in your dataset.

[3]:
# Defining a powerful selection function
def lat_selection(data):
    return xr.where(data["LATITUDE"] > 0, 1, 0)


# Generating the new flag and storing it in our dataset
ds["new_flag"] = lat_selection(ds)

Put everything back into your NadirData

Each new field will be considered as a raw data using the field’s name.

From the outside this operation is similar to ad.add_raw_data(name="new_flag", field=Field("new_flag").

[4]:
# Setting everything back into our NadirData object
ad.data = ds

Play with your new fields

We can now use the merged fields as any other:

  • To compute a statistic

  • To create new fields usings CLIPs

[5]:
# Note: We're using the Python CLIP syntax here (might be confusing but you'll get used to it !).
sla_select = Field(name="SLA_SEL", source="IIF(new_flag:==1, SLA, DV)")
var_wtc_rad_sel = Field(
    name="WTC_SEL",
    source="IIF(new_flag:==1 && FLAG_VAL.ALTI:==0,"
    "WET_TROPOSPHERIC_CORRECTION.RAD, DV)",
)

# These new fields can now be used like any other
ad.add_raw_data(name="SLA selected", field=sla_select)
ad.add_raw_data(name="WTC selected", field=var_wtc_rad_sel)

ad.add_time_stat(name="WTC sel stat", field=var_wtc_rad_sel, freq="15min", stats=["mean"])
[6]:
ad.compute()
2025-05-14 10:59:36 INFO    Reading ['WET_TROPOSPHERIC_CORRECTION.RAD', 'FLAG_VAL.ALTI']
2025-05-14 10:59:39 INFO    Reading ['WTC_SEL', 'SLA_SEL'] (external source)
2025-05-14 10:59:39 INFO    Computing diagnostics ['WTC sel stat']
2025-05-14 10:59:39 INFO    Computing done.
[7]:
ad.data
[7]:
<xarray.Dataset> Size: 47MB
Dimensions:         (time: 582789)
Coordinates:
  * time            (time) datetime64[ns] 5MB 2019-06-01T05:30:29.807497 ... ...
Data variables:
    LONGITUDE       (time) float64 5MB 17.01 17.14 17.27 ... -123.5 -123.3
    LATITUDE        (time) float64 5MB -66.15 -66.15 -66.15 ... 66.15 66.15
    wind            (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    SLA             (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    SIGMA0.ALTI     (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    EDITING.CUSTOM  (time) int64 5MB 1 1 1 1 1 1 2 1 1 1 ... 1 1 1 1 1 1 1 1 1 1
    new_flag        (time) int64 5MB 0 0 0 0 0 0 0 0 0 0 ... 1 1 1 1 1 1 1 1 1 1
    WTC_SEL         (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
    SLA_SEL         (time) float64 5MB nan nan nan nan nan ... nan nan nan nan
[8]:
cp_sla = CasysPlot(
    data=ad, data_name="SLA selected", data_params=DataParams(remove_nan=False)
)
cp_sla.show()
[8]:
../_images/howto_model_alti_data_enrich_12_0.png
[9]:
cp_sla = CasysPlot(
    data=ad, data_name="SLA selected", data_params=DataParams(remove_nan=True)
)
cp_sla.show()
[9]:
../_images/howto_model_alti_data_enrich_13_0.png
[10]:
cp_wtc = CasysPlot(data=ad, data_name="WTC sel stat")
cp_wtc.show()
[10]:
../_images/howto_model_alti_data_enrich_14_0.png