loadmon Subroutine

public subroutine loadmon(path, ifirst, ilast)

Load models of MONASH seq file into data.

Skips models outside ifirst to ilast. Pass one builds records and widens field ranges, pass two re-reads file to bin field layers, then sort_models. Stops w/ error if file changed between passes or fewer than 2 models in range.

Arguments

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

Path of the seq file.

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 loadmon(path, ifirst, ilast)
      !! Load models of MONASH `seq` file into `data`.
      !!
      !! Skips models outside `ifirst` to `ilast`. Pass one builds records and widens field ranges,
      !! pass two re-reads file to bin field layers, then `sort_models`. Stops w/ error if file
      !! changed between passes or fewer than 2 models in range.
      ! Arguments
      character(len=*), intent(in) :: path
         !! Path of the `seq` file.
      integer(int32), intent(in) :: ifirst
         !! First model number to load.
      integer(int32), intent(in) :: ilast
         !! Last model number to load.

      integer :: iunit, iostat, ncurrent, npts, i, j
      integer(int32) :: ndata, ncap, m, fld
      real(real64) :: time, tint, tintn, mass, lolsun
      real(real64) :: ltc, lpc, ltca, lpca
      type(convtype), allocatable :: recs(:)

      open (NEWUNIT=iunit, FILE=trim(path), STATUS='OLD', ACTION='READ', &
            FORM='UNFORMATTED', ACCESS='SEQUENTIAL', IOSTAT=iostat)
      if (iostat /= 0) &
         error stop '[loadmon] cannot open '//trim(path)

      ncap = 128
      ndata = 0
      allocate (recs(ncap))
      call reset_field_registry(MON_NFIELDS)
      field_names = MON_FIELD_NAMES

      do
         read (iunit, IOSTAT=iostat) ncurrent, time, tint, tintn, mass, &
            lolsun, ltc, lpc, ltca, lpca, npts
         if (iostat /= 0) exit
         if (npts < 1) error stop '[loadmon] invalid cell count'
         backspace (iunit, IOSTAT=iostat)
         if (iostat /= 0) error stop '[loadmon] cannot backspace record'

         block
            real(real64), allocatable :: omx(:), up(:), rhop(:)
            real(real64), allocatable :: f(:, :), fa(:, :)
            real, allocatable :: dx(:), vconvp(:), w(:, :), wd(:, :)
            integer(int16), allocatable :: kcvtn(:)
            type(rawset) :: raw

            allocate (omx(npts), dx(npts), kcvtn(npts), up(npts), &
                      rhop(npts), vconvp(npts), f(4, npts), fa(4, npts), &
                      w(7, npts), wd(7, npts))
            read (iunit, IOSTAT=iostat) ncurrent, time, tint, tintn, mass, &
               lolsun, ltc, lpc, ltca, lpca, npts, &
               (omx(j), dx(j), kcvtn(j), up(j), rhop(j), vconvp(j), &
                (f(i, j), fa(i, j), i=1, 4), &
                (w(i, j), wd(i, j), i=1, 7), j=1, npts)
            if (iostat /= 0) exit
            if (ncurrent < ifirst .or. ncurrent > ilast) cycle

            ndata = ndata + 1
            if (ndata > ncap) call grow_models(recs, ncap, ndata - 1)
            call build_mon_record(recs(ndata), raw, ncurrent, npts, &
                                  time, tint, mass, ltc, lpc, omx, rhop, &
                                  up, vconvp, kcvtn, f, w, wd)
         end block
      end do

      if (ndata < 2) then
         close (iunit)
         error stop '[loadmon] need at least 2 models in range'
      end if

      do fld = 1, MON_NFIELDS
         call finalize_field_log(fld)
      end do

      rewind (iunit, IOSTAT=iostat)
      if (iostat /= 0) error stop '[loadmon] cannot rewind '//trim(path)
      m = 0
      do
         read (iunit, IOSTAT=iostat) ncurrent, time, tint, tintn, mass, &
            lolsun, ltc, lpc, ltca, lpca, npts
         if (iostat /= 0) exit
         if (npts < 1) error stop '[loadmon] invalid cell count'
         backspace (iunit, IOSTAT=iostat)
         if (iostat /= 0) error stop '[loadmon] cannot backspace record'

         block
            real(real64), allocatable :: omx(:), up(:), rhop(:)
            real(real64), allocatable :: f(:, :), fa(:, :)
            real, allocatable :: dx(:), vconvp(:), w(:, :), wd(:, :)
            integer(int16), allocatable :: kcvtn(:)
            type(rawset) :: raw

            allocate (omx(npts), dx(npts), kcvtn(npts), up(npts), &
                      rhop(npts), vconvp(npts), f(4, npts), fa(4, npts), &
                      w(7, npts), wd(7, npts))
            read (iunit, IOSTAT=iostat) ncurrent, time, tint, tintn, mass, &
               lolsun, ltc, lpc, ltca, lpca, npts, &
               (omx(j), dx(j), kcvtn(j), up(j), rhop(j), vconvp(j), &
                (f(i, j), fa(i, j), i=1, 4), &
                (w(i, j), wd(i, j), i=1, 7), j=1, npts)
            if (iostat /= 0) exit
            if (ncurrent < ifirst .or. ncurrent > ilast) cycle

            m = m + 1
            if (m > ndata) error stop '[loadmon] file changed between passes'
            if (ncurrent /= recs(m)%ncyc .or. &
                npts /= recs(m)%ncoord) &
               error stop '[loadmon] file changed between passes'
            call build_mon_fields(raw, npts, ltc, lpc, rhop, up, vconvp, &
                                  f, w, wd)
            recs(m)%nfld = MON_NFIELDS
            allocate (recs(m)%fld(2, MON_NFIELDS))
            do fld = 1, MON_NFIELDS
               call build_field_layer(raw%v(:, fld), npts, fld, &
                                      recs(m)%fld(:, fld))
            end do
         end block
      end do
      close (iunit)
      if (m /= ndata) error stop '[loadmon] file changed between passes'

      if (allocated(data)) deallocate (data)
      allocate (data(ndata))
      data = recs(1:ndata)
      deallocate (recs)

      call sort_models()
      print *, '[loadmon] Loaded models', data(1)%ncyc, ' - ', &
         data(size(data))%ncyc
   end subroutine loadmon