2
2
import pandas as pd
3
3
import xarray as xr
4
4
import plotly .express as px
5
+ from dash import Dash , dcc , html , Input , Output
6
+ from urllib .request import urlopen
7
+ import json
5
8
6
9
def compute_annual_monthly_avg (ds , variable , start_year = None , end_year = None ):
7
10
#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):
25
28
def compute_annual_seasonal_avg (monthly_ds , variable , start_year = None , end_year = None ):
26
29
#compute monthly avgs across all years
27
30
monthly_avg = monthly_ds .mean (dim = ["X" , "Y" ])
31
+
28
32
# Compute rolling seasonal average (3-month rolling window, center-aligned)
29
33
rolling_avg = monthly_avg .rolling (T = 3 , center = True ).mean ()
30
34
@@ -46,21 +50,20 @@ def write_to_csv(df, scenario, model, variable, output_dir):
46
50
# Write to CSV file
47
51
file_path = f'{ output_dir } /{ scenario } _{ model } _{ variable } _annual_monthly_avg.csv'
48
52
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')
52
53
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 ),
56
60
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
59
63
)
64
+ return fig
65
+
60
66
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
- '''
64
67
65
68
def main (scenario , model , variable , start_year = None , end_year = None , output_dir = '/home/sz3116/python-maprooms/pepsico/resources' ):
66
69
#main to run functions
@@ -78,28 +81,31 @@ def main(scenario, model, variable, start_year=None, end_year=None, output_dir='
78
81
79
82
#call seasonal function
80
83
rolling_seasonal_avg = compute_annual_seasonal_avg (ds , variable , start_year , end_year )
81
-
84
+
82
85
# Write the data to a CSV file
83
86
write_to_csv (annual_monthly_avg , scenario , model , variable , output_dir )
84
87
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
+
90
93
#also print in terminal
91
94
print (annual_monthly_avg )
92
95
print (rolling_seasonal_avg )
96
+
97
+
93
98
94
99
# testing
95
100
if __name__ == "__main__" :
96
101
scenario = "historical"
97
102
model = "GFDL-ESM4"
98
- variable = "pr "
103
+ variable = "tasmin "
99
104
start_year = 1950
100
105
end_year = 2014
101
106
output_dir = '/home/sz3116/python-maprooms/pepsico/resources'
102
107
103
108
main (scenario , model , variable , start_year , end_year , output_dir )
104
109
105
110
print ("done" )
111
+
0 commit comments