Stream raw .kipp cell file into data.
One contiguous float64 array of ncols-wide cells, grouped by model, surface to centre
within a model, column layout from sidecar header <path>.hdr (docs/kippfiles.md). Read in
large chunks holding only current model's cells and finished records, so multi-GB files
never fully reside in memory. Pass one builds records and field ranges; fill_kipp_fields
bins fields in pass two.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
Path of |
||
| integer(kind=int32), | intent(in) | :: | ifirst |
First model number to load. |
||
| integer(kind=int32), | intent(in) | :: | ilast |
Last model number to load. |
subroutine loadkipp(path, ifirst, ilast) !! Stream raw `.kipp` cell file into `data`. !! !! One contiguous float64 array of `ncols`-wide cells, grouped by model, surface to centre !! within a model, column layout from sidecar header `<path>.hdr` (docs/kippfiles.md). Read in !! large chunks holding only current model's cells and finished records, so multi-GB files !! never fully reside in memory. Pass one builds records and field ranges; `fill_kipp_fields` !! bins fields in pass two. ! Arguments character(len=*), intent(in) :: path !! Path of `.kipp` file. integer(int32), intent(in) :: ifirst !! First model number to load. integer(int32), intent(in) :: ilast !! Last model number to load. integer(int32), parameter :: CHUNK = 65536 character(len=512) :: hdrfile integer(int32) :: ncols, ncols2 integer(int32) :: c_model, c_age, c_dt, c_mass, c_radius, c_mix, c_eps integer(int32) :: iunit, iostat, j, m, ndata, ncap, cur, nz, zcap, nf, f integer(int64) :: fbytes, ncells, ndone, take real(real64) :: tsec, dtsec, fage, fdt, fmass, frad logical :: stopping real(real64), allocatable :: buf(:, :) real(real64), allocatable :: am(:), ar(:), ae(:) integer(int32), allocatable :: amt(:) integer(int32), allocatable :: fld_col(:) character(len=32), allocatable :: colnames(:), colunits(:) character(len=4) :: dtunit type(convtype), allocatable :: recs(:) call find_kipp_header(path, hdrfile) call parse_kipp_header(hdrfile, ncols, c_model, c_age, c_dt, & c_mass, c_radius, c_mix, c_eps) call kipp_colnames(hdrfile, ncols2, colnames, colunits) call register_kipp_fields(colnames, colunits, ncols, fld_col, nf) ! factors to s, g and cm; unstated units are those the names imply call kipp_axis_unit(colnames(c_age), colunits(c_age), DIM_TIME, YR, & fage, kipp_axis_units(DIM_TIME)) fdt = 1.d0 if (c_dt > 0) call kipp_axis_unit(colnames(c_dt), colunits(c_dt), DIM_TIME, & 1.d0, fdt, dtunit) call kipp_axis_unit(colnames(c_mass), colunits(c_mass), DIM_MASS, 1.d0, & fmass, kipp_axis_units(DIM_MASS)) call kipp_axis_unit(colnames(c_radius), colunits(c_radius), DIM_LENGTH, 1.d0, & frad, kipp_axis_units(DIM_LENGTH)) open (NEWUNIT=iunit, FILE=trim(path), STATUS='OLD', ACTION='READ', & FORM='UNFORMATTED', ACCESS='STREAM', CONVERT='little_endian', & IOSTAT=iostat) if (iostat /= 0) & error stop '[loadkipp] cannot open '//trim(path) inquire (UNIT=iunit, SIZE=fbytes) ncells = fbytes/(int(ncols, int64)*8_int64) if (ncells < 2) & error stop '[loadkipp] file too small' ncap = 1024 allocate (recs(ncap)) ndata = 0 zcap = 8192 allocate (am(zcap), ar(zcap), ae(zcap), amt(zcap)) nz = 0 cur = -huge(1_int32) tsec = 0.d0; dtsec = 0.d0 stopping = .false. ! pass A: build the coord/convection/eps records and, in the same sweep, ! accumulate the global min/max of every registered field. fields are ! quantized in a second streaming pass (below) once the range is known, ! which keeps only the compact step functions resident rather than the ! full raw columns of the whole file. 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 '[loadkipp] short read' ndone = ndone + take do j = 1, int(take, int32) m = nint(buf(c_model, j)) if (m /= cur) then ! model_number rises monotonically across the stream, so a ! change means the previous model is complete call flush_kipp(recs, ndata, ncap, cur, nz, tsec, dtsec, & am, ar, amt, ae, 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_accum(am, ar, ae, amt, zcap) am(nz) = buf(c_mass, j)*fmass ar(nz) = buf(c_radius, j)*frad amt(nz) = nint(buf(c_mix, j)) ae(nz) = buf(c_eps, j) tsec = buf(c_age, j)*fage if (c_dt > 0) dtsec = buf(c_dt, j)*fdt do f = 1, nf call update_field_range(f, buf(fld_col(f), j)) end do end do end do call flush_kipp(recs, ndata, ncap, cur, nz, tsec, dtsec, & am, ar, amt, ae, ifirst, ilast) close (iunit) deallocate (buf, am, ar, ae, amt) if (ndata < 2) & error stop '[loadkipp] need at least 2 models in range' do f = 1, nf call finalize_field_log(f) end do ! pass B: quantize each field into recs(k)%fld. recs is still in stream ! order (sort_models runs after), so the k-th in-range model of this pass ! lands in the same record it built in pass A. if (nf > 0) & call fill_kipp_fields(path, ncols, c_model, fld_col, nf, & recs, ndata, ifirst, ilast) if (allocated(data)) deallocate (data) allocate (data(ndata)) data(1:ndata) = recs(1:ndata) deallocate (recs) call sort_models() print *, '[loadkipp] Loaded models', data(1)%ncyc, ' - ', & data(size(data))%ncyc end subroutine loadkipp