program TestHandleNetCDF

      use selectrealkind, only : kpr 
      use HandleNetCDF
      !use HandleTimeSeries 

      implicit none 

      integer(kind=4) :: Status , id 
      character(len=:),allocatable :: vname
      integer(kind=4) :: xtype,ndims
      integer(kind=4),dimension(2) :: dimids
      type(output) :: out

      integer :: nRows,nColumns,nTime
      integer :: iAllocate 
      real(kind=kpr),dimension(:,:,:),allocatable :: data 
      real(kind=kpr),dimension(:),allocatable :: longitude,latitude,time

      !class(series),allocatable :: timeSeries 

      integer :: i,iUnit,iError
      character(len=:),allocatable :: refDir 
      real(kind=kpr),parameter :: mjd2000=51544.0_kpr
      logical,parameter :: lDebug=.false.

      refDir='/ecomm/sideshow/ftp/anon/pub/usrs/cabbonda/netCDF/data'

      out=netcdfopen(fname=refDir//'/GRCTellus.JPL.200204_202002.GLO.RL06M.MSCNv02.nc',&
                     action='read')
      out=netcdfinquiry(id=out.id) 

      out=netcdfinquiredimension(id=out.id,id_variable=1)
      nColumns=out.nDimension
      out=netcdfinquiredimension(id=out.id,id_variable=2)
      nRows=out.nDimension
      out=netcdfinquiredimension(id=out.id,id_variable=3)
      nTime=out.nDimension

      !Allocate
      allocate(data(nTime,nRows,nColumns),stat=iAllocate)
      if(iAllocate/=0)stop' Allocation Error - data ' 

      out=netcdfinquirevariable(id=out.id,id_variable=1)
      if (lDebug) write(*,'(a,a,\)') ' var name ' , out.variablename
      out=netcdfinquirevariableid(id=out.id,vname=out.variablename)
      out.nDimension=nColumns
      longitude=out.getVariable() 

      out=netcdfinquirevariable(id=out.id,id_variable=2)
      if (lDebug) write(*,*) ' var name ' , out.variablename
      out=netcdfinquirevariableid(id=out.id,vname=out.variablename)
      out.nDimension=nRows
      latitude=out.getVariable() 

      out=netcdfinquirevariable(id=out.id,id_variable=3)
      if (lDebug) write(*,*) ' var name ' , out.variablename
      out=netcdfinquirevariableid(id=out.id,vname=out.variablename)
      out.nDimension=nTime
      time=out.getVariable() 
      time=time+mjd2000

      out=netcdfinquirevariable(id=out.id,id_variable=4)
      if (lDebug) write(*,*) ' var name ' , out.variablename
      out=netcdfinquirevariableid(id=out.id,vname=out.variablename)
      data=out.get3DVariable(dim1=nColumns,dim2=nRows,dim3=nTime) 
      write(*,fmt=1)  shape(data)

      !timeSeries=series(x=time,y=data(10,10,:)) 
      !call timeSeries.write(fname='timeseries.ts',kind='xy',timeunit='mjd')
      !print*,data(10,10,:)

      !Write Output Time Series 
      open(newunit=iUnit, &
           file='timeseries.ts',&
           action='write',&
           access='sequential',&
           status='unknown',& 
           iostat=iError)
      if (iError/=0) stop '[TestHandleNetCDF] I/O Error - timeseries.ts not Opened '
      do i=1,size(time,dim=1)
         write(unit=iUnit,fmt=*) time(i),data(10,10,i)
      enddo 
      close(unit=iUnit) 
      1 format( /,' [Dimensions of Tensor Array] Dim1: ',i3,' Dim2: ',i3,' Dim3:',i3,/ )

end program TestHandleNetCDF
