; $Id: davedat__set.pro,v 1.5 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::set
;
; PURPOSE:
;   Main set method for class. Used to retrieve class variables (all
;   class variables are 'private' in IDL)
;
; PARAMETERS:
;
; KEYWORDS:
;   filename [in] - assign value (expects string) to object's filename variable
;
;   nameTag [in] - A string name tag for object
;
;   davePtr [in] - assign value (expects a DAVE structure pointer) to
;                  object's davePtr variable.
;
;   scalefactor [in] - assign value (expects a number) to object's
;                      scalefactor variable.
;
;   descriPtr [in] - assign value (expects a descriPtr structure
;                    pointer) to object's descriPtr variable.
;
;   instrument [in] - assign value (expects scaler string) to object's
;                     intrument variable.
;
;   xVal [in] - assign value (expects float array) to objects xVal
;               (x-axis values) variable.
;
;   xLabel [in] - assign value (expects scaler string) to object's
;                 xLabel (x-axis label) variable
;
;   xType [in] - assign value (expects scaler string) to object's
;                xType (x-axis type) variable.
;
;   xUnits [in] - assign value (expects scaler string) to object's
;                 xUnits (x-axis units) variable.
;
;   zVal [in] - assign value (expects float array) to objects zVal
;               (z-axis values) variable.
;
;   zErr [in] - assign value (expects float array) to objects zErr
;               (z-axis values) variable.
;
;   zLabel [in] - assign value (expects scaler string) to object's
;                 zLabel (z-axis label) variable.
;
;   zUnits [in] - assign value (expects scaler string) to object's
;                 zUnits (z-axis units) variable.
;
;   treatment [in] - assign value (expects string array) to object's
;                    treatPtr (treatment records) variable.
;
;   nameTag [in] - assign the string value to the object's nameTag
;                  variable.
;
;   addRef [in] - increment the object's nRefrences variable by 1.
;
;   remRef [in] - decrement the object's nRefrences variable by 1.
;
;   xDir [in] - assign int value (0:1) to the object's xDir (is x-axis
;               increasing or decreasing?) variable.
;
; RETURN VALUE:
;   0 => failure
;   1 => success
;
function DaveDat::set, filename=filename, davePtr=davePtr, descriPtr=descriPtr, $
                scalefactor=scalefactor, instrument=instrument, xVal=xval, xLabel=xlabel, $
                xType=xtype, xUnits=xunits, zVal=zval, zErr=zerr, zLabel=zlabel, zUnits=zunits, $
                treatment=treatment, nameTag=nameTag, addRef=addRef, remRef=remRef, xDir=xDir

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

;use this to return a fail condition if at least one error is encountered
retVal = 1

;
if (n_elements(xDir)) then self.xDir=xDir

; Increment the nos of references for this object
if (keyword_set(addRef)) then begin
    self.nReferences = self.nReferences + 1
endif

; Decrement the nos of references by 1
if (keyword_set(remRef)) then begin
    self.nReferences = self.nReferences - 1
    ;; if -ve then something is wrong
    if (self.nReferences lt 0) then begin
        !error_state.msg = 'DaveDat::set: Attempting to remove non-existing reference'
        !error_state.code = -1
        self.nReference = 0
        retVal = 0        
    endif
endif

; filename keyword
if (n_elements(filename) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(filename) eq 'STRING') and $
         (size(filename,/n_dimensions) eq 0) ) then begin
        self.filename = filename
    endif else begin
        !error_state.msg = 'DaveDat::set: filename keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

; name tag
if (n_elements(nameTag)) then begin
    if (vartype(nameTag) eq 'STRING') then self.nameTag = nameTag
endif

;
if (n_elements(davePtr) gt 0) then begin
    if (ptr_valid(davePtr) ne 1) then begin
        !error_state.msg = 'DaveDat::set: davePtr keyword value is an invalid pointer'
        !error_state.code = -1
        retVal = 0
    endif else begin
        if ( (vartype(*davePtr) eq 'DAVEPTRSTR') or $
             (where(tag_names(*davePtr) eq 'DATASTRPTR') ge 0) ) then begin
            heap_free, self.davePtr ; cleanup object's current davePtr
            dave_beast_ops, 'dup', davePtr, davePtr_copy ; make a copy
            self.davePtr = davePtr_copy ; and assign the copy to the object
        endif else begin
            !error_state.msg = 'DaveDat::set: davePtr keyword value does not point to a valid davePtr structure'
            !error_state.code = -1
            retVal = 0
        endelse
    endelse
endif

