Skip to content

Seawater Module Documentation

This module provides functions to calculate various seawater properties.

Functions:

Name Description
ctd2svel

Calculate sound velocity from conductivity, temperature, and depth.

depth2pressure

Calculate pressure from depth.

ctp2salinity

Calculate absolute salinity from conductivity, temperature, and pressure.

ctd2salinity

Calculate absolute salinity from conductivity, temperature, and depth.

ctp2pden

Calculate potential density from conductivity, temperature, and pressure.

ctd2oxygen_saturation

Calculate oxygen saturation from conductivity, temperature, and depth.

Calculates the oxygen saturation value (ml/l) from salinity and temperature.

Calculates the oxygen concentration expected at equilibrium with air at an Absolute Pressure of 101325 Pa (sea pressure of 0 dbar) including saturated water vapor. This function uses the solubility coefficients derived from the data of Benson and Krause (1984), as fitted by Garcia and Gordon (1992, 1993).

Parameters:

Name Type Description Default
conductivity Union[float, ndarray, List[float]]

Conductivity in S/m.

required
temperature Union[float, ndarray, List[float]]

Temperature in degrees Celsius (ITS-90).

required
depth Union[float, ndarray, List[float]]

Depth in meters. Positive is upward.

required
latitude Union[float, ndarray, List[float]]

Latitude in degrees. Defaults to None.

None
longitude Union[float, ndarray, List[float]]

Longitude in degrees. Defaults to None.

None

Returns:

Name Type Description
solubility Union[float, ndarray]

Solubility of oxygen in micro-moles per kg

Raises:

Type Description
TypeError

If the inputs are not of the correct type.

ValueError

If the inputs are not of the correct shape or range.

