# Define time rangestart_date="2024-07-01"end_date="2024-07-31"# Compute Euclidean distance using latitude and longitudedistances=np.sqrt((temperature['longitude']-zlon)**2+(temperature['latitude']-zlat)**2)# Find the indices of the minimum distancemin_distance_idx=np.unravel_index(np.argmin(distances.values),distances.shape)# Extract the nearest x and y indices correctlynearest_y_idx,nearest_x_idx=min_distance_idxnearest_x=temperature['x'].isel(x=nearest_x_idx)nearest_y=temperature['y'].isel(y=nearest_y_idx)# Select the temperature data for the nearest point within the time rangetemperature_point=temperature["TMP"].sel(time=slice(start_date,end_date),x=nearest_x,y=nearest_y,)# Load the data into a DataFrametemperature_p=temperature_point.load()-273.16# Convert from Kelvin to Celsius# Create a DataFrame from xarraydf_temperature=pd.DataFrame({"time":temperature_p.time.values,"TEMP":temperature_p.values# Extract values from xarray})# Set the time column as indexdf_temperature.set_index("time",inplace=True)
In [6]:
# Plot the time seriesplt.figure(figsize=(12,6))plt.plot(df_temperature.TEMP,color="tab:blue")# Ensure time is properly accessedplt.title("HRRR 2 m Temperature at Nearest Gridpoint to KSLC")# Format x-axis to display MM-DD-HH formatplt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))# Format dates as MM-DDplt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5))# Show labels every 2 daysplt.ylabel("Temperature (°C)")plt.grid()plt.show()
In [7]:
#area for plotlon1=-97.5lat1=38.5slat=38.5projData=ccrs.LambertConformal(central_longitude=lon1,central_latitude=lat1,standard_parallels=[slat])latN=47latS=31lonW=-122lonE=-107#get grid coordinatesx=temperature['x']y=temperature['y']# Define the exact timestamp for selectionmap_time="2024-08-11T00:00:00"selected_time=np.datetime64(map_time)# get temperature data for the given timetemperature_map=temperature["TMP"].sel(time=selected_time)-273.16
In [8]:
res='50m'fig=plt.figure(figsize=(12,12))ax=plt.subplot(1,1,1,projection=projData)ax.set_extent([lonW,lonE,latS,latN],crs=ccrs.PlateCarree())# Add the titletl1=str('HRRR 2 m Temperature ')+map_timeplt.title(tl1,fontsize=16)ax.add_feature(cfeature.COASTLINE.with_scale(res),edgecolor='green',zorder=97)ax.add_feature(cfeature.STATES.with_scale(res),edgecolor='green',zorder=97)# Contour fillCF=ax.contourf(x,y,temperature_map.values,cmap='Reds',zorder=10)cbar=fig.colorbar(CF,shrink=0.5)cbar.set_label("Temperature (°C)",size='large')plt.show()