PROGRAM TEST

    use mdlSimul
    use mdlTS

    call InitTS

END PROGRAM TEST

!===========================

MODULE mdlSimul

    use mdlTS
    implicit none

    ! If ts1 is accessed thru USE, it does not work
    TYPE(tTimeSeries)             ::  ts1

CONTAINS

SUBROUTINE InitTS

    ! But it works if ts1 is declared here
    !TYPE(tTimeSeries)             ::  ts1


    call TSAlloc( ts1 )

    call TSFill( ts1 )

    ! Timeset(1) should have values 11, 12, 13, 101, 102, 103, 1001, 1002, 1003
    ! Timeset(2) should have values 21, 22, 23, 201, 202, 203, 2001, 2002, 2003

END SUBROUTINE InitTS


END MODULE mdlSimul

!===========================

MODULE mdlTS

    TYPE tTimeSet
        SEQUENCE
        REAL, POINTER                       ::  X(:),    Y(:)
        REAL, POINTER                       ::  Value(:) => NULL()
    END TYPE tTimeSet

    TYPE tTimeSeries
        SEQUENCE
        INTEGER                             ::  NumTimesets, NumSamples
        TYPE(tTimeSet), POINTER             ::  Timesets(:)  => NULL()
    END TYPE tTimeSeries

CONTAINS

SUBROUTINE TSAlloc(TimeSeries)

    implicit none
    TYPE(tTimeSeries)                      ::  TimeSeries

    ! test values (would come from TS file header)
    integer, parameter                      ::  nTimesets = 2
    integer, parameter                      ::  nSamples = 3
    integer                                     i,  j,  iStat

    TimeSeries%NumTimesets = nTimesets
    TimeSeries%NumSamples = nSamples

    ! Allocate storage structure
    allocate(TimeSeries%Timesets(TimeSeries%NumTimesets))
    do i=1,TimeSeries%NumTimesets
        allocate(TimeSeries%Timesets(i)%Value(TimeSeries%NumSamples), STAT=iStat)
        allocate(TimeSeries%Timesets(i)%X(TimeSeries%NumSamples), STAT=iStat)
        allocate(TimeSeries%Timesets(i)%Y(TimeSeries%NumSamples), STAT=iStat)
        !Just fill with -1
        do j=1,TimeSeries%NumSamples
            TimeSeries%Timesets(i)%Value(j) = -1.0
            TimeSeries%Timesets(i)%X(j) = -1.0
            TimeSeries%Timesets(i)%Y(j) = -1.0
        enddo
    enddo
    return

END SUBROUTINE TSAlloc

!-----------------------------------

SUBROUTINE TSFill(TimeSeries)

    implicit none
    TYPE(tTimeSeries)                      ::  TimeSeries

    integer                                     i,  j

    ! test values (real ones would come from a section in the TS file)
    ! Fill structure
    do i=1,TimeSeries%NumTimesets
        do j=1,TimeSeries%NumSamples
            TimeSeries%Timesets(i)%Value(j) = i*10 + j
            TimeSeries%Timesets(i)%X(j) = i*100 + j
            TimeSeries%Timesets(i)%Y(j) = i*1000 + j
        enddo
    enddo

    return

END SUBROUTINE TSFill

END MODULE mdlTS
