Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Restructured the NetCDF writing and added NaN as fill values to all v…
Browse files Browse the repository at this point in the history
…ariables
  • Loading branch information
daminton committed Sep 15, 2021
1 parent 54624d4 commit 20af4a1
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/netcdf/netcdf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ module subroutine netcdf_initialize_output(self, param)
!! author: Carlisle A. Wishard, Dana Singh, and David A. Minton
!!
!! Initialize a NetCDF file system and defines all variables.
use, intrinsic :: ieee_arithmetic
implicit none
! Arguments
class(netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
! Internals
logical :: fileExists
integer(I4B) :: old_mode
integer(I4B) :: old_mode, nvar, varid, vartype
real(DP) :: nan

nan = ieee_value(nan, IEEE_QUIET_NAN)

!! Create the new output file, deleting any previously existing output file of the same name
call check( nf90_create(param%outfile, NF90_NETCDF4, self%ncid) )
call check( nf90_set_fill(self%ncid, nf90_nofill, old_mode) )

! Define the NetCDF dimensions with particle name as the record dimension
call check( nf90_def_dim(self%ncid, ID_DIMNAME, NF90_UNLIMITED, self%id_dimid) ) ! 'x' dimension
Expand Down Expand Up @@ -138,6 +141,12 @@ module subroutine netcdf_initialize_output(self, param)
call check( nf90_def_var(self%ncid, DISCARD_VHZ_VARNAME, self%out_type, self%id_dimid, self%discard_vhz_varid) )
call check( nf90_def_var(self%ncid, DISCARD_BODY_ID_VARNAME, NF90_INT, self%id_dimid, self%discard_body_id_varid) )

! Set fill mode to NaN for all variables
call check( nf90_inquire(self%ncid, nVariables=nvar) )
do varid = 1, nvar
call check( nf90_def_var_fill(self%ncid, varid, 0, nan) )
end do

return
end subroutine netcdf_initialize_output

Expand All @@ -150,11 +159,8 @@ module subroutine netcdf_open(self, param)
! Arguments
class(netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
! Internals
integer(I4B) :: old_mode

call check( nf90_open(param%outfile, nf90_write, self%ncid) )
call check( nf90_set_fill(self%ncid, nf90_nofill, old_mode) )

call check( nf90_inq_varid(self%ncid, TIME_DIMNAME, self%time_varid))
call check( nf90_inq_varid(self%ncid, ID_DIMNAME, self%id_varid))
Expand Down Expand Up @@ -249,14 +255,15 @@ module subroutine netcdf_write_frame_base(self, iu, param)
class(netcdf_parameters), intent(inout) :: iu !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
! Internals
integer(I4B) :: i, j, tslot, strlen, idslot
integer(I4B) :: i, j, tslot, strlen, idslot, old_mode
integer(I4B), dimension(:), allocatable :: ind
character(len=:), allocatable :: charstring

call self%write_particle_info(iu)

tslot = int(param%ioutput, kind=I4B) + 1

call check( nf90_set_fill(iu%ncid, nf90_nofill, old_mode) )
select type(self)
class is (swiftest_body)
associate(n => self%nbody)
Expand Down Expand Up @@ -332,6 +339,7 @@ module subroutine netcdf_write_frame_base(self, iu, param)
end if

end select
call check( nf90_set_fill(iu%ncid, old_mode, old_mode) )

return
end subroutine netcdf_write_frame_base
Expand All @@ -346,13 +354,14 @@ module subroutine netcdf_write_particle_info_base(self, iu)
class(swiftest_base), intent(in) :: self !! Swiftest particle object
class(netcdf_parameters), intent(inout) :: iu !! Parameters used to identify a particular NetCDF dataset
! Internals
integer(I4B) :: i, j, tslot, strlen, idslot
integer(I4B) :: i, j, tslot, strlen, idslot, old_mode
integer(I4B), dimension(:), allocatable :: ind
character(len=:), allocatable :: charstring
character(len=NAMELEN) :: emptystr, lenstr
character(len=:), allocatable :: fmtlabel

! This string of spaces of length NAMELEN is used to clear out any old data left behind inside the string variables
call check( nf90_set_fill(iu%ncid, nf90_nofill, old_mode) )
write(lenstr, *) NAMELEN
fmtlabel = "(A" // trim(adjustl(lenstr)) // ")"
write(emptystr, fmtlabel) " "
Expand Down Expand Up @@ -448,6 +457,7 @@ module subroutine netcdf_write_particle_info_base(self, iu)
call check( nf90_put_var(iu%ncid, iu%discard_vhz_varid, self%info%discard_vh(3), start=[idslot]) )
end select

call check( nf90_set_fill(iu%ncid, old_mode, old_mode) )
return
end subroutine netcdf_write_particle_info_base

Expand All @@ -464,12 +474,11 @@ module subroutine netcdf_write_hdr_system(self, iu, param)
class(netcdf_parameters), intent(inout) :: iu !! Parameters used to for writing a NetCDF dataset to file
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
! Internals
integer(I4B) :: tslot, old_mode
integer(I4B) :: tslot

tslot = int(param%ioutput, kind=I4B) + 1

call check( nf90_open(param%outfile, nf90_write, iu%ncid) )
call check( nf90_set_fill(iu%ncid, nf90_nofill, old_mode) )

call check( nf90_put_var(iu%ncid, iu%time_varid, param%t, start=[tslot]) )
call check( nf90_put_var(iu%ncid, iu%npl_varid, self%pl%nbody, start=[tslot]) )
Expand Down

0 comments on commit 20af4a1

Please sign in to comment.