Skip to content

Commit 046adb6

Browse files
committed
Finished functions except for mapping
1 parent c34e93e commit 046adb6

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

pepsico/monthly_seasonal_conv_mapping.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import pandas as pd
33
import xarray as xr
44
import plotly.express as px
5+
from dash import Dash, dcc, html, Input, Output
6+
from urllib.request import urlopen
7+
import json
58

69
def compute_annual_monthly_avg(ds, variable, start_year=None, end_year=None):
710
#compute the avg (variable) for each month across the specified years
@@ -25,6 +28,7 @@ def compute_annual_monthly_avg(ds, variable, start_year=None, end_year=None):
2528
def compute_annual_seasonal_avg(monthly_ds, variable, start_year=None, end_year=None):
2629
#compute monthly avgs across all years
2730
monthly_avg = monthly_ds.mean(dim=["X", "Y"])
31+
2832
# Compute rolling seasonal average (3-month rolling window, center-aligned)
2933
rolling_avg = monthly_avg.rolling(T=3, center=True).mean()
3034

@@ -46,21 +50,20 @@ def write_to_csv(df, scenario, model, variable, output_dir):
4650
# Write to CSV file
4751
file_path = f'{output_dir}/{scenario}_{model}_{variable}_annual_monthly_avg.csv'
4852
df.to_csv(file_path, index=False)
49-
'''
50-
def map_averages(df, month, scenario, model, variable, output_dir):
51-
df = df.sort_values(by='Y')
5253

53-
# Create the map
54-
fig = px.imshow(
55-
df.pivot(index='Y', columns='X', values='{variable}'),
54+
55+
def map_averages(df, model, variable):
56+
57+
df = df.sort_values(by='month')
58+
map_fig = px.imshow(
59+
df.pivot(index='Y', columns='X', values=variable),
5660
labels={'color': 'Kelvins'},
57-
title=f'{variable_long_name} for {month}',
58-
origin='lower' # Set the origin to lower to flip the y-axis
61+
title=f'{variable} Monthly Averages',
62+
origin='lower' # Set the origin to lower to flip the y-axis if needed
5963
)
64+
return fig
65+
6066

61-
# Save the map as an HTML file - access through port 8000
62-
fig.write_html(f'/home/sz3116/python-maprooms/pepsico/resources/map_month_{month}.html')
63-
'''
6467

6568
def main(scenario, model, variable, start_year=None, end_year=None, output_dir='/home/sz3116/python-maprooms/pepsico/resources'):
6669
#main to run functions
@@ -78,28 +81,31 @@ def main(scenario, model, variable, start_year=None, end_year=None, output_dir='
7881

7982
#call seasonal function
8083
rolling_seasonal_avg = compute_annual_seasonal_avg(ds, variable, start_year, end_year)
81-
84+
8285
# Write the data to a CSV file
8386
write_to_csv(annual_monthly_avg, scenario, model, variable, output_dir)
8487
write_to_csv(rolling_seasonal_avg, scenario, model, f'{variable}_rolling', output_dir)
85-
'''
86-
#map monthly averages
87-
for month in range(1, 13):
88-
map_averages(annual_monthly_avg, month, scenario, model, variable, output_dir, month)
89-
'''
88+
89+
fig = map_averages(df, variable)
90+
fig.show()
91+
92+
9093
#also print in terminal
9194
print(annual_monthly_avg)
9295
print(rolling_seasonal_avg)
96+
97+
9398

9499
# testing
95100
if __name__ == "__main__":
96101
scenario = "historical"
97102
model = "GFDL-ESM4"
98-
variable = "pr"
103+
variable = "tasmin"
99104
start_year = 1950
100105
end_year = 2014
101106
output_dir = '/home/sz3116/python-maprooms/pepsico/resources'
102107

103108
main(scenario, model, variable, start_year, end_year, output_dir)
104109

105110
print("done")
111+

0 commit comments

Comments
 (0)