fast-gsw 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fast_gsw-0.2.0/LICENSE ADDED
@@ -0,0 +1,73 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+ Preamble
9
+
10
+ The GNU General Public License is a free, copyleft license for
11
+ software and other kinds of works.
12
+
13
+ The licenses for most software and other practical works are designed
14
+ to take away your freedom to share and change the works. By contrast,
15
+ the GNU General Public License is intended to guarantee your freedom to
16
+ share and change all versions of a program--to make sure it remains free
17
+ software for all its users. We, the Free Software Foundation, use the
18
+ GNU General Public License for most of our software; it applies also to
19
+ any other work released this way by its authors. You can apply it to
20
+ your programs, too.
21
+
22
+ When we speak of free software, we are referring to freedom, not
23
+ price. Our General Public Licenses are designed to make sure that you
24
+ have the freedom to distribute copies of free software (and charge for
25
+ them if you wish), that you receive source code or can get it if you
26
+ want it, that you can change the software or use pieces of it in new
27
+ free programs, and that you know you can do these things.
28
+
29
+ This is the full text of the GNU General Public License, version 3.
30
+ The complete license text, including all seventeen numbered sections,
31
+ the definitions, and both appendices ("How to Apply These Terms to Your
32
+ New Programs" and the disclaimer of warranty), is available from the
33
+ Free Software Foundation at:
34
+
35
+ https://www.gnu.org/licenses/gpl-3.0.txt
36
+
37
+ This project is distributed under the terms of that license. A copy of
38
+ the full, canonical license text should accompany this distribution;
39
+ if it does not, you can obtain one from the URL above or by writing to:
40
+
41
+ Free Software Foundation, Inc.
42
+ 51 Franklin Street, Fifth Floor
43
+ Boston, MA 02110-1301, USA
44
+
45
+ END OF TERMS AND CONDITIONS
46
+
47
+ How to Apply These Terms to Your New Programs
48
+
49
+ If you develop a new program, and you want it to be of the greatest
50
+ possible use to the public, the best way to achieve this is to make it
51
+ free software which everyone can redistribute and change under these
52
+ terms.
53
+
54
+ To do so, attach the following notices to the program. It is safest
55
+ to attach them to the start of each source file to most effectively
56
+ state the exclusion of warranty; and each file should have at least
57
+ the "copyright" line and a pointer to where the full notice is found.
58
+
59
+ fast_gsw
60
+ Copyright (C) Lucas Merckelbach
61
+
62
+ This program is free software: you can redistribute it and/or modify
63
+ it under the terms of the GNU General Public License as published by
64
+ the Free Software Foundation, either version 3 of the License, or
65
+ (at your option) any later version.
66
+
67
+ This program is distributed in the hope that it will be useful,
68
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
69
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
70
+ GNU General Public License for more details.
71
+
72
+ You should have received a copy of the GNU General Public License
73
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
@@ -0,0 +1,3 @@
1
+ include LICENSE
2
+ recursive-include src/fast_gsw/_csrc *.c *.h
3
+ include src/fast_gsw/_csrc/LICENSE
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.4
2
+ Name: fast_gsw
3
+ Version: 0.2.0
4
+ Summary: Fast ctypes-based implementation of some gsw (TEOS-10) functions for vectorized CTD processing
5
+ Author-email: Lucas Merckelbach <lucas.merckelbach@hereon.de>
6
+ License-Expression: GPL-3.0-or-later
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Intended Audience :: Science/Research
9
+ Classifier: Operating System :: MacOS :: MacOS X
10
+ Classifier: Operating System :: POSIX
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Topic :: Scientific/Engineering :: Physics
13
+ Requires-Python: >=3.9
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: numpy
17
+ Dynamic: license-file
18
+
19
+ # fast_gsw
20
+
21
+ A ctypes-based Python interface for fast, vectorized execution of a few
22
+ [TEOS-10 GSW](http://www.teos-10.org/) (Gibbs SeaWater) oceanographic
23
+ functions, intended for processing large arrays of CTD
24
+ (conductivity/temperature/pressure) data, such as from ocean gliders.
25
+
26
+ Currently implemented:
27
+
28
+ - `rho(C, t, P, lon, lat)` — in-situ density
29
+ - `pot_rho(C, t, P, lon, lat)` — potential density (reference pressure 0 dbar)
30
+ - `SA(C, t, P, lon, lat)` — absolute salinity
31
+ - `CT(C, t, P, lon, lat)` — conservative temperature
32
+
33
+ All inputs/outputs are plain numpy arrays (scalars are broadcast); units are
34
+ conductivity in mS/cm, temperature in °C, pressure in dbar, and longitude /
35
+ latitude in decimal degrees.
36
+
37
+ ## Why
38
+
39
+ The pure-Python [`gsw`](https://pypi.org/project/gsw/) package computes
40
+ in-situ density (and similar derived quantities) as a chain of several
41
+ composed Python function calls per array (e.g.
42
+ `SP_from_C -> SA_from_SP -> rho_t_exact`). For large arrays of CTD triplets,
43
+ `fast_gsw` collapses that whole pipeline into a single C function call by
44
+ vendoring and compiling the TEOS-10 C implementation and calling it via
45
+ `ctypes`, which removes the Python-level per-step call overhead.
46
+
47
+ ## Install
48
+
49
+ ```
50
+ pip install fast_gsw
51
+ ```
52
+
53
+ This compiles the vendored TEOS-10 C sources at install time, so a C
54
+ compiler (gcc/clang) must be available. No other manual steps, root access,
55
+ or system-wide library installation are required.
56
+
57
+ To install from a local checkout:
58
+
59
+ ```
60
+ pip install .
61
+ ```
62
+
63
+ ## License
64
+
65
+ `fast_gsw` itself is licensed under the GPLv3 (see `LICENSE`). It vendors and
66
+ compiles source code from the [TEOS-10 GSW-C](https://github.com/TEOS-10/GSW-C)
67
+ Oceanographic Toolbox, C version 3.06.16, under the license in
68
+ `src/fast_gsw/_csrc/LICENSE`.
@@ -0,0 +1,50 @@
1
+ # fast_gsw
2
+
3
+ A ctypes-based Python interface for fast, vectorized execution of a few
4
+ [TEOS-10 GSW](http://www.teos-10.org/) (Gibbs SeaWater) oceanographic
5
+ functions, intended for processing large arrays of CTD
6
+ (conductivity/temperature/pressure) data, such as from ocean gliders.
7
+
8
+ Currently implemented:
9
+
10
+ - `rho(C, t, P, lon, lat)` — in-situ density
11
+ - `pot_rho(C, t, P, lon, lat)` — potential density (reference pressure 0 dbar)
12
+ - `SA(C, t, P, lon, lat)` — absolute salinity
13
+ - `CT(C, t, P, lon, lat)` — conservative temperature
14
+
15
+ All inputs/outputs are plain numpy arrays (scalars are broadcast); units are
16
+ conductivity in mS/cm, temperature in °C, pressure in dbar, and longitude /
17
+ latitude in decimal degrees.
18
+
19
+ ## Why
20
+
21
+ The pure-Python [`gsw`](https://pypi.org/project/gsw/) package computes
22
+ in-situ density (and similar derived quantities) as a chain of several
23
+ composed Python function calls per array (e.g.
24
+ `SP_from_C -> SA_from_SP -> rho_t_exact`). For large arrays of CTD triplets,
25
+ `fast_gsw` collapses that whole pipeline into a single C function call by
26
+ vendoring and compiling the TEOS-10 C implementation and calling it via
27
+ `ctypes`, which removes the Python-level per-step call overhead.
28
+
29
+ ## Install
30
+
31
+ ```
32
+ pip install fast_gsw
33
+ ```
34
+
35
+ This compiles the vendored TEOS-10 C sources at install time, so a C
36
+ compiler (gcc/clang) must be available. No other manual steps, root access,
37
+ or system-wide library installation are required.
38
+
39
+ To install from a local checkout:
40
+
41
+ ```
42
+ pip install .
43
+ ```
44
+
45
+ ## License
46
+
47
+ `fast_gsw` itself is licensed under the GPLv3 (see `LICENSE`). It vendors and
48
+ compiles source code from the [TEOS-10 GSW-C](https://github.com/TEOS-10/GSW-C)
49
+ Oceanographic Toolbox, C version 3.06.16, under the license in
50
+ `src/fast_gsw/_csrc/LICENSE`.
@@ -0,0 +1,36 @@
1
+ [build-system]
2
+ requires = ["setuptools>=64", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "fast_gsw"
7
+ version = "0.2.0"
8
+ description = "Fast ctypes-based implementation of some gsw (TEOS-10) functions for vectorized CTD processing"
9
+ readme = "README.md"
10
+ license = "GPL-3.0-or-later"
11
+ authors = [{ name = "Lucas Merckelbach", email = "lucas.merckelbach@hereon.de" }]
12
+ requires-python = ">=3.9"
13
+ dependencies = ["numpy"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Intended Audience :: Science/Research",
17
+ "Operating System :: MacOS :: MacOS X",
18
+ "Operating System :: POSIX",
19
+ "Programming Language :: Python",
20
+ "Topic :: Scientific/Engineering :: Physics",
21
+ ]
22
+
23
+ [tool.setuptools]
24
+ package-dir = { "" = "src" }
25
+ zip-safe = false
26
+
27
+ [tool.setuptools.packages.find]
28
+ where = ["src"]
29
+
30
+ [dependency-groups]
31
+ dev = [
32
+ "gsw>=3.6.20",
33
+ "ipython>=8.18.1",
34
+ "pytest>=8.4.2",
35
+ "setuptools>=82.0.1",
36
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,62 @@
1
+ import os
2
+ import sys
3
+
4
+ from setuptools import Extension, setup
5
+ from setuptools.command.build_ext import build_ext as _build_ext
6
+
7
+ CSRC = os.path.join("src", "fast_gsw", "_csrc")
8
+ SOURCES = [
9
+ os.path.join(CSRC, "gsw_oceanographic_toolbox.c"),
10
+ os.path.join(CSRC, "gsw_saar.c"),
11
+ os.path.join(CSRC, "gsw_vector.c"),
12
+ ]
13
+
14
+
15
+ class ctypes_build_ext(_build_ext):
16
+ """Build a plain shared library (no CPython C-API) for loading via
17
+ ctypes.CDLL, named like a normal shared object rather than an
18
+ importable CPython extension module.
19
+ """
20
+
21
+ def get_export_symbols(self, ext):
22
+ return []
23
+
24
+ def get_ext_filename(self, ext_name):
25
+ parts = ext_name.split(".")
26
+ if sys.platform.startswith("linux"):
27
+ return os.path.join(*parts) + ".so"
28
+ elif sys.platform == "darwin":
29
+ return os.path.join(*parts) + ".dylib"
30
+ elif sys.platform == "win32":
31
+ return os.path.join(*parts) + ".dll"
32
+ return super().get_ext_filename(ext_name)
33
+
34
+ def build_extension(self, ext):
35
+ objects = self.compiler.compile(
36
+ ext.sources,
37
+ output_dir=self.build_temp,
38
+ include_dirs=ext.include_dirs,
39
+ extra_postargs=ext.extra_compile_args,
40
+ )
41
+ ext_path = self.get_ext_fullpath(ext.name)
42
+ os.makedirs(os.path.dirname(ext_path), exist_ok=True)
43
+ self.compiler.link_shared_object(
44
+ objects,
45
+ ext_path,
46
+ libraries=ext.libraries,
47
+ extra_postargs=ext.extra_link_args,
48
+ )
49
+
50
+
51
+ libgsw_vector = Extension(
52
+ "fast_gsw.libgsw_vector",
53
+ sources=SOURCES,
54
+ include_dirs=[CSRC],
55
+ libraries=["m"],
56
+ extra_compile_args=["-O3", "-fPIC"],
57
+ )
58
+
59
+ setup(
60
+ ext_modules=[libgsw_vector],
61
+ cmdclass={"build_ext": ctypes_build_ext},
62
+ )
@@ -0,0 +1,165 @@
1
+ import ctypes
2
+ import glob
3
+ import os
4
+
5
+ import numpy as np
6
+
7
+ _pkg_dir = os.path.dirname(os.path.abspath(__file__))
8
+ _candidates = glob.glob(os.path.join(_pkg_dir, "libgsw_vector*"))
9
+ if not _candidates:
10
+ raise ImportError(
11
+ "Could not locate the compiled libgsw_vector shared library inside "
12
+ f"the fast_gsw package directory ({_pkg_dir}); was fast_gsw installed "
13
+ "correctly (pip install .)?"
14
+ )
15
+ libgsw=ctypes.CDLL(_candidates[0])
16
+
17
+ ## return and argument declarations.
18
+ libgsw.gsw_vector_rho.restype=ctypes.c_void_p
19
+ libgsw.gsw_vector_rho.argtypes=(ctypes.POINTER(ctypes.c_double),
20
+ ctypes.POINTER(ctypes.c_double),
21
+ ctypes.POINTER(ctypes.c_double),
22
+ ctypes.POINTER(ctypes.c_double),
23
+ ctypes.POINTER(ctypes.c_double),
24
+ ctypes.c_uint,
25
+ ctypes.POINTER(ctypes.c_double))
26
+
27
+ libgsw.gsw_vector_pot_rho.restype=ctypes.c_void_p
28
+ libgsw.gsw_vector_pot_rho.argtypes=(ctypes.POINTER(ctypes.c_double),
29
+ ctypes.POINTER(ctypes.c_double),
30
+ ctypes.POINTER(ctypes.c_double),
31
+ ctypes.POINTER(ctypes.c_double),
32
+ ctypes.POINTER(ctypes.c_double),
33
+ ctypes.c_uint,
34
+ ctypes.POINTER(ctypes.c_double))
35
+
36
+
37
+ libgsw.gsw_vector_SA.restype=ctypes.c_void_p
38
+ libgsw.gsw_vector_SA.argtypes=(ctypes.POINTER(ctypes.c_double),
39
+ ctypes.POINTER(ctypes.c_double),
40
+ ctypes.POINTER(ctypes.c_double),
41
+ ctypes.POINTER(ctypes.c_double),
42
+ ctypes.POINTER(ctypes.c_double),
43
+ ctypes.c_uint,
44
+ ctypes.POINTER(ctypes.c_double))
45
+
46
+ libgsw.gsw_vector_CT.restype=ctypes.c_void_p
47
+ libgsw.gsw_vector_CT.argtypes=(ctypes.POINTER(ctypes.c_double),
48
+ ctypes.POINTER(ctypes.c_double),
49
+ ctypes.POINTER(ctypes.c_double),
50
+ ctypes.POINTER(ctypes.c_double),
51
+ ctypes.POINTER(ctypes.c_double),
52
+ ctypes.c_uint,
53
+ ctypes.POINTER(ctypes.c_double))
54
+
55
+ class Caller(object):
56
+
57
+ def __call__(self,libgsw_vector_fun,C,t,P,lon,lat):
58
+ return self.call_function(libgsw_vector_fun,C,t,P,lon,lat)
59
+
60
+ def is_iterable(self,x):
61
+ ''' see if a parameter is iterable '''
62
+ try:
63
+ (_x for _x in x)
64
+ return True
65
+ except TypeError:
66
+ return False
67
+
68
+ def cast_arguments(self,*p):
69
+ ''' casts all arguments into double arrays.
70
+
71
+ Not intended to be called directly.
72
+ '''
73
+ pm=[]
74
+ plist=[self.is_iterable(_p) for _p in p]
75
+ s=[len(_p) for _p,_pl in zip(p,plist) if _pl]
76
+ if not all([_s==s[0] for _s in s]):
77
+ raise ValueError("Not all vectors are equally long.")
78
+ for i,(_p,_pl) in enumerate(zip(p,plist)):
79
+ if not _pl:
80
+ pm.append(np.array([_p]*s[0]).astype(float))
81
+ else:
82
+ try:
83
+ if _p.dtype!=np.float64:
84
+ pm.append(_p.astype('float'))
85
+ else:
86
+ pm.append(_p)
87
+ except AttributeError:
88
+ pm.append(np.array(_p).astype('float'))
89
+ return s[0],pm
90
+
91
+ def call_function(self,libgsw_vector_fun,C,t,P,lon,lat):
92
+ n,(C,t,P,lon,lat)=self.cast_arguments(C,t,P,lon,lat)
93
+ x=np.zeros(n,float)
94
+ libgsw_vector_fun(C.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
95
+ t.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
96
+ P.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
97
+ lon.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
98
+ lat.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
99
+ n,
100
+ x.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))
101
+ return x
102
+
103
+ __caller=Caller()
104
+
105
+ def rho(C,t,P,lon,lat):
106
+ '''
107
+ Calculates in situ density of seawater from conductivity, in-situ temperature and pressure,
108
+ and lat/lon.
109
+
110
+ C conductivity: mS/cm
111
+ t in-situ temperature: deg C
112
+ P pressure: dbar
113
+
114
+ lon longitude: decimal degrees
115
+ lat latitude: decimal degrees
116
+
117
+ '''
118
+ return __caller(libgsw.gsw_vector_rho,C,t,P,lon,lat)
119
+
120
+
121
+ def pot_rho(C,t,P,lon,lat):
122
+ '''
123
+ Calculates in potential density of seawater from conductivity, in-situ temperature and pressure,
124
+ and lat/lon.
125
+
126
+ C conductivity: mS/cm
127
+ t in-situ temperature: deg C
128
+ P pressure: dbar
129
+
130
+ lon longitude: decimal degrees
131
+ lat latitude: decimal degrees
132
+
133
+ '''
134
+ return __caller(libgsw.gsw_vector_pot_rho,C,t,P,lon,lat)
135
+
136
+
137
+ def CT(C,t,P,lon,lat):
138
+ '''
139
+ Calculates in conservative temperature from conductivity, in-situ temperature,pressure,
140
+ and lat/lon.
141
+
142
+ C conductivity: mS/cm
143
+ t in-situ temperature: deg C
144
+ P pressure: dbar
145
+
146
+ lon longitude: decimal degrees
147
+ lat latitude: decimal degrees
148
+
149
+ '''
150
+ return __caller(libgsw.gsw_vector_CT,C,t,P,lon,lat)
151
+
152
+ def SA(C,t,P,lon,lat):
153
+ ''' returns absolute salinity
154
+
155
+ C conductivity: mS/cm
156
+ t in-situ temperature: deg C
157
+ P pressure: dbar
158
+
159
+ lon longitude: decimal degrees
160
+ lat latitude: decimal degrees
161
+
162
+ '''
163
+ return __caller(libgsw.gsw_vector_SA,C,t,P,lon,lat)
164
+
165
+
@@ -0,0 +1,34 @@
1
+ Licence for the use of the Gibbs SeaWater (GSW) Oceanographic Toolbox
2
+
3
+ Copyright (c) 2011, SCOR/IAPSO WG127 (Scientific Committee on Oceanic Research/
4
+ International Association for the Physical Sciences of the Oceans, Working Group 127).
5
+
6
+ All rights reserved.
7
+
8
+ Redistribution and use, in source and binary forms, without modification, is permitted
9
+ provided that the following conditions are met:
10
+
11
+ • Redistributions of source code must retain the above copyright notice, this list
12
+ of conditions and the following disclaimer.
13
+
14
+ • Redistributions in binary form must reproduce the above copyright notice, this
15
+ list of conditions and the following disclaimer in the documentation and/or other
16
+ materials provided with the distribution.
17
+
18
+ • Neither the name of SCOR/IAPSO WG127 nor the names of its contributors may be used
19
+ to endorse or promote products derived from this software without specific prior
20
+ written permission.
21
+
22
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
+ SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31
+ DAMAGE.
32
+
33
+ The software is available from http://www.TEOS-10.org
34
+