1 | # pyOASIS - A Python wrapper for OASIS |
---|
2 | # Authors: Philippe Gambron, Rupert Ford |
---|
3 | # Copyright (C) 2019 UKRI - STFC |
---|
4 | |
---|
5 | # This program is free software: you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU Lesser General Public License as |
---|
7 | # published by the Free Software Foundation, either version 3 of the |
---|
8 | # License, or any later version. |
---|
9 | |
---|
10 | # This program is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | # GNU Lesser General Public License for more details. |
---|
14 | |
---|
15 | # A copy of the GNU Lesser General Public License, version 3, is supplied |
---|
16 | # with this program, in the file lgpl-3.0.txt. It is also available at |
---|
17 | # <https://www.gnu.org/licenses/lgpl-3.0.html>. |
---|
18 | |
---|
19 | import pyoasis.mod_oasis_grid |
---|
20 | import pyoasis.mod_oasis_part |
---|
21 | import numpy |
---|
22 | |
---|
23 | |
---|
24 | class Grid(object): |
---|
25 | """ |
---|
26 | Grid declared via pyoasis API and written to netcdf files grids.nc |
---|
27 | masks.nc areas.nc |
---|
28 | |
---|
29 | :param string cgrid: grid name (should be shorter than 64 char) |
---|
30 | :param int nx_global: global (non distributed) x size of the grid |
---|
31 | :param int ny_global: global (non distributed) y size of the grid |
---|
32 | :param numpy.array lon: longitudes of the cell centres (2D array) |
---|
33 | :param numpy.array lat: latitudes of the cell centres (2D array) |
---|
34 | :param type partition: optional partition object for distributed grids |
---|
35 | :raises PyOasisException: if the name is empty |
---|
36 | """ |
---|
37 | |
---|
38 | _counter = 0 |
---|
39 | |
---|
40 | def __init__(self, cgrid, nx_global, ny_global, lon, lat, partition=None): |
---|
41 | """Constructor""" |
---|
42 | pyoasis.checktypes.check_types([str, int, int, numpy.ndarray, |
---|
43 | numpy.ndarray, pyoasis.Partition], |
---|
44 | [cgrid, nx_global, ny_global, lon, |
---|
45 | lat, partition]) |
---|
46 | if len(cgrid) == 0: |
---|
47 | raise pyoasis.PyOasisException("Grid name empty.") |
---|
48 | self._cgrid = cgrid |
---|
49 | self._nx_glo = nx_global |
---|
50 | self._ny_glo = ny_global |
---|
51 | if partition: |
---|
52 | self._part_id = partition._id |
---|
53 | else: |
---|
54 | self._part_id = -1 |
---|
55 | |
---|
56 | if Grid._counter == 0: |
---|
57 | kinfo = pyoasis.mod_oasis_grid.start_grids_writing() |
---|
58 | Grid._counter += 1 |
---|
59 | pyoasis.mod_oasis_grid.write_grid(self._cgrid, self._nx_glo, |
---|
60 | self._ny_glo, lon, lat, |
---|
61 | self._part_id) |
---|
62 | |
---|
63 | def set_corners(self, clo, cla): |
---|
64 | """ |
---|
65 | Sets corner latitudes and longitudes |
---|
66 | |
---|
67 | :param clo: array longitudes |
---|
68 | :type clo: array of double-precision floating numbers |
---|
69 | |
---|
70 | :param cla: array longitudes |
---|
71 | :type cla: array of double-precision floating numbers |
---|
72 | """ |
---|
73 | pyoasis.checktypes.check_types([numpy.ndarray, numpy.ndarray], |
---|
74 | [clo, cla]) |
---|
75 | pyoasis.mod_oasis_grid.write_corner(self._cgrid, self._nx_glo, |
---|
76 | self._ny_glo, clo, cla, |
---|
77 | self._part_id) |
---|
78 | |
---|
79 | def set_mask(self, mask, companion=None): |
---|
80 | """ |
---|
81 | Sets integer mask values |
---|
82 | |
---|
83 | :param mask: mask array |
---|
84 | :type mask: array of integer numbers |
---|
85 | |
---|
86 | :param companion: companion |
---|
87 | :type companion: str |
---|
88 | """ |
---|
89 | pyoasis.checktypes.check_types([numpy.ndarray, str], [mask, |
---|
90 | companion]) |
---|
91 | if not companion: |
---|
92 | companion = "NULL-STRING" |
---|
93 | pyoasis.mod_oasis_grid.write_mask(self._cgrid, self._nx_glo, |
---|
94 | self._ny_glo, mask, |
---|
95 | self._part_id, companion) |
---|
96 | |
---|
97 | def set_frac(self, frac, companion=None): |
---|
98 | pyoasis.checktypes.check_types([numpy.ndarray, str], [frac, |
---|
99 | companion]) |
---|
100 | if not companion: |
---|
101 | companion = "NULL-STRING" |
---|
102 | pyoasis.mod_oasis_grid.write_frac(self._cgrid, self._nx_glo, |
---|
103 | self._ny_glo, frac, |
---|
104 | self._part_id, companion) |
---|
105 | |
---|
106 | def set_area(self, area): |
---|
107 | """ |
---|
108 | Set area values |
---|
109 | |
---|
110 | :param area: mask array |
---|
111 | |
---|
112 | :type area: array of double precision floating numbers |
---|
113 | """ |
---|
114 | pyoasis.checktypes.check_types([numpy.ndarray], [area]) |
---|
115 | pyoasis.mod_oasis_grid.write_area(self._cgrid, self._nx_glo, |
---|
116 | self._ny_glo, area, |
---|
117 | self._part_id) |
---|
118 | |
---|
119 | def set_angle(self, angle): |
---|
120 | """ |
---|
121 | Set angle values |
---|
122 | |
---|
123 | :param angle: mask array |
---|
124 | |
---|
125 | :type angle: array of double precision floating numbers |
---|
126 | """ |
---|
127 | pyoasis.checktypes.check_types([numpy.ndarray], [angle]) |
---|
128 | pyoasis.mod_oasis_grid.write_angle(self._cgrid, self._nx_glo, |
---|
129 | self._ny_glo, angle, |
---|
130 | self._part_id) |
---|
131 | |
---|
132 | def write(self): |
---|
133 | """ |
---|
134 | Writes the grid |
---|
135 | """ |
---|
136 | Grid._counter -= 1 |
---|
137 | if Grid._counter == 0: |
---|
138 | pyoasis.mod_oasis_grid.terminate_grids_writing() |
---|