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 | |
---|
20 | """OASIS grid data and methods""" |
---|
21 | |
---|
22 | import numpy |
---|
23 | import ctypes |
---|
24 | from ctypes import c_int, cdll, CDLL |
---|
25 | |
---|
26 | cdll.LoadLibrary("liboasis.cbind.so") |
---|
27 | LIB = CDLL("liboasis.cbind.so") |
---|
28 | |
---|
29 | |
---|
30 | LIB.oasis_c_start_grids_writing.argtypes = None |
---|
31 | LIB.oasis_c_start_grids_writing.restype = ctypes.c_int |
---|
32 | |
---|
33 | |
---|
34 | def start_grids_writing(): |
---|
35 | kinfo = c_int(0) |
---|
36 | kinfo = LIB.oasis_c_start_grids_writing() |
---|
37 | return kinfo |
---|
38 | |
---|
39 | |
---|
40 | LIB.oasis_c_write_grid.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int, |
---|
41 | ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
42 | ctypes.c_void_p, ctypes.c_int] |
---|
43 | LIB.oasis_c_write_grid.restype = ctypes.c_int |
---|
44 | |
---|
45 | |
---|
46 | def write_grid(cgrid, nx, ny, lon, lat, partid): |
---|
47 | kinfo = c_int(0) |
---|
48 | lon = numpy.asfortranarray(lon, dtype=numpy.float64) |
---|
49 | lat = numpy.asfortranarray(lat, dtype=numpy.float64) |
---|
50 | p_lon = lon.ctypes.data |
---|
51 | p_lat = lat.ctypes.data |
---|
52 | if lon.ndim != 2: |
---|
53 | raise pyoasis.PyOasisException("Write grid: lon and lat have to be 2D arrays") |
---|
54 | if lon.shape != lat.shape: |
---|
55 | raise pyoasis.PyOasisException("Write grid: lon and lat shape not conforming") |
---|
56 | nx_loc = lon.shape[0] |
---|
57 | ny_loc = lon.shape[1] |
---|
58 | kinfo = LIB.oasis_c_write_grid(cgrid.encode(), nx, ny, nx_loc, ny_loc, p_lon, p_lat, |
---|
59 | partid) |
---|
60 | return kinfo |
---|
61 | |
---|
62 | |
---|
63 | LIB.oasis_c_write_corner.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int, |
---|
64 | ctypes.c_int, ctypes.c_int, ctypes.c_int, |
---|
65 | ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] |
---|
66 | LIB.oasis_c_write_corner.restype = ctypes.c_int |
---|
67 | |
---|
68 | |
---|
69 | def write_corner(cgrid, nx, ny, clo, cla, partid): |
---|
70 | kinfo = c_int(0) |
---|
71 | clo = numpy.asfortranarray(clo, dtype=numpy.float64) |
---|
72 | cla = numpy.asfortranarray(cla, dtype=numpy.float64) |
---|
73 | p_clo = clo.ctypes.data |
---|
74 | p_cla = cla.ctypes.data |
---|
75 | if clo.ndim != 3: |
---|
76 | raise pyoasis.PyOasisException("Write grid: clo and cla have to be 3D arrays") |
---|
77 | if clo.shape != cla.shape: |
---|
78 | raise pyoasis.PyOasisException("Write corner: clo and cla shape not conforming") |
---|
79 | nx_loc = clo.shape[0] |
---|
80 | ny_loc = clo.shape[1] |
---|
81 | ncrn = clo.shape[2] |
---|
82 | kinfo = LIB.oasis_c_write_corner(cgrid.encode(), nx, ny, ncrn, nx_loc, ny_loc, p_clo, |
---|
83 | p_cla, partid) |
---|
84 | return kinfo |
---|
85 | |
---|
86 | |
---|
87 | LIB.oasis_c_write_mask.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int, |
---|
88 | ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
89 | ctypes.c_int, ctypes.c_char_p] |
---|
90 | LIB.oasis_c_write_mask.restype = ctypes.c_int |
---|
91 | |
---|
92 | |
---|
93 | def write_mask(cgrid, nx, ny, mask, partid, companion): |
---|
94 | kinfo = c_int(0) |
---|
95 | mask = numpy.asfortranarray(mask, dtype=numpy.int32) |
---|
96 | p_mask = mask.ctypes.data |
---|
97 | if mask.ndim != 2: |
---|
98 | raise pyoasis.PyOasisException("Write mask: maks has to be a 2D arrays") |
---|
99 | nx_loc = mask.shape[0] |
---|
100 | ny_loc = mask.shape[1] |
---|
101 | kinfo = LIB.oasis_c_write_mask(cgrid.encode(), nx, ny, nx_loc, ny_loc, p_mask, partid, |
---|
102 | companion.encode()) |
---|
103 | return kinfo |
---|
104 | |
---|
105 | |
---|
106 | LIB.oasis_c_write_frac.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int, |
---|
107 | ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
108 | ctypes.c_int, ctypes.c_char_p] |
---|
109 | LIB.oasis_c_write_frac.restype = ctypes.c_int |
---|
110 | |
---|
111 | |
---|
112 | def write_frac(cgrid, nx, ny, frac, partid, companion): |
---|
113 | kinfo = c_int(0) |
---|
114 | frac = numpy.asfortranarray(frac, dtype=numpy.float64) |
---|
115 | p_frac = frac.ctypes.data |
---|
116 | if frac.ndim != 2: |
---|
117 | raise pyoasis.PyOasisException("Write frac: frac has to be a 2D arrays") |
---|
118 | nx_loc = frac.shape[0] |
---|
119 | ny_loc = frac.shape[1] |
---|
120 | kinfo = LIB.oasis_c_write_frac(cgrid.encode(), nx, ny, nx_loc, ny_loc, p_frac, partid, |
---|
121 | companion.encode()) |
---|
122 | return kinfo |
---|
123 | |
---|
124 | |
---|
125 | LIB.oasis_c_write_area.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int, |
---|
126 | ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
127 | ctypes.c_int] |
---|
128 | LIB.oasis_c_write_area.restype= ctypes.c_int |
---|
129 | |
---|
130 | |
---|
131 | def write_area(cgrid, nx, ny, area, partid): |
---|
132 | kinfo = c_int(0) |
---|
133 | area = numpy.asfortranarray(area, dtype=numpy.float64) |
---|
134 | p_area = area.ctypes.data |
---|
135 | if area.ndim != 2: |
---|
136 | raise pyoasis.PyOasisException("Write area: area has to be a 2D arrays") |
---|
137 | nx_loc = area.shape[0] |
---|
138 | ny_loc = area.shape[1] |
---|
139 | kinfo = LIB.oasis_c_write_area(cgrid.encode(), nx, ny, nx_loc, ny_loc, p_area, partid) |
---|
140 | return kinfo |
---|
141 | |
---|
142 | |
---|
143 | LIB.oasis_c_write_angle.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_int, |
---|
144 | ctypes.c_int, ctypes.c_int, ctypes.c_void_p, |
---|
145 | ctypes.c_int] |
---|
146 | LIB.oasis_c_write_angle.restype = ctypes.c_int |
---|
147 | |
---|
148 | |
---|
149 | def write_angle(cgrid, nx, ny, angle, partid): |
---|
150 | kinfo = c_int(0) |
---|
151 | angle = numpy.asfortranarray(angle, dtype=numpy.float64) |
---|
152 | p_angle = angle.ctypes.data |
---|
153 | if angle.ndim != 2: |
---|
154 | raise pyoasis.PyOasisException("Write angle: angle has to be a 2D arrays") |
---|
155 | nx_loc = angle.shape[0] |
---|
156 | ny_loc = angle.shape[1] |
---|
157 | kinfo = LIB.oasis_c_write_angle(cgrid.encode(), nx, ny, nx_loc, ny_loc, p_angle, partid) |
---|
158 | return kinfo |
---|
159 | |
---|
160 | |
---|
161 | LIB.oasis_c_terminate_grids_writing.argtypes = None |
---|
162 | LIB.oasis_c_terminate_grids_writing.restype = ctypes.c_int |
---|
163 | |
---|
164 | def terminate_grids_writing(): |
---|
165 | kinfo = c_int(0) |
---|
166 | kinfo = LIB.oasis_c_terminate_grids_writing() |
---|
167 | return kinfo |
---|