Example Forecast Data

Inspect example contrail forecast data source.

Open In Colab

Download data

[1]:
# download example data file
file_id = "1HftWWr4Z9ho_mjm1kbaL35SnQS9pzyjG"
!wget "https://drive.usercontent.google.com/download?id={file_id}&export=download" -O forecast.nc
--2024-11-13 12:38:33--  https://drive.usercontent.google.com/download?id=1HftWWr4Z9ho_mjm1kbaL35SnQS9pzyjG&export=download
Resolving drive.usercontent.google.com (drive.usercontent.google.com)... 142.251.41.1
Connecting to drive.usercontent.google.com (drive.usercontent.google.com)|142.251.41.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 66477996 (63M) [application/octet-stream]
Saving to: ‘forecast.nc’

forecast.nc         100%[===================>]  63.40M  8.97MB/s    in 6.6s

2024-11-13 12:38:43 (9.57 MB/s) - ‘forecast.nc’ saved [66477996/66477996]

Load in xarray

[2]:
import xarray as xr
[3]:
ds = xr.load_dataset("forecast.nc")
ds
[3]:
<xarray.Dataset>
Dimensions:                  (longitude: 1440, latitude: 641, flight_level: 18,
                              time: 1)
Coordinates:
  * longitude                (longitude) float32 -180.0 -179.8 ... 179.5 179.8
  * latitude                 (latitude) float32 -80.0 -79.75 ... 79.75 80.0
  * flight_level             (flight_level) int32 270 280 290 ... 420 430 440
  * time                     (time) datetime64[ns] 2024-09-25T18:00:00
    forecast_reference_time  (time) datetime64[ns] 2024-09-25T12:00:00
Data variables:
    contrails                (longitude, latitude, flight_level, time) float32 ...
Attributes:
    aircraft_class:  default
    model:           v1

Plot continous data

[4]:
# Plot continuous data at Flight Level 320 for the first timestep
ds["contrails"] \
    .isel(time=0) \
    .sel(flight_level=320) \
    .plot(x="longitude", y="latitude", cmap="Reds", figsize=(12, 8));
../_images/notebooks_forecast-data_7_0.png

Interpret as polygons

Generate polygons for each contrail forcing index

[5]:
from pycontrails.core import polygon  # pip install pycontrails

import numpy as np
from matplotlib import pyplot as plt
[6]:
# Create 2D dataarray from one time and flight level
da = ds["contrails"].isel(time=0).sel(flight_level=320)

# Thresholds of interest
thresholds = [1, 2, 3, 4]

# Polygon options
longitude = da["longitude"].to_numpy()
latitude = da["latitude"].to_numpy()
min_area = 0
epsilon = 0

# Set up figure
fig, ax = plt.subplots(figsize=(12,8))
cmap = plt.get_cmap('Reds')
colors = cmap(np.linspace(0.3, 1, len(thresholds)))

for (c, th) in zip(colors, thresholds):

    # calculate polygons for threshold
    multipolygon = polygon.find_multipolygon(
        da.values.copy(),
        threshold=th,
        min_area=min_area,
        epsilon=epsilon,
        longitude=longitude,
        latitude=latitude,
    )

    # plot all polygons
    for (i, poly) in enumerate(multipolygon.geoms):
        if i == 0:
            ax.plot(*poly.exterior.xy, color=c, lw=1, label=th)
        else:
            ax.plot(*poly.exterior.xy, color=c, lw=1)

plt.legend();
plt.xlabel("Longitude");
plt.ylabel("Latitude");
../_images/notebooks_forecast-data_10_0.png