hydro.legacy.coards#

Legacy COARDS netcdf make from libcchdo ported to take a CCHDO CF/netCDF xarray.Dataset object as input

The goal is, as much as possible, to use the old code with minimal changes such that the following outputs are identical:

  • Exchange -> CF/netCDF -> COARDS netCDF (this library)

  • Exchange -> COARDS netCDF (using libcchdo)

The entrypoint function is to_coards()

Package Contents#

Functions#

strftime_woce_date_time(dt)

Take an xr.DataArray with time values in it and convert to strings

_ascii(x)

Force all codepoints into valid ascii range

simplest_str(s)

Give the simplest string representation.

_pad_station_cast(x)

Pad a station or cast identifier out to 5 characters.

get_filename(expocode, station, cast, extension)

Generate the filename for COARDS netCDF files

minutes_since_epoch(dt, epoch[, error])

Make the time value for netCDF files

define_dimensions(nc_file, length)

Create NetCDF file dimensions.

define_attributes(nc_file, expocode, sect_id, ...)

Sets the global attributes of the input nc_file as a side effect

set_original_header(nc_file, ds)

Sets the ORIGINAL_HEADER global attribute to whatever is in ds.attrs["comments"]

create_common_variables(nc_file, latitude, longitude, ...)

Add variables to the netcdf file object such as date, time etc.

create_and_fill_data_variables(nc_file, ds)

Add variables to the netcdf file object that correspond to data.

_create_common_variables(nc_file, ds)

Extracts the latitude, longitude, station, and cast from ds and passes them to create_common_variables

write_ctd(ds)

How to write a CTD NetCDF file.

write_bottle(ds)

How to write a Bottle NetCDF file.

to_coards(ds)

Convert an xr.Dataset to a zipfile with COARDS netCDF files inside

Attributes#

log

logger object for message logging

PARAMS

mapping of whp names to nc names

CTD_ZIP_FILE_EXTENSION

Filename extention for a zipped collection ctd coards netcdf files

BOTTLE_ZIP_FILE_EXTENSION

Filename extention for a zipped collection bottle coards netcdf files

FILL_VALUE

Const from old libcchdo, -999.0

QC_SUFFIX

Variable name suffix for flag variables

FILE_EXTENSION

filenmae extention for all netcdf files

EPOCH

dateime referenced in the units of time variables in netCDF files: 1980-01-01

STATIC_PARAMETERS_PER_CAST

List of WHP names that are ignored when calling create_and_fill_data_variables()

NON_FLOAT_PARAMETERS

params not in STATIC_PARAMETERS_PER_CAST that are also ignored by create_and_fill_data_variables()

UNKNOWN

Value used when some string value isn't found

UNSPECIFIED_UNITS

Value used when there are no units

STRLEN

length of char array variables, hardcoded to 40

hydro.legacy.coards.log[source]#

logger object for message logging

hydro.legacy.coards.PARAMS[source]#

mapping of whp names to nc names

This is loaded at module import time from a dump from the old internal params sqlite database

hydro.legacy.coards.CTD_ZIP_FILE_EXTENSION = 'nc_ctd.zip'[source]#

Filename extention for a zipped collection ctd coards netcdf files

hydro.legacy.coards.BOTTLE_ZIP_FILE_EXTENSION = 'nc_hyd.zip'[source]#

Filename extention for a zipped collection bottle coards netcdf files

hydro.legacy.coards.FILL_VALUE[source]#

Const from old libcchdo, -999.0

hydro.legacy.coards.QC_SUFFIX = '_QC'[source]#

Variable name suffix for flag variables

hydro.legacy.coards.FILE_EXTENSION = 'nc'[source]#

filenmae extention for all netcdf files

hydro.legacy.coards.EPOCH[source]#

dateime referenced in the units of time variables in netCDF files: 1980-01-01

