Skip to content

skynet

SKYNET Sun Photometer Reader.

SKYNETReader

Bases: PointReader

Reader for SKYNET sun photometer data.

This reader supports retrieving and loading SKYNET data from local or remote files, standardizing it into a common format, and optionally converting it to an xarray Dataset.

Source code in monetio/readers/skynet.py
 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
 47
 48
 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
@register_reader("skynet")
class SKYNETReader(PointReader):
    """
    Reader for SKYNET sun photometer data.

    This reader supports retrieving and loading SKYNET data from local or remote files,
    standardizing it into a common format, and optionally converting it to an
    xarray Dataset.
    """

    def open_dataset(
        self,
        files: str | list[str] | None = None,
        dates: pd.DatetimeIndex | list[datetime] | datetime | str | None = None,
        siteid: str | None = None,
        product: str = "AOT",
        as_xarray: bool = True,
        lazy: bool = False,
        **kwargs: dict,
    ) -> pd.DataFrame | xr.Dataset:
        """
        Retrieve and load SKYNET data.

        Parameters
        ----------
        files : Union[str, List[str]], optional
            File path, list of paths, or glob pattern.
        dates : Union[pd.DatetimeIndex, List[datetime], datetime, str], optional
            Dates to retrieve if files are not provided.
        siteid : str, optional
            Specific SKYNET site ID.
        product : str, optional
            SKYNET product (e.g., 'AOT', 'SSA'), by default "AOT".
        as_xarray : bool, optional
            Whether to return an xarray.Dataset, by default True.
        lazy : bool, optional
            Whether to return a dask-backed object, by default False.
        **kwargs : dict
            Additional arguments passed to the driver.

        Returns
        -------
        Union[pd.DataFrame, xr.Dataset]
            The loaded SKYNET data.

        Examples
        --------
        >>> reader = SKYNETReader()
        >>> ds = reader.open_dataset(siteid="POC", dates="2023-01-01")
        """
        if files is None:
            if dates is None:
                raise ValueError("Must provide either 'files' or 'dates'.")
            files = self.build_urls(dates, siteid=siteid, product=product, **kwargs)

        if not files:
            if as_xarray:
                return xr.Dataset()
            return pd.DataFrame()

        # Define per-file preprocessing
        read_func = read_skynet_csv

        df = super().open_dataset(
            files,
            read_method=read_func,
            as_xarray=False,
            lazy=lazy,
            **kwargs,
        )

        if as_xarray:
            ds = self.to_xarray(df, **kwargs)
            ds = update_history(ds, "Read SKYNET data.")
            return ds

        return df

    def harmonize(
        self, df: Union[pd.DataFrame, "dd.DataFrame"]
    ) -> Union[pd.DataFrame, "dd.DataFrame"]:
        """
        Standardize column names and types.

        Parameters
        ----------
        df : Union[pd.DataFrame, dd.DataFrame]
            Input dataframe.

        Returns
        -------
        Union[pd.DataFrame, dd.DataFrame]
            Harmonized dataframe.
        """
        df = super().harmonize(df)

        # Force string columns to object for Pandas 3.0 compatibility
        df = force_object_strings(df)
        return df

    def build_urls(
        self,
        dates: pd.DatetimeIndex | list[datetime] | datetime | str,
        siteid: str | None = None,
        product: str = "AOT",
        **kwargs: dict,
    ) -> list[str]:
        """
        Construct SKYNET URLs.

        Parameters
        ----------
        dates : Union[pd.DatetimeIndex, List[datetime], datetime, str]
            Dates to retrieve.
        siteid : str, optional
            SKYNET site ID.
        product : str, optional
            SKYNET product, by default "AOT".
        **kwargs : dict
            Additional arguments.

        Returns
        -------
        List[str]
            List of constructed URLs.

        Examples
        --------
        >>> urls = reader.build_urls("2023-01-01", siteid="POC")
        """
        dates = pd.DatetimeIndex(np.atleast_1d(pd.to_datetime(dates)))
        if dates.empty or siteid is None:
            return []

        # Placeholder for SKYNET ISDC URL structure
        # Example: https://www.skynet-isdc.org/data/L2/AOT/SITE/YYYY/SITE_YYYYMMDD.AOT
        base_url = "https://www.skynet-isdc.org/data/L2"
        urls = []
        for date in dates.normalize().unique():
            fname = f"{siteid}_{date.strftime('%Y%m%d')}.{product.upper()}"
            url = f"{base_url}/{product.upper()}/{siteid}/{date.year}/{fname}"
            urls.append(url)

        return urls

build_urls(dates, siteid=None, product='AOT', **kwargs)

Construct SKYNET URLs.

Parameters:

Name Type Description Default
dates Union[DatetimeIndex, List[datetime], datetime, str]

Dates to retrieve.

required
siteid str

SKYNET site ID.

None
product str

SKYNET product, by default "AOT".

