Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import re

hdf_file = "/srv/ccrc/RegClim2/z3540003/Winter_School/MOD13Q1.A2023001.h31v11.006.2023018081419.hdf"
data = xr.open_dataset(hdf_file, engine="netcdf4")

# Extract the X and Y dimensions from StructMetadata
struct_metadata = data.attrs['StructMetadata.0']
xdim = int(re.search(r'XDim=(\d+)', struct_metadata).group(1))
ydim = int(re.search(r'YDim=(\d+)', struct_metadata).group(1))

# Extract the coordinates and data for the region with valid data
valid_data = data["250m 16 days NDVI"]
valid_data = valid_data.where(valid_data != -3000)  # Assuming -3000 is the fill value

# Get the region's coordinates
x = valid_data.coords["XDim"].values
y = valid_data.coords["YDim"].values

# Create a figure and plot the data using PlateCarree projection
fig = plt.figure(figsize=(8, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
im = ax.imshow(valid_data, extent=(x.min(), x.max(), y.min(), y.max()), origin="upper", transform=ccrs.PlateCarree())

# Customize the plot
plt.colorbar(im, ax=ax, label="NDVI")
ax.set_title("MODIS NDVI for Region with Data")
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")

# Show the plot
plt.show()