ensure_netcdf_var_exists_manydims Subroutine

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

Uses

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


Contents


Source Code

  subroutine ensure_netcdf_var_exists_manydims(file_id, var_name, var_type, dim_id, var_id, &
       compress, deflate_level)
    use optionals, only: get_option_with_default
    !> ID of the file or group to look for or create the variable under
    integer(kind_nf), intent(in) :: file_id
    !> Name of the variable
    character(len=*), intent(in) :: var_name
    !> The netCDF type of the variable
    integer(kind_nf), intent(in) :: var_type
    !> Array of dimension IDs
    integer(kind_nf), dimension(:), intent(in) :: dim_id
    !> The netCDF ID of the variable under `file_id`
    integer(kind_nf), intent(out) :: var_id
    !> Do we want to use compression for this variable
    logical, intent(in), optional :: compress
    !> The compression level to use for this variable
    integer(kind_nf), intent(in), optional :: deflate_level

    ! Error code of the netCDF calls
    integer(kind_nf) :: status
    ! Do we want to use compression?
    logical :: use_compression
    ! What compression level do we want to use?
    integer(kind_nf) :: compression_level

    status = nf90_inq_varid(file_id, var_name, var_id)
    ! Variable doesn't exist, so let's create it
    if (status == NF90_ENOTVAR) then
       use_compression = get_option_with_default(compress, use_compression_default)
       compression_level = 0
       if (use_compression) &
            compression_level = get_option_with_default(deflate_level, deflate_level_default)

       status = nf90_def_var(file_id, var_name, var_type, dim_id, var_id, &
            deflate_level = compression_level, &
            shuffle = use_compression &
            )
    end if
    ! Something went wrong with one of the previous two calls
    if (status /= NF90_NOERR) then
      call netcdf_error(status, var=var_name, varid=var_id, &
                        message="(ensure_netcdf_var_exists)", abort=.true.)
    end if
  end subroutine ensure_netcdf_var_exists_manydims