-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
paper_figures.py
1220 lines (1139 loc) · 34.5 KB
/
paper_figures.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:hydrogen
# text_representation:
# extension: .py
# format_name: hydrogen
# format_version: '1.2'
# jupytext_version: 1.2.0
# kernelspec:
# display_name: deepbedmap
# language: python
# name: deepbedmap
# ---
# %% [markdown]
# # **DeepBedMap paper figures**
#
# Code used to produce each figure in the DeepBedMap paper.
# %%
import itertools
import os
import subprocess
import geopandas as gpd
import IPython.display
import numpy as np
import pandas as pd
import pygmt as gmt
import rasterio
import skimage
import xarray as xr
from paper.figures.PlotNeuralNet.pycore.tikzeng import (
to_head,
to_cor,
to_begin,
to_input,
to_Conv,
# to_ConvConvRelu,
to_Pool,
# to_UnPool,
# to_ConvRes,
# to_ConvSoftMax,
# to_SoftMax,
to_connection,
# to_skip,
to_end,
to_generate,
)
from paper.figures.PlotNeuralNet.pycore.tikzeng3 import (
to_scalefont,
to_flatimage,
to_curvedskip,
to_InOut,
to_RRDB,
to_ConvRelu,
to_Upsample,
)
from features.environment import _load_ipynb_modules
data_prep = _load_ipynb_modules("data_prep.ipynb")
deepbedmap = _load_ipynb_modules("deepbedmap.ipynb")
# %% [markdown]
# # **Methodology**
#
# Uses personal [fork](https://github.com/weiji14/PlotNeuralNet)
# of [PlotNeuralNet](https://github.com/HarisIqbal88/PlotNeuralNet)
# for drawing convolutional neural network architecture diagram.
# %% [markdown]
# ### **Figure 1: DeepBedMap Model Architecture**
# %% [markdown]
# ### Thumbnail figures
# %%
fig = gmt.Figure()
gmt.makecpt(cmap="jet", series=[-2800, 2800, 200], D="o")
fig.grdimage(
region=[-2700000, 2800000, -2200000, 2300000],
projection="X8c/7c",
grid="lowres/bedmap2_bed.tif",
cmap=True,
I="+d",
Q=True,
# frame='+t"BEDMAP2"'
)
fig.savefig(fname="paper/figures/fig1a_bedmap2.png")
fig.show()
# %%
fig = gmt.Figure()
fig.grdimage(
grid="https://www.the-cryosphere.net/13/665/2019/tc-13-665-2019-avatar-web.png",
# frame='+t"REMA"',
)
fig.savefig(fname="paper/figures/fig1b_rema.png")
fig.show()
# %%
fig = gmt.Figure()
fig.grdimage(
grid="https://news.agu.org/files/2019/07/AntarcticaMap.jpg",
# frame='+t"Ice Velocity"',
)
fig.savefig(fname="paper/figures/fig1c_measures.png")
fig.show()
# %%
fig = gmt.Figure()
fig.grdimage(
grid="https://wol-prod-cdn.literatumonline.com/cms/attachment/8bbfdd40-ea5b-409e-82a8-63a3b9ce4b7e/jgrd11996-fig-0005.png",
# frame='+t"Snow Accumulation"',
)
fig.savefig(fname="paper/figures/fig1d_accumulation.png")
fig.show()
# %%
fig = gmt.Figure()
gmt.makecpt(cmap="oleron", series=[-2000, 4500])
fig.grdimage(
grid="model/deepbedmap_dem.tif",
region=[-2700000, 2800000, -2200000, 2300000],
projection="x1:30000000",
cmap=True,
Q=True,
)
fig.savefig(fname="paper/figures/fig1e_deepbedmap.png")
fig.show()
# %% [markdown]
# ### Create [TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) vector graphics of model architecture
# %%
def sizes(cl=(64, 9), scale=(30, 2.5), offset="(1.0,0,0)"):
"""
Outputs pretty sized kwargs for PlotNeuralNet code,
given [c]hannel(s) and [l]ength (we assume a square input, i.e. height==width).
Optionally, adjust the channel and length scaling factor (default to (20, 2.5)).
"""
channels, length = cl[0], cl[1]
c_scale, l_scale = scale[0], scale[1]
kwargs = {
"n_filer": channels,
"s_filer": length,
"height": length / l_scale,
"depth": length / l_scale,
"offset": offset,
}
if isinstance(channels, tuple):
kwargs["width"] = tuple([c / c_scale for c in channels])
else: # (int, float)
kwargs["width"] = cl[0] / scale[0]
return kwargs
# %%
input_layers = [
# Actual raster images
to_flatimage(
"paper/figures/fig1a_bedmap2.png", to="(-1.8,4,0)", width=2, height=1.8
),
to_flatimage("paper/figures/fig1b_rema.png", to="(-1.8,0,0)", width=2, height=1.8),
to_flatimage(
"paper/figures/fig1c_measures.png", to="(-1.8,-4.5,0)", width=2, height=1.6
),
to_flatimage(
"paper/figures/fig1d_accumulation.png", to="(-1.8,-8,0)", width=2, height=1.8
),
# Input Raster Images
to_InOut(name="x0_img", **sizes(cl=(1, 11)), to="(-1.2,4,0)", caption="BEDMAP2"),
to_InOut(
name="w1_img",
**sizes(cl=(1, 110), scale=(20, 7.5)),
to="(-1.0,0,0)",
caption="REMA",
),
to_InOut(
name="w2_img", **sizes(cl=(2, 22)), to="(-1.2,-4.5,0)", caption="MEaSUREs"
),
to_InOut(
name="w3_img", **sizes(cl=(1, 11)), to="(-1.2,-8,0)", caption="Accumulation"
),
# First Convolution on input image
to_Conv(name="x0", **sizes(cl=(32, 11)), to="(x0_img-east)"),
to_Conv(name="w1", **sizes(cl=(32, 110), scale=(20, 7.5)), to="(w1_img-east)"),
to_Conv(name="w2", **sizes(cl=(32, 22)), to="(w2_img-east)"),
to_Conv(name="w3", **sizes(cl=(32, 11)), to="(w3_img-east)"),
to_connection(of="x0_img", to="x0"),
to_connection(of="w1_img", to="w1"),
to_connection(of="w2_img", to="w2"),
to_connection(of="w3_img", to="w3"),
# Second Convolution
to_Conv(name="x0_", **sizes(cl=(32, 9)), to="(x0-east)"),
to_Conv(name="w1_", **sizes(cl=(32, 9)), to="(w1-east)"),
to_Conv(name="w2_", **sizes(cl=(32, 9)), to="(w2-east)"),
to_Conv(name="w3_", **sizes(cl=(32, 9)), to="(w3-east)"),
to_connection(of="x0", to="x0_"),
to_connection(of="w1", to="w1_"),
to_connection(of="w2", to="w2_"),
to_connection(of="w3", to="w3_"),
# Concatenated Inputs
to_Conv(
name="concat",
**sizes(cl=(128, 9)),
to="(w1_-east)",
caption=r"Concatenated\\Inputs",
),
to_connection(of="x0_", to="concat"),
to_connection(of="w1_", to="concat"),
to_connection(of="w2_", to="concat"),
to_connection(of="w3_", to="concat"),
# Label for Input Module
to_Pool(
name="input-module-label",
offset="(0,-2.5,0)",
to="(w3_img-east)",
caption=r"Input Module",
width=24,
height=0.1,
depth=0.1,
opacity=0.2,
),
]
# %%
rrdb_layers_simple = [
# Residual in Residual Dense Block Layers
#
to_ConvRelu(
name="pre-residual",
**sizes(cl=((64,), 9)),
to="(concat-east)",
caption="Pre-residual",
),
to_connection(of="concat", to="pre-residual"),
# RRDB simplified box
to_RRDB(
name="rrdb-blocks",
**sizes(cl=((32,) * 12, 9)),
to="(pre-residual-east)",
caption=r"Residual-in-Residual\\Dense Blocks\\x12",
),
to_connection(of="pre-residual", to="rrdb-blocks"),
#
to_Conv(
name="post-residual",
**sizes(cl=(64, 9)),
to="(rrdb-blocks-east)",
caption="Post-residual",
),
to_connection(of="rrdb-blocks", to="post-residual"),
# Skip Connection
to_curvedskip(of="pre-residual", to="post-residual", xoffset=0.6),
to_Pool(
name="",
offset="(1.7,2.5,0)",
to="(rrdb-blocks-west)",
caption=r"Skip",
width=0,
height=0,
depth=0,
opacity=0,
),
# Label for Core RRDB module
to_Pool(
name="core-module-label",
offset="(0.3,0,0)",
to="(input-module-label-east)",
caption=r"Core Module",
width=32,
height=0.1,
depth=0.1,
opacity=0.2,
),
]
# RRDB full expansion
rrdb_layers_complex = [
# First Dense Block
to_Conv(
name="block1",
**sizes(cl=(160, 9), offset="(-3.0,-4.5,0)"),
to="(rrdb-blocks-west)",
caption="Dense Block 1",
),
#
# Second Dense Block
to_ConvRelu(name="block2a", **sizes(cl=((32,), 9)), to="(block1-east)"),
to_ConvRelu(name="block2b", **sizes(cl=((32,), 9)), to="(block2a-east)"),
to_ConvRelu(name="block2c", **sizes(cl=((32,), 9)), to="(block2b-east)"),
to_ConvRelu(name="block2d", **sizes(cl=((32,), 9)), to="(block2c-east)"),
to_Conv(name="block2e", **sizes(cl=(32, 9)), to="(block2d-east)"),
# TODO write a loop...
# inter-block connectors
to_connection(of="block1", to="block2a"),
to_connection(of="block2a", to="block2b"),
to_connection(of="block2b", to="block2c"),
to_connection(of="block2c", to="block2d"),
to_connection(of="block2d", to="block2e"),
# 1st order skips
to_curvedskip(of="block1", to="block2a", xoffset=0.45),
to_curvedskip(of="block2a", to="block2b", xoffset=0.45),
to_curvedskip(of="block2b", to="block2c", xoffset=0.45),
to_curvedskip(of="block2c", to="block2d", xoffset=0.45),
# 2nd order skips
to_curvedskip(of="block1", to="block2b", xoffset=0.45),
to_curvedskip(of="block2a", to="block2c", xoffset=0.45),
to_curvedskip(of="block2b", to="block2d", xoffset=0.45),
# 3rd order skips
to_curvedskip(of="block1", to="block2c", xoffset=0.45),
to_curvedskip(of="block2a", to="block2d", xoffset=0.45),
# 4th order skips
to_curvedskip(of="block1", to="block2d", xoffset=0.45),
#
# Third Dense Block
to_Conv(
name="block3",
**sizes(cl=(160, 9)),
to="(block2e-east)",
caption="Dense Block 3",
),
to_connection(of="block2e", to="block3"),
# ... connector
to_Pool(
name="ghost-block",
offset="(1.0,0,0)",
to="(block3-east)",
caption=r"\dots",
width=0,
height=0.1,
depth=0.1,
opacity=0.0,
),
to_connection(of="block3", to="ghost-block"),
]
# %%
upsampling_layers = [
# Upsampling layers
to_Upsample(name="upsample1", **sizes(cl=(64, 18)), to="(post-residual-east)"),
to_ConvRelu(
name="post-upsample1-conv",
**sizes(cl=(64, 18), offset="(0,0,0)"),
to="(upsample1-east)",
),
to_connection(of="post-residual", to="upsample1"),
#
to_Upsample(
name="upsample2",
**sizes(cl=(64, 36), offset="(0.6,0,0)"),
to="(post-upsample1-conv-east)",
caption=r"Upsampling\\Blocks",
),
to_ConvRelu(
name="post-upsample2-conv",
**sizes(cl=(64, 36), offset="(0,0,0)"),
to="(upsample2-east)",
),
to_connection(of="post-upsample1-conv", to="upsample2"),
# Deformable Convolution layers
to_ConvRelu(
name="final-conv-block1",
**sizes(cl=(64, 36), offset="(1.4,0,0)"),
to="(post-upsample2-conv-east)",
caption=r"Deformable\\Conv",
),
to_connection(of="post-upsample2-conv", to="final-conv-block1"),
to_Conv(
name="final-conv-block2",
**sizes(cl=(64, 36), offset="(0,0,0)"),
to="(final-conv-block1-east)",
),
# Output DeepBedMap DEM!
to_InOut(
name="deepbedmap-dem",
**sizes(cl=(1, 36), offset="(1.2,0,0)"),
to="(final-conv-block2-east)",
caption=r"DeepBedMap\\DEM",
),
to_connection(of="final-conv-block2", to="deepbedmap-dem"),
#
to_flatimage(
"paper/figures/fig1e_deepbedmap.png", to="(21,0,0)", width=5, height=5 * 18 / 22
),
# Label for Upsampling Module
to_Pool(
name="upsampling-module-label",
offset="(0.3,0,0)",
to="(core-module-label-east)",
caption=r"Upsampling Module",
width=33,
height=0.1,
depth=0.1,
opacity=0.2,
),
]
legend_key = [
to_Pool(
name="key",
offset="(2.7,-3,0)",
to="(deepbedmap-dem-east)",
caption=r"Key:",
width=0,
height=0,
depth=0,
opacity=0,
),
to_Conv(
name="conv",
s_filer="\scriptsize Pixels",
n_filer=("'Channels'", 0, 0),
offset="(-1.2,-2,0)",
to="(key-southwest)",
width=64 / 30,
height=3.6,
depth=3.6,
caption=r"Convolution\\Layer",
),
to_ConvRelu(
name="convrelu",
**sizes(offset="(2,0,0)"),
to="(conv-east)",
caption=r"Convolution\\+LeakyReLU",
),
# to_RRDB(name="rrdb-block", **sizes(offset="(2,0,0)"), to="(convrelu-east)", caption=r"RRDB"),
to_Upsample(
name="upsample",
**sizes(offset="(0,-2.5,0)"),
to="(conv-southwest)",
caption=r"NN\\Upsample",
),
to_InOut(
name="in-out",
**sizes(offset="(2,0,0)"),
to="(upsample-east)",
caption=r"Input/Output\\Images",
),
]
# %%
arch = [
to_head("paper/figures/PlotNeuralNet"),
to_cor(),
to_begin(),
to_scalefont(fontsize=r"\small"),
*input_layers,
*rrdb_layers_simple,
# *rrdb_layers_complex,
*upsampling_layers,
*legend_key,
to_end(),
]
to_generate(arch=arch, pathname="paper/figures/fig1_deepbedmap_architecture.tex")
# sudo apt install texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra
_ = subprocess.run(
[
"pdflatex",
"-output-directory=paper/figures",
"paper/figures/fig1_deepbedmap_architecture.tex",
],
stdout=subprocess.PIPE,
)
# Compress pdf following https://tex.stackexchange.com/a/19047
pdfcompress = lambda pdfpath: subprocess.run(
[
"gs",
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
"-dNOPAUSE",
"-dQUIET",
"-dBATCH",
"-dPrinted=false",
f"-sOutputFile={pdfpath}_compressed.pdf",
f"{pdfpath}.pdf",
],
stdout=subprocess.PIPE,
)
_ = pdfcompress(pdfpath="paper/figures/fig1_deepbedmap_architecture")
# %%
IPython.display.IFrame(
src="paper/figures/fig1_deepbedmap_architecture_compressed.pdf",
width=900,
height=450,
)
# %% [markdown]
# # **Results**
#
# Uses [PyGMT](https://github.com/GenericMappingTools/pygmt/) for drawing map plots.
# %% [markdown]
# ## **DeepBedMap_DEM Topography**
# %% [markdown]
# ### **Figure 2: 2D Digital Elevation Model over Antarctica**
# Also includes the following layers:
# - Grounding line
# - Pine Island Glacier extent in Figure 3
# - Thwaites Glacier extent in Figure 4
# - Bounding boxes of training tile regions
# %%
region_pineisle = rasterio.coords.BoundingBox(
left=-1631500.0, bottom=-259000.0, right=-1536500.0, top=-95000.0
)
region_thwaites = rasterio.coords.BoundingBox(
left=-1550000.0, bottom=-550000.0, right=-1250000.0, top=-300000.0
)
training_tiles = gpd.read_file("model/train/tiles_3031.geojson")
# %%
key_figure: bool = True # whether to produce key figure or Figure 2
fig = gmt.Figure()
# Plot DeepBedMap Digital Elevation Model (DEM)
gmt.makecpt(cmap="oleron", series=[-2000, 4500])
fig.grdimage(
grid="model/deepbedmap_dem.tif",
# grid="@BEDMAP_elevation.nc",
region=[-2700000, 2800000, -2200000, 2300000],
projection="x1:30000000",
cmap=True,
Q=True,
)
# Plot Antactic grounding line
fig.coast(
region="model/deepbedmap_dem.tif",
projection="s0/-90/-71/1:30000000",
area_thresh="+ag",
resolution="i",
shorelines="0.25p",
# frame="ag",
)
# Plot Pine Island and Thwaites Glacier study regions, and Training Tile areas
if not key_figure:
fig.plot(
data=pd.DataFrame([region_pineisle]).values,
region=[-2700000, 2800000, -2200000, 2300000],
projection="x1:30000000",
style="r+s",
pen="1.5p,purple2",
label='"Pine Island Glacier"',
)
fig.plot(
data=pd.DataFrame([region_thwaites]).values,
style="r+s",
pen="1.5p,yellow2",
label='"Thwaites Glacier"',
)
fig.plot(
data=training_tiles.bounds.values,
style="r+s",
pen="1p,orange2",
label='"Training Regions"',
)
# Plot map elements (colorbar, legend, frame)
fig.colorbar(
position="jBL+jBL+o2.0c/0.5c+w2.4c/0.3c+m",
box="+gwhite+p0.5p",
frame=["af", 'x+l"Elevation"', "y+lkm"],
cmap="+Uk", # kilo-units, i.e. divide by 1000
S=True, # no lines inside color scalebar
)
if not key_figure:
fig.legend(position="jBL+jBL+o2.7c/0.2c", box="+gwhite+p0.5p")
fig.basemap(
region=[-2700, 2800, -2200, 2300],
projection="x1:30000",
Bx='af+l"Polar Stereographic X (km)"',
By='af+l"Polar Stereographic Y (km)"',
frame="WSne",
)
# Save and show the figure
figname: str = "fig0_deepbedmap_dem" if key_figure else "fig2_deepbedmap_dem"
fig.savefig(fname=f"paper/figures/{figname}.pdf", dpi=300)
_ = pdfcompress(pdfpath=f"paper/figures/{figname}")
fig.show()
# %%
# %% [markdown]
# ### **Figure 3: 3D plots over Pine Island Glacier**
# %%
# Process BedMachine Antarctica grid
# Get from NSIDC at https://doi.org/10.5067/C2GFER6PTOS4
window_bound = region_pineisle
M_tile = data_prep.selective_tile(
filepath="netcdf:model/BedMachineAntarctica_2019-11-05_v01.nc:bed",
window_bounds=[[*window_bound]],
interpolate=True,
)
print(M_tile.shape)
bedmachinea = skimage.transform.rescale(
image=M_tile[0, 0, :, :].astype(np.int32),
scale=2, # 2x upscaling
order=3, # cubic interpolation
mode="reflect",
anti_aliasing=True,
multichannel=False,
preserve_range=True,
)
bedmachinea = np.expand_dims(np.expand_dims(bedmachinea, axis=0), axis=0)
print(bedmachinea.shape)
# Save Bicubic Resampled BedMachine Antarctica to GeoTiff and NetCDF format
bedmachinea_grid = data_prep.save_array_to_grid(
outfilepath="model/bedmachinea",
window_bound=window_bound,
array=bedmachinea[0, :, :, :],
save_netcdf=True,
)
# %%
fig = gmt.Figure()
deepbedmap.subplot(
directive="begin", row=2, col=2, A="+jCT+o-4c/-5c", Fs="9c/9c", M="2c/3c"
)
deepbedmap.plot_3d_view(
fig=fig,
img="model/deepbedmap3.nc", # DeepBedMap
ax=(0, 0),
zmin=-1400,
title="a) DeepBedMap", # ours
zlabel="Bed elevation (metres)",
)
deepbedmap.plot_3d_view(
fig=fig,
img="model/cubicbedmap.nc", # BEDMAP2
ax=(0, 1),
zmin=-1400,
title="b) BEDMAP2",
zlabel="Bed elevation (metres)",
)
deepbedmap.plot_3d_view(
fig=fig,
img="model/elevdiffmap.nc", # DeepBedMap - BEDMAP2
ax=(1, 0),
zmin=-400,
cmap="vik",
title="c) DeepBedMap - BEDMAP2",
zlabel="Difference (metres)",
)
deepbedmap.plot_3d_view(
fig=fig,
img="model/bedmachinea.nc", # BedMachine Antarctica
ax=(1, 1),
zmin=-1400,
title="d) BedMachine",
zlabel="Bed elevation (metres)",
)
deepbedmap.subplot(directive="end")
fig.savefig(
fname="paper/figures/fig3_qualitative_bed_comparison.png", dpi=300, crop=False
)
fig.show()
# %%
# %% [markdown]
# ### **Figure 4: Closeup images of DeepBedMap_DEM**
# %%
def closeup_fig(
letter: str,
name: str,
midx: int,
midy: int,
annot_xyt: list,
size: int = 100_000,
fig: gmt.Figure = None,
):
"""
Produces a closeup figure of a DeepBedMap_DEM area, with text annotations
"""
region = [midx - size, midx + size, midy - size, midy + size]
if fig is None: # initialize figure if no Figure is given
fig = gmt.Figure()
# Plot DeepBedMap Digital Elevation Model (DEM)
gmt.makecpt(cmap="oleron", series=[-2000, 4500])
fig.grdimage(
grid="model/deepbedmap_dem.tif",
# grid="@BEDMAP_elevation.nc",
region=region,
projection="x1:1500000",
cmap=True,
shading="+d", # default illumination from azimuth -45deg, intensity of +1
Q=True,
)
# Plot text annotation, black text against white background
for x, y, text in annot_xyt:
fig.text(
x=x,
y=y,
text=text,
font="12p,Helvetica-Bold,black",
G="white",
region=region,
projection="x1:1500000",
)
# Plot map elements (frame, colorbar)
fig.basemap(
region=[r / 1000 for r in region],
projection="x1:1500",
Bx='af+l"Polar Stereographic X (km)"',
By='af+l"Polar Stereographic Y (km)"',
frame=f'WSne+t"{letter}) {name}"',
)
fig.colorbar(
position="jBL+jBL+o2.0c/0.5c+w2.0c/0.2c+m",
box="+gwhite+p0.5p",
frame=["af", 'x+l"Elevation"', "y+lkm"],
cmap="+Uk", # kilo-units, i.e. divide by 1000
S=True, # no lines inside color scalebar
)
# Save and show the figure
# fig.savefig(fname=f"paper/figures/fig4{letter}_deepbedmap_closeup.png")
return fig
# %%
fig = gmt.Figure()
deepbedmap.subplot(directive="begin", row=3, col=3, B="wsne", Fs="15c/15c", M="1c/1c")
# Transantarctic Mountains - Scott Glacier
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="a",
name="Scott Glacier",
midx=-200_000,
midy=-400_000,
annot_xyt=[(-230000, -390000, "S")],
fig=fig,
)
# Siple Coast - Whillans Ice Stream
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="b",
name="Whillans Ice Stream",
midx=-400_000,
midy=-550_000,
annot_xyt=[(-350000, -540000, "R")],
fig=fig,
)
# Siple Coast - Bindschadler Ice Stream
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="c",
name="Bindschadler Ice Stream",
midx=-550_000,
midy=-800_000,
annot_xyt=[(-610000, -740000, "R")],
fig=fig,
)
# Weddell Sea Region - Evans Ice Stream
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="d",
name="Evans Ice Stream",
midx=-1500_000,
midy=350_000,
annot_xyt=[(-1450000, 320000, "R")],
fig=fig,
)
# Weddell Sea Region - Rutford Ice Stream
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="e",
name="Rutford Ice Stream",
midx=-1300_000,
midy=150_000,
annot_xyt=[(-1220000, 150000, "R")],
fig=fig,
)
# Weddell Sea Region - Foundation Ice Stream
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="f",
name="Foundation Ice Stream",
midx=-600_000,
midy=350_000,
annot_xyt=[(-630000, 360000, "R")],
fig=fig,
)
# East Antarctica - Totten Glacier
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="g",
name="Totten Glacier",
midx=2250_000,
midy=-1050_000,
annot_xyt=[(2270000, -1070000, "R"), (2180000, -970000, "W")],
fig=fig,
)
# East Antarctica - Byrd Glacier
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="h",
name="Byrd Glacier",
midx=400_000,
midy=-950_000,
annot_xyt=[(380000, -980000, "R"), (400000, -1040000, "S")],
fig=fig,
)
# East Antarctica - Gamburtsev Subglacial Mountains
deepbedmap.subplot(directive="set")
fig = closeup_fig(
letter="i",
name="Gamburtsev Subglacial Mountains",
midx=800_000,
midy=200_000,
annot_xyt=[(750000, 280000, "T")],
fig=fig,
)
deepbedmap.subplot(directive="end")
fig.savefig(fname="paper/figures/fig4_deepbedmap_closeups.eps")
fig.savefig(fname="paper/figures/fig4_deepbedmap_closeups.pdf")
fig.show()
# %%
# %% [markdown]
# ## **Surface Roughness**
# %%
region = [
region_thwaites.left,
region_thwaites.right,
region_thwaites.bottom,
region_thwaites.top,
]
kmregion = [r / 1000 for r in region] # coordinates in km instead of m
# %%
def standard_deviation_2d(grid: xr.DataArray, window_length: int):
"""
Get standard deviation of each pixel in a grid over a rolling 2d window.
A fairly basic proxy for the 'roughness' of a terrain.
>>> grid = xr.DataArray(data=np.arange(0, 15, 1).reshape(3, 5), dims=("x", "y"))
>>> standard_deviation_2d(grid=grid, window_length=3)
<xarray.DataArray (x: 3, y: 5)>
array([[2.54951 , 2.629956, 2.629956, 2.629956, 2.54951 ],
[4.112988, 4.163332, 4.163332, 4.163332, 4.112988],
[2.54951 , 2.629956, 2.629956, 2.629956, 2.54951 ]])
Dimensions without coordinates: x, y
"""
yrol = grid.rolling(y=window_length, center=True).construct("y_window")
xrol = yrol.rolling(x=window_length, center=True).construct("x_window")
# xr.apply_ufunc(np.std, xrol, input_core_dims=[["y_window", "x_window"]])
roughness = xrol.std(dim=["y_window", "x_window"])
return roughness
# %%
def prepare_grid(file: str, region: list):
"""
Prepares a raster grid for further plotting in PyGMT.
Reads in the grid from file using xarray.open_rasterio,
selects the first band and slices it using a bounding box region.
Also changes data type to float32 if required.
"""
grid = xr.open_rasterio(file).sel(
band=1, x=slice(region[0], region[1]), y=slice(region[3], region[2])
)
if grid.dtype != np.float32:
grid = grid.astype(np.float32)
return grid
# %%
#!gmt grdcut model/deepbedmap_dem.tif -Gmodel/deepbedmap_thwaites.nc -R-1550000/-1250000/-550000/-300000
# deepbedmap3grid = prepare_grid(file="model/deepbedmap_thwaites.nc", region=region)
deepbedmap3grid = prepare_grid(file="model/deepbedmap_dem.tif", region=region)
groundtruthgrid = prepare_grid(file="highres/20xx_Antarctica_DC8.nc", region=region)
bedmap2grid = prepare_grid(file="lowres/bedmap2_bed.tif", region=region)
cubicbedmap2 = skimage.transform.rescale(
image=bedmap2grid.astype(np.int32),
scale=4, # 4x upscaling
order=3, # cubic interpolation
mode="reflect",
anti_aliasing=True,
multichannel=False,
preserve_range=True,
)
cubicbedmap2grid = xr.DataArray(
data=cubicbedmap2[2:-2, 2:-2], coords=deepbedmap3grid.coords
)
bedmachinegrid = prepare_grid(
file="netcdf:model/BedMachineAntarctica_2019-11-05_v01.nc:bed", region=region
)
cubicbedmachine = skimage.transform.rescale(
image=bedmachinegrid.astype(np.int32),
scale=2, # 2x upscaling
order=3, # cubic interpolation
mode="reflect",
anti_aliasing=True,
multichannel=False,
preserve_range=True,
)
cubicbedmachinegrid = xr.DataArray(
data=cubicbedmachine[1:-1, 1:-1], coords=deepbedmap3grid.coords
)
gridDict = {
"DeepBedMap": deepbedmap3grid,
"Groundtruth": groundtruthgrid,
# "BEDMAP2": cubicbedmap2grid,
"BedMachine": cubicbedmachinegrid,
}
roughDict = {}
for name, grid in gridDict.items():
roughness = standard_deviation_2d(grid=grid, window_length=5)
roughDict[name] = roughness
# %%
# Subset and Plot Operation IceBridge (OIB) groundtruth points over a transect
# oibpoints = data_prep.ascii_to_xyz(pipeline_file="highres/20xx_Antarctica_DC8.json")
_ = data_prep.download_to_path(
path="highres/Data_20141121_05.csv",
url="https://data.cresis.ku.edu/data/rds/2014_Antarctica_DC8/csv_good/Data_20141121_05.csv",
)
oibpoints = data_prep.ascii_to_xyz(pipeline_file="highres/Data_20141121_05.json")
oibpoints = oibpoints.where(
cond=(
(oibpoints.x > region[0])
& (oibpoints.x < -1300_000) # (oibpoints.x < region[1])
& (oibpoints.y > region[2])
& (oibpoints.y < -425_000) # (oibpoints.y < region[3])
)
)
oibpoints.dropna(inplace=True)
oibpoints.reset_index(drop=True, inplace=True)
len(oibpoints)
# %% [raw]
# # Start and Stop Longitude/Latitude coordinates manually picked from
# # Operation IceBridge dataset, specifically Data_20141121_05.json
# import pyproj
#
# start_lonlat = (-106.415404, -76.343903)
# stop_lonlat = (-104.371744, -77.692208)
#
#
# reprj_func = pyproj.Transformer.from_crs(
# crs_from=pyproj.CRS.from_epsg(4326),
# crs_to=pyproj.CRS.from_epsg(3031),
# always_xy=True,
# )
#
# start_xy = reprj_func.transform(xx=start_lonlat[0], yy=start_lonlat[1])
# stop_xy = reprj_func.transform(xx=stop_lonlat[0], yy=stop_lonlat[1])
# start_stop_inc = f"{start_xy[0]}/{start_xy[1]}/{stop_xy[0]}/{stop_xy[1]}/+i125"
# print(start_stop_inc)
# %%
elevpoints = {}
# Get elevation (z) values
for name, grid in gridDict.items():
elevpoints[name] = gmt.grdtrack(
points=oibpoints[["x", "y"]],
grid=grid,
newcolname="elevation",
R="/".join(str(r) for r in region),
# E="-1573985/-470866/-993375/-464996+i250",
)
elevpoints[name] = elevpoints[name].dropna()
elevpoints[name].x = elevpoints[name].x.astype(float)
print(len(elevpoints[name]), name)
# Get roughness values
for name, grid in roughDict.items():
roughness = gmt.grdtrack(
points=oibpoints[["x", "y"]],
grid=grid,
newcolname="roughness",
R="/".join(str(r) for r in region),
# E=start_stop_inc,
).roughness
elevpoints[name]["roughness"] = roughness
# %% [raw]