-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplot_Regional_Extent.py
235 lines (156 loc) · 6.52 KB
/
plot_Regional_Extent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# coding: utf-8
# In[1]:
'''
This code is part of the SIPN2 project focused on improving sub-seasonal to seasonal predictions of Arctic Sea Ice.
If you use this code for a publication or presentation, please cite the reference in the README.md on the
main page (https://github.com/NicWayand/ESIO).
Questions or comments should be addressed to [email protected]
Copyright (c) 2018 Nic Wayand
GNU General Public License v3.0
'''
'''
Plot exetent/area from observations and models (past and future)
'''
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt, mpld3
from collections import OrderedDict
import itertools
import numpy as np
import numpy.ma as ma
import pandas as pd
import struct
import os
import xarray as xr
import glob
import datetime
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import seaborn as sns
np.seterr(divide='ignore', invalid='ignore')
from esio import EsioData as ed
from esio import ice_plot
from esio import metrics
# General plotting settings
sns.set_style('whitegrid')
sns.set_context("talk", font_scale=1.5, rc={"lines.linewidth": 2.5})
# In[2]:
# Plotting Info
runType = 'forecast'
variables = ['sic'] #, 'hi'
metric1 = 'extent'
# In[3]:
# Initialization times to plot
cd = datetime.datetime.now()
cd = datetime.datetime(cd.year, cd.month, cd.day) # Assumes hours 00, min 00
SD = cd - datetime.timedelta(days=90)
# SD = cd - datetime.timedelta(days=4*365)
# ED = cd + datetime.timedelta(days=365)
# In[4]:
# Info about models runs
# icePredicted = {'gfdlsipn':True, 'piomas':True, 'yopp':True, 'bom':False, 'cma':True, 'ecmwf':True,
# 'hcmr':False, 'isaccnr':False, 'jma':False, 'metreofr':True, 'ukmo':True, 'eccc':False,
# 'kma':True, 'ncep':True, 'ukmetofficesipn':True, 'ecmwfsipn':True}
# biasCorrected =
# In[5]:
#############################################################
# Load in Data
#############################################################
E = ed.EsioData.load()
# In[6]:
# Load obs
import timeit
start_time = timeit.default_timer()
ds_obs = xr.open_mfdataset(E.obs['NSIDC_0081']['sipn_nc']+'_yearly/*.nc', concat_dim='time')#,
print(timeit.default_timer() - start_time)
# In[7]:
# Load in regional data
# Note minor -0.000004 degree differences in latitude
ds_region = xr.open_dataset(os.path.join(E.grid_dir, 'sio_2016_mask_Update.nc'))
# In[8]:
cdate = datetime.datetime.now()
# # Plot Raw extents and only models that predict sea ice
# In[9]:
# cmap_c = itertools.cycle(sns.color_palette("Paired", len(E.model.keys()) ))
# linecycler = itertools.cycle(["-","--","-.",":","--"])
for cvar in variables:
fig_dir = os.path.join(E.fig_dir, 'model', 'all_model', cvar, "regional_timeseries")
if not os.path.exists(fig_dir):
os.makedirs(fig_dir)
# For each region
for cR in ds_region.ocean_regions.values:
cR_name = ds_region.region_names.sel(nregions=cR).item(0)
print(cR_name)
# New Plot
f = plt.figure(figsize=(15,10))
ax1 = plt.subplot(1, 1, 1) # Observations
for (i, cmod) in enumerate(E.model.keys()):
# for (i, cmod) in enumerate(['usnavyncep','usnavysipn']):
if not E.icePredicted[cmod]:
continue
print(cmod)
# Load in Model
model_forecast = os.path.join(E.model[cmod][runType]['sipn_nc_agg'], '*.nc')
# Check we have files
files = glob.glob(model_forecast)
if not files:
#print("Skipping model", cmod, "no forecast files found.")
continue # Skip this model
ds_model = xr.open_mfdataset(model_forecast, concat_dim='init_time')
# Get Extent
ds_model = ds_model.Extent
# Select init of interest
ds_model = ds_model.where(ds_model.init_time>=np.datetime64(SD), drop=True)
# # Take mean of ensemble
# ds_model = ds_model.mean(dim='ensemble')
# Select region
ds_model = ds_model.sel(nregions=cR)
# Get model plotting specs
cc = E.model_color[cmod]
cl = E.model_linestyle[cmod]
# Plot Model
if i == 1: # Control only one initiailzation label in legend
no_init_label = False
else:
no_init_label = True
import timeit
start_time = timeit.default_timer()
ice_plot.plot_reforecast(ds=ds_model, axin=ax1,
labelin=E.model[cmod]['model_label'],
color=cc, marker=None,
linestyle=cl,
no_init_label=no_init_label)
print( (timeit.default_timer() - start_time), ' seconds.' )
# Memeory clean up
ds_model = None
# Plot observations
print('Plotting observations')
ds_obs_reg = ds_obs.sic.where(ds_obs.time>=np.datetime64(SD), drop=True)
ds_obs_reg = ds_obs_reg.where(ds_region.mask==cR)
ds_obs_reg = ((ds_obs_reg >= 0.15).astype('int') * ds_region.area).sum(dim='x').sum(dim='y')/(10**6)
ds_obs_reg.plot(ax=ax1, label=str(cdate.year)+' Observed', color='m', linewidth=8)
ax1.set_ylabel('Sea Ice Extent\n [Millions of square km]')
cxlims = ax1.get_xlim()
# # 1980-2010 Historical Interquartile Range
# plt.fill_between(ds_per_mean.time.values, ds_per_mean + ds_per_std,
# ds_per_mean - ds_per_std, alpha=0.35, label='1980-2010\nInterquartile Range', color='m')
ax1.set_xlim(cxlims) # fix x limits
cylims = ax1.get_ylim()
# Plot current date line
ax1.plot([cd, cd], [cylims[0], cylims[1]], color='k', linestyle='--')
ax1.set_title(cR_name)
# Add legend (static)
handles, labels = ax1.get_legend_handles_labels()
ax1.legend(handles[::-1], labels[::-1], loc='lower right',bbox_to_anchor=(1.35, 0))
f.autofmt_xdate()
ax1.set_ylim(cylims)
plt.subplots_adjust(right=.8)
# Save to file
base_name_out = 'Region_'+cR_name.replace(" ", "_")+'_'+metric1+'_'+runType+'_raw_predicted'
f_out = os.path.join(fig_dir, base_name_out+'.png')
f.savefig(f_out,bbox_inches='tight',dpi=200)
mpld3.save_html(f, os.path.join(fig_dir, base_name_out+'.html'))
# Mem clean up
ds_model = None
ds_obs_reg = None
f = None