Skip to content

Commit

Permalink
feat: Control-file directive to allow minor differences between proje…
Browse files Browse the repository at this point in the history
…ct grid and netCDF file grid coordinates - ex. 'TMIN_COORDINATE_TOLERANCE 0.01' directs SWB to use the netCDF file unless min and max coordinate values differ from the SWB grid bounds by > 0.01 degrees
  • Loading branch information
smwesten-usgs committed Mar 19, 2021
1 parent f6c592f commit eebbbce
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 60 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ project( swb2 LANGUAGES C Fortran )
message("Processing top-level CMakelists.txt for project swb2")

set(SWB_MAJOR_VERSION "2")
set(SWB_MINOR_VERSION "1")
set(SWB_PATCH_VERSION "1")
set(SWB_LATEST_VERSION_TAG "v2.1.1")
set(SWB_MINOR_VERSION "2")
set(SWB_PATCH_VERSION "0")
set(SWB_LATEST_VERSION_TAG "v2.2.0")

#------------------------------------------------------------------------------------#
# Force out-of-source build
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1, build 0
2.1.1, build 1
2 changes: 1 addition & 1 deletion build/linux_x64/gfortran/run_cmake_gfortran.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export INSTALL_PREFIX=/usr/local/bin

# define other variables for use in the CMakeList.txt file
# options are "Release" or "Debug"
export BUILD_TYPE="Debug"
export BUILD_TYPE="Release"

# define platform and compiler specific compilation flags
export CMAKE_Fortran_FLAGS_DEBUG="-O0 -g -ggdb -Wuninitialized -fbacktrace -fcheck=all -fexceptions -fmax-errors=6 -fbackslash -ffree-line-length-none -Wno-maybe-uninitialized"
Expand Down
119 changes: 71 additions & 48 deletions src/data_catalog_entry.F90
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module data_catalog_entry
real (c_double) :: rUserAddOffset = 0_c_double
real (c_double) :: rX_Coord_AddOffset = 0.0_c_double
real (c_double) :: rY_Coord_AddOffset = 0.0_c_double
real (c_double) :: rCoordinateTolerance = 0.0_c_double

logical (c_bool) :: lAllowMissingFiles = FALSE
logical (c_bool) :: lAllowAutomaticDataFlipping = TRUE
Expand Down Expand Up @@ -150,10 +151,11 @@ module data_catalog_entry

procedure :: initialize_netcdf => initialize_netcdf_data_object_sub

procedure :: set_scale => set_scale_sub
procedure :: set_offset => set_offset_sub
procedure :: set_X_offset => set_X_coord_offset_sub
procedure :: set_Y_offset => set_Y_coord_offset_sub
procedure :: set_scale => set_scale_sub
procedure :: set_offset => set_offset_sub
procedure :: set_X_offset => set_X_coord_offset_sub
procedure :: set_Y_offset => set_Y_coord_offset_sub
procedure :: set_coordinate_tolerance => set_coordinate_tolerance_sub

procedure :: set_majority_filter_flag => set_majority_filter_flag_sub

Expand Down Expand Up @@ -1330,7 +1332,6 @@ function test_for_need_to_pad_values_fn(this, dt ) result(lNeedToPadData)

enddo


end function test_for_need_to_pad_values_fn

!--------------------------------------------------------------------------------------------------
Expand All @@ -1346,6 +1347,9 @@ subroutine getvalues_dynamic_netcdf_sub( this, dt )
logical (c_bool) :: lDateTimeFound
real (c_double) :: dAddOffset
real (c_double) :: dScaleFactor
integer (c_int) :: iMonth
integer (c_int) :: iDay
integer (c_int) :: iYear

if ( .not. associated(this%pGrdBase) ) &
call die("Internal programming error--attempt to use null pointer", __SRCNAME__, __LINE__)
Expand Down Expand Up @@ -1398,56 +1402,59 @@ subroutine getvalues_dynamic_netcdf_sub( this, dt )
if( ( len_trim( this%sSourcePROJ4_string ) > 0 ) &
.and. ( .not. ( this%sSourcePROJ4_string .strequal. "<NA>") ) ) then

