; $Id: dm_load_phx.pro,v 1.3 2006/07/17 14:52:02 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_load_phx
;
; PURPOSE:
;  load phx file
;
; CATEGORY:
;  dcs_mslice
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-8562
;  United States
;  yiming.qiu@nist.gov
;  July, 2006
;
; 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.
;
;#######################################################################

; parameter:
;   unit:   input file unit
; keywords:
;   error:  error flag
;   tth:    two theta [ndet]
;   psi:    psi       [ndet]
;   dtth:
;   dpsi:
pro dm_load_phx,unit,tth=tth,psi=psi,dtth=dtth,dpsi=dpsi,error=error
    error = 0b

    ;catch and clear possible io errors
    catch, myerror
    if myerror ne 0 then begin
       error = 1b
       catch,/cancel
       if n_elements(unit) ne 0 then free_lun,unit
       return
    end

    ndet = 0L
    readf,unit,ndet

    tmp  = fltarr(7,ndet)
    readf,unit,tmp
    tth  = reform(tmp[2,*])
    psi  = reform(tmp[3,*])
    dtth = reform(tmp[4,*])
    dpsi = reform(tmp[5,*])

    free_lun,unit
end

;this procedure reads a phx file for dcs_mslice program and store the info into self.detPosPtr
;keywords:
;   phx:    if set, prompt for phx file name
;   tth,psi,dtth,dpsi:  if set, used as backup data, and check if phx file is valid by comparing
;                       number of detectors
;   nomask: if present, only those specified in the nomask array are good
pro dcs_mslice::dm_load_phx,phx=phx,tth=tth,psi=psi,dtth=dtth,dpsi=dpsi,nomask=nomask

    if keyword_set(phx) then begin
       fname = dm_choose_file('phx',dialog_parent=self.tlb,/read,title=$
            'Please choose a detector PHX file. Press cancel to skip.')
       error = 1b
       if strlen(fname) ne 0 then begin
          openr,unit,fname,/get_lun,error=fileerror
          if fileerror eq 0 then begin   ;read phx file
             dm_load_phx,unit,tth=newtth,psi=newpsi,dtth=newdtth,dpsi=newdpsi,error=error
             if n_elements(nomask) ne 0 then begin
                if n_elements(newtth) gt max(nomask) then begin
                   newtth  = newtth[nomask]
                   newpsi  = newpsi[nomask]
                   newdtth = newdtth[nomask]
                   newdpsi = newdpsi[nomask]
                endif
             endif
          endif else begin
             ok = dialog_message("Can't open "+fname,/error,dialog_parent=self.tlb)
          endelse
       endif
       if not keyword_set(error) then begin    ;newtth etc. are available
          replace = 1b
          if n_elements(tth) ne 0 then begin
             if n_elements(tth) ne n_elements(newtth) then begin
                replace = 0b
                ok = dialog_message([fname+' is incompatible with the SPE file.',$
                      "The PHX file is discarded."],/error,dialog_parent=self.tlb)
             endif
          endif
          if replace then begin
             tth  = temporary(newtth)
             psi  = temporary(newpsi)
             dtth = temporary(newdtth)
             dpsi = temporary(newdpsi)
          endif
       endif
    endif

    if n_elements(tth) eq 0 then return ;no data available
    ptr_free,self.detPosPtr
    self.detPosPtr = ptr_new({two_theta:temporary(tth),dtwo_theta:temporary(dtth),$
                     psi:temporary(psi),dpsi:temporary(dpsi)})
end