loadconv Subroutine

public subroutine loadconv(namecnv, ifirst, ilast)

Uses

Load models of .cnv file into data.

Decodes each record by version (loadconv_record), skips to first model number of at least ifirst, stops at end of file or first model number above ilast, then calls sort_models.

Arguments

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

Path of the .cnv 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 loadconv(namecnv, ifirst, ilast)
    !! Load models of `.cnv` file into `data`.
    !!
    !! Decodes each record by version (`loadconv_record`), skips to first model number of at least
    !! `ifirst`, stops at end of file or first model number above `ilast`, then calls `sort_models`.

    use convdata, only: &
         convtype, data

    implicit none

    ! Arguments
    character(len=*), intent(in):: &
         namecnv
         !! Path of the `.cnv` file.
    integer(kind=int32), intent(IN) :: &
         ifirst
         !! First model number to load.
    integer(kind=int32), intent(IN) :: &
         ilast
         !! Last model number to load.

    integer(kind=int32):: &
         iunit, n, i, &
         nvers, nmodel, &
         iostat
    type(convtype) :: &
         cnv
    type(convtype), dimension(:), allocatable :: &
         data1

    if (allocated(data)) &
         deallocate(data)
    n = cnv_start
    allocate(data(n))

    open(NEWUNIT=iunit, &
         FILE=namecnv, &
         STATUS='OLD', &
         POSITION='REWIND', &
         FORM='UNFORMATTED', &
         CONVERT=convert)

    ! call fseek(UNIT=iunit, OFFSET=ifirst, WHENCE=0)

    do while (.True.)
       READ(iunit,IOSTAT=iostat) nvers, nmodel
       if (IS_IOSTAT_END(iostat)) &
           exit
       if (iostat /= 0) &
            error stop '[loadconv] Error finding record'
       if (nmodel >= ifirst) then
            backspace(iunit)
            exit
         endif
    enddo

    do i = 1, 2**30
       cnv = loadconv_record(iunit)
       if (cnv%nvers == 0) &
          exit
       if (cnv%ncyc > ilast) &
          exit
       if (i > n) then
          n = int(n * cnv_fac)
          allocate(data1(n))
          data1(1:i-1) = data(1:i-1)
          deallocate(data)
          call move_alloc(data1, data)
       endif
       data(i) = cnv
    enddo
    close(UNIT=iunit, STATUS='keep')

    n = i-1
    allocate(data1(n))
    data1(1:n) = data(1:n)
    deallocate(data)
    call move_alloc(data1, data)

    call sort_models()
    n = int(size(data), int32)

    print*, '[loadconv] Loaded models', data(1)%ncyc, ' - ', data(n)%ncyc

  end subroutine loadconv