File: layout.py

package info (click to toggle)
metview-python 1.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,896 kB
  • sloc: python: 11,306; makefile: 84; ansic: 51; sh: 7
file content (306 lines) | stat: -rw-r--r-- 9,606 bytes parent folder | download
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
#
# (C) Copyright 2020- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#

import logging
import math

import metview as mv

LOG = logging.getLogger(__name__)


class Layout:

    GRID_DEF = {
        1: [1, 1],
        2: [2, 1],
        3: [3, 1],
        4: [2, 2],
        8: [3, 3],
        9: [3, 3],
        10: [4, 3],
    }

    def _grid_row_col(self, page_num=0, rows=None, columns=None, layout=None):
        if rows is None and columns is None:
            if layout is not None and layout != "":
                v = layout.split("x")
                if len(v) == 2:
                    try:
                        r = int(v[0])
                        c = int(v[1])
                        # LOG.debug(f"r{r} c={c}")
                    except:
                        raise Exception(f"Invalid layout specification ({layout}")
                    if page_num > 0 and r * c < page_num:
                        raise Exception(
                            f"Layout specification={layout} does not match number of scenes={page_num}"
                        )
            else:
                if page_num in self.GRID_DEF:
                    r, c = self.GRID_DEF[page_num]
                elif page_num >= 1:
                    r, c = self._compute_row_col(page_num)
                else:
                    raise Exception(f"Cannot create a layout for {page_num} pages!")
        return r, c

    def build_grid(self, page_num=0, rows=None, columns=None, layout=None, view=None):
        r, c = self._grid_row_col(
            page_num=page_num, rows=rows, columns=columns, layout=layout
        )

        if isinstance(view, mv.style.GeoView):
            v = view.to_request()
        else:
            v = view

        return self._build_grid(rows=r, columns=c, view=v)

    def _build_grid(self, rows=1, columns=1, view=None):
        assert rows >= 1 and columns >= 1
        if rows == 1 and columns == 1:
            return mv.plot_superpage(pages=[mv.plot_page(view=view)])
        else:
            return mv.plot_superpage(
                pages=mv.mvl_regular_layout(
                    view, columns, rows, 1, 1, [5, 100, 15, 100]
                )
            )

    def _compute_row_col(self, page_num):
        # has been checked for 100
        r = int(math.sqrt(page_num))
        c = r
        if c * r < page_num:
            if (page_num - c * r) % c == 0:
                c += (page_num - c * r) // c
            else:
                c += 1 + (page_num - c * r) // c
        return (r, c)

    def build_diff(self, view):
        page_1 = mv.plot_page(top=5, bottom=50, left=25, right=75, view=view)

        page_2 = mv.plot_page(top=52, bottom=97, right=50, view=view)

        page_3 = mv.plot_page(top=52, bottom=97, left=50, right=100, view=view)

        return mv.plot_superpage(pages=[page_1, page_2, page_3])

    def build_xs(self, line, map_view):
        xs_view = mv.mxsectview(
            line=line,
            bottom_level=1000,
            top_level=150
            # wind_perpendicular : "off",
            # wind_parallel :"on",
            # wind_intensity :"off"
        )

        page_1 = mv.plot_page(top=35 if map_view is not None else 5, view=xs_view)

        if map_view is not None:
            page = mv.plot_page(top=5, bottom=35, view=map_view)
            return mv.plot_superpage(pages=[page_1, page])
        else:
            return mv.plot_superpage(pages=[page_1])

    def build_xs_avg(
        self,
        area,
        direction,
        bottom_level,
        top_level,
        vertical_scaling,
        axis_label_height=0.4,
    ):
        axis = mv.maxis(axis_tick_label_height=axis_label_height)

        return mv.maverageview(
            area=area,
            direction=direction,
            bottom_level=bottom_level,
            top_level=top_level,
            vertical_scaling=vertical_scaling,
            horizontal_axis=axis,
            vertical_axis=axis,
        )

    def build_stamp(self, page_num=0, layout="", view=None):

        if True:
            coast_empty = mv.mcoast(
                map_coastline="off", map_grid="off", map_label="off"
            )

            empty_view = mv.geoview(
                page_frame="off",
                subpage_frame="off",
                coastlines=coast_empty,
                subpage_x_position=40,
                subpage_x_length=10,
                subpage_y_length=10,
            )

            title_page = mv.plot_page(
                top=0, bottom=5, left=30, right=70, view=empty_view
            )

        r, c = self._grid_row_col(page_num=page_num, layout=layout)

        pages = mv.mvl_regular_layout(view, c, r, 1, 1, [5, 100, 15, 100])
        pages.append(title_page)
        return mv.plot_superpage(pages=pages)

        # g = self.build_grid(page_num=page_num, layout=layout, view=view)
        # return g

    def build_rmse(self, xmin, xmax, ymin, ymax, xtick, ytick, xtitle, ytitle):

        horizontal_axis = mv.maxis(
            axis_type="date",
            axis_orientation="horizontal",
            axis_position="bottom",
            # axis_title               :  'on',
            # axis_title_height        :   0.4,
            axis_grid="on",
            axis_grid_colour="grey",
            # axis_title_text          :  xtitle,
            # axis_title_quality       :  'high',
            axis_tick_interval=xtick,
            axis_tick_label_height=0.6,
            axis_date_type="days",
            axis_years_label="off",
            axis_months_label="off",
            axis_days_label_height="0.3",
        )

        vertical_axis = mv.maxis(
            axis_orientation="vertical",
            axis_position="left",
            axis_grid="on",
            axis_grid_colour="grey",
            axis_title="on",
            axis_title_height=0.4,
            axis_title_text=ytitle,
            # axis_title_quality       :  'high',
            axis_tick_interval=ytick,
            axis_tick_label_height=0.3,
        )

        cview = mv.cartesianview(
            page_frame="off",
            x_axis_type="date",
            y_axis_type="regular",
            y_automatic="off",
            x_automatic="off",
            y_min=ymin,
            y_max=ymax,
            x_date_min=xmin,
            x_date_max=xmax,
            horizontal_axis=horizontal_axis,
            vertical_axis=vertical_axis,
        )

        return cview

    def build_xy(self, xmin, xmax, ymin, ymax, xtick, ytick, xtitle, ytitle):

        horizontal_axis = mv.maxis(
            axis_orientation="horizontal",
            axis_position="bottom",
            axis_title="on",
            axis_title_height=0.5,
            axis_title_text=xtitle,
            # axis_title               :  'on',
            # axis_title_height        :   0.4,
            axis_grid="on",
            axis_grid_colour="grey",
            # axis_title_text          :  xtitle,
            # axis_title_quality       :  'high',
            axis_tick_interval=xtick,
            axis_tick_label_height=0.6,
            axis_date_type="days",
            axis_years_label="off",
            axis_months_label="off",
            axis_days_label_height="0.3",
        )

        vertical_axis = mv.maxis(
            axis_orientation="vertical",
            axis_position="left",
            axis_grid="on",
            axis_grid_colour="grey",
            axis_title="on",
            axis_title_height=0.6,
            axis_title_text=ytitle,
            # axis_title_quality       :  'high',
            axis_tick_interval=ytick,
            axis_tick_label_height=0.3,
        )

        cview = mv.cartesianview(
            page_frame="off",
            x_axis_type="regular",
            y_axis_type="regular",
            y_automatic="off",
            x_automatic="off",
            y_min=ymin,
            y_max=ymax,
            x_min=xmin,
            x_max=xmax,
            horizontal_axis=horizontal_axis,
            vertical_axis=vertical_axis,
        )

        return cview

    @staticmethod
    def compute_axis_range(v_min, v_max):
        count = 15
        d = math.fabs(v_max - v_min)
        if d > 0:
            b = d / count
            n = math.floor(math.log10(b))
            v = b / math.pow(10, n)
            # print("d={} b={} n={} v={}".format(d,b,n,v))

            if v <= 1:
                v = 1
            elif v <= 2:
                v = 2
            elif v <= 5:
                v = 5
            else:
                v = 10

            bin_size = v * math.pow(10, n)
            bin_start = math.ceil(v_min / bin_size) * bin_size
            if bin_start >= v_min and math.fabs(bin_start - v_min) < bin_size / 10000:
                bin_start = bin_start - bin_size / 100000
                av = v_min
            else:
                bin_start = bin_start - bin_size
                av = bin_start

            max_iter = 100
            act_iter = 0
            while av < v_max:
                av += bin_size
                act_iter += 1
                if act_iter > max_iter:
                    return (0, v_min, v_max)

            bin_end = av  # + bin_size / 100000
            return bin_size, bin_start, bin_end
        else:
            return (0, v_min, v_max)