import matplotlib.pyplot as plt
import netCDF4 as nc
import calendar # Import the calendar module
filename="2023.monthly_rain.nc"
data=nc.Dataset(filename)
# Extract the latitude, longitude, and monthly_rain data for the last month
lat=data.variables["lat"][:]
lon=data.variables["lon"][:]
time=data.variables["time"][:]
monthly_rain = data.variables['monthly_rain'][-1, :, :]
# Get the last time value (assuming the last time step represents the last month)
last_time_value = time[-1]
# Check if the result is within the valid range
if last_time_value >= 0:
curent_month=calendar.month_name[int(last_time_value / 30.5)+1]
else:
print("Invalid time value for the previous month.")
# Create a figure and plot the data on a regular grid
plt.figure(figsize=(10, 8))
# Plot the monthly_rain data as a filled contour plot
plt.contourf(lon, lat, monthly_rain, cmap='Blues')
plt.colorbar(label='Monthly Rainfall (mm)')
# Add grid lines and labels
plt.xlabel('Longitude')
plt.ylabel('Latitude')
# Add a title
plt.title('Monthly Rainfall for September')
# Save the map as an HTML file
plt.savefig('rainfall_map.png')
# Show the plot
plt.show()
# Close the NetCDF dataset
data.close()