; $Id: dm_write_spe.pro,v 1.9 2007/03/29 14:13:58 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_write_spe
;
; PURPOSE:
;  write into a spe file xdat[nx] or [nx+1],ydat[ny] or [ny+1],zdat[nx,ny],err[nx,ny]
;
; CATEGORY:
;  dcs_mslice
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-8562
;  United States
;  yiming.qiu@nist.gov
;  Mar, 2007
;
; LICENSE:
;  The software in this file is written by an employee of
;  National Institute of Standards and Technology
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used or if the code in this file is
;  included in another product.
;
;#######################################################################

pro dm_write_spe,funit,xdat,ydat,zdat,err   ;twotheta,emean,qty,err
    sformat = ['(8(g10.5))','(8(g10.4))','(8(g10.3))']

    nx  = n_elements(xdat)
    ny  = n_elements(ydat)
    nz  = size(zdat,/dimension)
    printf,funit,nz[0],nz[1],format='(2i5)'

    ;write phi grid
    printf,funit,'### Phi Grid'
    ;search for the correct string format
    for fid=0,2 do begin
        ok  = 1b
        tmp = string(xdat,format=sformat(fid))
        for i=0,n_elements(tmp)-1 do begin
            pos = strpos(tmp[i],'*')
            if pos ne -1 then begin
               ok = 0b
               break
            endif
        endfor
        if ok then break
    endfor
    if nx eq nz[0] then $
        printf,funit,[xdat,0.0],format=sformat[fid] $
    else $
        printf,funit,xdat,format=sformat[fid]

    ;write energy grid
    printf,funit,'### Energy Grid'
        ;search for the correct string format
    for fid=0,2 do begin
        ok  = 1b
        tmp = string(ydat,format=sformat(fid))
        for i=0,n_elements(tmp)-1 do begin
            pos = strpos(tmp[i],'*')
            if pos ne -1 then begin
               ok = 0b
               break
            endif
        endfor
        if ok then break
    endfor
    if ny eq nz[1] then $
        printf,funit,[ydat,0],format=sformat[fid] $
    else $
        printf,funit,ydat,format=sformat[fid]

    ;write S(ndet,nenergy) and Err(ndet,nenergy)
    for i=0L,nz[0]-1L do begin
       printf,funit,'### S(Phi,w)'
       printf,funit,zdat[i,*],format=sformat[2]
       printf,funit,'### Errors'
       printf,funit,err[i,*],format=sformat[2]
    endfor
    free_lun,funit
end