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

Commit

Permalink
Fixed problem with NetCDF string writing
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Sep 4, 2021
1 parent c8bf996 commit df7a719
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions src/netcdf/netcdf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ 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, NAMELEN, idslot
integer(I4B) :: i, j, tslot, strlen, idslot
integer(I4B), dimension(:), allocatable :: ind
character(len=:), allocatable :: charstring

Expand All @@ -261,7 +261,6 @@ module subroutine netcdf_write_frame_base(self, iu, param)
class is (swiftest_body)
associate(n => self%nbody)
if (n == 0) return


allocate(ind(n))
call util_sort(self%id(1:n), ind)
Expand Down Expand Up @@ -317,12 +316,6 @@ module subroutine netcdf_write_frame_base(self, iu, param)
idslot = self%id + 1
call check( nf90_put_var(iu%ncid, iu%id_varid, self%id, start=[idslot]) )

charstring = adjustl(self%info%name)
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )

charstring = adjustl(self%info%particle_type)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )

call check( nf90_put_var(iu%ncid, iu%Gmass_varid, self%Gmass, start=[idslot, tslot]) )
call check( nf90_put_var(iu%ncid, iu%radius_varid, self%radius, start=[idslot, tslot]) )
if (param%lrotation) then
Expand Down Expand Up @@ -353,9 +346,16 @@ 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, NAMELEN, idslot
integer(I4B) :: i, j, tslot, strlen, idslot
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
write(lenstr, *) NAMELEN
fmtlabel = "(A" // trim(adjustl(lenstr)) // ")"
write(emptystr, fmtlabel) " "

select type(self)
class is (swiftest_body)
Expand All @@ -368,17 +368,26 @@ module subroutine netcdf_write_particle_info_base(self, iu)
j = ind(i)
idslot = self%id(j) + 1
call check( nf90_put_var(iu%ncid, iu%id_varid, self%id(j), start=[idslot]) )
charstring = adjustl(self%info(j)%name)
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )

charstring = adjustl(self%info(j)%particle_type)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )
charstring = trim(adjustl(self%info(j)%name))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%name_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info(j)%particle_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = adjustl(self%info(j)%status)
call check( nf90_put_var(iu%ncid, iu%status_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )
charstring = trim(adjustl(self%info(j)%status))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%status_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%status_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = adjustl(self%info(j)%origin_type)
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )
charstring = trim(adjustl(self%info(j)%origin_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

call check( nf90_put_var(iu%ncid, iu%origin_time_varid, self%info(j)%origin_time, start=[idslot]) )
call check( nf90_put_var(iu%ncid, iu%origin_xhx_varid, self%info(j)%origin_xh(1), start=[idslot]) )
Expand All @@ -402,17 +411,25 @@ module subroutine netcdf_write_particle_info_base(self, iu)
idslot = self%id + 1
call check( nf90_put_var(iu%ncid, iu%id_varid, self%id, start=[idslot]) )

charstring = adjustl(self%info%name)
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )

charstring = adjustl(self%info%particle_type)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )

charstring = adjustl(self%info%status)
call check( nf90_put_var(iu%ncid, iu%status_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )

charstring = adjustl(self%info%origin_type)
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, charstring, start=[1, idslot], count=[NAMELEN, 1]) )
charstring = trim(adjustl(self%info%name))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%name_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info%particle_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info%status))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%status_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%status_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info%origin_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, emptystr, start=[1, idslot], count=[NAMELEN, 1]) )
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

call check( nf90_put_var(iu%ncid, iu%origin_time_varid, self%info%origin_time, start=[idslot]) )
call check( nf90_put_var(iu%ncid, iu%origin_xhx_varid, self%info%origin_xh(1), start=[idslot]) )
Expand Down

0 comments on commit df7a719

Please sign in to comment.