'AOT'
**kwargs dict

Additional arguments.

{}

Returns:

Type Description
List[str]

List of constructed URLs.

Examples:

>>> urls = reader.build_urls("2023-01-01", siteid="POC")
Source code in monetio/readers/skynet.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def build_urls(
    self,
    dates: pd.DatetimeIndex | list[datetime] | datetime | str,
    siteid: str | None = None,
    product: str = "AOT",
    **kwargs: dict,
) -> list[str]:
    """
    Construct SKYNET URLs.

    Parameters
    ----------
    dates : Union[pd.DatetimeIndex, List[datetime], datetime, str]
        Dates to retrieve.
    siteid : str, optional
        SKYNET site ID.
    product : str, optional
        SKYNET product, by default "AOT".
    **kwargs : dict
        Additional arguments.

    Returns
    -------
    List[str]
        List of constructed URLs.

    Examples
    --------
    >>> urls = reader.build_urls("2023-01-01", siteid="POC")
    """
    dates = pd.DatetimeIndex(np.atleast_1d(pd.to_datetime(dates)))
    if dates.empty or siteid is None:
        return []

    # Placeholder for SKYNET ISDC URL structure
    # Example: https://www.skynet-isdc.org/data/L2/AOT/SITE/YYYY/SITE_YYYYMMDD.AOT
    base_url = "https://www.skynet-isdc.org/data/L2"
    urls = []
    for date in dates.normalize().unique():
        fname = f"{siteid}_{date.strftime('%Y%m%d')}.{product.upper()}"
        url = f"{base_url}/{product.upper()}/{siteid}/{date.year}/{fname}"
        urls.append(url)

    return urls

harmonize(df)

Standardize column names and types.

Parameters:

Name Type Description Default
df Union[DataFrame, DataFrame]

Input dataframe.

required

Returns:

Type Description
Union[DataFrame, DataFrame]

Harmonized dataframe.

Source code in monetio/readers/skynet.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def harmonize(
    self, df: Union[pd.DataFrame, "dd.DataFrame"]
) -> Union[pd.DataFrame, "dd.DataFrame"]:
    """
    Standardize column names and types.

    Parameters
    ----------
    df : Union[pd.DataFrame, dd.DataFrame]
        Input dataframe.

    Returns
    -------
    Union[pd.DataFrame, dd.DataFrame]
        Harmonized dataframe.
    """
    df = super().harmonize(df)

    # Force string columns to object for Pandas 3.0 compatibility
    df = force_object_strings(df)
    return df

open_dataset(files=None, dates=None, siteid=None, product='AOT', as_xarray=True, lazy=False, **kwargs)

Retrieve and load SKYNET data.

Parameters:

Name Type Description Default
files Union[str, List[str]]

File path, list of paths, or glob pattern.

None
dates Union[DatetimeIndex, List[datetime], datetime, str]

Dates to retrieve if files are not provided.

None
siteid str

Specific SKYNET site ID.

None
product str

SKYNET product (e.g., 'AOT', 'SSA'), by default "AOT".

'AOT'
as_xarray bool

Whether to return an xarray.Dataset, by default True.

True
lazy bool

Whether to return a dask-backed object, by default False.

False
**kwargs dict

Additional arguments passed to the driver.

{}

Returns:

Type Description
Union[DataFrame, Dataset]

The loaded SKYNET data.

Examples:

>>> reader = SKYNETReader()
>>> ds = reader.open_dataset(siteid="POC", dates="2023-01-01")
Source code in monetio/readers/skynet.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
def open_dataset(
    self,
    files: str | list[str] | None = None,
    dates: pd.DatetimeIndex | list[datetime] | datetime | str | None = None,
    siteid: str | None = None,
    product: str = "AOT",
    as_xarray: bool = True,
    lazy: bool = False,
    **kwargs: dict,
) -> pd.DataFrame | xr.Dataset:
    """
    Retrieve and load SKYNET data.

    Parameters
    ----------
    files : Union[str, List[str]], optional
        File path, list of paths, or glob pattern.
    dates : Union[pd.DatetimeIndex, List[datetime], datetime, str], optional
        Dates to retrieve if files are not provided.
    siteid : str, optional
        Specific SKYNET site ID.
    product : str, optional
        SKYNET product (e.g., 'AOT', 'SSA'), by default "AOT".
    as_xarray : bool, optional
        Whether to return an xarray.Dataset, by default True.
    lazy : bool, optional
        Whether to return a dask-backed object, by default False.
    **kwargs : dict
        Additional arguments passed to the driver.

    Returns
    -------
    Union[pd.DataFrame, xr.Dataset]
        The loaded SKYNET data.

    Examples
    --------
    >>> reader = SKYNETReader()
    >>> ds = reader.open_dataset(siteid="POC", dates="2023-01-01")
    """
    if files is None:
        if dates is None:
            raise ValueError("Must provide either 'files' or 'dates'.")
        files = self.build_urls(dates, siteid=siteid, product=product, **kwargs)

    if not files:
        if as_xarray:
            return xr.Dataset()
        return pd.DataFrame()

    # Define per-file preprocessing
    read_func = read_skynet_csv

    df = super().open_dataset(
        files,
        read_method=read_func,
        as_xarray=False,
        lazy=lazy,
        **kwargs,
    )

    if as_xarray:
        ds = self.to_xarray(df, **kwargs)
        ds = update_history(ds, "Read SKYNET data.")
        return ds

    return df