Source code in navlib/environment/seawater.py
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
def ctd2oxygen_saturation(
    conductivity: Union[float, np.ndarray, List[float]],
    temperature: Union[float, np.ndarray, List[float]],
    depth: Union[float, np.ndarray, List[float]],
    latitude: Union[float, np.ndarray, List[float]] = None,
    longitude: Union[float, np.ndarray, List[float]] = None,
) -> Union[float, np.ndarray]:
    """
    Calculates the oxygen saturation value (ml/l) from salinity and temperature.

    Calculates the oxygen concentration expected at equilibrium with air at an
    Absolute Pressure of 101325 Pa (sea pressure of 0 dbar) including saturated
    water vapor. This function uses the solubility coefficients derived from the
    data of Benson and Krause (1984), as fitted by Garcia and Gordon (1992, 1993).

    Args:
        conductivity (Union[float, np.ndarray, List[float]]): Conductivity in S/m.
        temperature (Union[float, np.ndarray, List[float]]): Temperature in degrees Celsius (ITS-90).
        depth (Union[float, np.ndarray, List[float]]): Depth in meters. Positive is upward.
        latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None.
        longitude (Union[float, np.ndarray, List[float]], optional): Longitude in degrees. Defaults to None.

    Returns:
        solubility (Union[float, np.ndarray]): Solubility of oxygen in micro-moles per kg

    Raises:
        TypeError: If the inputs are not of the correct type.
        ValueError: If the inputs are not of the correct shape or range.
    """
    # Convert to numpy arrays
    if isinstance(conductivity, list):
        conductivity = np.array(conductivity)
    if isinstance(temperature, list):
        temperature = np.array(temperature)
    if isinstance(depth, list):
        depth = np.array(depth)
    if isinstance(latitude, list):
        latitude = np.array(latitude)
    if isinstance(longitude, list):
        longitude = np.array(longitude)

    # Check inputs type
    if not isinstance(conductivity, (np.ndarray, float)):
        raise TypeError("Conductivity must be a float or an array.")
    if not isinstance(temperature, (np.ndarray, float)):
        raise TypeError("Temperature must be a float or an array.")
    if not isinstance(depth, (np.ndarray, float)):
        raise TypeError("Depth must be a float or an array.")
    if latitude is not None and not isinstance(latitude, (np.ndarray, float)):
        raise TypeError("Latitude must be a float or an array.")
    if longitude is not None and not isinstance(longitude, (np.ndarray, float)):
        raise TypeError("Longitude must be a float or an array.")

    # Check shape of inputs if they are numpy arrays
    if isinstance(conductivity, np.ndarray):
        conductivity = conductivity.squeeze()
        if conductivity.ndim > 1:
            raise ValueError("Conductivity must be a 1D array.")
    if isinstance(temperature, np.ndarray):
        temperature = temperature.squeeze()
        if temperature.ndim > 1:
            raise ValueError("Temperature must be a 1D array.")
    if isinstance(depth, np.ndarray):
        depth = depth.squeeze()
        if depth.ndim > 1:
            raise ValueError("Depth must be a 1D array.")
    if latitude is not None and isinstance(latitude, np.ndarray):
        latitude = latitude.squeeze()
        if latitude.ndim > 1:
            raise ValueError("Latitude must be a 1D array.")
    if longitude is not None and isinstance(longitude, np.ndarray):
        longitude = longitude.squeeze()
        if longitude.ndim > 1:
            raise ValueError("Longitude must be a 1D array.")

    # If latitude is not provided, set it to 0
    if latitude is None:
        latitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(latitude, float) and isinstance(depth, np.ndarray):
        latitude = np.full_like(depth, latitude)

    # If longitude is not provided, set it to 0
    if longitude is None:
        longitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(longitude, float) and isinstance(depth, np.ndarray):
        longitude = np.full_like(depth, longitude)

    # Check that the inputs are of the same shape
    if isinstance(conductivity, np.ndarray):
        if (
            conductivity.shape != temperature.shape
            or conductivity.shape != depth.shape
            or conductivity.shape != latitude.shape
            or conductivity.shape != longitude.shape
        ):
            raise ValueError(
                "Conductivity, temperature, depth, latitude, and longitude must have the same shape."
            )

    # Check that the depth is negative
    if isinstance(depth, np.ndarray):
        if np.any(depth > 0):
            raise ValueError("Depth must be negative.")
    else:
        if depth > 0:
            raise ValueError("Depth must be negative.")

    # Check that the latitude is between -90 and 90
    if isinstance(latitude, np.ndarray):
        if np.any(latitude < -90) or np.any(latitude > 90):
            raise ValueError("Latitude must be between -90 and 90 degrees.")
    else:
        if latitude < -90 or latitude > 90:
            raise ValueError("Latitude must be between -90 and 90 degrees.")

    # Check that the longitude is between -360 and 360
    if isinstance(longitude, np.ndarray):
        if np.any(longitude < -360) or np.any(longitude > 360):
            raise ValueError("Longitude must be between -360 and 360 degrees.")
    else:
        if longitude < -360 or longitude > 360:
            raise ValueError("Longitude must be between -360 and 360 degrees.")

    # Calculate sound velocity
    pressure = gsw.p_from_z(depth, latitude)
    practical_salinity = gsw.SP_from_C(conductivity, temperature, pressure)
    absolute_salinity = gsw.SA_from_SP(
        practical_salinity, pressure, longitude, latitude
    )
    conservative_temperature = gsw.CT_from_t(absolute_salinity, temperature, pressure)
    o2sol = gsw.O2sol(
        absolute_salinity, conservative_temperature, pressure, longitude, latitude
    )
    return o2sol

Calculates absolute salinity in g/kg from conductivity, temperature, and depth.

Parameters:

Name Type Description Default
conductivity Union[float, ndarray, List[float]]

Conductivity in S/m.

required
temperature Union[float, ndarray, List[float]]

Temperature in degrees Celsius (ITS-90).

required
depth Union[float, ndarray, List[float]]

Depth in meters. Positive is upward.

required
latitude Union[float, ndarray, List[float]]

Latitude in degrees. Defaults to None.

None
longitude Union[float, ndarray, List[float]]

Longitude in degrees. Defaults to None.

None

Raises:

Type Description
TypeError

If the inputs are not of the correct type.

ValueError

If the inputs are not of the correct shape or range.

