; $Id: dm_q2hkl.pro,v 1.6 2005/06/10 20:31:21 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_q2hkl
;
; PURPOSE:
;  project Q[3,ne,ndet] onto three vectors abc_r[3,3]
;
; CATEGORY:
;  dcs_mslice
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-8562
;  United States
;  yiming.qiu@nist.gov
;  June, 2005
;
; 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.
;
;#######################################################################

function dm_q2hkl,Q,abc_r,debug=debug
    if keyword_set(debug) then begin
       print,'%%Q to hkl...'
       current=systime(/sec)
    endif
    ABC   = dm_basis_hkl(abc_r)
    Qsize = size(Q,/dimension)
    nQdim = n_elements(Qsize)
    ;old and slower code
    ;hkl = fltarr(Qsize[0],Qsize[1],Qsize[2])
    ;hkl[0,*,*] = Q[0,*,*]*ABC[0,0]+Q[1,*,*]*ABC[1,0]+Q[2,*,*]*ABC[2,0]
    ;hkl[1,*,*] = Q[0,*,*]*ABC[0,1]+Q[1,*,*]*ABC[1,1]+Q[2,*,*]*ABC[2,1]
    ;hkl[2,*,*] = Q[0,*,*]*ABC[0,2]+Q[1,*,*]*ABC[1,2]+Q[2,*,*]*ABC[2,2]
    case nQdim of
       2:    tmp = Q                                         ;Q=[3,ndet] diffuse, 1 file, single E
       3:    tmp = reform(Q,3,Qsize[1]*Qsize[2])             ;Q=[3,nen,ndet] or Q=[3,ndet,nfile]
       4:    tmp = reform(Q,3,Qsize[1]*Qsize[2]*Qsize[3])    ;Q=[3,nen,ndet,nfile]
       else: tmp = Q
    endcase
    hkl = temporary(tmp)##transpose(ABC)
    case nQdim of
       3:    hkl = reform(hkl,3,Qsize[1],Qsize[2])
       4:    hkl = reform(hkl,3,Qsize[1],Qsize[2],Qsize[3])
       else:
    endcase
    if keyword_set(debug) then begin
       print,'%%Q to hkl finished in ',systime(/sec)-current,' sec.'
    endif
    return,hkl
end