; $Id: dm_plot_cut.pro,v 1.28 2007/08/01 15:18:14 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_plot_cut
;
; PURPOSE:
;  plot the cut and save the data in a file if wanted
;
; CATEGORY:
;  dcs_mslice
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-6102
;  United States
;  yiming.qiu@nist.gov
;  July, 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 dcs_mslice::dm_plot_cut,overplot=overplot
    if (not ptr_valid(self.projStrPtr)) or (not ptr_valid(self.dataStrPtr)) or self.error then begin
       ok = dialog_message('Please load data and calculate projection first.',dialog_parent=self.tlb)
       return
    endif

    ;select data
    self->dm_choose_cutdata,xdat=xdat,ydat=ydat,yerr=yerr,xtit=xtit,ytit=ytit
    if (not self.errbar) and (n_elements(yerr) ne 0) then tmp=temporary(yerr)   ;destroy yerr

    ;y range
    yran = self.cut_yran
    if finite(yran[0]) eq 0 then begin
       yran[0] = min(ydat)
       yran[0] = yran[0]-0.1*abs(yran[0])
    endif
    if finite(yran[1]) eq 0 then begin
       yran[1] = max(ydat)
       yran[1] = yran[1]+0.1*abs(yran[1])
    endif
    symbol = self.symbols[self.cut_symbol]

    ;extra info in the title
    tmp_tit = ''
    switch self.samp_typ of
        0:
        2:  begin    ;powder or diffuse single E
            if total(finite(self.cut_t1ran)) eq 2 then begin
               labels = [self.view_u2lab[self.samp_typ ge 1],self.view_u1lab[self.samp_typ ge 1]]
               index = where(strlowcase(labels) eq 'energy',count)
               if count ne 0 then labels[index] = 'E'
               tmp_tit = ' '+labels[self.cut_x[self.samp_typ]]+'=['+dm_to_string(self.cut_t1ran[0])+','+$
                         dm_to_string(self.cut_t1ran[1])+']'
               if labels[self.cut_x[self.samp_typ]] eq 'E' then tmp_tit=tmp_tit+'meV'
            endif
            break
            end
        1:
        3:  begin    ;single crsytal or diffuse multipe E
            labels = [self.view_u1lab[1],self.view_u2lab[1],self.view_u3lab]
            if self.samp_typ eq 1 then begin
               index = 0>((where([self.view_u1[3],self.view_u2[3],self.view_u3[3]] eq 1))[0])
               labels[index] = 'E'
            endif else labels[2] = 'E'
            if total(finite(self.cut_t1ran)) eq 2 then begin
               tmp_tit = ' '+labels[(self.cut_x[self.samp_typ]+1) mod 3]+'=['+dm_to_string(self.cut_t1ran[0])+','+$
                         dm_to_string(self.cut_t1ran[1])+']'
               if labels[(self.cut_x[self.samp_typ]+1) mod 3] eq 'E' then tmp_tit=tmp_tit+'meV'
            endif
            if total(finite(self.cut_t2ran)) eq 2 then begin
               tmp_tit = tmp_tit+' '+labels[(self.cut_x[self.samp_typ]+2) mod 3]+'=['+dm_to_string(self.cut_t2ran[0])+','+$
                         dm_to_string(self.cut_t2ran[1])+']'
               if labels[(self.cut_x[self.samp_typ]+2) mod 3] eq 'E' then tmp_tit=tmp_tit+'meV'
            endif
            break
            end
    endswitch
    if (self.samp_typ eq 2) or ((self.samp_typ eq 3) and (strlen(tmp_tit) eq 0)) then begin
       case self.e_bin[self.samp_typ] of
          0:  tmp_tit = tmp_tit+''
          1:  tmp_tit = tmp_tit+' Elastic'
          2:  tmp_tit = tmp_tit+' E=['+dm_to_string(self.e_range[0])+', '+dm_to_string(self.e_range[1])+']meV'
       endcase
    endif

    if (self.samp_typ gt 0) and (self.hklran_yn eq 1) and (total(finite(self.disp_hklran)) ne 0) then $
       tmp_tit = tmp_tit+' '+(['H','K','L'])[self.disp_hkl]+'=['+dm_to_string(self.disp_hklran[0])+','+$
                 dm_to_string(self.disp_hklran[1])+']'

    title = self.titl_lab+tmp_tit

    ;plot the data
    isnew = 0b
    if self.useitool then begin
       ok = dialog_message('Itools plotting is not implemented yet. Soooorry!',dialog_parent=self.tlb)
       return
    endif else begin
       obj_name=  'dm_plot'
       if not obj_valid(self.current) then begin
          self.current=obj_new(obj_name,xdat,ydat,yerr=yerr,xtit=xtit,ytit=ytit,$
                parentobj=self,linestyle=self.lines[self.cut_line],$
                psym=symbol,color=self.colors[self.cut_color],$
                xran=self.cut_ran,yran=yran,title=title,/no_copy,legend=self.legd_lab)
          isnew = 1b
       endif else begin
          self.current->getproperty,keep=keep
          if keep eq 1 then begin
             isnew = 1b
             self.current=obj_new(obj_name,parentobj=self)
          endif
          if keyword_set(overplot) then $
             self.current->add_plot,xdat,ydat,yerr=yerr,/no_copy,legend=self.legd_lab,$
                color=self.colors[self.cut_color],psym=symbol,linestyle=self.lines[self.cut_line] $
          else $
             self.current->setproperty,xdat=xdat,ydat=ydat,yerr=yerr,xtit=xtit,/no_copy,$
                    legend=self.legd_lab,ytit=ytit,xran=self.cut_ran,yran=yran,$
                    color=self.colors[self.cut_color],psym=symbol,linestyle=self.lines[self.cut_line],$
                    title=title,/nodraw,hidelegend=0
       endelse
       self.current->draw
    endelse
    if keyword_set(overplot) and (not isnew) then $
       self->dm_add_plotwindow,type=1,title=title+'[overplot]' $
    else $
       self->dm_add_plotwindow,type=1,title=title
end