Source code in navlib/environment/seawater.py
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
def ctd2salinity(
    conductivity: Union[float, np.ndarray, List[float]],
    temperature: Union[float, np.ndarray, List[float]],
    depth: Union[float, np.ndarray, List[float]],
    latitude: Union[float, np.ndarray, List[float]] = None,
    longitude: Union[float, np.ndarray, List[float]] = None,
) -> Union[float, np.ndarray]:
    """
    Calculates absolute salinity in g/kg from conductivity, temperature, and depth.

    Args:
        conductivity (Union[float, np.ndarray, List[float]]): Conductivity in S/m.
        temperature (Union[float, np.ndarray, List[float]]): Temperature in degrees Celsius (ITS-90).
        depth (Union[float, np.ndarray, List[float]]): Depth in meters. Positive is upward.
        latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None.
        longitude (Union[float, np.ndarray, List[float]], optional): Longitude in degrees. Defaults to None.
    Returns:
        absolute_salinity (Union[float, np.ndarray]): Absolute salinity in g/kg.

    Raises:
        TypeError: If the inputs are not of the correct type.
        ValueError: If the inputs are not of the correct shape or range.
    """
    # Convert to numpy arrays
    if isinstance(conductivity, list):
        conductivity = np.array(conductivity)
    if isinstance(temperature, list):
        temperature = np.array(temperature)
    if isinstance(depth, list):
        depth = np.array(depth)
    if isinstance(latitude, list):
        latitude = np.array(latitude)
    if isinstance(longitude, list):
        longitude = np.array(longitude)

    # Check inputs type
    if not isinstance(conductivity, (np.ndarray, float)):
        raise TypeError("Conductivity must be a float or an array.")
    if not isinstance(temperature, (np.ndarray, float)):
        raise TypeError("Temperature must be a float or an array.")
    if not isinstance(depth, (np.ndarray, float)):
        raise TypeError("Depth must be a float or an array.")
    if latitude is not None and not isinstance(latitude, (np.ndarray, float)):
        raise TypeError("Latitude must be a float or an array.")
    if longitude is not None and not isinstance(longitude, (np.ndarray, float)):
        raise TypeError("Longitude must be a float or an array.")

    # Check shape of inputs if they are numpy arrays
    if isinstance(conductivity, np.ndarray):
        conductivity = conductivity.squeeze()
        if conductivity.ndim > 1:
            raise ValueError("Conductivity must be a 1D array.")
    if isinstance(temperature, np.ndarray):
        temperature = temperature.squeeze()
        if temperature.ndim > 1:
            raise ValueError("Temperature must be a 1D array.")
    if isinstance(depth, np.ndarray):
        depth = depth.squeeze()
        if depth.ndim > 1:
            raise ValueError("Depth must be a 1D array.")
    if latitude is not None and isinstance(latitude, np.ndarray):
        latitude = latitude.squeeze()
        if latitude.ndim > 1:
            raise ValueError("Latitude must be a 1D array.")
    if longitude is not None and isinstance(longitude, np.ndarray):
        longitude = longitude.squeeze()
        if longitude.ndim > 1:
            raise ValueError("Longitude must be a 1D array.")

    # If latitude is not provided, set it to 0
    if latitude is None:
        latitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(latitude, float) and isinstance(depth, np.ndarray):
        latitude = np.full_like(depth, latitude)

    # If longitude is not provided, set it to 0
    if longitude is None:
        longitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(longitude, float) and isinstance(depth, np.ndarray):
        longitude = np.full_like(depth, longitude)

    # Check that the inputs are of the same shape
    if isinstance(conductivity, np.ndarray):
        if (
            conductivity.shape != temperature.shape
            or conductivity.shape != depth.shape
            or conductivity.shape != latitude.shape
            or conductivity.shape != longitude.shape
        ):
            raise ValueError(
                "Conductivity, temperature, depth, latitude, and longitude must have the same shape."
            )

    # Check that the depth is negative
    if isinstance(depth, np.ndarray):
        if np.any(depth > 0):
            raise ValueError("Depth must be negative.")
    else:
        if depth > 0:
            raise ValueError("Depth must be negative.")

    # Check that the latitude is beetwen -90 and 90
    if isinstance(latitude, np.ndarray):
        if np.any(latitude < -90) or np.any(latitude > 90):
            raise ValueError("Latitude must be between -90 and 90 degrees.")
    else:
        if latitude < -90 or latitude > 90:
            raise ValueError("Latitude must be between -90 and 90 degrees.")

    # Check that the longitude is beetwen -360 and 360
    if isinstance(longitude, np.ndarray):
        if np.any(longitude < -360) or np.any(longitude > 360):
            raise ValueError("Longitude must be between -360 and 360 degrees.")
    else:
        if longitude < -360 or longitude > 360:
            raise ValueError("Longitude must be between -360 and 360 degrees.")

    # Calculate sound velocity
    pressure = gsw.p_from_z(depth, latitude)
    practical_salinity = gsw.SP_from_C(conductivity, temperature, pressure)
    absolute_salinity = gsw.SA_from_SP(
        practical_salinity, pressure, longitude, latitude
    )
    return absolute_salinity

