Skip to content

tropomi_l2

Read TROPOMI data into MELODIES-MONET

apply_quality_flag(variable, netcdf_tropomi)

Applies quality_flags inplace

Parameters:

Name Type Description Default
variable str

Variable containing the attribute qa_thersh_min.

required
netcdf_tropomi Dataset

Dataset containing the netCDF4 tropomi file

required

Returns:

Type Description
DataArray

DataArray with applied quality flag

Source code in monetio/sat/tropomi_l2.py
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
def apply_quality_flag(variable, netcdf_tropomi):
    """Applies quality_flags inplace

    Parameters
    ----------
    variable : str
        Variable containing the attribute qa_thersh_min.
    netcdf_tropomi : nc4.Dataset
        Dataset containing the netCDF4 tropomi file

    Returns
    -------
    xr.DataArray
        DataArray with applied quality flag
    """
    assert "quality_flag" in variable.attrs, f"quality_flag not in {variable.name}"
    assert ("qa_thresh_min" in variable.attrs) or ("qa_thresh_max" in variable.attrs), (
        f"Neither qa_thresh_min nor qa_thresh_max in {variable.name}"
    )
    qa = _add_variable(variable.attrs["quality_flag"], netcdf_tropomi)
    if "qa_thresh_min" in variable.attrs:
        variable = variable.where(qa >= variable.attrs["qa_thresh_min"])
    if "qa_thresh_max" in variable.attrs:
        variable = variable.where(qa <= variable.attrs["qa_thresh_max"])
    return variable

ensure_increasing_altitude(ds)

Ensures that the altitude is increasing (i.e, the pressure should decrease as z increases)

Parameters:

Name Type Description Default
ds Dataset

Dataset with the satellite data. If pressure is not included, nothing will be done.

required

Returns:

Type Description
Dataset

Dataset with corrected pressure

Source code in monetio/sat/tropomi_l2.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def ensure_increasing_altitude(ds):
    """Ensures that the altitude is increasing (i.e, the pressure should
    decrease as z increases)

    Parameters
    ----------
    ds : xr.Dataset
        Dataset with the satellite data. If pressure is not included,
        nothing will be done.

    Returns
    -------
    xr.Dataset
        Dataset with corrected pressure
    """
    if ("pres_pa_mid" not in ds) and ("pres_pa_int" not in ds):
        warnings.warn("Missing pressure information. Ignoring vertical directionality check")
        return ds
    vertical_dim = {"pres_pa_mid": "z", "pres_pa_int": "z_stagg"}
    for pres_var, vert_dim in vertical_dim.items():
        if (ds[pres_var].isel(time=0).isel(**{vert_dim: slice(0, 10)}).diff(dim="z") > 0).any():
            ds = ds.sel(**{vert_dim: slice(None, None, -1)})
    return ds

open_datasets(all_files, variable_dict)

Creates a dict containing all the datasets

Parameters:

Name Type Description Default
all_files (str, list[str])

String or list of strings containing all the files that should be opened. Wildcards are supported.

required
variable_dict dict[str, dict]

Dictionary of dictionaries for each variable.

required

Returns:

Type Description
dict[str, Dataset]

Dictionary with time reference as keys and xr.Dataset containing satellite information as values.

Source code in monetio/sat/tropomi_l2.py
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
def open_datasets(all_files, variable_dict):
    """Creates a dict containing all the datasets

    Parameters
    ----------
    all_files : str, list[str]
        String or list of strings containing all the files that should
        be opened. Wildcards are supported.
    variable_dict : dict[str, dict]
        Dictionary of dictionaries for each variable.

    Returns
    -------
    dict[str, xr.Dataset]
        Dictionary with time reference as keys and xr.Dataset containing
        satellite information as values.
    """

    if isinstance(all_files, str):
        datasets = sorted(glob.glob(all_files))
    elif isinstance(all_files, list):
        datasets = []
        for ds in all_files:
            datasets.extend(glob.glob(ds))
        datasets = sorted(datasets)
    ds_collection = {}
    for data in datasets:
        d = _open_one_dataset(data, variable_dict)
        # Select time coverage start as key, removing the trailing Z
        ds_collection[d.attrs["time_coverage_start"].replace("Z", "")] = d
    return ds_collection

open_var_no_format(variable, netcdf_dataset)

Opens only one variable from a netCDF4 dataset

Parameters:

Name Type Description Default
variable str

Variable name

required
netcdf_dataset Dataset

nc4.Dataset with data to search

required

Returns:

Type Description
Variable

The variable object that was searched for

Source code in monetio/sat/tropomi_l2.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
def open_var_no_format(variable, netcdf_dataset):
    """Opens only one variable from a netCDF4 dataset

    Parameters
    ----------
    variable : str
        Variable name
    netcdf_dataset : nc4.Dataset
        nc4.Dataset with data to search

    Returns
    -------
    Variable
        The variable object that was searched for
    """
    path = _walktree_search(variable, netcdf_dataset)
    # Robustly navigate path
    parts = path.strip("/").split("/")
    varname = parts[-1]
    group_path = "/".join(parts[:-1])
    return get_nc_var(netcdf_dataset, group_path, varname)