Skip to content

Commit

Permalink
Add a check for compatibility based on the version in the surface dat…
Browse files Browse the repository at this point in the history
…aset
  • Loading branch information
ekluzek committed Aug 31, 2024
1 parent 6be8c55 commit 7118d70
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/clm_initializeMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ subroutine initialize1(dtime)
use clm_varcon , only: clm_varcon_init
use landunit_varcon , only: landunit_varcon_init
use clm_varctl , only: fsurdat, version
use surfrdMod , only: surfrd_get_num_patches, surfrd_get_nlevurb
use surfrdMod , only: surfrd_get_num_patches, surfrd_get_nlevurb, surfrd_check_file
use controlMod , only: control_init, control_print, NLFilename
use ncdio_pio , only: ncd_pio_init
use initGridCellsMod , only: initGridCells
Expand Down Expand Up @@ -100,6 +100,7 @@ subroutine initialize1(dtime)

call control_init(dtime)
call ncd_pio_init()
call surfrd_check_file(fsurdat)
call surfrd_get_num_patches(fsurdat, actual_maxsoil_patches, actual_numpft, actual_numcft)
call surfrd_get_nlevurb(fsurdat, actual_nlevurb)

Expand Down
67 changes: 66 additions & 1 deletion src/main/surfrdMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module surfrdMod
!
! !USES:
#include "shr_assert.h"
use shr_kind_mod , only : r8 => shr_kind_r8
use shr_kind_mod , only : r8 => shr_kind_r8, r4 => shr_kind_r4
use shr_log_mod , only : errMsg => shr_log_errMsg
use abortutils , only : endrun
use clm_varpar , only : nlevsoifl
Expand All @@ -27,6 +27,7 @@ module surfrdMod
save
!
! !PUBLIC MEMBER FUNCTIONS:
public :: surfrd_check_file ! Check that this surface dataset is compatible
public :: surfrd_get_data ! Read surface dataset and determine subgrid weights
public :: surfrd_get_num_patches ! Read surface dataset to determine maxsoil_patches and numcft
public :: surfrd_get_nlevurb ! Read surface dataset to determine nlevurb
Expand All @@ -45,6 +46,70 @@ module surfrdMod

contains

!-----------------------------------------------------------------------
subroutine surfrd_check_file ( lfsurdat )
!
! !DESCRIPTION:
! Check compatability for this surface dataset and abort with an error if it's not
!
! !USES:
use ncdio_pio, only : check_att
! !ARGUMENTS:
character(len=*), intent(in) :: lfsurdat ! surface dataset filename
! !LOCAL VARIABLES:
type(file_desc_t) :: ncid ! netcdf id
logical :: exists ! If attribute or variable was found on the file
integer :: status ! Status return code
real(r4) :: version ! Version number on the dataset
! NOTE: Only increment the expected_version when surface datasets are incompatible with the previous version
! If datasets are just updated data and backwards compatble leave the expected version alone
real(r4), parameter :: expected_version = 5.3_r4
character(len=50) :: description
character(len=*), parameter :: version_name = 'Dataset_Version'

call ncd_pio_openfile (ncid, trim(lfsurdat), 0)
call check_att(ncid, pio_global, version_name, exists)
if (exists) then
status = pio_get_att(ncid, pio_global, version_name, version)
else
! For a few previous versions guess on the compatability version based on existence of variables
call check_var( ncid, 'PCT_OCEAN', exists)
if (exists) then
version = 5.2_r4
else
call check_var( ncid, 'PCT_CFT', exists)
if (exists) then
version = 5.0_r4
else
call check_var( ncid, 'PCT_GLC_MEC_GIC', exists)
if (exists) then
version = 4.5_r4
else
! This is a version before the main clm4_5 dataseta so marking it as 0 for unknown
version = 0.0_r4
end if
end if
end if
end if
call ncd_pio_closefile(ncid)
if ( (version /= expected_version) )then
if ( version < expected_version )then
description = 'older'
if ( version == 0.0_r4 ) description = trim(description)//' than 4.5'
else if ( version > expected_version )then
description = 'newer'
end if
if ( masterproc )then
write(iulog,*) 'Input surface dataset is: ', trim(lfsurdat)
write(iulog,'(3a)') 'This surface dataset is ', trim(description), ' and incompatable with this version of CTSM'
write(iulog,'(a,f3.1,a,f3.1)') 'Dataset version = ', version, ' Version expected = ', expected_version
write(iulog,*) errMsg(sourcefile, __LINE__)
end if
call endrun(msg="ERROR: Incompatble surface dataset")
end if

end subroutine surfrd_check_file

!-----------------------------------------------------------------------
subroutine surfrd_get_data (begg, endg, ldomain, lfsurdat, actual_numcft)
!
Expand Down

0 comments on commit 7118d70

Please sign in to comment.