Calculates sound velocity in m/s from conductivity, temperature, and depth.

Parameters:

Name Type Description Default
conductivity Union[float, ndarray, List[float]]

Conductivity in S/m.

required
temperature Union[float, ndarray, List[float]]

Temperature in degrees Celsius (ITS-90).

required
depth Union[float, ndarray, List[float]]

Depth in meters. Positive is upward.

required
latitude Union[float, ndarray, List[float]]

Latitude in degrees. Defaults to None.

None
longitude Union[float, ndarray, List[float]]

Longitude in degrees. Defaults to None.

None

Raises:

Type Description
TypeError

If the inputs are not of the correct type.

ValueError

If the inputs are not of the correct shape or range.

Source code in navlib/environment/seawater.py
 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
def ctd2svel(
    conductivity: Union[float, np.ndarray, List[float]],
    temperature: Union[float, np.ndarray, List[float]],
    depth: Union[float, np.ndarray, List[float]],
    latitude: Union[float, np.ndarray, List[float]] = None,
    longitude: Union[float, np.ndarray, List[float]] = None,
) -> Union[float, np.ndarray]:
    """
    Calculates sound velocity in m/s from conductivity, temperature, and depth.

    Args:
        conductivity (Union[float, np.ndarray, List[float]]): Conductivity in S/m.
        temperature (Union[float, np.ndarray, List[float]]): Temperature in degrees Celsius (ITS-90).
        depth (Union[float, np.ndarray, List[float]]): Depth in meters. Positive is upward.
        latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None.
        longitude (Union[float, np.ndarray, List[float]], optional): Longitude in degrees. Defaults to None.
    Returns:
        sound_velocity (Union[float, np.ndarray]): Sound velocity in m/s.

    Raises:
        TypeError: If the inputs are not of the correct type.
        ValueError: If the inputs are not of the correct shape or range.
    """
    # Convert to numpy arrays
    if isinstance(conductivity, list):
        conductivity = np.array(conductivity)
    if isinstance(temperature, list):
        temperature = np.array(temperature)
    if isinstance(depth, list):
        depth = np.array(depth)
    if isinstance(latitude, list):
        latitude = np.array(latitude)
    if isinstance(longitude, list):
        longitude = np.array(longitude)

    # Check inputs type
    if not isinstance(conductivity, (np.ndarray, float)):
        raise TypeError("Conductivity must be a float or an array.")
    if not isinstance(temperature, (np.ndarray, float)):
        raise TypeError("Temperature must be a float or an array.")
    if not isinstance(depth, (np.ndarray, float)):
        raise TypeError("Depth must be a float or an array.")
    if latitude is not None and not isinstance(latitude, (np.ndarray, float)):
        raise TypeError("Latitude must be a float or an array.")
    if longitude is not None and not isinstance(longitude, (np.ndarray, float)):
        raise TypeError("Longitude must be a float or an array.")

    # Check shape of inputs if they are numpy arrays
    if isinstance(conductivity, np.ndarray):
        conductivity = conductivity.squeeze()
        if conductivity.ndim > 1:
            raise ValueError("Conductivity must be a 1D array.")
    if isinstance(temperature, np.ndarray):
        temperature = temperature.squeeze()
        if temperature.ndim > 1:
            raise ValueError("Temperature must be a 1D array.")
    if isinstance(depth, np.ndarray):
        depth = depth.squeeze()
        if depth.ndim > 1:
            raise ValueError("Depth must be a 1D array.")
    if latitude is not None and isinstance(latitude, np.ndarray):
        latitude = latitude.squeeze()
        if latitude.ndim > 1:
            raise ValueError("Latitude must be a 1D array.")
    if longitude is not None and isinstance(longitude, np.ndarray):
        longitude = longitude.squeeze()
        if longitude.ndim > 1:
            raise ValueError("Longitude must be a 1D array.")

    # If latitude is not provided, set it to 0
    if latitude is None:
        latitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(latitude, float) and isinstance(depth, np.ndarray):
        latitude = np.full_like(depth, latitude)

    # If longitude is not provided, set it to 0
    if longitude is None:
        longitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(longitude, float) and isinstance(depth, np.ndarray):
        longitude = np.full_like(depth, longitude)

    # Check that the inputs are of the same shape
    if isinstance(conductivity, np.ndarray):
        if (
            conductivity.shape != temperature.shape
            or conductivity.shape != depth.shape
            or conductivity.shape != latitude.shape
            or conductivity.shape != longitude.shape
        ):
            raise ValueError(
                "Conductivity, temperature, depth, latitude, and longitude must have the same shape."
            )

    # Check that the depth is negative
    if isinstance(depth, np.ndarray):
        if np.any(depth > 0):
            raise ValueError("Depth must be negative.")
    else:
        if depth > 0:
            raise ValueError("Depth must be negative.")

    # Check that the latitude is between -90 and 90
    if isinstance(latitude, np.ndarray):
        if np.any(latitude < -90) or np.any(latitude > 90):
            raise ValueError("Latitude must be between -90 and 90 degrees.")
    else:
        if latitude < -90 or latitude > 90:
            raise ValueError("Latitude must be between -90 and 90 degrees.")

    # Check that the longitude is between -360 and 360
    if isinstance(longitude, np.ndarray):
        if np.any(longitude < -360) or np.any(longitude > 360):
            raise ValueError("Longitude must be between -360 and 360 degrees.")
    else:
        if longitude < -360 or longitude > 360:
            raise ValueError("Longitude must be between -360 and 360 degrees.")

    # Calculate sound velocity
    pressure = gsw.p_from_z(depth, latitude)
    practical_salinity = gsw.SP_from_C(conductivity, temperature, pressure)
    absolute_salinity = gsw.SA_from_SP(
        practical_salinity, pressure, longitude, latitude
    )
    conservative_temperature = gsw.CT_from_t(absolute_salinity, temperature, pressure)
    sound_speed = gsw.sound_speed(absolute_salinity, conservative_temperature, pressure)
    return sound_speed

