-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_sea_ice_regrid_final.py
308 lines (213 loc) · 9.03 KB
/
10_sea_ice_regrid_final.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.3.0
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# +
##################################################################################################################
# IMPORT MODULES
##################################################################################################################
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.colors import LogNorm
import matplotlib.ticker as mticker
import matplotlib.path as mpath
import xarray as xr
import numpy as np
import numpy.ma as ma
import glob
import pandas as pd
from scipy.stats import norm
import scipy
import seaborn as sns
from scipy import stats
from pyhdf.SD import SD, SDC, SDAttr, HDF4Error
from pyhdf import HDF, VS, V
from pyhdf.HDF import *
from pyhdf.VS import *
import pprint
import os
import os.path
import sys
import matplotlib as mpl
import cartopy.crs as ccrs
#import pyresample
from basepath import data_path_base
import netCDF4
import datetime
label_size=14
mpl.rcParams['xtick.labelsize'] = label_size
mpl.rcParams['ytick.labelsize'] = label_size
#from cmcrameri import cm as cm_crameri
import xesmf as xe
def discrete_cmap(N, base_cmap=None):
"""Create an N-bin discrete colormap from the specified input map"""
# Note that if base_cmap is a string or None, you can simply do
# return plt.cm.get_cmap(base_cmap, N)
# The following works for string, None, or a colormap instance:
base = plt.cm.get_cmap(base_cmap)
color_list = base(np.linspace(0, 1, N))
cmap_name = base.name + str(N)
return base.from_list(cmap_name, color_list, N)
##########################################
def plt_var_polar(fig, ax, lon2d, lat2d, var, label, hemisphere, custom_cmap, vmin, vmax, n_cmap):
theta = np.linspace(0, 2*np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)
ax.coastlines(resolution='110m', linewidth = 2.0)
if hemisphere == 'S':
ax.set_extent([-180, 180, -90, -42.5], ccrs.PlateCarree())
if hemisphere == 'N':
ax.set_extent([-180, 180, 90, 42.5], ccrs.PlateCarree())
gl = ax.gridlines(crs = ccrs.PlateCarree(), draw_labels=False, linewidth = 1.4, color = 'k', linestyle = '--')
gl.xlocator = mticker.FixedLocator([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180])
if hemisphere == 'S':
gl.ylocator = mticker.FixedLocator([-90, -80, -70, -60, -50, -40])
if hemisphere == 'N':
gl.ylocator = mticker.FixedLocator([90, 80, 70, 60, 50, 40])
gl.n_steps = 50
ax.set_axis_off()
cs = ax.pcolormesh(lon2d, lat2d, var, cmap = custom_cmap, vmin = vmin, vmax = vmax, alpha = 1.0, transform = ccrs.PlateCarree())
return(cs)
# +
ds_grid = xr.open_dataset('LongitudeLatitudeGrid-n6250-Arctic.hdf')
lon = ds_grid.Longitudes
lat = ds_grid.Latitudes
ds_grid = xr.open_dataset('LongitudeLatitudeGrid-s6250-Antarctic.hdf')
lon_s = ds_grid.Longitudes
lat_s = ds_grid.Latitudes
# +
sic_avg_n_max = xr.DataArray(np.zeros([1792,1216],dtype=np.float64),coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic')
sic_avg_n_min = xr.DataArray(np.zeros([1792,1216],dtype=np.float64),coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic')
sic_avg_s_max = xr.DataArray(np.zeros([1328,1264],dtype=np.float64),coords={'lon': (["x","y"], lon_s),
'lat': (["x","y"], lat_s)},
dims=['x','y'], name = 'sic')
sic_avg_s_min = xr.DataArray(np.zeros([1328,1264],dtype=np.float64),coords={'lon': (["x","y"], lon_s),
'lat': (["x","y"], lat_s)},
dims=['x','y'], name = 'sic')
# +
# Averaging SIC over days of maximum/minimum sea ice extent in Arctic/Antarctica
###################################################################################
files = sorted(glob.glob(data_path_base+'/data/sea_ice/arctic_maximum/*-v5.4.nc'))
for f in files:
print(f)
ds = xr.open_dataset(f)
sic = xr.DataArray(ds.z,coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic')
sic_avg_n_max += sic
sic_avg_n_max = sic_avg_n_max/len(files)
###################################################################################
files = sorted(glob.glob(data_path_base+'/data/sea_ice/arctic_minimum/*-v5.4.nc'))
for f in files:
print(f)
ds = xr.open_dataset(f)
sic = xr.DataArray(ds.z,coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic')
sic_avg_n_min += sic
sic_avg_n_min = sic_avg_n_min/len(files)
###################################################################################
files = sorted(glob.glob(data_path_base+'/data/sea_ice/antarctica_maximum/*-v5.4.nc'))
for f in files:
print(f)
ds = xr.open_dataset(f)
sic = xr.DataArray(ds.z,coords={'lon': (["x","y"], lon_s),
'lat': (["x","y"], lat_s)},
dims=['x','y'], name = 'sic')
sic_avg_s_max += sic
sic_avg_s_max = sic_avg_s_max/len(files)
###################################################################################
files = sorted(glob.glob(data_path_base+'/data/sea_ice/antarctica_minimum/*-v5.4.nc'))
for f in files:
print(f)
ds = xr.open_dataset(f)
sic = xr.DataArray(ds.z,coords={'lon': (["x","y"], lon_s),
'lat': (["x","y"], lat_s)},
dims=['x','y'], name = 'sic')
sic_avg_s_min += sic
sic_avg_s_min = sic_avg_s_min/len(files)
# -
ice_n_djf = xr.where(sic_avg_n_max >= 15.0, 100.0, sic_avg_n_max)
ice_n_jja = xr.where(sic_avg_n_min >= 15.0, 100.0, sic_avg_n_min)
ice_s_djf = xr.where(sic_avg_s_min >= 15.0, 100.0, sic_avg_s_min)
ice_s_jja = xr.where(sic_avg_s_max >= 15.0, 100.0, sic_avg_s_max)
# +
lon = ice_s_djf.lon
lat = ice_s_djf.lat
ice_s_xr = xr.DataArray(ice_s_djf,coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic_djf')
ds_ice_s_djf = ice_s_xr.to_dataset(dim=None, name='sic_djf')
dr_ice_s_djf = ds_ice_s_djf.sic_djf
ice_s_xr = xr.DataArray(ice_s_jja,coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic_jja')
ds_ice_s_jja = ice_s_xr.to_dataset(dim=None, name='sic_jja')
dr_ice_s_jja = ds_ice_s_jja.sic_jja
#########################
lon = ice_n_djf.lon
lat = ice_n_djf.lat
ice_n_xr = xr.DataArray(ice_n_djf,coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic_djf')
ds_ice_n_djf = ice_n_xr.to_dataset(dim=None, name='sic_djf')
dr_ice_n_djf = ds_ice_n_djf.sic_djf
ice_n_xr = xr.DataArray(ice_n_jja,coords={'lon': (["x","y"], lon),
'lat': (["x","y"], lat)},
dims=['x','y'], name = 'sic_jja')
ds_ice_n_jja = ice_n_xr.to_dataset(dim=None, name='sic_jja')
dr_ice_n_jja = ds_ice_n_jja.sic_jja
# -
ds_out = xr.Dataset({'lat': (['lat'], np.arange(-89.875, 90., 0.25)),
'lon': (['lon'], np.arange(0.125, 360., 0.25)),
}
)
ds_out
# +
regridder = xe.Regridder(ds_ice_s_djf, ds_out, 'bilinear')
dr_out_ice_s_djf = regridder(dr_ice_s_djf)
dr_out_ice_s_djf.to_netcdf('output_files/sea_ice/ice_s_djf_regrid.nc')
ds_out = xr.Dataset({'lat': (['lat'], np.arange(-89.875, 90., 0.25)),
'lon': (['lon'], np.arange(0.125, 360., 0.25)),
}
)
regridder = xe.Regridder(ds_ice_s_jja, ds_out, 'bilinear')
dr_out_ice_s_jja = regridder(dr_ice_s_jja)
dr_out_ice_s_jja.to_netcdf('output_files/sea_ice/ice_s_jja_regrid.nc')
########################################
ds_out = xr.Dataset({'lat': (['lat'], np.arange(-89.875, 90., 0.25)),
'lon': (['lon'], np.arange(0.125, 360., 0.25)),
}
)
regridder = xe.Regridder(ds_ice_n_jja, ds_out, 'bilinear')
dr_out_ice_n_jja = regridder(dr_ice_n_jja)
dr_out_ice_n_jja.to_netcdf('output_files/sea_ice/ice_n_jja_regrid.nc')
ds_out = xr.Dataset({'lat': (['lat'], np.arange(-89.875, 90., 0.25)),
'lon': (['lon'], np.arange(0.125, 360., 0.25)),
}
)
regridder = xe.Regridder(ds_ice_n_djf, ds_out, 'bilinear')
dr_out_ice_n_djf = regridder(dr_ice_n_djf)
dr_out_ice_n_djf.to_netcdf('output_files/sea_ice/ice_n_djf_regrid.nc')
sys.exit()
# -