pro readall, iunitbin

; IDL/DDSCAR postprocessing code
; Version corresponding to DDSCAT 5a5 stamp
; reads DDSCAT binary file
; History:
; 96.11.14      (PJF) First version
;end history
;Copyright (C) 1996 B.T. Draine and P.J. Flatau
;This code is covered by the GNU General Public License.
common ddcom1,  iorth, nx, ny, nz,$
      nat0, nat, nat3, nscat, ncomp,$
      nori, nwav, nrad, ntheta, nphi, nbeta, ntimers
common ddcom2, mxnx, mxny, mxnz, mxcomp, mxsca
common ddcom3,  BETMID, BETMXD, THTMID, THTMXD, PHIMID, PHIMXD
common ddcom4, phin, thetan
common ddcom5, ICTHM, IPHIM, TOL
common ddcom6, wavea, aeffa, theta, beta, phi, a1, a2, icomp, ix0, iy0, iz0
common ddcom7, qext_all, qext_abs, g_all, cxrfr_all

common ddcom8, sm11a, sm22a

cstamp='1234567890123456789012'
cdescr='1234567890123456789012345678901234567890123456789012345678901234567'
cmdfft='123456'
calpha='123456'
cshape='123456'
cmdtrq='123456'
readu, iunitbin,$
 CSTAMP,CDESCR,CMDFFT,CALPHA,CSHAPE,CMDTRQ
help,CSTAMP,CDESCR,CMDFFT,CALPHA,CSHAPE,CMDTRQ

;we need to declare most integers as long
iorth=long(0)
nx=long(0)
ny=long(0)
nz=long(0)
nat0=long(0)
nat=long(0)
nat3=long(0)
nscat=long(0)
ncomp=long(0)
nori=long(0)
nwav=long(0)
nrad=long(0)
ntheta=long(0)
nphi=long(0)
nbeta=long(0)
ntimers=long(0)
readu, iunitbin,  iorth, nx, ny, nz,$
      nat0, nat, nat3, nscat, ncomp,$
      nori, nwav, nrad, ntheta, nphi, nbeta, ntimers

help,  iorth, nx, ny, nz,$
      nat0, nat, nat3, nscat, ncomp,$
      nori, nwav, nrad, ntheta, nphi, nbeta, ntimers

mxnx=long(0)
mxny=long(0)
mxnz=long(0)
mxcomp=long(0)
mxsca=long(0)
readu, iunitbin, mxnx, mxny, mxnz, mxcomp, mxsca
help, mxnx, mxny, mxnz, mxcomp, mxsca

readu,iunitbin, BETMID, BETMXD, THTMID, THTMXD, PHIMID, PHIMXD
help,  BETMID, BETMXD, THTMID, THTMXD, PHIMID, PHIMXD

phin=fltarr(nscat)
thetan=fltarr(nscat)
readu, iunitbin, phin, thetan
help,  phin, thetan

icthm=long(0)
iphim=long(0)
readu, iunitbin, ICTHM, IPHIM, TOL
help, ICTHM, IPHIM, TOL

wavea=fltarr(nwav)
aeffa=fltarr(nrad)
theta=fltarr(ntheta)
beta=fltarr(nbeta)
phi=fltarr(nphi)
a1=fltarr(3)
a2=fltarr(3)
icomp=intarr(nat3)
;ixyz0=intarr(nat,3)
;it is hsort array (integer*2)
ix0=intarr(nat)
iy0=intarr(nat)
iz0=intarr(nat)

readu, iunitbin, wavea, aeffa, theta, beta, phi, a1, a2, icomp, ix0, iy0, iz0
help, wavea, aeffa, theta, beta, phi, a1, a2, icomp, ix0, iy0, iz0

iwav=long(0)
irad=long(0)
iori=long(0)
timers=fltarr(ntimers)


akr=fltarr(3)
cxe01r=complexarr(3)
cxe02r=complexarr(3)
cxrfr=complexarr(ncomp)
cxeps=complexarr(ncomp)


qext=fltarr(2)
qabs=fltarr(2)
qscat=fltarr(2)
g=fltarr(2)
qbksca=fltarr(2)
qpha=fltarr(2)
qav=fltarr(17)
qtrqab=fltarr(3,2)
qtrqsc=fltarr(3,2)
sm11=fltarr(nscat)
sm12=fltarr(nscat)
sm13=fltarr(nscat)
sm14=fltarr(nscat)
sm21=fltarr(nscat)
sm22=fltarr(nscat)
sm23=fltarr(nscat)
sm24=fltarr(nscat)
sm31=fltarr(nscat)
sm32=fltarr(nscat)
sm33=fltarr(nscat)
sm34=fltarr(nscat)
sm41=fltarr(nscat)
sm42=fltarr(nscat)
sm43=fltarr(nscat)
sm44=fltarr(nscat)
;If the amplitude matrix fij was written in "writesca.f" you need to read it
;cxf11=complexarr(nscat)
;cxf12=complexarr(nscat)
;cxf21=complexarr(nscat)
;cxf22=complexarr(nscat)

qext_all=fltarr(nwav, nrad, ntheta,nbeta,nphi,2)
qabs_all=fltarr(nwav, nrad, ntheta,nbeta,nphi,2)
g_all=fltarr(nwav, nrad, ntheta,nbeta,nphi,2)
cxrfr_all=complexarr(nwav,ncomp)
sm11a=fltarr(nwav,nrad,ntheta,nbeta,nphi,nscat)
sm22a=fltarr(nwav,nrad,ntheta,nbeta,nphi,nscat)

for inwav=1, nwav do begin
  for inrad=1, nrad do begin
    for inthet=1, ntheta do begin
      for inbeta=1, nbeta do begin
        for inphi=1, nphi do begin
        readu, iunitbin,  IWAV, IRAD, IORI, timers
        readu, iunitbin,    AEFF,WAVE,XX, AK1, BETAD,THETAD,PHID,$
          AKR, CXE01R, CXE02R,   $
         cxrfr, cxeps
        readu, iunitbin, QEXT, QABS, QSCAT, G, QBKSCA, QPHA, QAV,$
           QTRQAB, QTRQSC
        readu, iunitbin, sm11
        readu, iunitbin, sm12
        readu, iunitbin, sm13
        readu, iunitbin, sm14
        readu, iunitbin, sm21
        readu, iunitbin, sm22
        readu, iunitbin, sm23
        readu, iunitbin, sm24
        readu, iunitbin, sm31
        readu, iunitbin, sm32
        readu, iunitbin, sm33
        readu, iunitbin, sm34
        readu, iunitbin, sm41
        readu, iunitbin, sm42
        readu, iunitbin, sm43
        readu, iunitbin, sm44
;fij not written        readu, iunitbin, cxf11, cxf12, cxf21,cxf22
        i1wav=inwav-1
        i1rad=inrad-1
        i1thet=inthet-1
        i1beta=inbeta-1
        i1phi=inphi-1
        qext_all(i1wav,i1rad,i1thet,i1beta,i1phi,*)=qext
        qabs_all(i1wav,i1rad,i1thet,i1beta,i1phi,*)=qabs
        g_all(i1wav,i1rad,i1thet,i1beta,i1phi,*)=g
        cxrfr_all(i1wav,*)=cxrfr
        sm11a(i1wav, i1rad,i1thet,i1beta,i1phi,*)=sm11
        sm22a(i1wav, i1rad,i1thet,i1beta,i1phi,*)=sm22
     endfor
    endfor
   endfor
  endfor
endfor

		   
stop
;return

end

