; $Id: dm_choose_trajdata.pro,v 1.14 2007/03/02 19:19:57 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_choose_trajdata
;
; PURPOSE:
;  choose the data according to plot trajectory type
;
; CATEGORY:
;  dcs_mslice
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-8562
;  United States
;  yiming.qiu@nist.gov
;  February, 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.
;
;#######################################################################

; parameter:
;     id:           an integer
; keywords:
;  input:
;     range:        if=[NAN, NAN], then assume all possible data will be returned, and return empty=0
;  output:
;     data:         qty
;     error:        dqty
;     label:
;     index:
;     empty:        a flag to show whether empty for given range, if empty=1,
;                   then index will be all possible data index
pro dcs_mslice::dm_choose_trajdata,id,data=data,label=label,index=index,range=range,$
    empty=empty,error=error,debug=debug
    if keyword_set(debug) then begin
       print,'[[choose traj data...'
       current = systime(/sec)
    endif
    nen  = n_elements((*self.projStrPtr).en)
    n2th = n_elements((*self.projStrPtr).two_theta)
    case self.samp_typ of
     0:begin   ;powder
       case id of
         -2:begin
            data  = (*self.projStrPtr).ewid#(fltarr(n2th)+1.0)
            label = 'E Width'
            end
         -1:begin
            data  = (*self.projStrPtr).qty
            error = (*self.projStrPtr).err
            label = 'Intensity'
            if strlen(self.intn_lab) ne 0 then label=self.intn_lab
            end
         0: data  = (*self.projStrPtr).en#(fltarr(n2th)+1.0)
         1: data  = (*self.projStrPtr).Q
         2: data  = (fltarr(nen)+1)#(*self.projStrPtr).two_theta
         3: data  = (fltarr(nen)+1)#(findgen(n2th)+1.0)
         4: begin
            if self.ftype eq 0 then begin
               data  = (*self.projStrPtr).time#(fltarr(n2th)+1.0)
            endif else begin
               id    = 0
               data  = (*self.projStrPtr).en#(fltarr(n2th)+1.0)
            endelse
            end
         else:begin
            data  = (fltarr(nen)+1.0)#(findgen(n2th)+1.0)
            label = ' None '
            end
       endcase
       if (id ge 0) and (id le 4) then $
          label = self.view_label[id]+((strlen(self.view_unit[id]) ne 0)?' ('+self.view_unit[id]+')':'')
       end
     1:begin   ;single crystal
       case id of
         -2:begin
            data  = (*self.projStrPtr).ewid#(fltarr(n2th)+1.0)
            label = 'E Width'
            end
         -1:begin
            data  = (*self.projStrPtr).qty
            error = (*self.projStrPtr).err
            label = 'Intensity'
            if strlen(self.intn_lab) ne 0 then label=self.intn_lab
            end
         0: begin
            data  = reform((*self.projStrPtr).hkl[0,*,*])
            label = 'H (rlu)'
            end
         1: begin
            data  = reform((*self.projStrPtr).hkl[1,*,*])
            label = 'K (rlu)'
            end
         2: begin
            data  = reform((*self.projStrPtr).hkl[2,*,*])
            label = 'L (rlu)'
            end
         3: begin
            data  = reform((*self.projStrPtr).V[0,*,*])
            label = self.traj_type[3]
            end
         4: begin
            data  = reform((*self.projStrPtr).V[1,*,*])
            label = self.traj_type[4]
            end
         5: begin
            data  = reform((*self.projStrPtr).V[2,*,*])
            label = self.traj_type[5]
            end
         6: begin
            data  = (*self.projStrPtr).en#(fltarr(n2th)+1.0)
            label = 'E (meV)'
            end
         else:begin
            data  = (fltarr(nen)+1.0)#(findgen(n2th)+1.0)
            label = ' None '
            end
       endcase
       end
     2:begin   ;diffuse scattering single E
       tmp = size((*self.projStrPtr).V)
       if tmp[0] eq 2 then nfile=1L else nfile=tmp[3]
       case id of
         -1:begin
            data  = (*self.projStrPtr).qty
            error = (*self.projStrPtr).err
            label = 'Intensity'
            if strlen(self.intn_lab) ne 0 then label=self.intn_lab
            end
         0: begin
            data  = reform((*self.projStrPtr).hkl[0,*,*])
            label = 'H (rlu)'
            end
         1: begin
            data  = reform((*self.projStrPtr).hkl[1,*,*])
            label = 'K (rlu)'
            end
         2: begin
            data  = reform((*self.projStrPtr).hkl[2,*,*])
            label = 'L (rlu)'
            end
         3: begin
            data  = reform((*self.projStrPtr).V[0,*,*])
            label = self.traj_type[3]
            end
         4: begin
            data  = reform((*self.projStrPtr).V[1,*,*])
            label = self.traj_type[4]
            end
         else:begin
            data  = (findgen(n2th)+1.0)#(fltarr(nfile)+1.0)
            label = ' None '
            end
       endcase
       end
     3:begin   ;diffuse scattering multiple E
       tmp = size((*self.projStrPtr).V)
       if tmp[0] eq 3 then nfile=1L else nfile=tmp[4]
       case id of
         -2:begin
            data  = fltarr(nen,n2th,nfile)
            for i=0L,nen-1L do begin
                data[i,*,*] = (*self.projStrPtr).ewid[i]
            endfor
            label = 'E Width'
            end
         -1:begin
            data  = (*self.projStrPtr).qty
            error = (*self.projStrPtr).err
            label = 'Intensity'
            if strlen(self.intn_lab) ne 0 then label=self.intn_lab
            end
         0: begin
            data  = reform((*self.projStrPtr).hkl[0,*,*,*])
            label = 'H (rlu)'
            end
         1: begin
            data  = reform((*self.projStrPtr).hkl[1,*,*,*])
            label = 'K (rlu)'
            end
         2: begin
            data  = reform((*self.projStrPtr).hkl[2,*,*,*])
            label = 'L (rlu)'
            end
         3: begin
            data  = reform((*self.projStrPtr).V[0,*,*,*])
            label = self.traj_type[3]
            end
         4: begin
            data  = reform((*self.projStrPtr).V[1,*,*,*])
            label = self.traj_type[4]
            end
         5: begin
            data  = fltarr(nen,n2th,nfile)
            for i=0L,nen-1L do begin
                data[i,*,*] = (*self.projStrPtr).en[i]
            endfor
            label = 'E (meV)'
            end
         else:begin
            data  = fltarr(nen,n2th,nfile)
            for i=0L,n2th-1L do begin
                data[*,i,*] = i+1
            endfor
            label = ' None '
            end
       endcase
       end
     else:
    endcase
    count = 0
    empty = 0b
    if (n_elements(range) eq 2) and (label ne ' None ') then begin
       tmp_ran = range
       if total(finite(tmp_ran)) ne 0 then begin
          if finite(tmp_ran[0]) eq 0 then tmp_ran[0]=-1e30 ;assign a very large negative number
          if finite(tmp_ran[1]) eq 0 then tmp_ran[1]=1e30  ;assing a very large positive number
          if tmp_ran[1] ge tmp_ran[0] then begin
             index = where((data ge tmp_ran[0]) and (data le tmp_ran[1]),count)
             if count eq 0 then empty=1b
          endif else begin
             empty = 1b
          endelse
       endif
    endif
    if (count eq 0) or (label eq ' None ') then begin
       if self.samp_typ eq 2 then index=lindgen(nfile*n2th)  $          ;diffuse single E
       else if self.samp_typ eq 3 then index=lindgen(nfile*n2th*nen) $  ;diffuse multiple E
       else index=lindgen(nen*n2th)                                     ;powder,single crystal
    endif
    if keyword_set(debug) then begin
       print,'[[choose traj data finished in ',systime(/sec)-current,' sec.'
    endif
end