! calculate the project boundaries in the coordinate system of
! calculate the project boundaries in the coordinate system of
! the native data file
call this%calc_project_boundaries(pGrdBase=this%pGrdBase)

if ( this%lRequireCompleteSpatialCoverage ) then

call netcdf_open_and_prepare_as_input(NCFILE=this%NCFILE, &
sFilename=this%sSourceFilename, &
lFlipHorizontal=this%lFlipHorizontal, &
lFlipVertical=this%lFlipVertical, &
lAllowAutomaticDataFlipping=this%lAllowAutomaticDataFlipping, &
rX_Coord_AddOffset = this%rX_Coord_AddOffset, &
rY_Coord_AddOffset = this%rY_Coord_AddOffset, &
sVariableOrder=this%sVariableOrder, &
sVarName_x=this%sVariableName_x, &
sVarName_y=this%sVariableName_y, &
sVarName_z=this%sVariableName_z, &
sVarName_time=this%sVariableName_time, &
call netcdf_open_and_prepare_as_input(NCFILE=this%NCFILE, &
sFilename=this%sSourceFilename, &
lFlipHorizontal=this%lFlipHorizontal, &
lFlipVertical=this%lFlipVertical, &
lAllowAutomaticDataFlipping=this%lAllowAutomaticDataFlipping, &
rX_Coord_AddOffset = this%rX_Coord_AddOffset, &
rY_Coord_AddOffset = this%rY_Coord_AddOffset, &
sVariableOrder=this%sVariableOrder, &
sVarName_x=this%sVariableName_x, &
sVarName_y=this%sVariableName_y, &
sVarName_z=this%sVariableName_z, &
sVarName_time=this%sVariableName_time, &
rCoordinateTolerance=this%rCoordinateTolerance, &
tGridBounds=this%GRID_BOUNDS_NATIVE )

else

call netcdf_open_and_prepare_as_input(NCFILE=this%NCFILE, &
sFilename=this%sSourceFilename, &
lFlipHorizontal=this%lFlipHorizontal, &
lFlipVertical=this%lFlipVertical, &
lAllowAutomaticDataFlipping=this%lAllowAutomaticDataFlipping, &
rX_Coord_AddOffset = this%rX_Coord_AddOffset, &
rY_Coord_AddOffset = this%rY_Coord_AddOffset, &
sVariableOrder=this%sVariableOrder, &
sVarName_x=this%sVariableName_x, &
sVarName_y=this%sVariableName_y, &
sVarName_z=this%sVariableName_z, &
sVarName_time=this%sVariableName_time )
call netcdf_open_and_prepare_as_input(NCFILE=this%NCFILE, &
sFilename=this%sSourceFilename, &
lFlipHorizontal=this%lFlipHorizontal, &
lFlipVertical=this%lFlipVertical, &
lAllowAutomaticDataFlipping=this%lAllowAutomaticDataFlipping, &
rX_Coord_AddOffset = this%rX_Coord_AddOffset, &
rY_Coord_AddOffset = this%rY_Coord_AddOffset, &
sVariableOrder=this%sVariableOrder, &
sVarName_x=this%sVariableName_x, &
sVarName_y=this%sVariableName_y, &
sVarName_z=this%sVariableName_z, &
sVarName_time=this%sVariableName_time, &
rCoordinateTolerance=this%rCoordinateTolerance )

endif

else ! PROJ4 string is blank

! assume source NetCDF file is in same projection and
! of same dimensions as base grid
call netcdf_open_and_prepare_as_input(NCFILE=this%NCFILE, &
sFilename=this%sSourceFilename, &
lFlipHorizontal=this%lFlipHorizontal, &
lFlipVertical=this%lFlipVertical, &
lAllowAutomaticDataFlipping=this%lAllowAutomaticDataFlipping, &
sVariableOrder=this%sVariableOrder, &
sVarName_x=this%sVariableName_x, &
sVarName_y=this%sVariableName_y, &
sVarName_z=this%sVariableName_z, &
call netcdf_open_and_prepare_as_input(NCFILE=this%NCFILE, &
sFilename=this%sSourceFilename, &
lFlipHorizontal=this%lFlipHorizontal, &
lFlipVertical=this%lFlipVertical, &
lAllowAutomaticDataFlipping=this%lAllowAutomaticDataFlipping, &
sVariableOrder=this%sVariableOrder, &
sVarName_x=this%sVariableName_x, &
sVarName_y=this%sVariableName_y, &
sVarName_z=this%sVariableName_z, &
rCoordinateTolerance=this%rCoordinateTolerance, &
sVarName_time=this%sVariableName_time )

this%NCFILE%iNX = this%pGrdBase%iNX
Expand Down Expand Up @@ -1492,15 +1499,12 @@ subroutine getvalues_dynamic_netcdf_sub( this, dt )
else
! Projection settings can be left alone; read values from new
! NetCDF file with same grid boundaries, projection, etc.

! call netcdf_open_file(NCFILE=this%NCFILE, sFilename=this%sSourceFilename, iLU=LU_LOG)
call netcdf_open_file(NCFILE=this%NCFILE, sFilename=this%sSourceFilename)

this%iNC_FILE_STATUS = NETCDF_FILE_OPEN

endif


if ( netcdf_date_within_range(NCFILE=this%NCFILE, &
iJulianDay=int(dt%iJulianDay, c_int) ) ) then

Expand All @@ -1517,13 +1521,21 @@ subroutine getvalues_dynamic_netcdf_sub( this, dt )

else

call LOGS%write("Valid date range (NetCDF): "//trim(asCharacter(this%NCFILE%iFirstDayJD)) &
//" to "//trim(asCharacter(this%NCFILE%iLastDayJD)) )

call LOGS%write("Current Julian Day value: "//trim(asCharacter(dt%iJulianDay)) )
call gregorian_date(this%NCFILE%iFirstDayJD,iYear, iMonth, iDay )
call LOGS%write("NetCDF start date: "//trim(asCharacter(iMonth, "i2.2")) &
//"/"//trim(asCharacter(iDay, "i2.2"))//"/"//trim(asCharacter(iYear, "i4.4")))
!//" to "//trim(asCharacter(this%NCFILE%iLastDayJD)) )

call gregorian_date(this%NCFILE%iLastDayJD,iYear, iMonth, iDay )
call LOGS%write("NetCDF end date: "//trim(asCharacter(iMonth, "i2.2")) &
//"/"//trim(asCharacter(iDay, "i2.2"))//"/"//trim(asCharacter(iYear, "i4.4")))

call gregorian_date(int(dt%iJulianDay, c_int),iYear, iMonth, iDay )
call LOGS%write("Current SWB simulation date: "//trim(asCharacter(iMonth, "i2.2")) &
//"/"//trim(asCharacter(iDay, "i2.2"))//"/"//trim(asCharacter(iYear, "i4.4")))

call assert (FALSE, "Date range for currently open NetCDF file" &
//" does not include the present simulation date.", &
//" does not include the current simulation date.", &
__SRCNAME__, __LINE__)

endif
Expand Down Expand Up @@ -1941,14 +1953,25 @@ end subroutine set_X_coord_offset_sub
subroutine set_Y_coord_offset_sub(this, rYOffset)

class (DATA_CATALOG_ENTRY_T) :: this
real (c_double) :: rYOffset
real (c_double) :: rYOffset

this%rY_Coord_AddOffset = rYOffset

end subroutine set_Y_coord_offset_sub

!----------------------------------------------------------------------

subroutine set_coordinate_tolerance_sub(this, rCoordinateTolerance)

class (DATA_CATALOG_ENTRY_T) :: this
real (c_double) :: rCoordinateTolerance

this%rCoordinateTolerance = rCoordinateTolerance

end subroutine set_coordinate_tolerance_sub

!----------------------------------------------------------------------

subroutine set_offset_sub(this, rAddOffset)

class (DATA_CATALOG_ENTRY_T) :: this
Expand Down
7 changes: 3 additions & 4 deletions src/main.F90
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ program main
asCharacter, right
use version_control, only : SWB_VERSION, GIT_COMMIT_HASH_STRING, &
GIT_BRANCH_STRING, COMPILE_DATE, &
COMPILE_TIME, SYSTEM_NAME
COMPILE_TIME, SYSTEM_NAME, &
WINDOWS_SYSTEM
use fstring_list, only : FSTRING_LIST_T
use timer, only : TIMER_T
use iso_fortran_env
Expand Down Expand Up @@ -74,9 +75,7 @@ program main
sCompilerVersion = COMPILER_VERSION()
sExecutableDescription = "USGS Soil-Water-Balance Code version "//trim( sVersionString )

if ( (SYSTEM_NAME .containssimilar. "Windows") &
.or. (SYSTEM_NAME .containssimilar. "Mingw") &
.or. (sCompilerVersion .containssimilar. "Windows")) then
if ( WINDOWS_SYSTEM ) then
OS_NATIVE_PATH_DELIMITER = "\"
else
OS_NATIVE_PATH_DELIMITER = "/"
Expand Down
6 changes: 5 additions & 1 deletion src/model_initialize.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,10 @@ subroutine initialize_generic_grid(sKey, sPathname, lOptional, iDataType )

call pENTRY%set_offset(asFloat(sArgText_1))

elseif ( sCmdText .containssimilar. "_COORDINATE_TOLERANCE" ) then

call pENTRY%set_coordinate_tolerance( asDouble(sArgText_1))

elseif ( sCmdText .containssimilar. "NETCDF_X_VAR" ) then

pENTRY%sVariableName_x = trim(sArgText_1)
Expand Down Expand Up @@ -1289,7 +1293,7 @@ subroutine initialize_generic_grid(sKey, sPathname, lOptional, iDataType )

! if an unadorned grid specification directive was processed, then we can add the key and
! the data_catalog_entry to the data_catalog
if ( lGridPresent )call DAT%add( key=sKey, data=pENTRY )
if ( lGridPresent ) call DAT%add( key=sKey, data=pENTRY )

pENTRY => null()

Expand Down
23 changes: 21 additions & 2 deletions src/netcdf4_support.F90
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ module netcdf4_support
character (len=3) :: sVariableOrder = "tyx"
real (c_double), dimension(0:1) :: rX
real (c_double), dimension(0:1) :: rY
real (c_double) :: rCoordinateTolerance = 0.0_c_double ! set this to be > 0.0 to allow some 'slop' when comparing coordinates
logical (c_bool) :: lX_IncreasesWithIndex = TRUE
logical (c_bool) :: lY_IncreasesWithIndex = FALSE
logical (c_bool) :: lAllowAutomaticDataFlipping = TRUE
Expand Down Expand Up @@ -301,6 +302,7 @@ module netcdf4_support
public :: netcdf_put_packed_variable_array
public :: netcdf_put_variable_vector
public :: netcdf_coord_to_col_row
public :: netcdf_set_coordinate_tolerance
public :: netcdf_get_variable_id_for_variable

contains
Expand Down Expand Up @@ -731,6 +733,7 @@ subroutine netcdf_open_and_prepare_as_input(NCFILE, sFilename, &
rX_Coord_AddOffset, rY_Coord_AddOffset, &
sVariableOrder, sVarName_x, &
sVarName_y, sVarName_z, sVarName_time, &
rCoordinateTolerance, &
tGridBounds, iLU)

type (T_NETCDF4_FILE ) :: NCFILE
Expand All @@ -745,6 +748,7 @@ subroutine netcdf_open_and_prepare_as_input(NCFILE, sFilename, &
character (len=*), optional :: sVarName_y
character (len=*), optional :: sVarName_z
character (len=*), optional :: sVarName_time
real (c_double), optional :: rCoordinateTolerance
type (GRID_BOUNDS_T), optional :: tGridBounds
integer (c_int), optional :: iLU

Expand All @@ -768,6 +772,8 @@ subroutine netcdf_open_and_prepare_as_input(NCFILE, sFilename, &
if (present(rX_Coord_AddOffset)) NCFILE%rX_Coord_AddOffset = rX_Coord_AddOffset
if (present(rY_Coord_AddOffset)) NCFILE%rY_Coord_AddOffset = rY_Coord_AddOffset

if (present(rCoordinateTolerance)) NCFILE%rCoordinateTolerance = rCoordinateTolerance

if (present(sVariableOrder) ) NCFILE%sVariableOrder = sVariableOrder

if( present(iLU) ) then
Expand Down Expand Up @@ -2029,6 +2035,19 @@ subroutine netcdf_get_attribute_list_for_variable( NCFILE, variable_name, &

end subroutine netcdf_get_attribute_list_for_variable

!----------------------------------------------------------------------

subroutine netcdf_set_coordinate_tolerance(NCFILE, tolerance)

type (T_NETCDF4_FILE) :: NCFILE
real (c_double), intent(in) :: tolerance

NCFILE%rCoordinateTolerance = tolerance

end subroutine netcdf_set_coordinate_tolerance

!----------------------------------------------------------------------

subroutine netcdf_get_variable_list( NCFILE, variable_list )

type (T_NETCDF4_FILE) :: NCFILE
Expand Down Expand Up @@ -3197,8 +3216,8 @@ function netcdf_coord_to_col_row(NCFILE, rX, rY) result(iColRow)
real (c_double) :: x_offset
real (c_double) :: y_offset

x_offset = NCFILE%rGridCellSizeX / 2.0_c_double
y_offset = NCFILE%rGridCellSizeY / 2.0_c_double
x_offset = NCFILE%rGridCellSizeX / 2.0_c_double + NCFILE%rCoordinateTolerance
y_offset = NCFILE%rGridCellSizeY / 2.0_c_double + NCFILE%rCoordinateTolerance

call assert( allocated( NCFILE%rX_Coords ), "Internal programming error -- attempt " &
//"to access unallocated array rX_Coords.", __SRCNAME__, __LINE__ )
Expand Down
3 changes: 3 additions & 0 deletions src/version_control.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module version_control

use iso_c_binding, only : c_bool
implicit none

#ifndef VERSION_H
Expand All @@ -9,6 +10,7 @@ module version_control
#define EXPAND_AND_QUOTE(str) QUOTE(str)

#define PLATFORM_NAME "@CMAKE_SYSTEM@"
#define WINDOWS_SYSTEM_BOOL "@WIN32"
#define GIT_BRANCH_VALUE "@GIT_BRANCH@"
#define GIT_COMMIT_HASH_VALUE "@GIT_COMMIT_HASH@"
#define GIT_COMMITS_ON_BRANCH_VALUE "@GIT_COMMITS_ON_BRANCH@"
Expand All @@ -35,6 +37,7 @@ module version_control
character (len=*), parameter :: COMPILE_DATE = trim(__DATE__)
character (len=*), parameter :: COMPILE_TIME = trim(__TIME__)
character (len=*), parameter :: COMPILATION_TIMESTAMP = trim(COMPILE_DATE)//" "//trim(COMPILE_TIME)
logical (c_bool), parameter :: WINDOWS_SYSTEM = (trim(WINDOWS_SYSTEM_BOOL) == "True")

#endif

Expand Down

0 comments on commit eebbbce

Please sign in to comment.