; $Id: dm_load_parameter.pro,v 1.34 2008/03/19 20:46:50 ymqiu Exp $
;#######################################################################
;
; NAME:
;  dm_load_parameter
;
; PURPOSE:
;  load parameter file
;
; CATEGORY:
;  dcs_mslice
;
; AUTHOR:
;  Yiming Qiu
;  NIST Center for Neutron Research
;  100 Bureau Drive, Gaithersburg, MD 20899-6102
;  United States
;  yiming.qiu@nist.gov
;  March, 2008
;
; 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_load_parameter,parmfile,init=init,debug=debug
    WIDGET_CONTROL,/HOURGLASS
    openr,unit,parmfile,/get_lun
    tmp = ''
    ptr_free,self.absp_info         ;clear any existing self-shielding information
    clear_history = 1b
    newftype      = 0b
    ;catch and ignore all errors in this program
    catch, myerror
    if myerror ne 0 then begin
       catch,/cancel
       ok=dialog_message(!error_state.msg,/error)
       return
    end
    while(not eof(unit)) do begin
       readf,unit,tmp
       head = strmid(tmp,0,8)
       tmp  = strmid(tmp,8,strlen(tmp)-8)
       case head of
         'sshield:':self.absp_info    = ptr_new(dm_to_number(strsplit(tmp,'&',/extract,/preserve)))
         'in_geom:':self.instrgeom    = dm_to_number(tmp,/int)
         'f__type:':begin
                    old               = self.ftype
                    self.ftype        = dm_to_number(tmp,/int)
                    newftype          = (old ne self.ftype)
                    end
         'dcor_eq:':self.dcor_eqran[0:3] = dm_to_number(strsplit(tmp,'&',/extract,/preserve))
         'dcor_yn:':self.dcor_yn      = dm_to_number(tmp,/int)
         'hklr_yn:':self.hklran_yn    = dm_to_number(tmp,/int)
         'edep_yn:':self.edep_yn      = dm_to_number(tmp,/int)
         'eadj_yn:':self.eadjust      = dm_to_number(tmp,/int)
         'eint_yn:':self.eint_yn      = dm_to_number(tmp,/int)
         'selfshd:':self.ssfac        = dm_to_number(tmp,/float)
         'datadir:':if not keyword_set(debug) then self.fileSel->set_path,tmp
         'viewwin:':self.view[0:2]    = dm_to_number(strsplit(tmp,'&',/extract,/preserve))
         'samptyp:':self.samp_typ     = dm_to_number(tmp,/int)
         'intnlab:':self.intn_lab     = tmp
         'intntyp:':self.intn_typ     = dm_to_number(tmp,/int)
         'binmeth:':self.bin_avgsum   = dm_to_number(tmp,/int)
         'ebinsiz:':begin
                    tmpvalue          = dm_to_number(strsplit(tmp,'&',/extract,/preserve),/int)
                    if n_elements(tmpvalue) eq 3 then tmpvalue=[tmpvalue[1],tmpvalue[0],tmpvalue[2],1,1] $
                    else if n_elements(tmpvalue) eq 4 then tmpvalue=[tmpvalue,1]
                    self.e_bin        = tmpvalue
                    end
         'e_range:':self.e_range      = dm_to_number(strsplit(tmp,'&',/extract,/preserve))
         'mon_fit:':self.mon_sumfit   = dm_to_number(tmp,/int)
         'dcs_mon:':self.dcs_mon      = 0>(dm_to_number(tmp,/int))<2
         'ortharb:':self.view_ortharbi= dm_to_number(tmp,/int)
         'latt__a:':self.latt_parm[0] = dm_to_number(tmp,/double)
         'latt__b:':self.latt_parm[1] = dm_to_number(tmp,/double)
         'latt__c:':self.latt_parm[2] = dm_to_number(tmp,/double)
         'latt_aa:':self.latt_parm[3] = dm_to_number(tmp,/double)
         'latt_bb:':self.latt_parm[4] = dm_to_number(tmp,/double)
         'latt_cc:':self.latt_parm[5] = dm_to_number(tmp,/double)
         'orie_ux:':self.orie_u[0]    = dm_to_number(tmp,/float)
         'orie_uy:':self.orie_u[1]    = dm_to_number(tmp,/float)
         'orie_uz:':self.orie_u[2]    = dm_to_number(tmp,/float)
         'orie_vx:':self.orie_v[0]    = dm_to_number(tmp,/float)
         'orie_vy:':self.orie_v[1]    = dm_to_number(tmp,/float)
         'orie_vz:':self.orie_v[2]    = dm_to_number(tmp,/float)
         'oriepsi:':self.orie_psi     = dm_to_number(tmp,/float)
         'view1tp:':self.view_u1typ   = dm_to_number(tmp,/float)
         'view2tp:':self.view_u2typ   = dm_to_number(tmp,/float)
         'viewu1h:':self.view_u1[0]   = dm_to_number(tmp,/float)
         'viewu1k:':self.view_u1[1]   = dm_to_number(tmp,/float)
         'viewu1l:':self.view_u1[2]   = dm_to_number(tmp,/float)
         'viewu1e:':self.view_u1[3]   = dm_to_number(tmp,/float)
         'view1lb:':begin
                    tmpvalue          = strsplit(tmp,'&',/extract,/preserve_null)
                    if n_elements(tmpvalue) eq 3 then begin ;old format parameter file
                       tmpvalue       = [tmpvalue[1],tmpvalue[0]]
                       if self.samp_typ lt 2 then self.samp_typ = 1-self.samp_typ
                    endif
                    self.view_u1lab   = tmpvalue
                    self.view_label[self.view_u1typ] = self.view_u1lab[0]
                    end
         'viewu2h:':self.view_u2[0]   = dm_to_number(tmp,/float)
         'viewu2k:':self.view_u2[1]   = dm_to_number(tmp,/float)
         'viewu2l:':self.view_u2[2]   = dm_to_number(tmp,/float)
         'viewu2e:':self.view_u2[3]   = dm_to_number(tmp,/float)
         'view2lb:':begin
                    tmpvalue          = strsplit(tmp,'&',/extract,/preserve_null)
                    if n_elements(tmpvalue) eq 3 then tmpvalue = [tmpvalue[1],tmpvalue[0]]
                    self.view_u2lab   = tmpvalue
                    self.view_label[self.view_u2typ] = self.view_u2lab[0]
                    end
         'viewu3h:':self.view_u3[0]   = dm_to_number(tmp,/float)
         'viewu3k:':self.view_u3[1]   = dm_to_number(tmp,/float)
         'viewu3l:':self.view_u3[2]   = dm_to_number(tmp,/float)
         'viewu3e:':self.view_u3[3]   = dm_to_number(tmp,/float)
         'view3lb:':self.view_u3lab   = tmp
         'dispper:':self.disp_perp    = dm_to_number(tmp,/int)
         'disptfr:':self.disp_tran[0] = dm_to_number(tmp,/float)
         'disptto:':self.disp_tran[1] = dm_to_number(tmp,/float)
         'disphkl:':self.disp_hkl     = dm_to_number(tmp,/int)
         'hklr_fr:':self.disp_hklran[0] = dm_to_number(tmp,/float)
         'hklr_to:':self.disp_hklran[1] = dm_to_number(tmp,/float)
         'dispfld:':self.disp_fold    = dm_to_number(tmp,/int)
         'disphfr:':self.disp_hran[0] = dm_to_number(tmp,/float)
         'disphto:':self.disp_hran[1] = dm_to_number(tmp,/float)
         'disphst:':self.disp_hstep   = dm_to_number(tmp,/float)
         'dispvfr:':self.disp_vran[0] = dm_to_number(tmp,/float)
         'dispvto:':self.disp_vran[1] = dm_to_number(tmp,/float)
         'dispvst:':self.disp_vstep   = dm_to_number(tmp,/float)
         'dispifr:':self.disp_iran[0] = dm_to_number(tmp,/float)
         'dispito:':self.disp_iran[1] = dm_to_number(tmp,/float)
         'dispsmo:':self.disp_smooth  = dm_to_number(tmp,/int)
         'dispker:':self.disp_sm_kernel[0:8] = dm_to_number(strsplit(tmp,'&',/extract,/preserve))
         'dispsha:':self.disp_shading = dm_to_number(tmp,/int)
         'dispcol:':self.disp_color   = dm_to_number(tmp,/int)
         'cut___x:':begin
                    tmpvalue          = dm_to_number(strsplit(tmp,'&',/extract,/preserve),/int)
                    if n_elements(tmpvalue) eq 3 then tmpvalue=[tmpvalue[1],tmpvalue[0],tmpvalue[2],0]
                    self.cut_x        = tmpvalue
                    end
         'cut__fr:':self.cut_ran[0]   = dm_to_number(tmp,/float)
         'cut__to:':self.cut_ran[1]   = dm_to_number(tmp,/float)
         'cut__st:':self.cut_step     = dm_to_number(tmp,/float)
         'cutt1fr:':self.cut_t1ran[0] = dm_to_number(tmp,/float)
         'cutt1to:':self.cut_t1ran[1] = dm_to_number(tmp,/float)
         'cutt2fr:':self.cut_t2ran[0] = dm_to_number(tmp,/float)
         'cutt2to:':self.cut_t2ran[1] = dm_to_number(tmp,/float)
         'cut___y:':begin
                    tmpvalue          = dm_to_number(strsplit(tmp,'&',/extract,/preserve),/int)
                    tmpid             = where(tmpvalue gt 7,count)
                    if count ne 0 then tmpvalue[tmpid] = 0
                    if n_elements(tmpvalue) eq 3 then tmpvalue=[tmpvalue[1],tmpvalue[0],tmpvalue[2],0]
                    self.cut_y        = tmpvalue
                    end
         'cut_yfr:':self.cut_yran[0]  = dm_to_number(tmp,/float)
         'cut_yto:':self.cut_yran[1]  = dm_to_number(tmp,/float)
         'cut_sym:':self.cut_symbol   = dm_to_number(tmp,/int)
         'cut_col:':self.cut_color    = dm_to_number(tmp,/int)
         'cutline:':self.cut_line     = dm_to_number(tmp,/int)
         'traj__x:':begin
                    tmpvalue          = dm_to_number(strsplit(tmp,'&',/extract,/preserve),/int)
                    tmpid             = where(tmpvalue gt 7,count)
                    if count ne 0 then tmpvalue[tmpid] = 3
                    if n_elements(tmpvalue) eq 3 then tmpvalue=[tmpvalue[1],tmpvalue[0],tmpvalue[2],3]
                    self.traj_x       = tmpvalue
                    end
         'traj_xf:':self.traj_xran[0] = dm_to_number(tmp,/float)
         'traj_xt:':self.traj_xran[1] = dm_to_number(tmp,/float)
         'traj__y:':begin
                    tmpvalue          = dm_to_number(strsplit(tmp,'&',/extract,/preserve),/int)
                    tmpid             = where(tmpvalue gt 7,count)
                    if count ne 0 then tmpvalue[tmpid] = 4
                    if n_elements(tmpvalue) eq 3 then tmpvalue=[tmpvalue[1],tmpvalue[0],tmpvalue[2],4]
                    self.traj_y       = tmpvalue
                    end
         'traj_yf:':self.traj_yran[0] = dm_to_number(tmp,/float)
         'traj_yt:':self.traj_yran[1] = dm_to_number(tmp,/float)
         'traj__z:':begin
                    tmpvalue          = dm_to_number(strsplit(tmp,'&',/extract,/preserve),/int)
                    tmpid             = where(tmpvalue gt 7,count)
                    if count ne 0 then tmpvalue[tmpid] = 5>(tmpvalue[tmpid]-6)
                    if n_elements(tmpvalue) eq 3 then tmpvalue=[tmpvalue[1],tmpvalue[0],tmpvalue[2],6]
                    self.traj_z       = tmpvalue
                    end
         'traj_zf:':self.traj_zran[0] = dm_to_number(tmp,/float)
         'traj_zt:':self.traj_zran[1] = dm_to_number(tmp,/float)
         'history:':begin
                    if clear_history then begin
                       clear_history  = 0b
                       self.n_history = 0
                       totnum         = n_elements(self.file_hist)  ;make sure the capacity is not exceeded
                       ptr_free,self.file_hist
                    endif
                    self.file_hist[self.n_history mod totnum] = ptr_new(strsplit(tmp,string('A4'xb),/extract,/preserve))
                    if strmid((*(self.file_hist[self.n_history mod totnum]))[0],0,6) ne 'ftype_' then $
                       *(self.file_hist[self.n_history mod totnum]) = ['ftype_'+dm_to_string(self.ftype,/int),$
                            *(self.file_hist[self.n_history mod totnum])]
                    self.n_history = self.n_history+1
                    end
         else:
       endcase
    endwhile
    free_lun,unit
    ptr_free,self.projStrPtr
    if not keyword_set(debug) then begin
       if (self.dcs_mon ne 1) and (self.ftype eq 0) then $
          ok = dialog_message((['FC0','BM1','Duration'])[self.dcs_mon]+' is used as the monitor.',dialog_parent=self.tlb)
       self->set_filehistory
       self->reset_file
       if newftype then begin
          tmp = self.ftype & self.ftype = -1
          ok  = self->dm_filetools('ftype_'+dm_to_string(tmp),init=init)
       endif else $
          self->reset_menu
       if not keyword_set(init) then $
          self->reset_pdt
    endif
end