;
if (n_elements(descriPtr) gt 0) then begin
    if (ptr_valid(descriPtr) ne 1) then begin
        !error_state.msg = 'DaveDat::set: descriPtr keyword value is an invalid pointer'
        !error_state.code = -1
        retVal = 0
    endif else begin
        if (vartype(*descriPtr) eq 'DESCRIPTRSTR') then begin ;DESCRIPTRSTR
            if (ptr_valid(self.descriPtr)) then begin
                (*self.descriPtr) = *descriPtr 
            endif else begin
                self.descriPtr = ptr_new(*descriPtr) ; assign contents of descriPtr to object 
            endelse
        endif else begin
            !error_state.msg = 'DaveDat::set: descriPtr keyword value does not point to a valid descriPtr structure'
            !error_state.code = -1
            retVal = 0
        endelse
    endelse
endif

;
if (n_elements(scalefactor) gt 0) then begin
    sfType = vartype(scalefactor) 
    if ( (sfType eq 'FLOAT') or $ ; scalefactor must be a float, int, double or long
         (sfType eq 'INT') or $
         (sfType eq 'DOUBLE') or $
         (sfType eq 'LONG') ) then begin

        self.scalefactor = float(scalefactor)
    endif else begin
        !error_state.msg = 'DaveDat::set: scalefactor keyword value is not of type float, int, double or long'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(instrument) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(instrument) eq 'STRING') and $
         (size(instrument,/n_dimensions) eq 0) ) then begin
        self.instrument = instrument
    endif else begin
        !error_state.msg = 'DaveDat::set: instrument keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(xval) gt 0) then begin
    xvalType = vartype(xval)
    if ( (xvalType eq 'FLOAT') or $
         (xvalType eq 'DOUBLE') or $
         (xvalType eq 'INT') or $
         (xvalType eq 'LONG') ) then begin
        if (ptr_valid(self.xvPtr)) then begin
            (*self.xvPtr) = xval ; assign contents to object 
        endif else begin
            self.xvPtr = ptr_new(xval)
        endelse 
    endif else begin
        !error_state.msg = 'DaveDat::set: xval keyword value is not a float or double'
        !error_state.code = -1
        retVal = 0
    endelse
    ;;endelse
endif

;
if (n_elements(xlabel) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(xlabel) eq 'STRING') and $
         (size(xlabel,/n_dimensions) eq 0) ) then begin
        self.xLabel = xlabel 
    endif else begin
        !error_state.msg = 'DaveDat::set: xlabel keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(xtype) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(xtype) eq 'STRING') and $
         (size(xtype,/n_dimensions) eq 0) ) then begin
        self.xtype = xtype
    endif else begin
        !error_state.msg = 'DaveDat::set: xtype keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(xunits) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(xunits) eq 'STRING') and $
         (size(xunits,/n_dimensions) eq 0) ) then begin
        self.xunits = xunits 
    endif else begin
        !error_state.msg = 'DaveDat::set: xunits keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(zval) gt 0) then begin
    zvalType = vartype(zval)
    if ( (zvalType eq 'FLOAT') or $
         (zvalType eq 'DOUBLE') or $
         (zvalType eq 'INT') or $
         (zvalType eq 'LONG') ) then begin
        if (ptr_valid(self.zvPtr)) then begin
            (*self.zvPtr) = zval ; assign contents to object 
        endif else begin
            self.zvPtr = ptr_new(zval)
        endelse 
    endif else begin
        !error_state.msg = 'DaveDat::set: zval keyword value is not a float/double'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(zerr) gt 0) then begin
    zerrType = vartype(zerr)
    if ( (zerrType eq 'FLOAT') or $
         (zerrType eq 'DOUBLE') or $
         (zerrType eq 'INT') or $
         (zerrType eq 'LONG') ) then begin
        if (ptr_valid(self.zePtr)) then begin
            (*self.zePtr) = zerr ; assign contents to object 
        endif else begin
            self.zePtr = ptr_new(zerr)
        endelse 
    endif else begin
        !error_state.msg = 'DaveDat::set: zerr keyword value is not a float/double'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(zlabel) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(zlabel) eq 'STRING') and $
         (size(zlabel,/n_dimensions) eq 0) ) then begin
        self.zlabel = zlabel
    endif else begin
        !error_state.msg = 'DaveDat::set: zlabel keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(zunits) gt 0) then begin
    ;; make sure value is a _scaler string_
    if ( (vartype(zunits) eq 'STRING') and $
         (size(zunits,/n_dimensions) eq 0) ) then begin
        self.zunits = zunits 
    endif else begin
        !error_state.msg = 'DaveDat::set: zunits keyword value is not a scaler string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

;
if (n_elements(treatment) gt 0) then begin
    ;; make sure value is a string
    if (vartype(treatment) eq 'STRING') then begin
        if (ptr_valid(self.treatPtr)) then begin
            (*self.treatPtr) = treatment
        endif else begin
            self.treatPtr = ptr_new(treatment)
        endelse 
    endif else begin
        !error_state.msg = 'DaveDat::set: treatment keyword value is not a string'
        !error_state.code = -1
        retVal = 0
    endelse
endif

return, retVal
end