read_skynet_csv(fn, **kwargs)

Read a single SKYNET ASCII file.

Parameters:

Name Type Description Default
fn str

Path to the SKYNET file.

required
**kwargs dict

Additional arguments.

{}

Returns:

Type Description
DataFrame

Data from the SKYNET file.

Examples:

>>> df = read_skynet_csv("site_20230101.AOT")
Source code in monetio/readers/skynet.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def read_skynet_csv(fn: str, **kwargs: dict) -> pd.DataFrame:
    """
    Read a single SKYNET ASCII file.

    Parameters
    ----------
    fn : str
        Path to the SKYNET file.
    **kwargs : dict
        Additional arguments.

    Returns
    -------
    pd.DataFrame
        Data from the SKYNET file.

    Examples
    --------
    >>> df = read_skynet_csv("site_20230101.AOT")
    """
    fs = FileUtility.get_fs(fn)
    try:
        with fs.open(fn, mode="rb") as f:
            content = f.read()
            if isinstance(content, bytes):
                content = content.decode("utf-8", errors="ignore")
    except Exception as e:
        warnings.warn(f"Failed to read {fn}: {e}")
        return pd.DataFrame()

    lines = content.splitlines()
    if not lines:
        return pd.DataFrame()

    # Generic SKYNET ASCII parsing logic
    # Assume metadata in header lines starting with '#' or some keyword
    # and a CSV-like structure for the rest.
    metadata = {}
    data_start = 0
    for i, line in enumerate(lines):
        if line.startswith("#"):
            # Try to parse key: value
            if ":" in line:
                parts = line[1:].split(":", 1)
                metadata[parts[0].strip().lower()] = parts[1].strip()
            data_start = i + 1
        elif not line.strip():
            data_start = i + 1
            continue
        else:
            # First non-empty, non-comment line is assumed to be the header or data
            data_start = i
            break

    try:
        df = pd.read_csv(
            BytesIO(content.encode("utf-8")),
            skiprows=data_start,
            sep=r"\s+|,",
            engine="python",
            na_values=["-999", "-999.9", "-9.999"],
        )
    except Exception as e:
        warnings.warn(f"Error parsing SKYNET file {fn}: {e}")
        return pd.DataFrame()

    if df.empty:
        return df

    df.columns = [c.lower() for c in df.columns]

    # Handle Time
    # Common SKYNET formats might have 'date' and 'time' or 'year', 'month', 'day', 'hour' etc.
    if "date" in df.columns and "time" in df.columns:
        df["time"] = pd.to_datetime(df["date"] + " " + df["time"], errors="coerce")
    elif all(c in df.columns for c in ["year", "month", "day", "hour", "minute"]):
        df["time"] = pd.to_datetime(df[["year", "month", "day", "hour", "minute"]], errors="coerce")

    # Standard names for coordinates and variables
    rename_dict = {
        "lat": "latitude",
        "lon": "longitude",
        "alt": "elevation",
        "site": "siteid",
        "aot": "aerosol_optical_thickness",
        "ssa": "single_scattering_albedo",
        "ae": "angstrom_exponent",
        "ri": "refractive_index",
    }
    # For AOT at specific wavelengths, we want to map them to 'aod_XXXnm'
    for col in df.columns:
        if col.startswith("aot_"):
            rename_dict[col] = col.replace("aot_", "aod_")
        elif col.endswith("aot"):
            # Some files might have 500aot
            import re

            match = re.match(r"(\d+)aot", col)
            if match:
                rename_dict[col] = f"aod_{match.group(1)}nm"

    df = df.rename(columns=rename_dict)

    # If metadata contains location, add it if not in DF
    if "latitude" not in df.columns:
        for k in ["latitude", "lat"]:
            if k in metadata:
                df["latitude"] = float(metadata[k])
                break
    if "longitude" not in df.columns:
        for k in ["longitude", "lon"]:
            if k in metadata:
                df["longitude"] = float(metadata[k])
                break
    if "elevation" not in df.columns:
        for k in ["elevation", "alt", "altitude"]:
            if k in metadata:
                df["elevation"] = float(metadata[k])
                break
    if "siteid" not in df.columns:
        for k in ["siteid", "site"]:
            if k in metadata:
                df["siteid"] = metadata[k]
                break

    return df