ensure_netcdf_var_exists_scalar Subroutine

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


Contents


Source Code

  subroutine ensure_netcdf_var_exists_scalar(file_id, var_name, var_type, var_id)
    !> 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
    !> The netCDF ID of the variable under `file_id`
    integer(kind_nf), intent(out) :: var_id

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

    status = nf90_inq_varid(file_id, var_name, var_id)
    ! Variable doesn't exist, so let's create it
    if (status == NF90_ENOTVAR) then
       status = nf90_def_var(file_id, var_name, var_type, var_id)
    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_scalar