; $Id: davedat__resetdata.pro,v 1.7 2005/02/08 17:17:34 azuah Exp $
;###############################################################################
;
; CLASS NAME:
;   DaveDat
;
; PURPOSE:
;   This class encapsulates a DAVE formated dataset
;
; CATEGORY:
;   Data Browser
;
; SUPERCLASSES:
;
; PUBLIC METHODS:
;   Get
;   Set
;   ResetData
;
; AUTHOR:
;   Richard Tumanjong Azuah
;   NIST Center for Neutron Research
;   100 Bureau Drive, Gaithersburg, MD 20899
;   United States
;   azuah@nist.gov; (301) 9755604
;   Oct, 2002
;
; 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.
;
;###############################################################################



;===============================================================================
; DaveDat::resetData
;
; PURPOSE:
;   This method is used to 
;   ** set (initially) most of the DaveDat class variables or 
;   ** reset them (subsequestly).
;   The values to be set/reset are taken from the davePtr class variable
;   which, once set, would typically remain unaltered for the lifetime of
;   an object.
;
; PARAMETERS:
;
; KEYWORDS:
;
; RETURN VALUE:
;   0 => failure
;   1 => success
;
function DaveDat::resetData

; reset !error_state structure, ie clear all errors
message,/reset

; reset !error_state structure, ie clear all errors
message,/reset

; davePtr must be valid
if (ptr_valid(self.davePtr) ne 1) then begin
    !error_state.msg = 'DaveDat::setDataPtr: invalid davePtr!'
    !error_state.code = -1
    return, 0
endif

; go through (the 3 types of) elements and set/reset as appropriate


;** 1) descriPtr pointer variable. Handle several posibilities
; first, for backward compatibility with old davePtr's (before descriPtr
; was included). Need to fist search through tag_names(davePtr) for
; descriPtr before proceeding
void = {descriPtrStr, $ 
        name:'', $              ; name of series variable eg 'HField'
        units:'', $             ; units, eg 'T'
        legend:'', $            ; descriptive info, eg 'Average Sample Field'
        qty: 0.0, $             ; value, eg 0.05
        err: 0.0 $              ; 0.0
       }
res = where(tag_names(*self.davePtr) eq 'DESCRIPTR')
if (res lt 0) then begin        ; descriPtr tag not found ==> probably old style davePtr
    if (ptr_valid(self.descriPtr)) then begin  
        (*self.descriPtr) = {descriPtrStr}
    endif else begin
        self.descriPtr = ptr_new({descriPtrStr})
    endelse
    ;; also insert a descriPtr in davePtr
    tmpPtr = ptr_new({davePtrStr,dataStrPtr:ptr_new(),descriPtr:ptr_new({descriPtrStr})})
    (*tmpPtr).dataStrPtr = (*self.davePtr).dataStrPtr
    ptr_free, self.davePtr
    self.davePtr = tmpPtr
endif else begin
    if (ptr_valid((*self.davePtr).descriPtr)) then begin ; davePtr has a valid descriPtr
        if (ptr_valid(self.descriPtr)) then begin ; reset descriPtr with values from davePtr
            if (n_elements(*(*self.davePtr).descriPtr) gt 0) then begin ; make sure *(*self.davePtr).descriPtr is defined!
                (*self.descriPtr) = (*(*self.davePtr).descriPtr)
            endif else begin
                (*self.descriPtr) = {descriPtrStr}
            endelse
        endif else begin        ; set descriPtr
            if (n_elements(*(*self.davePtr).descriPtr) gt 0) then begin ; make sure *(*self.davePtr).descriPtr is defined!
                self.descriPtr = ptr_new(*(*self.davePtr).descriPtr)
            endif else begin
                self.descriPtr =  ptr_new({descriPtrStr})
                (*(*self.davePtr).descriPtr) = {descriPtrStr}
            endelse
        endelse
    endif else begin            ; davePtr has invalid descriPtr
        if (ptr_valid(self.descriPtr)) then begin  
            (*self.descriPtr) = {descriPtrStr}
        endif else begin
            self.descriPtr = ptr_new({descriPtrStr})
        endelse
        ;; also define descriPtr for the objects davePtr
        (*self.davePtr).descriPtr = ptr_new({descriPtrStr})
    endelse
endelse 

;** 2) Normal pointers, set and reset are treated differently
if (ptr_valid(self.treatPtr)) then begin ; reset
    (*self.treatPtr) = (*(*(*self.davePtr).dataStrPtr).commonStr.treatmentPtr)
endif else begin                ; set
    self.treatPtr = ptr_new(*(*(*self.davePtr).dataStrPtr).commonStr.treatmentPtr)
endelse
if (ptr_valid(self.xvPtr)) then begin ; reset
    (*self.xvPtr) = (*(*(*self.davePtr).dataStrPtr).commonStr.histPtr).x
endif else begin                ; set
    self.xvPtr = ptr_new((*(*(*self.davePtr).dataStrPtr).commonStr.histPtr).x)
endelse
if (ptr_valid(self.zvPtr)) then begin ; reset
    (*self.zvPtr) = (*(*(*self.davePtr).dataStrPtr).commonStr.histPtr).qty
endif else begin                ; set
    self.zvPtr = ptr_new((*(*(*self.davePtr).dataStrPtr).commonStr.histPtr).qty)
endelse 
if (ptr_valid(self.zePtr)) then begin ; reset
    (*self.zePtr) = (*(*(*self.davePtr).dataStrPtr).commonStr.histPtr).err
endif else begin                ; set
    self.zePtr = ptr_new((*(*(*self.davePtr).dataStrPtr).commonStr.histPtr).err)
endelse

; Check if x-axis is not increasing with index then reverse it
self.xDir = 0
nx = n_elements((*self.xvPtr))
if ((*self.xvPtr)[nx-1] lt (*self.xvPtr)[0]) then begin
    self.xDir = 1
    (*self.xvPtr) = reverse((*self.xvPtr))
    (*self.zvPtr) = reverse((*self.zvPtr))    
    (*self.zePtr) = reverse((*self.zePtr))    
endif

;** 3) Normal variables, set/reset identical
self.instrument = (*(*self.davePtr).dataStrPtr).commonStr.instrument
self.xLabel = (*(*self.davePtr).dataStrPtr).commonStr.xlabel
self.xType = (*(*self.davePtr).dataStrPtr).commonStr.xtype
self.xUnits = (*(*self.davePtr).dataStrPtr).commonStr.xunits 
self.zLabel = (*(*self.davePtr).dataStrPtr).commonStr.histlabel
self.zUnits = (*(*self.davePtr).dataStrPtr).commonStr.histunits

; xtype must be defined!
if (strlen(strtrim(self.xType,2)) eq 0) then self.xType = 'POINTS' 

; reset scalefactor
self.scalefactor=1.0

return, 1
end 
;_______________________________________________________________________________