hydro.legacy.coards.STATIC_PARAMETERS_PER_CAST = ('EXPOCODE', 'SECT_ID', 'STNNBR', 'CASTNO', '_DATETIME', 'LATITUDE', 'LONGITUDE', 'DEPTH',...[source]#

List of WHP names that are ignored when calling create_and_fill_data_variables()

hydro.legacy.coards.NON_FLOAT_PARAMETERS = ('CTDNOBS',)[source]#

params not in STATIC_PARAMETERS_PER_CAST that are also ignored by create_and_fill_data_variables()

hydro.legacy.coards.UNKNOWN = 'UNKNOWN'[source]#

Value used when some string value isn’t found

This is mmostly mitigated by the guarantees of the new CF format, but e.g. section id might be missing

hydro.legacy.coards.UNSPECIFIED_UNITS = 'unspecified'[source]#

Value used when there are no units

hydro.legacy.coards.STRLEN = 40[source]#

length of char array variables, hardcoded to 40

hydro.legacy.coards.strftime_woce_date_time(dt)[source]#

Take an xr.DataArray with time values in it and convert to strings

Parameters:

dt (xarray.DataArray) –

hydro.legacy.coards._ascii(x)[source]#

Force all codepoints into valid ascii range

Works by encoding the str into ascii bytes with the replace err param, then decoding the bytes to str again

Parameters:

x (str) – string with any unicode codepoint in it

Returns:

string with all non ascii codepoints replaced with whatever “replace” does in str.encode()

Return type:

str

hydro.legacy.coards.simplest_str(s)[source]#

Give the simplest string representation.

If a float is almost equivalent to an integer, swap out for the integer.

Return type:

str

hydro.legacy.coards._pad_station_cast(x)[source]#

Pad a station or cast identifier out to 5 characters.

This is usually for use in a file name.

Parameters:

x (str) – a string to be padded

Return type:

str

hydro.legacy.coards.get_filename(expocode, station, cast, extension)[source]#

Generate the filename for COARDS netCDF files

Was ported directly from libcchdo and should have the same formatting behavior

hydro.legacy.coards.minutes_since_epoch(dt, epoch, error=-9)[source]#

Make the time value for netCDF files

The custom implimentation in libcchdo was discarded in favor of the date2num function from cftime. Not sure if cftime exsited in the netCDF4 python library at the time.

Parameters:

dt (xarray.DataArray) –

hydro.legacy.coards.define_dimensions(nc_file, length)[source]#

Create NetCDF file dimensions.

This creates all the COARDS dimensions in the input nc_file as a side effect (does not return) Dimensions created are:

  • time

  • pressure

  • latitude

  • longitude

  • string_dimension

Parameters:
  • nc_file (netCDF4.Dataset) –

  • length (int) –

hydro.legacy.coards.define_attributes(nc_file, expocode, sect_id, data_type, stnnbr, castno, bottom_depth)[source]#

Sets the global attributes of the input nc_file as a side effect

Parameters:

nc_file (netCDF4.Dataset) –

hydro.legacy.coards.set_original_header(nc_file, ds)[source]#

Sets the ORIGINAL_HEADER global attribute to whatever is in ds.attrs[“comments”]

Parameters:
hydro.legacy.coards.create_common_variables(nc_file, latitude, longitude, woce_datetime, stnnbr, castno)[source]#

Add variables to the netcdf file object such as date, time etc.

Parameters:
  • nc_file (netCDF4.Dataset) –

  • latitude (float) –

  • longitude (float) –

hydro.legacy.coards.create_and_fill_data_variables(nc_file, ds)[source]#

Add variables to the netcdf file object that correspond to data.

Parameters:

ds (xarray.Dataset) –

hydro.legacy.coards._create_common_variables(nc_file, ds)[source]#

Extracts the latitude, longitude, station, and cast from ds and passes them to create_common_variables

This logic could eventually just move to create_common_variables as it was previously rather complicated

Parameters:
hydro.legacy.coards.write_ctd(ds)[source]#

How to write a CTD NetCDF file.

Parameters:

ds (xarray.Dataset) –

Return type:

bytes

hydro.legacy.coards.write_bottle(ds)[source]#

How to write a Bottle NetCDF file.

Parameters:

ds (xarray.Dataset) – CCHDO CF/netCDF xarray dataset containing only a single bottle profile

Returns:

the bytes of a netCDF3 COARDS file

Return type:

bytes

hydro.legacy.coards.to_coards(ds)[source]#

Convert an xr.Dataset to a zipfile with COARDS netCDF files inside

This function does support mixed CTD and Bottle datasets and will convert using profile_type var on a per profile basis.

Parameters:

ds (xarray.Dataset) – A dataset conforming to CCHDO CF/netCDF

Returns:

a zipfile with one or more COARDS netCDF files as members.

Return type:

bytes