Skip to content

nasa_modis

NASA MODIS Reader

NASAMODISReader

Bases: GriddedReader

Reader for NASA MODIS HDF files.

Source code in monetio/readers/nasa_modis.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@register_reader("nasa_modis")
class NASAMODISReader(GriddedReader):
    """
    Reader for NASA MODIS HDF files.
    """

    def open_dataset(self, files: str | list[str], **kwargs) -> xr.Dataset:
        """
        Reads NASA MODIS swath data.

        Parameters
        ----------
        files : str | list[str]
            File path, list of paths, or glob pattern.
        **kwargs : Any
            Additional arguments passed to the Xarray driver.

        Returns
        -------
        xr.Dataset
            The processed NASA MODIS dataset.

        Examples
        --------
        >>> from monetio.readers.nasa_modis import NASAMODISReader
        >>> reader = NASAMODISReader()
        >>> ds = reader.open_dataset("MOD43A4.A2023001.h10v05.006.2023010123456.hdf")
        """
        if "preprocess" not in kwargs:
            kwargs["preprocess"] = nasa_modis_preprocess

        ds = super().open_dataset(files, **kwargs)

        # Update history
        ds = update_history(ds, "Read NASA MODIS data.")

        return ds

open_dataset(files, **kwargs)

Reads NASA MODIS swath data.

Parameters:

Name Type Description Default
files str | list[str]

File path, list of paths, or glob pattern.

required
**kwargs Any

Additional arguments passed to the Xarray driver.

{}

Returns:

Type Description
Dataset

The processed NASA MODIS dataset.

Examples:

>>> from monetio.readers.nasa_modis import NASAMODISReader
>>> reader = NASAMODISReader()
>>> ds = reader.open_dataset("MOD43A4.A2023001.h10v05.006.2023010123456.hdf")
Source code in monetio/readers/nasa_modis.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def open_dataset(self, files: str | list[str], **kwargs) -> xr.Dataset:
    """
    Reads NASA MODIS swath data.

    Parameters
    ----------
    files : str | list[str]
        File path, list of paths, or glob pattern.
    **kwargs : Any
        Additional arguments passed to the Xarray driver.

    Returns
    -------
    xr.Dataset
        The processed NASA MODIS dataset.

    Examples
    --------
    >>> from monetio.readers.nasa_modis import NASAMODISReader
    >>> reader = NASAMODISReader()
    >>> ds = reader.open_dataset("MOD43A4.A2023001.h10v05.006.2023010123456.hdf")
    """
    if "preprocess" not in kwargs:
        kwargs["preprocess"] = nasa_modis_preprocess

    ds = super().open_dataset(files, **kwargs)

    # Update history
    ds = update_history(ds, "Read NASA MODIS data.")

    return ds

nasa_modis_preprocess(ds)

Preprocess NASA MODIS dataset: standardize coordinates, handle time, and hygiene.

Parameters:

Name Type Description Default
ds Dataset

The raw NASA MODIS dataset.

required

Returns:

Type Description
Dataset

The preprocessed NASA MODIS dataset.

Examples:

>>> ds = nasa_modis_preprocess(ds)
Source code in monetio/readers/nasa_modis.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def nasa_modis_preprocess(ds: xr.Dataset) -> xr.Dataset:
    """
    Preprocess NASA MODIS dataset: standardize coordinates, handle time, and hygiene.

    Parameters
    ----------
    ds : xr.Dataset
        The raw NASA MODIS dataset.

    Returns
    -------
    xr.Dataset
        The preprocessed NASA MODIS dataset.

    Examples
    --------
    >>> ds = nasa_modis_preprocess(ds)
    """
    from ..grids import get_modis_latlon_from_swath_hv, get_sinu_area_def

    # Standardize dimensions
    ds = standardize_satellite_coords(
        ds, y_dim=["YDim:MOD_Grid_BRDF", "y"], x_dim=["XDim:MOD_Grid_BRDF", "x"]
    )
    ds = update_history(ds, "Standardized satellite coordinates.")

    # Extract tile info from attributes
    h = ds.attrs.get("HORIZONTALTILENUMBER")
    v = ds.attrs.get("VERTICALTILENUMBER")

    if h is not None and v is not None:
        ds = get_modis_latlon_from_swath_hv(h, v, ds)
        ds.attrs["area"] = get_sinu_area_def(ds)
        ds = update_history(ds, f"Assigned coordinates for tile h{h}v{v}.")

    # Handle Time
    if "time" not in ds.coords:
        # Try to get time from attributes
        range_start = ds.attrs.get("RANGEBEGINNINGDATE")
        time_start = ds.attrs.get("RANGEBEGINNINGTIME")
        if range_start and time_start:
            # We use xarray-native assignment to maintain laziness if possible,
            # though these are usually scalar attributes.
            dt = pd.to_datetime(f"{range_start} {time_start}")
            ds = ds.assign_coords(time=dt).expand_dims("time")
            ds = update_history(ds, f"Assigned time coordinate from attributes: {dt}.")

    # Scientific Hygiene
    ds = _scientific_hygiene(ds)

    # Update history
    ds = update_history(ds, "Preprocessed NASA MODIS data.")

    return ds