TEOS-10 / GibbsSeaWater.jl

Gibbs-SeaWater (GSW) Oceanographic Toolbox in Julia
http://www.teos-10.org
Other
23 stars 3 forks source link

gsw_cp0 #27

Closed ThomasHaine closed 1 year ago

ThomasHaine commented 1 year ago

How do I access the seawater specific heat capacity gsw_cp0 constant (and other constants, for that matter)? The MATLAB gsw package has a function that returns it.

Thanks for building this Julia gsw package!

Alexander-Barth commented 1 year ago

Yes, that would be useful. I think that they have to be copied over manually from the C source:

https://github.com/TEOS-10/GSW-C/blob/master/gsw_internal_const.h#L33

Are you interested to make a pull request?

ThomasHaine commented 1 year ago

Yes, I can try, although I'm a Julia newbie so will probably need some help.

dankelley commented 1 year ago

That could be a one-liner, as defined and tested below, but the harder thing would be finding where in the source to put this, so that it would sit alongside similar functions.

julia> gsw_cp0() = 3991.86795711963
gsw_cp0 (generic function with 1 method)

julia> gsw_cp0()
3991.86795711963
Alexander-Barth commented 1 year ago

I guess that we can also declare it as an irrational as we did for Ωe (https://github.com/TEOS-10/GibbsSeaWater.jl/blob/master/src/utils.jl#L1 )

Base.@irrational gsw_cp0  3991.86795711963 big(3991.86795711963)
export gsw_cp0 

It can go maybe the same file src/utils.jl. The advantage of the irrational type is that when multiplied with a single precision float, the result will remain single precision (and not automatically promoted to double precision).

julia> Base.@irrational gsw_cp0  3991.86795711963 big(3991.86795711963)

julia> gsw_cp0
gsw_cp0 = 3991.8679571196...

julia> gsw_cp0 * 1.0
3991.86795711963

julia> gsw_cp0 * 1f0
3991.868f0

julia> typeof(gsw_cp0 * 1f0)
Float32

julia> typeof(gsw_cp0 * 1.0)
Float64
kouketsu commented 1 year ago

@Alexander-Barth, thanks for your support. @ThomasHaine, please wait I can update.

kouketsu commented 1 year ago

@ThomasHaine, I've updated.

You can try after update the package, like pkg> add GibbsSeaWater#master and check it.

julia> using GibbsSeaWater
julia> Float32(GibbsSeaWater.gsw_cp0)
3991.868f0

Now, to check it without a warning, we may need to cast them to a type like Float32() or Float64() under julia version <= 1.8.3.

ThomasHaine commented 1 year ago

Works! Thanks @kouketsu