Calculates potential density (kg/m^3) relative to a reference pressure (default 0 dbar) from conductivity, temperature, and pressure using the gsw library.

Parameters:

Name Type Description Default
conductivity Union[float, ndarray, List[float]]

Conductivity in S/m.

required
temperature Union[float, ndarray, List[float]]

Temperature in degrees Celsius (ITS-90).

required
pressure Union[float, ndarray, List[float]]

Pressure in dbars.

required
latitude Union[float, ndarray, List[float]]

Latitude in degrees. Defaults to None.

None
longitude Union[float, ndarray, List[float]]

Longitude in degrees. Defaults to None.

None
pressure_reference Union[float, ndarray, List[float]]

Reference pressure in dbars. Defaults to None.

None

Returns:

Name Type Description
potential_density Union[float, ndarray]

Potential density in kg/m^3.

Raises:

Type Description
TypeError

If the inputs are not of the correct type.

ValueError

If the inputs are not of the correct shape or range.

Source code in navlib/environment/seawater.py
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
def ctp2pden(
    conductivity: Union[float, np.ndarray, List[float]],
    temperature: Union[float, np.ndarray, List[float]],
    pressure: Union[float, np.ndarray, List[float]],
    latitude: Union[float, np.ndarray, List[float]] = None,
    longitude: Union[float, np.ndarray, List[float]] = None,
    pressure_reference: Union[float, np.ndarray, List[float]] = None,
) -> Union[float, np.ndarray]:
    """
    Calculates potential density (kg/m^3) relative to a reference pressure
    (default 0 dbar) from conductivity, temperature, and pressure using the gsw library.

    Args:
        conductivity (Union[float, np.ndarray, List[float]]): Conductivity in S/m.
        temperature (Union[float, np.ndarray, List[float]]): Temperature in degrees Celsius (ITS-90).
        pressure (Union[float, np.ndarray, List[float]]): Pressure in dbars.
        latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None.
        longitude (Union[float, np.ndarray, List[float]], optional): Longitude in degrees. Defaults to None.
        pressure_reference (Union[float, np.ndarray, List[float]], optional): Reference pressure in dbars. Defaults to None.

    Returns:
        potential_density (Union[float, np.ndarray]): Potential density in kg/m^3.

    Raises:
        TypeError: If the inputs are not of the correct type.
        ValueError: If the inputs are not of the correct shape or range.
    """
    # Convert to numpy arrays
    if isinstance(conductivity, list):
        conductivity = np.array(conductivity)
    if isinstance(temperature, list):
        temperature = np.array(temperature)
    if isinstance(pressure, list):
        pressure = np.array(pressure)
    if isinstance(latitude, list):
        latitude = np.array(latitude)
    if isinstance(longitude, list):
        longitude = np.array(longitude)
    if isinstance(pressure_reference, list):
        pressure_reference = np.array(pressure_reference)

    # Check inputs type
    if not isinstance(conductivity, (np.ndarray, float)):
        raise TypeError("Conductivity must be a float or an array.")
    if not isinstance(temperature, (np.ndarray, float)):
        raise TypeError("Temperature must be a float or an array.")
    if not isinstance(pressure, (np.ndarray, float)):
        raise TypeError("Pressure must be a float or an array.")
    if latitude is not None and not isinstance(latitude, (np.ndarray, float)):
        raise TypeError("Latitude must be a float or an array.")
    if longitude is not None and not isinstance(longitude, (np.ndarray, float)):
        raise TypeError("Longitude must be a float or an array.")
    if pressure_reference is not None and not isinstance(
        pressure_reference, (np.ndarray, float)
    ):
        raise TypeError("Pressure reference must be a float or an array.")

    # Check shape of inputs if they are numpy arrays
    if isinstance(conductivity, np.ndarray):
        conductivity = conductivity.squeeze()
        if conductivity.ndim > 1:
            raise ValueError("Conductivity must be a 1D array.")
    if isinstance(temperature, np.ndarray):
        temperature = temperature.squeeze()
        if temperature.ndim > 1:
            raise ValueError("Temperature must be a 1D array.")
    if isinstance(pressure, np.ndarray):
        pressure = pressure.squeeze()
        if pressure.ndim > 1:
            raise ValueError("Pressure must be a 1D array.")
    if latitude is not None and isinstance(latitude, np.ndarray):
        latitude = latitude.squeeze()
        if latitude.ndim > 1:
            raise ValueError("Latitude must be a 1D array.")
    if longitude is not None and isinstance(longitude, np.ndarray):
        longitude = longitude.squeeze()
        if longitude.ndim > 1:
            raise ValueError("Longitude must be a 1D array.")
    if pressure_reference is not None and isinstance(pressure_reference, np.ndarray):
        pressure_reference = pressure_reference.squeeze()
        if pressure_reference.ndim > 1:
            raise ValueError("Pressure reference must be a 1D array.")

    # If latitude is not provided, set it to 0
    if latitude is None:
        latitude = 0 if isinstance(pressure, float) else np.zeros_like(pressure)
    elif isinstance(latitude, float) and isinstance(pressure, np.ndarray):
        latitude = np.full_like(pressure, latitude)

    # If longitude is not provided, set it to 0
    if longitude is None:
        longitude = 0 if isinstance(pressure, float) else np.zeros_like(pressure)
    elif isinstance(longitude, float) and isinstance(pressure, np.ndarray):
        longitude = np.full_like(pressure, longitude)

    # If pressure reference is not provided, set it to 0
    if pressure_reference is None:
        pressure_reference = (
            0 if isinstance(pressure, float) else np.zeros_like(pressure)
        )
    elif isinstance(pressure_reference, float) and isinstance(pressure, np.ndarray):
        pressure_reference = np.full_like(pressure, pressure_reference)

    # Check that the inputs are of the same shape
    if isinstance(conductivity, np.ndarray):
        if (
            conductivity.shape != temperature.shape
            or conductivity.shape != pressure.shape
            or conductivity.shape != latitude.shape
            or conductivity.shape != longitude.shape
            or conductivity.shape != pressure_reference.shape
        ):
            raise ValueError(
                "Conductivity, temperature, pressure, latitude, longitude and reference pressure must have the same shape."
            )

    # Check that the latitude is beetwen -90 and 90
    if isinstance(latitude, np.ndarray):
        if np.any(latitude < -90) or np.any(latitude > 90):
            raise ValueError("Latitude must be between -90 and 90 degrees.")
    else:
        if latitude < -90 or latitude > 90:
            raise ValueError("Latitude must be between -90 and 90 degrees.")

    # Check that the longitude is beetwen -360 and 360
    if isinstance(longitude, np.ndarray):
        if np.any(longitude < -360) or np.any(longitude > 360):
            raise ValueError("Longitude must be between -360 and 360 degrees.")
    else:
        if longitude < -360 or longitude > 360:
            raise ValueError("Longitude must be between -360 and 360 degrees.")

    # Compute potential density using the exact potential density function
    practical_salinity = gsw.SP_from_C(conductivity, temperature, pressure)
    absolute_salinity = gsw.SA_from_SP(
        practical_salinity, pressure, longitude, latitude
    )
    pden = gsw.pot_rho_t_exact(
        absolute_salinity, temperature, pressure, pressure_reference
    )
    return pden

