ensure_netcdf_dim_exists Subroutine

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

Uses

    • netcdf

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


Contents


Source Code

  subroutine ensure_netcdf_dim_exists(file_id, dim_name, dim_size, dim_id)
    use netcdf, only: nf90_inq_dimid, nf90_def_dim, NF90_NOERR, NF90_EBADDIM
    !> ID of the file or group to look for or create the dimension under
    integer(kind_nf), intent(in) :: file_id
    !> Name of the dimension
    character(len=*), intent(in) :: dim_name
    !> The size of the dimension
    integer(kind_nf), intent(in) :: dim_size
    !> The netCDF ID of the dimension under `file_id`
    integer(kind_nf), intent(out) :: dim_id

    ! Error code of the netCDF calls
    integer(kind_nf) :: status

    status = nf90_inq_dimid(file_id, dim_name, dim_id)
    ! Dimension doesn't exist, so let's create it
    if (status == NF90_EBADDIM) then
      status = nf90_def_dim(file_id, dim_name, dim_size, dim_id)
    end if
    ! Something went wrong with one of the previous two calls
    if (status /= NF90_NOERR) then
      call netcdf_error(status, dim=dim_name, message="(ensure_netcdf_dim_exists)", abort=.true.)
    end if
  end subroutine ensure_netcdf_dim_exists