Skip to content

Commit

Permalink
Fix: update plot_gridded_variable to handle HH:MM:SS and make lat_sli…
Browse files Browse the repository at this point in the history
…ce optional
  • Loading branch information
lbesnard committed Nov 19, 2024
1 parent d589365 commit 8b5e9aa
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions aodn_cloud_optimised/lib/ParquetDataQuery.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ def plot_gridded_radar_velocity(
def plot_gridded_variable(
ds,
start_date,
lon_slice,
lat_slice,
lon_slice=None,
lat_slice=None,
var_name="sea_surface_temperature",
n_days=6,
lat_name="lat",
Expand All @@ -653,22 +653,29 @@ def plot_gridded_variable(

ds = ds.sortby(time_name)

# Get latitude and longitude extents
lat_min, lat_max = ds[lat_name].min().item(), ds[lat_name].max().item()
lon_min, lon_max = ds[lon_name].min().item(), ds[lon_name].max().item()

if lat_slice is None:
lat_slice = (lat_min, lat_max)

if lon_slice is None:
lon_slice = (lon_min, lon_max)

# Decide on the slice order
if ds[lat_name][0] < ds[lat_name][-1]:
lat_slice = lat_slice
elif ds[lat_name][0] > ds[lat_name][-1]:
# Reverse the slice
lat_slice = lat_slice[::-1]

# Get latitude and longitude extents
lat_min, lat_max = ds[lat_name].min().item(), ds[lat_name].max().item()
lon_min, lon_max = ds[lon_name].min().item(), ds[lon_name].max().item()

# Test if lat_slice and lon_slice are within bounds
if not (lat_min <= lat_slice[0] <= lat_max and lat_min <= lat_slice[1] <= lat_max):
raise ValueError(
f"Latitude slice {lat_slice} is out of bounds. Dataset latitude extent is ({lat_min}, {lat_max})"
)

if not (lon_min <= lon_slice[0] <= lon_max and lon_min <= lon_slice[1] <= lon_max):
raise ValueError(
f"Longitude slice {lon_slice} is out of bounds. Dataset longitude extent is ({lon_min}, {lon_max})"
Expand Down Expand Up @@ -721,7 +728,7 @@ def plot_gridded_variable(
try:
data = ds[var_name].sel(
{
time_name: date.strftime("%Y-%m-%d"),
time_name: date.strftime("%Y-%m-%d %H:%M:%S"),
lon_name: slice(lon_slice[0], lon_slice[1]),
lat_name: slice(lat_slice[0], lat_slice[1]),
}
Expand All @@ -730,7 +737,7 @@ def plot_gridded_variable(
# Check for NaNs
if data.isnull().all():
print(
f"No valid data for {date.strftime('%Y-%m-%d')}, skipping this date."
f"No valid data for {date.strftime('%Y-%m-%d %H:%M:%S')}, skipping this date."
)
continue

Expand Down Expand Up @@ -771,15 +778,17 @@ def plot_gridded_variable(
try:
data = ds[var_name].sel(
{
time_name: date.strftime("%Y-%m-%d"),
time_name: date.strftime("%Y-%m-%d %H:%M:%S"),
lon_name: slice(lon_slice[0], lon_slice[1]),
lat_name: slice(lat_slice[0], lat_slice[1]),
}
)

# Skip if no valid data is found for the date
if data.isnull().all():
print(f"No data for {date.strftime('%Y-%m-%d')}, skipping plot.")
print(
f"No data for {date.strftime('%Y-%m-%d %H:%M:%S')}, skipping plot."
)
continue

var_units = ds[var_name].attrs.get("units", "unknown units")
Expand All @@ -804,11 +813,11 @@ def plot_gridded_variable(
ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)

# Set the title with the date
ax.set_title(date.strftime("%Y-%m-%d"))
ax.set_title(date.strftime("%Y-%m-%d %H:%M:%S"))

except Exception as err:
print(f"Error processing date {date.strftime('%Y-%m-%d')}: {err}")
ax.set_title(f"No data for {date.strftime('%Y-%m-%d')}")
print(f"Error processing date {date.strftime('%Y-%m-%d %H:%M:%S')}: {err}")
ax.set_title(f"No data for {date.strftime('%Y-%m-%d %H:%M:%S')}")
ax.axis("off")

# Create a single colorbar for all plots
Expand Down

0 comments on commit 8b5e9aa

Please sign in to comment.