Calculates absolute salinity in g/kg from conductivity, temperature, and pressure.

Args: conductivity (Union[float, np.ndarray, List[float]]): Conductivity in S/m. temperature (Union[float, np.ndarray, List[float]]): Temperature in degrees Celsius (ITS-90). pressure (Union[float, np.ndarray, List[float]]): Pressure in dbars. latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None. longitude (Union[float, np.ndarray, List[float]], optional): Longitude in degrees. Defaults to None.

Returns:

Name Type Description
absolute_salinity Union[float, ndarray]

Absolute salinity in g/kg.

Raises:

Type Description
TypeError

If the inputs are not of the correct type.

ValueError

If the inputs are not of the correct shape or range.

Source code in navlib/environment/seawater.py
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def ctp2salinity(
    conductivity: Union[float, np.ndarray, List[float]],
    temperature: Union[float, np.ndarray, List[float]],
    pressure: Union[float, np.ndarray, List[float]],
    latitude: Union[float, np.ndarray, List[float]] = None,
    longitude: Union[float, np.ndarray, List[float]] = None,
) -> Union[float, np.ndarray]:
    """
     Calculates absolute salinity in g/kg from conductivity, temperature, and pressure.

     Args:
        conductivity (Union[float, np.ndarray, List[float]]): Conductivity in S/m.
        temperature (Union[float, np.ndarray, List[float]]): Temperature in degrees Celsius (ITS-90).
        pressure (Union[float, np.ndarray, List[float]]): Pressure in dbars.
        latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None.
        longitude (Union[float, np.ndarray, List[float]], optional): Longitude in degrees. Defaults to None.

    Returns:
        absolute_salinity (Union[float, np.ndarray]): Absolute salinity in g/kg.

    Raises:
        TypeError: If the inputs are not of the correct type.
        ValueError: If the inputs are not of the correct shape or range.
    """
    # Convert to numpy arrays
    if isinstance(conductivity, list):
        conductivity = np.array(conductivity)
    if isinstance(temperature, list):
        temperature = np.array(temperature)
    if isinstance(pressure, list):
        pressure = np.array(pressure)
    if isinstance(latitude, list):
        latitude = np.array(latitude)
    if isinstance(longitude, list):
        longitude = np.array(longitude)

    # Check inputs type
    if not isinstance(conductivity, (np.ndarray, float)):
        raise TypeError("Conductivity must be a float or an array.")
    if not isinstance(temperature, (np.ndarray, float)):
        raise TypeError("Temperature must be a float or an array.")
    if not isinstance(pressure, (np.ndarray, float)):
        raise TypeError("Pressure must be a float or an array.")
    if latitude is not None and not isinstance(latitude, (np.ndarray, float)):
        raise TypeError("Latitude must be a float or an array.")
    if longitude is not None and not isinstance(longitude, (np.ndarray, float)):
        raise TypeError("Longitude must be a float or an array.")

    # Check shape of inputs if they are numpy arrays
    if isinstance(conductivity, np.ndarray):
        conductivity = conductivity.squeeze()
        if conductivity.ndim > 1:
            raise ValueError("Conductivity must be a 1D array.")
    if isinstance(temperature, np.ndarray):
        temperature = temperature.squeeze()
        if temperature.ndim > 1:
            raise ValueError("Temperature must be a 1D array.")
    if isinstance(pressure, np.ndarray):
        pressure = pressure.squeeze()
        if pressure.ndim > 1:
            raise ValueError("Pressure must be a 1D array.")
    if latitude is not None and isinstance(latitude, np.ndarray):
        latitude = latitude.squeeze()
        if latitude.ndim > 1:
            raise ValueError("Latitude must be a 1D array.")
    if longitude is not None and isinstance(longitude, np.ndarray):
        longitude = longitude.squeeze()
        if longitude.ndim > 1:
            raise ValueError("Longitude must be a 1D array.")

    # If latitude is not provided, set it to 0
    if latitude is None:
        latitude = 0 if isinstance(pressure, float) else np.zeros_like(pressure)
    elif isinstance(latitude, float) and isinstance(pressure, np.ndarray):
        latitude = np.full_like(pressure, latitude)

    # If longitude is not provided, set it to 0
    if longitude is None:
        longitude = 0 if isinstance(pressure, float) else np.zeros_like(pressure)
    elif isinstance(longitude, float) and isinstance(pressure, np.ndarray):
        longitude = np.full_like(pressure, longitude)

    # Check that the inputs are of the same shape
    if isinstance(conductivity, np.ndarray):
        if (
            conductivity.shape != temperature.shape
            or conductivity.shape != pressure.shape
            or conductivity.shape != latitude.shape
            or conductivity.shape != longitude.shape
        ):
            raise ValueError(
                "Conductivity, temperature, pressure, latitude, and longitude must have the same shape."
            )

    # Check that the latitude is beetwen -90 and 90
    if isinstance(latitude, np.ndarray):
        if np.any(latitude < -90) or np.any(latitude > 90):
            raise ValueError("Latitude must be between -90 and 90 degrees.")
    else:
        if latitude < -90 or latitude > 90:
            raise ValueError("Latitude must be between -90 and 90 degrees.")

    # Check that the longitude is beetwen -360 and 360
    if isinstance(longitude, np.ndarray):
        if np.any(longitude < -360) or np.any(longitude > 360):
            raise ValueError("Longitude must be between -360 and 360 degrees.")
    else:
        if longitude < -360 or longitude > 360:
            raise ValueError("Longitude must be between -360 and 360 degrees.")
    practical_salinity = gsw.SP_from_C(conductivity, temperature, pressure)
    absolute_salinity = gsw.SA_from_SP(
        practical_salinity, pressure, longitude, latitude
    )
    return absolute_salinity

