! History:
! 03/01/2010 CLF: get info from ufhdck and allocate arrays 

!================
!1D
!================

SUBROUTINE read_u1d(iochan,idev,x1,nx1,data_out,nscal,ier)
 
implicit none

! Innput
character*(*)  :: idev
integer        :: iochan,nx1,nscal
!Output
real           :: data_out(nx1), x1(nx1)
integer        :: ier

! Processing
integer      :: iproc, iflag
integer      :: idum1, idum4
character*10 :: sdate
character*30 :: dlabstr, x1labstr

real,         dimension(:), allocatable :: valscal
character*30, dimension(:), allocatable :: strscal

  iflag=0

  allocate(valscal(nscal),strscal(nscal))

  call uf1drd(iochan,idev,sdate,x1,data_out,nx1,idum1,iproc,&
       x1labstr, dlabstr,nscal,idum4,valscal,strscal,ier)
  call ufclos(iochan)
 
  deallocate(valscal, strscal)

return
end

!================
!2D
!================



SUBROUTINE read_u2d(iochan,idev,x1,nx1,x2,nx2,data_out,nscal,ier)
 
implicit none

! Innput
character*(*)  :: idev
integer        :: iochan,nx1,nx2,nscal
!Output
real           :: data_out(nx1,nx2), x1(nx1), x2(nx2)
integer        :: ier

! Processing
integer      :: iproc, iflag
integer      :: idum1, idum2, idum4
character*10 :: sdate
character*30 :: dlabstr, x1labstr, x2labstr

real,         dimension(:), allocatable :: valscal
character*30, dimension(:), allocatable :: strscal

  iflag=0

  allocate(valscal(nscal),strscal(nscal))

  call uf2drd(iochan,idev,sdate,data_out,nx1,x1,nx1,x2,nx2,iproc,&
       idum1,idum2,iflag,&
       dlabstr, x1labstr,x2labstr, &
       nscal,idum4,valscal,strscal,ier)
  call ufclos(iochan)
 
  deallocate(valscal, strscal)

return
end


!================
!3D
!================

SUBROUTINE read_u3d(iochan,idev,x1,nx1,x2,nx2,x3,nx3,data_out,nscal,ier)
 
implicit none

! Innput
character*(*)  :: idev
integer        :: iochan,nx1,nx2,nx3,nscal
!Output
real           :: data_out(nx1,nx2,nx3), x1(nx1), x2(nx2), x3(nx3)
integer        :: ier
! Processing
integer      :: iproc, iflag
integer      :: idum1, idum2, idum3, idum4
character*10 :: sdate
character*30 :: dlabstr, x1labstr, x2labstr, x3labstr

real,         dimension(:), allocatable :: valscal
character*30, dimension(:), allocatable :: strscal

  iflag=0

  allocate(valscal(nscal),strscal(nscal))

  call uf3drd(iochan,idev,sdate,data_out,&
       nx1,nx2,x1,nx1,x2,nx2,x3,nx3,iproc,&
       idum1,idum2,idum3,iflag,&
       dlabstr, x1labstr,x2labstr, x3labstr,&
       nscal,idum4,valscal,strscal,ier)
  call ufclos(iochan)
 
  deallocate(valscal, strscal)

!write(6,'(6e13.6)') (data(j,1,1),     j=nx1+1,nx1+nx1)
!write(6,'(6e13.6)') (data_out(j,2,1), j=1,nx1)
!write(6,*) 'Warning: reading of 3d-ufiles not really tested!!!'

return
end

!======================================
! Manage u-files (common part)
!======================================

SUBROUTINE managerd_u(in_dir,runid,prestr,extstr,iochan,nx1,nx2,nx3,nscal,ier)

implicit none
! input
character*(*) :: in_dir
character*(*) :: prestr
character*(*) :: extstr
character*(*) :: runid
! Output
integer :: iochan,nx1,nx2,nx3,nscal,ier
! Processing
integer :: nshot
character*12 :: shot_str
character*10 :: disk
integer :: info(8)

! Setting variables for u-file output
call ufncap(2)
disk=' '     

!03/01/10 CLF: handle non AUGD runid
if (len_trim(runid) .eq. 8) then
   shot_str=runid(1:5)
else if (len_trim(runid) .eq. 9) then
   shot_str=runid(1:6)
endif
read(shot_str,*) nshot

iochan=20
ier=0

call ufsetr(iochan,prestr,extstr,disk,in_dir)
call ufemsg(1)
call ufimsg(1)
call ufoprd(iochan,nshot,ier)
if (ier .ne. 0) then
   call ufclos(iochan)
   return
endif

! Read header
call ufhdck(iochan,info,ier)
if (ier .ne. 0) then
   call ufclos(iochan)
   return
endif

nx1=info(3)
nx2=info(4)
nx3=info(5)
nscal=info(8)


return
end
