cisma_js_standalone.py 5.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
import os
import Ngl

import numpy.ma as ma

from netCDF4 import Dataset
from livvkit.util import elements as el


describe = """CISMA_js plot"""
# plots velnorm, zooms in on Jakobshavn 69.2N 51.1W


14
def make_plot(config, out_path='.'):
15
    # ---------------- Read Data -----------------------------------
16
17
18
19
    input_file1 = os.path.join(config['cism_data'], 'greenland_1km_2016_12_01.mcb_velnorm.nc')
    input_file4 = os.path.join(config['cism_data'], 'albany.out.tstep.ANN_velnorm.nc')
    input_file5 = os.path.join(config['cism_data'], 'albany.out.tstep.ANN_thk.nc')
    input_file6 = os.path.join(config['cism_data'], 'climo_greenland_1km_2017_05_06_notime.nc')
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
    # --------------------------------------------------------------

    img_list = []

    # read input source, x1, y1
    ncid1 = Dataset(input_file1, 'r')
    init = ncid1.variables['velnorm'][0, :, :]
    # read lat and lon for original CISMA, x1 y1
    ncid6 = Dataset(input_file6, 'r')
    lat1 = ncid6.variables['lat'][:]
    lon1 = ncid6.variables['lon'][:]

    # read CISMA data output x0, y0
    ncid4 = Dataset(input_file4, 'r')
    cisma = ncid4.variables['velnorm'][0, 0, :, :]
    # read lat and lon for CISMA output, velnorm  x0, y0

    # read simulated thk of CISM x1, y1
    ncid5 = Dataset(input_file5, 'r')
    thk = ncid5.variables['thk'][0, :, :]
    # read lat and lon for CISMA output, thk x1 y1

    thk_mask = ma.masked_less(thk, 0.001)
    vel_mask = ma.masked_less(cisma, 0.001)
    cisma = ma.masked_array(cisma, mask=vel_mask.mask)
    init = ma.masked_array(init, mask=thk_mask.mask)

    init = init[:-1, :-1]

    diff = cisma - init

    # ------- PLOT --------
    # Open a workstation for drawing the plots
    wkres = Ngl.Resources()
    wkres.wkColorMap = "WhiteBlueGreenYellowRed"
    # wkres.wkOrientation = "portrait"  # "portrait" or "landscape"
    wks_type = "png"
    wks_img = str(os.path.join(out_path, "CISMA_js"))

    wks = Ngl.open_wks(wks_type, wks_img, wkres)

    # --- for the map -------
    # Define plotting area, Greenland
    mres = Ngl.Resources()
    mres.nglDraw = False  # Don't draw individual plots
    mres.nglFrame = False  # Don't advance frame.

    mres.pmTickMarkDisplayMode = "Never"  # Turn off map tickmarks.
    mres.mpGridAndLimbOn = False  # Turn off grid and limb lines.
    mres.mpProjection = "Aitoff"
    mres.mpLimitMode = "LatLon"  # limit map via lat/lon, to zoom in
    mres.mpCenterLatF = 72.  # map area
    mres.mpCenterLonF = -48.
    mres.mpMinLatF = 68.
    mres.mpMaxLatF = 78.
    mres.mpMinLonF = -55.
    mres.mpMaxLonF = -42.
    mres.mpOutlineOn = False
    mres.mpFillOn = False
    mres.mpPerimOn = True  # add box around map

    # --- for the file1 contour -------
    res1 = Ngl.Resources()
    res1.nglDraw = False  # Don't draw individual plots
    res1.nglFrame = False  # Don't advance frame.
    res1.cnLineLabelsOn = False
    res1.cnFillOn = True
    res1.cnLinesOn = False
    res1.cnLineLabelsOn = False
    res1.cnFillMode = "RasterFill"
    res1.trGridType = "TriangularMesh"
    res1.cnLevelSelectionMode = "ExplicitLevels"
    res1.cnLevels = (0, 1, 2, 10, 30, 70, 100, 200, 500, 1000, 2000)
    res1.lbLabelBarOn = True  # Turn on labelbar.
    res1.lbOrientation = "Horizontal"  # Verticle labelbar
    res1.lbLabelFontHeightF = 0.02  # Make fonts smaller.
    res1.pmLabelBarOrthogonalPosF = -0.04  # move label bar closer

    res1.sfXArray = lon1
    res1.sfYArray = lat1

    # --- for the file2 contour -------
    res2 = Ngl.Resources()
    res2.nglDraw = False  # Don't draw individual plots
    res2.nglFrame = False  # Don't advance frame.
    res2.cnLineLabelsOn = False
    res2.cnFillOn = True
    res2.cnLinesOn = False
    res2.cnLineLabelsOn = False
    res2.cnFillMode = "RasterFill"
    res2.trGridType = "TriangularMesh"
    res2.cnLevelSelectionMode = "ExplicitLevels"
    res2.cnLevels = (0, 1, 2, 10, 30, 70, 100, 200, 500, 1000, 2000)
    res2.lbLabelBarOn = True  # Turn on labelbar.
    res2.lbOrientation = "Horizontal"  # Verticle labelbar
    res2.lbLabelFontHeightF = 0.02  # Make fonts smaller.
    res2.pmLabelBarOrthogonalPosF = -0.04  # move label bar closer

    res2.sfXArray = lon1
    res2.sfYArray = lat1

    # --- for diff=file1-file2 contour -------
    res22 = Ngl.Resources()
    res22.nglDraw = False  # Don't draw individual plots
    res22.nglFrame = False  # Don't advance frame.
    res22.cnLineLabelsOn = False
    res22.cnFillOn = True
    res22.cnLinesOn = False
    res22.cnLineLabelsOn = False
    res22.cnFillMode = "RasterFill"
    res22.trGridType = "TriangularMesh"
    res22.cnLevelSelectionMode = "ExplicitLevels"
    res22.cnLevels = (0, 1, 2, 10, 30, 70, 100, 200, 500, 1000, 2000)
    res22.lbLabelBarOn = True  # Turn on labelbar.
    res22.lbOrientation = "Horizontal"  # Verticle labelbar
    res22.lbLabelFontHeightF = 0.02  # Make fonts smaller.
    res22.pmLabelBarOrthogonalPosF = -0.04  # move label bar closer

    res22.sfXArray = lon1
    res22.sfYArray = lat1

    init_plot = Ngl.contour(wks, init, res1)
    cisma_plot = Ngl.contour(wks, cisma, res2)
    diff_plot = Ngl.contour(wks, diff, res22)

    # Creat multiple figures and draw, which now contains the elevation and temperature
    # "[1,3]" indicates 1 row, 3 columns.
    map_title = ["Init", "CISM-A", "CISM-A - Init"]

    nmap = 3
    plot = []
    for i in range(nmap):
        mres.tiMainString = map_title[i]
        plot.append(Ngl.map(wks, mres))

    # Overlay everything on the map plot.
    Ngl.overlay(plot[0], cisma_plot)
    Ngl.overlay(plot[1], init_plot)
    Ngl.overlay(plot[2], diff_plot)

    Ngl.panel(wks, plot, [1, 3])

    img_link = os.path.join(os.path.basename(out_path),
                            os.path.basename(wks_img + '.' + wks_type))
    img_elem = el.image('CISMA_js',
                        ' '.join(describe.split()),
                        img_link)
167
    img_elem['Height'] = config['image_height']
168
169
170
    img_list.append(img_elem)

    return img_list