Calculates pressure in dbars from depth in meters.

Parameters:

Name Type Description Default
depth Union[float, ndarray, List[float]]

Depth in meters.

required
latitude Union[float, ndarray, List[float]]

Latitude in degrees. Defaults to None.

None

Returns:

Name Type Description
pressure Union[float, ndarray]

Pressure in dbars.

Raises:

Type Description
ValueError

If the latitude is not between -90 and 90 degrees.

Source code in navlib/environment/seawater.py
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
def depth2pressure(
    depth: Union[float, np.ndarray, List[float]],
    latitude: Union[float, np.ndarray, List[float]] = None,
) -> Union[float, np.ndarray]:
    """
    Calculates pressure in dbars from depth in meters.

    Args:
        depth (Union[float, np.ndarray, List[float]]): Depth in meters.
        latitude (Union[float, np.ndarray, List[float]], optional): Latitude in degrees. Defaults to None.

    Returns:
        pressure (Union[float, np.ndarray]): Pressure in dbars.

    Raises:
        ValueError: If the latitude is not between -90 and 90 degrees.
    """
    # Convert to numpy arrays
    if isinstance(depth, list):
        depth = np.array(depth)
    if isinstance(latitude, list):
        latitude = np.array(latitude)

    # Check inputs type
    if not isinstance(depth, (np.ndarray, float)):
        raise TypeError("Depth must be a float or an array.")
    if latitude is not None and not isinstance(latitude, (np.ndarray, float)):
        raise TypeError("Latitude must be a float or an array.")

    # Check shape of inputs if they are numpy arrays
    if isinstance(depth, np.ndarray):
        depth = depth.squeeze()
        if depth.ndim > 1:
            raise ValueError("Depth must be a 1D array.")
    if latitude is not None and isinstance(latitude, np.ndarray):
        latitude = latitude.squeeze()
        if latitude.ndim > 1:
            raise ValueError("Latitude must be a 1D array.")

    # If latitude is not provided, set it to 0
    if latitude is None:
        latitude = 0 if isinstance(depth, float) else np.zeros_like(depth)
    elif isinstance(latitude, float) and isinstance(depth, np.ndarray):
        latitude = np.full_like(depth, latitude)

    # Check that the inputs are of the same shape
    if isinstance(depth, np.ndarray):
        if depth.shape != latitude.shape:
            raise ValueError("Depth and latitude must have the same shape.")

    # Check that the depth is negative
    if isinstance(depth, np.ndarray):
        if np.any(depth > 0):
            raise ValueError("Depth must be negative.")
    else:
        if depth > 0:
            raise ValueError("Depth must be negative.")

    # Check that the latitude is beetwen -90 and 90
    if isinstance(latitude, np.ndarray):
        if np.any(latitude < -90) or np.any(latitude > 90):
            raise ValueError("Latitude must be between -90 and 90 degrees.")
    else:
        if latitude < -90 or latitude > 90:
            raise ValueError("Latitude must be between -90 and 90 degrees.")

    return gsw.p_from_z(depth, latitude)