netcdf_utils Module

Provides some convenience wrappers around netCDF procedures, mostly for handling netCDF errors in a consistent manner, removing the need to manually check the returned status code.


Uses

    • netcdf

Contents


Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: kind_nf = kind(NF90_NOERR)

Kind of netCDF types and constants

integer(kind=kind_nf), public, parameter :: netcdf_real = NF90_DOUBLE

netCDF type of default real

integer(kind=kind_nf), public, parameter :: netcdf_int = NF90_INT

netCDF type of default integer

logical, private, parameter :: test = .false.
logical, private :: use_compression_default = .true.
integer(kind=kind_nf), private :: deflate_level_default = 1

Interfaces

public interface ensure_netcdf_var_exists

Get the netCDF ID for a variable, creating it if it doesn't exist already

  • private subroutine ensure_netcdf_var_exists_scalar(file_id, var_name, var_type, var_id)

    Get the netCDF ID for a variable, creating it if it doesn't exist already. Aborts if any errors are detected.

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=kind_nf), intent(in) :: file_id

    ID of the file or group to look for or create the variable under

    character(len=*), intent(in) :: var_name

    Name of the variable

    integer(kind=kind_nf), intent(in) :: var_type

    The netCDF type of the variable

    integer(kind=kind_nf), intent(out) :: var_id

    The netCDF ID of the variable under file_id

  • private subroutine ensure_netcdf_var_exists_onedim(file_id, var_name, var_type, dim_id, var_id, compress, deflate_level)

    Get the netCDF ID for a variable, creating it if it doesn't exist already. Aborts if any errors are detected.

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=kind_nf), intent(in) :: file_id

    ID of the file or group to look for or create the variable under

    character(len=*), intent(in) :: var_name

    Name of the variable

    integer(kind=kind_nf), intent(in) :: var_type

    The netCDF type of the variable

    integer(kind=kind_nf), intent(in) :: dim_id

    Array of dimension IDs

    integer(kind=kind_nf), intent(out) :: var_id

    The netCDF ID of the variable under file_id

    logical, intent(in), optional :: compress

    Do we want to use compression for this variable

    integer(kind=kind_nf), intent(in), optional :: deflate_level

    The compression level to use for this variable

  • private subroutine ensure_netcdf_var_exists_manydims(file_id, var_name, var_type, dim_id, var_id, compress, deflate_level)

    Get the netCDF ID for a variable, creating it if it doesn't exist already. Aborts if any errors are detected.

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=kind_nf), intent(in) :: file_id

    ID of the file or group to look for or create the variable under

    character(len=*), intent(in) :: var_name

    Name of the variable

    integer(kind=kind_nf), intent(in) :: var_type

    The netCDF type of the variable

    integer(kind=kind_nf), intent(in), dimension(:) :: dim_id

    Array of dimension IDs

    integer(kind=kind_nf), intent(out) :: var_id

    The netCDF ID of the variable under file_id

    logical, intent(in), optional :: compress

    Do we want to use compression for this variable

    integer(kind=kind_nf), intent(in), optional :: deflate_level

    The compression level to use for this variable

public interface ensure_netcdf_att_exists

Add an attribute to an existing netCDF variable

  • private subroutine ensure_netcdf_att_exists_text(file_id, var_id, att_name, att_text)

    Add an attribute to an existing netCDF variable. Aborts if any errors are detected.

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=kind_nf), intent(in) :: file_id

    ID of the file or group to look for or create the variable under

    integer(kind=kind_nf), intent(in) :: var_id

    The netCDF ID of the variable under file_id

    character(len=*), intent(in) :: att_name

    Name of the attribute

    character(len=*), intent(in) :: att_text

    Attribute text to store


Functions

public function get_netcdf_code_precision() result(code_real)

Returns the correct netCDF constant for the real kind in use

Arguments

None

Return Value integer

public function get_netcdf_dim_length(file_id, dim_id) result(length)

Wrapper around nf90_inquire_dimension. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

NetCDF ID of the file

integer(kind=kind_nf), intent(in) :: dim_id

NetCDF ID of the dimension

Return Value integer

public function get_default_netcdf_compression() result(compression_flag)

Returns the current value of use_compression_default

Arguments

None

Return Value logical

public function get_default_netcdf_compression_level() result(compression_level)

Returns the current value of deflate_level_default

Arguments

None

