[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math

Search:


View post   

>> No.15585698 [View]
File: 1.72 MB, 640x480, us_active_stations.gif [View same] [iqdb] [saucenao] [google]
15585698

I was hoping to see more, but it's really a lot of stations. Maybe someone finds this interesting nevertheless. If I wasn't going to bed, I'd get the main data and plot the stations exceeding 95°F in a different color than the others.

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
from PIL import Image
import os

# Define column names
columns = ['ID', 'Latitude', 'Longitude', 'Type', 'StartYear', 'EndYear']

# Load data from .txt file into pandas DataFrame
df = pd.read_csv('ghcnd-inventory.txt', delim_whitespace=True, names=columns)

# Filter rows where ID starts with 'US' and Type is 'TMAX'
df = df[df['ID'].str.startswith('US') & (df['Type'] == 'TMAX')]

# Define the range of years to consider
start_year = 1900
end_year = 2023

# Create a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))

# Define path to save images
img_path = 'images'
os.makedirs(img_path, exist_ok=True)

# Create images for each year
for year in range(start_year, end_year + 1):
print(year)
# Filter active stations
active_stations = gdf[(gdf['StartYear'] <= year) & (gdf['EndYear'] >= year)]

# Only plot if there are active stations
if not active_stations.empty:
fig, ax = plt.subplots()

# Manually set the plot limits to correspond to the geographical extent of the United States
ax.set_xlim(-125, -67)
ax.set_ylim(24, 50)

# Plot active stations with smaller markers
active_stations.plot(ax=ax, color='red', markersize=5)

plt.title(f'Active Stations in {year}')
plt.axis('off')

# Save image
plt.savefig(f'{img_path}/map_{year}.png')
plt.close()

# Create gif from images
images = [Image.open(f'{img_path}/map_{year}.png') for year in range(start_year, end_year + 1) if os.path.isfile(f'{img_path}/map_{year}.png')]
images[0].save('us_active_stations.gif', save_all=True, append_images=images[1:], loop=0, duration=200)

Navigation
View posts[+24][+48][+96]