fill_kipp_fields Subroutine

private subroutine fill_kipp_fields(path, ncols, c_model, fld_col, nf, recs, ndata, ifirst, ilast)

Re-read .kipp file and bin each registered field into recs(k)%fld.

Second streaming pass of loadkipp: grouping mirrors pass one exactly (same model boundaries and range filter), so a running counter maps each completed in-range model to its record.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: path

Path of .kipp file.

integer(kind=int32), intent(in) :: ncols

Number of values per cell.

integer(kind=int32), intent(in) :: c_model

Column of model number.

integer(kind=int32), intent(in) :: fld_col(:)

Column of each registered field.

integer(kind=int32), intent(in) :: nf

Number of registered fields.

type(convtype), intent(inout) :: recs(:)

Records of pass one, in stream order.

integer(kind=int32), intent(in) :: ndata

Number of records pass one built.

integer(kind=int32), intent(in) :: ifirst

First model number to load.

integer(kind=int32), intent(in) :: ilast

Last model number to load.


Source Code

   subroutine fill_kipp_fields(path, ncols, c_model, fld_col, nf, &
                               recs, ndata, ifirst, ilast)
      !! Re-read `.kipp` file and bin each registered field into `recs(k)%fld`.
      !!
      !! Second streaming pass of `loadkipp`: grouping mirrors pass one exactly (same model
      !! boundaries and range filter), so a running counter maps each completed in-range model to
      !! its record.
      ! Arguments
      character(len=*), intent(in) :: path
         !! Path of `.kipp` file.
      integer(int32), intent(in) :: ncols
         !! Number of values per cell.
      integer(int32), intent(in) :: c_model
         !! Column of model number.
      integer(int32), intent(in) :: nf
         !! Number of registered fields.
      integer(int32), intent(in) :: ndata
         !! Number of records pass one built.
      integer(int32), intent(in) :: ifirst
         !! First model number to load.
      integer(int32), intent(in) :: ilast
         !! Last model number to load.
      integer(int32), intent(in) :: fld_col(:)
         !! Column of each registered field.
      type(convtype), intent(inout) :: recs(:)
         !! Records of pass one, in stream order.

      integer(int32), parameter :: CHUNK = 65536
      integer(int32) :: iunit, iostat, j, m, cur, nz, zcap, counter, f
      integer(int64) :: fbytes, ncells, ndone, take
      logical :: stopping
      real(real64), allocatable :: buf(:, :), fb(:, :)

      open (NEWUNIT=iunit, FILE=trim(path), STATUS='OLD', ACTION='READ', &
            FORM='UNFORMATTED', ACCESS='STREAM', CONVERT='little_endian', &
            IOSTAT=iostat)
      if (iostat /= 0) &
         error stop '[fill_kipp_fields] cannot reopen '//trim(path)
      inquire (UNIT=iunit, SIZE=fbytes)
      ncells = fbytes/(int(ncols, int64)*8_int64)

      zcap = 8192
      allocate (fb(nf, zcap))
      nz = 0
      cur = -huge(1_int32)
      counter = 0
      stopping = .false.

      allocate (buf(ncols, CHUNK))
      ndone = 0
      do while (ndone < ncells .and. .not. stopping)
         take = min(int(CHUNK, int64), ncells - ndone)
         read (iunit, IOSTAT=iostat) buf(:, 1:take)
         if (iostat /= 0) &
            error stop '[fill_kipp_fields] short read'
         ndone = ndone + take
         do j = 1, int(take, int32)
            m = nint(buf(c_model, j))
            if (m /= cur) then
               call flush_kipp_fields(recs, counter, cur, nz, fb, nf, &
                                      ifirst, ilast)
               cur = m
               nz = 0
               if (m > ilast) then
                  stopping = .true.
                  exit
               end if
            end if
            if (m < ifirst .or. m > ilast) cycle
            nz = nz + 1
            if (nz > zcap) call grow_fb(fb, zcap)
            do f = 1, nf
               fb(f, nz) = buf(fld_col(f), j)
            end do
         end do
      end do
      call flush_kipp_fields(recs, counter, cur, nz, fb, nf, ifirst, ilast)
      close (iunit)
      deallocate (buf, fb)

      if (counter /= ndata) &
         error stop '[fill_kipp_fields] model grouping drifted between passes'
   end subroutine fill_kipp_fields