Return Value integer(kind=kind_nf)


Subroutines

public subroutine check_netcdf_file_precision(ncid, filename)

Check that an existing netCDF file uses the same real kind as the current simulation. Only prints a warning!

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in), optional :: ncid
character(len=*), intent(in), optional :: filename

public subroutine netcdf_error(istatus, ncid, varid, dimid, file, dim, var, att, message, abort)

Convert a netCDF error code to a nice error message. Writes to error_unit

Read more…

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: istatus

The netCDF error code to convert to a message

integer(kind=kind_nf), intent(in), optional :: ncid

netCDF ID of the file

integer(kind=kind_nf), intent(in), optional :: varid

netCDF ID of the variable

integer(kind=kind_nf), intent(in), optional :: dimid

netCDF ID of the dimension

character(len=*), intent(in), optional :: file

Name of the file

character(len=*), intent(in), optional :: dim

Name of the dimension

character(len=*), intent(in), optional :: var

Name of the variable

character(len=*), intent(in), optional :: att

Name of the attribute

character(len=*), intent(in), optional :: message

Custom text to append to the error message

logical, intent(in), optional :: abort

Immediately abort the program if present and true

private subroutine ensure_netcdf_var_exists_scalar(file_id, var_name, var_type, var_id)

Get the netCDF ID for a variable, creating it if it doesn't exist already. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

ID of the file or group to look for or create the variable under

character(len=*), intent(in) :: var_name

Name of the variable

integer(kind=kind_nf), intent(in) :: var_type

The netCDF type of the variable

integer(kind=kind_nf), intent(out) :: var_id

The netCDF ID of the variable under file_id

private subroutine ensure_netcdf_var_exists_onedim(file_id, var_name, var_type, dim_id, var_id, compress, deflate_level)

Get the netCDF ID for a variable, creating it if it doesn't exist already. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

ID of the file or group to look for or create the variable under

character(len=*), intent(in) :: var_name

Name of the variable

integer(kind=kind_nf), intent(in) :: var_type

The netCDF type of the variable

integer(kind=kind_nf), intent(in) :: dim_id

Array of dimension IDs

integer(kind=kind_nf), intent(out) :: var_id

The netCDF ID of the variable under file_id

logical, intent(in), optional :: compress

Do we want to use compression for this variable

integer(kind=kind_nf), intent(in), optional :: deflate_level

The compression level to use for this variable

private subroutine ensure_netcdf_var_exists_manydims(file_id, var_name, var_type, dim_id, var_id, compress, deflate_level)

Get the netCDF ID for a variable, creating it if it doesn't exist already. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

ID of the file or group to look for or create the variable under

character(len=*), intent(in) :: var_name

Name of the variable

integer(kind=kind_nf), intent(in) :: var_type

The netCDF type of the variable

integer(kind=kind_nf), intent(in), dimension(:) :: dim_id

Array of dimension IDs

integer(kind=kind_nf), intent(out) :: var_id

The netCDF ID of the variable under file_id

logical, intent(in), optional :: compress

Do we want to use compression for this variable

integer(kind=kind_nf), intent(in), optional :: deflate_level

The compression level to use for this variable

private subroutine ensure_netcdf_att_exists_text(file_id, var_id, att_name, att_text)

Add an attribute to an existing netCDF variable. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

ID of the file or group to look for or create the variable under

integer(kind=kind_nf), intent(in) :: var_id

The netCDF ID of the variable under file_id

character(len=*), intent(in) :: att_name

Name of the attribute

character(len=*), intent(in) :: att_text

Attribute text to store

public subroutine ensure_netcdf_dim_exists(file_id, dim_name, dim_size, dim_id)

Get the netCDF ID for a dimension, creating it if it doesn't exist already. Aborts if any errors are detected.

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: file_id

ID of the file or group to look for or create the dimension under

character(len=*), intent(in) :: dim_name

Name of the dimension

integer(kind=kind_nf), intent(in) :: dim_size

The size of the dimension

integer(kind=kind_nf), intent(out) :: dim_id

The netCDF ID of the dimension under file_id

public subroutine set_default_netcdf_compression(compression_flag)

Set the value of use_compression_default

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: compression_flag

public subroutine set_default_netcdf_compression_level(compression_level)

Set the value of deflate_level_default

Arguments

Type IntentOptional Attributes Name
integer(kind=kind_nf), intent(in) :: compression_level