utils
canari_ml.plotting.utils
¶
canari_ml.plotting.utils.broadcast_forecast(start_date, end_date, datafiles=None, dataset=None, target=None, frequency=Frequency.DAY)
¶
Broadcasts forecast data across a specified time range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Timestamp
|
Start date for the forecast period |
required |
end_date
|
Timestamp
|
End date for the forecast period |
required |
datafiles
|
list | None
|
List of input data files |
None
|
dataset
|
Dataset | None
|
Input xarray Dataset |
None
|
target
|
PathLike | str | None
|
Output file path to save results |
None
|
frequency
|
optional
|
Forecast frequency (e.g., daily, monthly) |
DAY
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Broadcasted forecast dataset |
Examples:
# Example usage:
start = pd.Timestamp('2023-01-01')
end = pd.Timestamp('2023-01-07')
datafiles = ['input.nc']
result = broadcast_forecast(start, end, datafiles=datafiles, frequency=Frequency.DAY)
Notes
Based on IceNet implementation: https://github.com/icenet-ai/icenet/blob/a7554a617c1eff1eceefa4a0f0a5782c5a0a34ea/icenet/plotting/utils.py
Source code in src/canari_ml/plotting/utils.py
canari_ml.plotting.utils.get_forecast_data(forecast_file, forecast_date, stddev=False)
¶
Get forecast data for a specific date and optional standard deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forecast_file
|
PathLike
|
Path to a NetCDF (.nc) file containing forecast data. |
required |
forecast_date
|
str
|
Initialisation date of the forecast in string format. |
required |
stddev
|
bool
|
Whether to return standard deviation instead of mean values. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[DataArray, dict]
|
A tuple containing two elements: - The forecast dataarray (either ua700_mean or ua700_stddev) - Spatial reference attributes from the dataset (created by rioxarray) |
Source code in src/canari_ml/plotting/utils.py
canari_ml.plotting.utils.filter_ds_by_obs(ds, obs_da, forecast_date, frequency=Frequency.DAY)
¶
Filter a dataset based on observational data for a specific forecast date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Input forecast dataset. |
required |
obs_da
|
DataArray
|
Observational data array to use for filtering. |
required |
forecast_date
|
str
|
Initialisation date of the forecast in string format. |
required |
frequency
|
Frequency
|
Time frequency of the observational dataset.
Defaults to |
DAY
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Filtered forecast dataset compatible with observational data range. |
Source code in src/canari_ml/plotting/utils.py
canari_ml.plotting.utils.get_forecast_obs_data(forecast_file, obs_ds_config, forecast_date, stddev=False)
¶
Get both forecast and observational data for a specific date.
This function retrieves forecast data and corresponding observational data for a given forecast file, dataset configuration, and initialisation date. It returns the filtered forecast data, observational data, and spatial reference attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forecast_file
|
PathLike
|
Path to a NetCDF (.nc) file containing forecast data. |
required |
obs_ds_config
|
PathLike
|
Path to JSON config file for the observational dataset.
Will by default be under |
required |
forecast_date
|
str
|
Initialisation date of the forecast in string format. |
required |
stddev
|
bool
|
Whether to return standard deviation instead of mean values. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[DataArray, DataArray, dict]
|
A tuple containing three elements: - forecast_da: Filtered forecast data array. - obs_da: Observational data array. - spatial_ref: Spatial reference attributes from the dataset (created by rioxarray) |
Source code in src/canari_ml/plotting/utils.py
canari_ml.plotting.utils.high_res_rectangle(lon_min, lon_max, lat_min, lat_max, target_crs=None, num_points=100, source_crs=ccrs.PlateCarree())
¶
Create a high-resolution lat/lon "rectangle" by subdividing the corners into smaller segments.
Creates a smooth polygon boundary around a rectangular region defined by longitude and latitude limits. It subdivides each edge into smaller segments for higher resolution, projects the coordinates using specified CRS, and returns the boundary as an matplotlib Path object for plotting. The result can be used for creating custom gridlines or region boundaries in cartographic visualisations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lon_min
|
int | float
|
The minimum longitude (left edge). |
required |
lon_max
|
int | float
|
The maximum longitude (right edge). |
required |
lat_min
|
int | float
|
The minimum latitude (bottom edge). |
required |
lat_max
|
int | float
|
The maximum latitude (top edge). |
required |
target_crs
|
optional
|
Target coordinate reference system for projection. Defaults to None. |
None
|
num_points
|
optional
|
Number of points to interpolate along each side of the rectangle. Defaults to 100. |
100
|
source_crs
|
CRS
|
Source coordinate reference system for input data. Defaults to ccrs.PlateCarree(). |
PlateCarree()
|
Returns: A high-resolution projected lat/lon bounded Path for matplotlib custom boundary.
Source code in src/canari_ml/plotting/utils.py
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |