Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Arctic GWL analyses
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gieße, Dr. Céline
Arctic GWL analyses
Commits
b4011a1d
Commit
b4011a1d
authored
11 months ago
by
Gieße, Dr. Céline
Browse files
Options
Downloads
Patches
Plain Diff
clean up code (sea-ice)
parent
84d5f9fb
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Sea-ice.ipynb
+24
-32
24 additions, 32 deletions
Sea-ice.ipynb
with
24 additions
and
32 deletions
Sea-ice.ipynb
+
24
−
32
View file @
b4011a1d
...
...
@@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count":
4
,
"execution_count":
3
,
"id": "6fa849af-5817-47d9-a15a-76d6f1d82b00",
"metadata": {},
"outputs": [],
...
...
@@ -78,19 +78,19 @@
"GWLcross_ind = {T: np.empty(50, dtype=int) for T in Tglob}\n",
"GWLcross_ind[\"T0\"] = 50 * [20]\n",
"\n",
"for T
i
in range(1, 3):\n",
"for T in range(1, 3):\n",
" for i in range(50):\n",
" j = 20\n",
" while GSATa_rm[i, j] < Tglob_values[T
i
]:\n",
" while GSATa_rm[i, j] < Tglob_values[T]:\n",
" j += 1\n",
" GWLcross_ind[Tglob[T
i
]][i] = j\n",
" GWLcross_ind[Tglob[T]][i] = j\n",
"\n",
"GWL_ind = {T: np.zeros((50, 20), dtype=int) for T in Tglob}\n",
"\n",
"for T
i
in range(3):\n",
"for T in range(3):\n",
" for i in range(50):\n",
" GWL_ind[Tglob[T
i
]][i, :] = np.arange(GWLcross_ind[Tglob[T
i
]][i] - 10,\n",
" GWLcross_ind[Tglob[T
i
]][i] + 10)"
" GWL_ind[Tglob[T]][i, :] = np.arange(GWLcross_ind[Tglob[T]][i] - 10,\n",
" GWLcross_ind[Tglob[T]][i] + 10)"
]
},
{
...
...
@@ -103,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count":
5
,
"execution_count":
4
,
"id": "e99f932a-601d-409f-8fb2-892de7fba8b9",
"metadata": {},
"outputs": [],
...
...
@@ -117,8 +117,8 @@
" SIA[i,:] = np.array(xr.open_dataset(file)['sia_nh']).reshape((251,12))\n",
"\n",
"# Create GWL samples of September and March SIA\n",
"SIA_sep_samples = {Tglob[T
i
]: [SIA[i, GWL_ind[Tglob[T
i
]][i, :], 8] for i in range(50)] for T
i
in range(3)}\n",
"SIA_mar_samples = {Tglob[T
i
]: [SIA[i, GWL_ind[Tglob[T
i
]][i, :], 2] for i in range(50)] for T
i
in range(3)}"
"SIA_sep_samples = {Tglob[T]: [SIA[i, GWL_ind[Tglob[T]][i, :], 8] for i in range(50)] for T in range(3)}\n",
"SIA_mar_samples = {Tglob[T]: [SIA[i, GWL_ind[Tglob[T]][i, :], 2] for i in range(50)] for T in range(3)}"
]
},
{
...
...
@@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count":
6
,
"execution_count":
5
,
"id": "b7d94506-010a-4378-a4c3-59af4914014e",
"metadata": {},
"outputs": [],
...
...
@@ -161,19 +161,19 @@
"\n",
"# Bias corrected samples for September and March\n",
"SIA_sep_corr_samples = {\n",
" Tglob[T
i
]: [SIA_sep_corr[i, GWL_ind[Tglob[T
i
]][i, :]] for i in range(50)]\n",
" for T
i
in range(3)\n",
" Tglob[T]: [SIA_sep_corr[i, GWL_ind[Tglob[T]][i, :]] for i in range(50)]\n",
" for T in range(3)\n",
"}\n",
"\n",
"SIA_mar_corr_samples = {\n",
" Tglob[T
i
]: [SIA_mar_corr[i, GWL_ind[Tglob[T
i
]][i, :]] for i in range(50)]\n",
" for T
i
in range(3)\n",
" Tglob[T]: [SIA_mar_corr[i, GWL_ind[Tglob[T]][i, :]] for i in range(50)]\n",
" for T in range(3)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count":
7
,
"execution_count":
6
,
"id": "ece4a709-832a-4316-ab0e-dfa9699f0b76",
"metadata": {},
"outputs": [],
...
...
@@ -204,7 +204,7 @@
},
{
"cell_type": "code",
"execution_count":
8
,
"execution_count":
7
,
"id": "adc41e36-aefc-41be-a496-4a50f4fe47f3",
"metadata": {},
"outputs": [
...
...
@@ -232,22 +232,22 @@
"fig, ax = plt.subplots(1, 3, figsize=(15, 5))\n",
"\n",
"# Loop over the three GWLs\n",
"for T
i
in range(3):\n",
"for T in range(3):\n",
" circle_AO = Circle(center, radius_AO, fc=cmap(0.99))\n",
" ax[T
i
].add_patch(circle_AO)\n",
" ax[T].add_patch(circle_AO)\n",
" \n",
" # Draw sea ice area circles\n",
" for perc in range(100, -1, -1):\n",
" radius_SIA = radius_AO * math.sqrt(np.percentile(SIA_sep_corr_detrend_samples[Tglob[T
i
]], perc) / AO_area)\n",
" radius_SIA = radius_AO * math.sqrt(np.percentile(SIA_sep_corr_detrend_samples[Tglob[T]], perc) / AO_area)\n",
" circle_SIA = Circle(center, radius_SIA, fc=cmap(perc / 100))\n",
" ax[T
i
].add_patch(circle_SIA)\n",
" ax[T].add_patch(circle_SIA)\n",
" \n",
" # Add a white circle at the end\n",
" circle_SIA = Circle(center, radius_SIA, fc='white')\n",
" ax[T
i
].add_patch(circle_SIA)\n",
" ax[T].add_patch(circle_SIA)\n",
" \n",
" ax[T
i
].set_aspect('equal')\n",
" ax[T
i
].axis('off')\n",
" ax[T].set_aspect('equal')\n",
" ax[T].axis('off')\n",
"\n",
"# Add a colorbar\n",
"cax = fig.add_axes([0.94, 0.18, 0.015, 0.64])\n",
...
...
@@ -260,14 +260,6 @@
"# Show plot\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f9fe742-19db-47fb-9b60-c7b6e26bda4f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
...
...
%% Cell type:code id:fb7d6d3e-7726-49d2-a8c5-85bad5854f10 tags:
```
python
import
numpy
as
np
import
xarray
as
xr
import
matplotlib.pyplot
as
plt
import
pandas
as
pd
import
math
from
sklearn.linear_model
import
LinearRegression
from
matplotlib.patches
import
Circle
from
matplotlib
import
cm
,
colors
# Set font sizes for plots
SMALL_SIZE
=
12
MEDIUM_SIZE
=
14
BIGGER_SIZE
=
16
plt
.
rc
(
'
font
'
,
size
=
MEDIUM_SIZE
)
# controls default text sizes
plt
.
rc
(
'
axes
'
,
titlesize
=
MEDIUM_SIZE
)
# fontsize of the axes title
plt
.
rc
(
'
axes
'
,
labelsize
=
MEDIUM_SIZE
)
# fontsize of the x and y labels
plt
.
rc
(
'
xtick
'
,
labelsize
=
SMALL_SIZE
)
# fontsize of the tick labels
plt
.
rc
(
'
ytick
'
,
labelsize
=
SMALL_SIZE
)
# fontsize of the tick labels
plt
.
rc
(
'
legend
'
,
fontsize
=
MEDIUM_SIZE
)
# legend fontsize
plt
.
rc
(
'
figure
'
,
titlesize
=
BIGGER_SIZE
)
# fontsize of the figure title
plt
.
rc
(
'
font
'
,
size
=
MEDIUM_SIZE
)
# Global warming levels
Tglob
=
[
"
T0
"
,
"
T15
"
,
"
T27
"
]
Tglob_values
=
[
0
,
1.5
,
2.7
]
# Data path
data_path
=
'
/work/uo1075/u241297/data/large_ensembles/CMIP6/
'
```
%% Cell type:markdown id:6528eeee-cbb0-44f2-9621-1fb397b2fb3d tags:
# Global warming levels
%% Cell type:code id:c60ff72f-9206-4cf6-84fc-d7e67828a052 tags:
```
python
path
=
data_path
+
'
MPI-ESM1-2-LR/global-mean/
'
# Load global-mean surface air temperature (GSAT) data
GSAT
=
np
.
zeros
((
50
,
251
))
for
i
in
range
(
50
):
file
=
path
+
"
tas_hist_ssp585_r
"
+
str
(
i
+
1
)
+
"
_global-mean.nc
"
GSAT
[
i
,
:]
=
np
.
squeeze
(
xr
.
open_dataset
(
file
)[
'
tas
'
])
-
273.15
GSAT_rm
=
np
.
array
(
pd
.
DataFrame
(
GSAT
).
rolling
(
20
,
axis
=
1
,
center
=
True
).
mean
())
Tzero_GSAT
=
np
.
mean
(
GSAT
[:,
0
:
51
])
# 1850-1900
GSATa
=
GSAT
-
Tzero_GSAT
GSATa_rm
=
GSAT_rm
-
Tzero_GSAT
```
%% Cell type:code id:6fa849af-5817-47d9-a15a-76d6f1d82b00 tags:
```
python
# Determine the indices where GSAT crosses the global warming levels
GWLcross_ind
=
{
T
:
np
.
empty
(
50
,
dtype
=
int
)
for
T
in
Tglob
}
GWLcross_ind
[
"
T0
"
]
=
50
*
[
20
]
for
T
i
in
range
(
1
,
3
):
for
T
in
range
(
1
,
3
):
for
i
in
range
(
50
):
j
=
20
while
GSATa_rm
[
i
,
j
]
<
Tglob_values
[
T
i
]:
while
GSATa_rm
[
i
,
j
]
<
Tglob_values
[
T
]:
j
+=
1
GWLcross_ind
[
Tglob
[
T
i
]][
i
]
=
j
GWLcross_ind
[
Tglob
[
T
]][
i
]
=
j
GWL_ind
=
{
T
:
np
.
zeros
((
50
,
20
),
dtype
=
int
)
for
T
in
Tglob
}
for
T
i
in
range
(
3
):
for
T
in
range
(
3
):
for
i
in
range
(
50
):
GWL_ind
[
Tglob
[
T
i
]][
i
,
:]
=
np
.
arange
(
GWLcross_ind
[
Tglob
[
T
i
]][
i
]
-
10
,
GWLcross_ind
[
Tglob
[
T
i
]][
i
]
+
10
)
GWL_ind
[
Tglob
[
T
]][
i
,
:]
=
np
.
arange
(
GWLcross_ind
[
Tglob
[
T
]][
i
]
-
10
,
GWLcross_ind
[
Tglob
[
T
]][
i
]
+
10
)
```
%% Cell type:markdown id:a9134a75-ce3e-4062-b4ef-7652ba0b5a34 tags:
# Sea ice area
%% Cell type:code id:e99f932a-601d-409f-8fb2-892de7fba8b9 tags:
```
python
# Load Sea Ice Area (SIA) data
path
=
data_path
+
'
MPI-ESM1-2-LR/sia_nh/
'
SIA
=
np
.
zeros
((
50
,
251
,
12
))
for
i
in
range
(
50
):
file
=
path
+
"
sia_nh_MPI-ESM1-2-LR_r
"
+
str
(
i
+
1
)
+
"
i1p1f1.nc
"
SIA
[
i
,:]
=
np
.
array
(
xr
.
open_dataset
(
file
)[
'
sia_nh
'
]).
reshape
((
251
,
12
))
# Create GWL samples of September and March SIA
SIA_sep_samples
=
{
Tglob
[
T
i
]:
[
SIA
[
i
,
GWL_ind
[
Tglob
[
T
i
]][
i
,
:],
8
]
for
i
in
range
(
50
)]
for
T
i
in
range
(
3
)}
SIA_mar_samples
=
{
Tglob
[
T
i
]:
[
SIA
[
i
,
GWL_ind
[
Tglob
[
T
i
]][
i
,
:],
2
]
for
i
in
range
(
50
)]
for
T
i
in
range
(
3
)}
SIA_sep_samples
=
{
Tglob
[
T
]:
[
SIA
[
i
,
GWL_ind
[
Tglob
[
T
]][
i
,
:],
8
]
for
i
in
range
(
50
)]
for
T
in
range
(
3
)}
SIA_mar_samples
=
{
Tglob
[
T
]:
[
SIA
[
i
,
GWL_ind
[
Tglob
[
T
]][
i
,
:],
2
]
for
i
in
range
(
50
)]
for
T
in
range
(
3
)}
```
%% Cell type:markdown id:a5d79ef0-78e6-49a5-bc79-b56988fafac8 tags:
## Bias correction (based on Niederdrenk and Notz, 2018)
%% Cell type:code id:b7d94506-010a-4378-a4c3-59af4914014e tags:
```
python
# Observational regression parameters (Niederdrenk and Notz, 2018)
slope_obs_sep
,
intercept_obs_sep
=
-
4.1
,
7.97
slope_obs_mar
,
intercept_obs_mar
=
-
1.6
,
15.08
# Model regression line
GSATa_ensmean
=
np
.
mean
(
GSATa
,
axis
=
0
)
SIA_sep_ensmean
=
np
.
mean
(
SIA
[:,
:,
8
],
axis
=
0
)
SIA_mar_ensmean
=
np
.
mean
(
SIA
[:,
:,
2
],
axis
=
0
)
model_sep
=
LinearRegression
().
fit
(
GSATa_ensmean
[
103
:
167
].
reshape
((
-
1
,
1
)),
SIA_sep_ensmean
[
103
:
167
])
model_mar
=
LinearRegression
().
fit
(
GSATa_ensmean
[
103
:
167
].
reshape
((
-
1
,
1
)),
SIA_mar_ensmean
[
103
:
167
])
# Bias correction function
def
bias_correction
(
SIA
,
model
,
slope_obs
,
intercept_obs
,
GSATa
):
correction
=
intercept_obs
-
model
.
intercept_
+
(
slope_obs
-
model
.
coef_
[
0
])
*
GSATa
corrected_SIA
=
SIA
+
correction
#[:, np.newaxis]
corrected_SIA
[
corrected_SIA
<
0
]
=
0
return
corrected_SIA
# Apply bias correction for September and March
SIA_sep_corr
=
bias_correction
(
SIA
[:,
:,
8
],
model_sep
,
slope_obs_sep
,
intercept_obs_sep
,
GSATa
)
SIA_mar_corr
=
bias_correction
(
SIA
[:,
:,
2
],
model_mar
,
slope_obs_mar
,
intercept_obs_mar
,
GSATa
)
# Bias corrected samples for September and March
SIA_sep_corr_samples
=
{
Tglob
[
T
i
]:
[
SIA_sep_corr
[
i
,
GWL_ind
[
Tglob
[
T
i
]][
i
,
:]]
for
i
in
range
(
50
)]
for
T
i
in
range
(
3
)
Tglob
[
T
]:
[
SIA_sep_corr
[
i
,
GWL_ind
[
Tglob
[
T
]][
i
,
:]]
for
i
in
range
(
50
)]
for
T
in
range
(
3
)
}
SIA_mar_corr_samples
=
{
Tglob
[
T
i
]:
[
SIA_mar_corr
[
i
,
GWL_ind
[
Tglob
[
T
i
]][
i
,
:]]
for
i
in
range
(
50
)]
for
T
i
in
range
(
3
)
Tglob
[
T
]:
[
SIA_mar_corr
[
i
,
GWL_ind
[
Tglob
[
T
]][
i
,
:]]
for
i
in
range
(
50
)]
for
T
in
range
(
3
)
}
```
%% Cell type:code id:ece4a709-832a-4316-ab0e-dfa9699f0b76 tags:
```
python
# Define a function for detrending by subtraction of ensemble mean
def
detrend_samples
(
samples
,
Tglob
):
ensmean_samples
=
{
T
:
np
.
mean
(
samples
[
T
],
axis
=
0
)
for
T
in
Tglob
}
detrend_samples
=
{
T
:
samples
[
T
]
-
ensmean_samples
[
T
][
np
.
newaxis
,
:]
+
np
.
mean
(
ensmean_samples
[
T
])
for
T
in
Tglob
}
for
T
in
Tglob
:
detrend_samples
[
T
][
detrend_samples
[
T
]
<
0
]
=
0
return
detrend_samples
# Detrend by ensemble mean
SIA_sep_corr_detrend_samples
=
detrend_samples
(
SIA_sep_corr_samples
,
Tglob
)
SIA_mar_corr_detrend_samples
=
detrend_samples
(
SIA_mar_corr_samples
,
Tglob
)
```
%% Cell type:markdown id:8771f460-7cb6-475f-865a-e8c5ac2c2fbc tags:
## Circle plots
%% Cell type:code id:adc41e36-aefc-41be-a496-4a50f4fe47f3 tags:
```
python
# Constants
AO_area
=
14.06
center
=
(
0.5
,
0.5
)
radius_AO
=
0.5
# Load the colormap
cmap
=
cm
.
get_cmap
(
'
Blues
'
,
101
)
# Create subplots
fig
,
ax
=
plt
.
subplots
(
1
,
3
,
figsize
=
(
15
,
5
))
# Loop over the three GWLs
for
T
i
in
range
(
3
):
for
T
in
range
(
3
):
circle_AO
=
Circle
(
center
,
radius_AO
,
fc
=
cmap
(
0.99
))
ax
[
T
i
].
add_patch
(
circle_AO
)
ax
[
T
].
add_patch
(
circle_AO
)
# Draw sea ice area circles
for
perc
in
range
(
100
,
-
1
,
-
1
):
radius_SIA
=
radius_AO
*
math
.
sqrt
(
np
.
percentile
(
SIA_sep_corr_detrend_samples
[
Tglob
[
T
i
]],
perc
)
/
AO_area
)
radius_SIA
=
radius_AO
*
math
.
sqrt
(
np
.
percentile
(
SIA_sep_corr_detrend_samples
[
Tglob
[
T
]],
perc
)
/
AO_area
)
circle_SIA
=
Circle
(
center
,
radius_SIA
,
fc
=
cmap
(
perc
/
100
))
ax
[
T
i
].
add_patch
(
circle_SIA
)
ax
[
T
].
add_patch
(
circle_SIA
)
# Add a white circle at the end
circle_SIA
=
Circle
(
center
,
radius_SIA
,
fc
=
'
white
'
)
ax
[
T
i
].
add_patch
(
circle_SIA
)
ax
[
T
].
add_patch
(
circle_SIA
)
ax
[
T
i
].
set_aspect
(
'
equal
'
)
ax
[
T
i
].
axis
(
'
off
'
)
ax
[
T
].
set_aspect
(
'
equal
'
)
ax
[
T
].
axis
(
'
off
'
)
# Add a colorbar
cax
=
fig
.
add_axes
([
0.94
,
0.18
,
0.015
,
0.64
])
norm
=
colors
.
Normalize
(
vmin
=
0
,
vmax
=
100
)
sm
=
plt
.
cm
.
ScalarMappable
(
cmap
=
cmap
.
reversed
(),
norm
=
norm
)
sm
.
set_array
([])
cbar
=
plt
.
colorbar
(
sm
,
cax
=
cax
)
cbar
.
set_label
(
'
Probability of sea-ice cover [%]
'
)
# Show plot
plt
.
show
()
```
%% Output
%% Cell type:code id:0f9fe742-19db-47fb-9b60-c7b6e26bda4f tags:
```
python
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment