-
Notifications
You must be signed in to change notification settings - Fork 3
/
mip_std_massloss.py
290 lines (272 loc) · 12.5 KB
/
mip_std_massloss.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
from numpy import *
def mip_std_massloss (roms_logfile, roms_logfile_bs, fesom_logfile_lr, fesom_logfile_bs_lr, fesom_logfile_hr, fesom_logfile_bs_hr):
year_start = 1992
year_end = 2016
# First year to consider
calc_start = 2002
# Days per output in FESOM
days_per_output = 5
# Name of each ice shelf
names = ['Total Mass Loss', 'Larsen D Ice Shelf', 'Larsen C Ice Shelf', 'Wilkins & George VI & Stange & Bach Ice Shelves', 'Ronne-Filchner Ice Shelf', 'Abbot Ice Shelf', 'Pine Island Glacier Ice Shelf', 'Thwaites Ice Shelf', 'Dotson Ice Shelf', 'Getz Ice Shelf', 'Nickerson Ice Shelf', 'Sulzberger Ice Shelf', 'Mertz Ice Shelf', 'Totten & Moscow University Ice Shelves', 'Shackleton Ice Shelf', 'West Ice Shelf', 'Amery Ice Shelf', 'Prince Harald Ice Shelf', 'Baudouin & Borchgrevink Ice Shelves', 'Lazarev Ice Shelf', 'Nivl Ice Shelf', 'Fimbul & Jelbart & Ekstrom Ice Shelves', 'Brunt & Riiser-Larsen Ice Shelves', 'Ross Ice Shelf']
num_shelves = len(names)
# Some Bellingshausen ice shelves were split up later
names_bs = ['Wilkins Ice Shelf', 'Stange Ice Shelf', 'George VI Ice Shelf']
num_shelves_bs = len(names_bs)
num_years = year_end-year_start+1
num_years_calc = year_end-calc_start+1
# Read ROMS logfile
roms_time = []
f = open(roms_logfile, 'r')
# Skip the first line (header for time array)
f.readline()
for line in f:
try:
roms_time.append(float(line))
except(ValueError):
# Reached the header for the next variable
break
# Add start year to time array
roms_time = array(roms_time) + year_start
# Set up array for mass loss values at each ice shelf
roms_massloss = empty([num_shelves, len(roms_time)])
index = 0
# Loop over ice shelves
while index < num_shelves:
t = 0
for line in f:
try:
roms_massloss[index, t] = float(line)
t += 1
except(ValueError):
# Reached the header for the next ice shelf
break
index +=1
f.close()
# Repeat for Bellingshausen
f = open(roms_logfile_bs, 'r')
f.readline()
# Skip the time values (should be the same)
for line in f:
try:
tmp = float(line)
except(ValueError):
# Reached the header for the next variable
break
roms_massloss_bs = empty([num_shelves_bs, len(roms_time)])
index = 0
while index < num_shelves_bs:
t = 0
for line in f:
try:
roms_massloss_bs[index, t] = float(line)
t += 1
except(ValueError):
break
index +=1
# Read FESOM logfiles
# Low-res
tmp = []
f = open(fesom_logfile_lr, 'r')
# Skip the first line (header)
f.readline()
# Read total mass loss
num_time = 0
for line in f:
try:
tmp.append(float(line))
num_time += 1
except(ValueError):
# Reached the header for the next variable
break
# Set up array for mass loss values at each ice shelf
fesom_massloss_lr = empty([num_shelves, num_time])
# Save the total values in the first index
fesom_massloss_lr[0,:] = array(tmp)
# Loop over ice shelves
index = 1
while index < num_shelves:
t = 0
for line in f:
try:
fesom_massloss_lr[index,t] = float(line)
t += 1
except(ValueError):
# Reached the header for the next ice shelf
break
index += 1
f.close()
# Repeat for Bellingshausen
f = open(fesom_logfile_bs_lr, 'r')
f.readline()
fesom_massloss_bs_lr = empty([num_shelves_bs, num_time])
index = 0
while index < num_shelves_bs:
t = 0
for line in f:
try:
fesom_massloss_bs_lr[index,t] = float(line)
t += 1
except(ValueError):
break
index += 1
f.close()
# High-res
tmp = []
f = open(fesom_logfile_hr, 'r')
f.readline()
num_time = 0
for line in f:
try:
tmp.append(float(line))
num_time += 1
except(ValueError):
break
fesom_massloss_hr = empty([num_shelves, num_time])
fesom_massloss_hr[0,:] = array(tmp)
index = 1
while index < num_shelves:
t = 0
for line in f:
try:
fesom_massloss_hr[index,t] = float(line)
t += 1
except(ValueError):
break
index += 1
f.close()
f = open(fesom_logfile_bs_hr, 'r')
f.readline()
fesom_massloss_bs_hr = empty([num_shelves_bs, num_time])
index = 0
while index < num_shelves_bs:
t = 0
for line in f:
try:
fesom_massloss_bs_hr[index,t] = float(line)
t += 1
except(ValueError):
break
index += 1
f.close()
# Annually average
# ROMS
roms_massloss_avg = empty([num_shelves, num_years])
for index in range(num_shelves):
roms_massloss_avg[index,:] = roms_annual_avg(roms_time, roms_massloss[index,:], year_start)
roms_massloss_bs_avg = empty([num_shelves_bs, num_years])
for index in range(num_shelves_bs):
roms_massloss_bs_avg[index,:] = roms_annual_avg(roms_time, roms_massloss_bs[index,:], year_start)
# Low-res FESOM
fesom_massloss_lr_avg = empty([num_shelves, num_years])
for index in range(num_shelves):
fesom_massloss_lr_avg[index,:] = fesom_annual_avg(fesom_massloss_lr[index,:], days_per_output)
fesom_massloss_bs_lr_avg = empty([num_shelves_bs, num_years])
for index in range(num_shelves_bs):
fesom_massloss_bs_lr_avg[index,:] = fesom_annual_avg(fesom_massloss_bs_lr[index,:], days_per_output)
# High-res FESOM
fesom_massloss_hr_avg = empty([num_shelves, num_years])
for index in range(num_shelves):
fesom_massloss_hr_avg[index,:] = fesom_annual_avg(fesom_massloss_hr[index,:], days_per_output)
fesom_massloss_bs_hr_avg = empty([num_shelves_bs, num_years])
for index in range(num_shelves_bs):
fesom_massloss_bs_hr_avg[index,:] = fesom_annual_avg(fesom_massloss_bs_hr[index,:], days_per_output)
# Slice off the years we don't care about
roms_massloss_avg = roms_massloss_avg[:,calc_start-year_start:]
roms_massloss_bs_avg = roms_massloss_bs_avg[:,calc_start-year_start:]
fesom_massloss_lr_avg = fesom_massloss_lr_avg[:,calc_start-year_start:]
fesom_massloss_bs_lr_avg = fesom_massloss_bs_lr_avg[:,calc_start-year_start:]
fesom_massloss_hr_avg = fesom_massloss_hr_avg[:,calc_start-year_start:]
fesom_massloss_bs_hr_avg = fesom_massloss_bs_hr_avg[:,calc_start-year_start:]
# Loop over ice shelves
for index in range(num_shelves):
print names[index]
# Calculate standard deviation as percent of annual mean
print 'MetROMS: ' + str(std(roms_massloss_avg[index])/mean(roms_massloss_avg[index])*100) + '%'
print 'FESOM low-res: ' + str(std(fesom_massloss_lr_avg[index])/mean(fesom_massloss_lr_avg[index])*100) + '%'
print 'FESOM high-res: ' + str(std(fesom_massloss_hr_avg[index])/mean(fesom_massloss_hr_avg[index])*100) + '%'
#print 'MetROMS: ' + str((median(roms_massloss_avg[index])-mean(roms_massloss_avg[index]))/mean(roms_massloss_avg[index])*100)
#print 'FESOM low-res: ' + str((median(fesom_massloss_lr_avg[index])-mean(fesom_massloss_lr_avg[index]))/mean(fesom_massloss_lr_avg[index])*100)
#print 'FESOM high-res: ' + str((median(fesom_massloss_hr_avg[index])-mean(fesom_massloss_hr_avg[index]))/mean(fesom_massloss_hr_avg[index])*100)
#print 'MetROMS: mean ' + str(mean(roms_massloss_avg[index])) + ', median ' + str(median(roms_massloss_avg[index]))
#print 'FESOM low-res: mean ' + str(mean(fesom_massloss_lr_avg[index])) + ', median ' + str(median(fesom_massloss_lr_avg[index]))
#print 'FESOM high-res: mean ' + str(mean(fesom_massloss_hr_avg[index])) + ', median ' + str(median(fesom_massloss_hr_avg[index]))
# Repeat for Bellingshausen ice shelves
for index in range(num_shelves_bs):
print names_bs[index]
print 'MetROMS: ' + str(std(roms_massloss_bs_avg[index])/mean(roms_massloss_bs_avg[index])*100) + '%'
print 'FESOM low-res: ' + str(std(fesom_massloss_bs_lr_avg[index])/mean(fesom_massloss_bs_lr_avg[index])*100) + '%'
print 'FESOM high-res: ' + str(std(fesom_massloss_bs_hr_avg[index])/mean(fesom_massloss_bs_hr_avg[index])*100) + '%'
#print 'MetROMS: ' + str((median(roms_massloss_bs_avg[index])-mean(roms_massloss_bs_avg[index]))/mean(roms_massloss_bs_avg[index])*100)
#print 'FESOM low-res: ' + str((median(fesom_massloss_bs_lr_avg[index])-mean(fesom_massloss_bs_lr_avg[index]))/mean(fesom_massloss_bs_lr_avg[index])*100)
#print 'FESOM high-res: ' + str((median(fesom_massloss_bs_hr_avg[index])-mean(fesom_massloss_bs_hr_avg[index]))/mean(fesom_massloss_bs_hr_avg[index])*100)
#print 'MetROMS: mean ' + str(mean(roms_massloss_bs_avg[index])) + ', median ' + str(median(roms_massloss_bs_avg[index]))
#print 'FESOM low-res: mean ' + str(mean(fesom_massloss_bs_lr_avg[index])) + ', median ' + str(median(fesom_massloss_bs_lr_avg[index]))
#print 'FESOM high-res: mean ' + str(mean(fesom_massloss_bs_hr_avg[index])) + ', median ' + str(median(fesom_massloss_bs_hr_avg[index]))
# Annually average the given ROMS data. Note that ROMS takes leap years into
# account for its output, i.e. the 5-day averaging period holds true throughout
# the entire simulation with no days skipped.
# Input:
# time = 1D array of ROMS time values, in years, with year_start added (eg
# 1992.001, 1993.50) assuming year length of 365.25 days
# data = 1D array of ROMS data corresponding to time array
# year_start = integer containing the first year to process
# Output: data_avg = 1D array of annually averaged data
def roms_annual_avg (time, data, year_start):
data_avg = []
year = year_start
# Loop over years until we run out of data
while True:
# Find the first index after the beginning of this year
tmp1 = nonzero(time >= year)[0]
if len(tmp1)==0:
# No such index, we have run out of data
break
t_start = tmp1[0]
# Find the first index after the beginning of next year
tmp2 = nonzero(time >= year+1)[0]
if len(tmp2)==0:
# No such index, but we might not have run out of data
# eg simulation that ends on 31 December 2016
t_end = len(time)
else:
t_end = tmp2[0]
if t_end - t_start < 73:
# This is a partial year, don't count it
break
# Calculate mean between these bounds
data_avg.append(mean(array(data[t_start:t_end])))
# Increment year
year += 1
return data_avg
# Annually average the given FESOM data. Note that FESOM neglects leap years
# in its output, i.e. the 5-day averaging period will skip 1 day every 4 years,
# so every year has exactly 365/5 = 73 batches of output.
# Input:
# data = 1D array of FESOM data
# days_per_output = averaging period for data
# Output: data_avg = 1D array of annually averaged data
def fesom_annual_avg (data, days_per_output):
peryear = 365/days_per_output
data_avg = []
year = 0
# Loop over years
while True:
if len(data) < peryear*(year+1):
# Run out of data
# FESOM output comes in annual files so we don't have to worry about
# partial years like in ROMS
break
# Calculate mean for this year
data_avg.append(mean(array(data[peryear*year:peryear*(year+1)])))
# Increment year
year += 1
return data_avg
# Command-line interface
if __name__ == "__main__":
roms_logfile = raw_input("Path to ROMS logfile from timeseries_massloss.py: ")
roms_logfile_bs = raw_input("Path to ROMS logfile from timeseries_massloss_bellingshausen.py: ")
fesom_logfile_lr = raw_input("Path to FESOM low-res logfile from timeseries_massloss.py: ")
fesom_logfile_bs_lr = raw_input("Path to FESOM low-res logfile from timeseries_massloss_bellingshausen.py: ")
fesom_logfile_hr = raw_input("Path to FESOM high-res logfile from timeseries_massloss.py: ")
fesom_logfile_bs_hr = raw_input("Path to FESOM high-res logfile from timeseries_massloss_bellingshausen.py: ")
mip_std_massloss(roms_logfile, roms_logfile_bs, fesom_logfile_lr, fesom_logfile_bs_lr, fesom_logfile_hr, fesom_logfile_bs_hr)