!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! p_load.greg	Load a GILDAS image, FITS file or SIC Structure
!		in image-like variable &1
!
! 	The image is defined by variable 'NAME' and 'TYPE'
!
!  The extrema are computed in the file header itself only if allowed by
!  user. Otherwise avoid changing file date, or troubles with read-only
!  files.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if .not.(exist(&1__date)) then
   define character &1__name*256 &1__type*12 /global
   define integer &1__date /global
   let &1__date -1
else
   if .not.exist(&1) then
      let &1__date -1
   endif
endif
!
define logical change
! The simpled minded command below can crash on odd file names.  
!!  if (exist('name'%data)) then
! Use a less elegant but more robust method to test whether
! 'name'%data is an existing SIC variable 
compute is_var is_a_sic_var 'name'%data
if (is_var) then
   on error pause
   let change change.or.name.ne.&1__name
   if (change) then
      delete /var &1
      define image &1 gag_scratch:header.tab real /like 'name'%data /global
      let &1% 'name'%
      let &1 'name'%data
      if (&1%extrema.eq.0) then
         compute &1%min min &1
         compute &1%max max &1
      endif
      message i go "Copied SIC structure"
      let &1__name 'name'
      let &1__type %
   else
      message i go "Re-using SIC structure"
   endif
   symbol drawn &1% ! Needed for "GO FLUX"
   return
endif
!
on error "return base"
define integer new__date
define character fichier*512
let fichier 'name'"."'type'
define logical ok
let ok file(fichier)
if (.not.ok) then
   message e go "No such file "'fichier'
   return base
endif
compute new__date date 'name'.'type'
let change new__date.ne.&1__date
if (.not.change) then
   let change change.or.name.ne.&1__name
   let change change.or.type.ne.&1__type
endif
!
if (change) then
   define char head*6
   define integer ftype
   accept head /binary 'name'.'type'
   if (head.eq."SIMPLE") then
      let ftype 1
      message i go "File "'name'.'type'" is a FITS file"
   else if (head.eq."GILDAS") then
      let ftype 0
   else
      message e go ""'name'.'type'" is not a GILDAS image, FITS file, or SIC structure"
      return base
   endif
   !
   message i go "Changing to new (or updated) file " 'name'.'type'
   if (exist(&1)) then
      delete /variable &1
   endif
   define header &1 'name'.'type' read
   !
   define logical do_trim
   if &1%ndim.gt.3 then
     let do_trim yes
   else
     let do_trim no
   endif
   if (&1%extrema.eq.0.and."&2none".eq."writenone") then
      ! Compute extrema directly within the file, only if write access is
      ! allowed by user
      delete /var &1
      vector\header 'name'"."'type' /extrema
   else
      ! Extrema will be computed by p_scale
      delete /var &1
   endif
   if ("&2none".eq."writenone") then
      define image &1 'name'.'type' write /global
      message w go "Image "'name'"."'type'" opened in write mode"
   else
      if do_trim then
        define image &1 'name'.'type' read /global /trim 3
      else
        define image &1 'name'.'type' read /global
      endif
   endif
   delete /var do_trim
   !
   let &1__date new__date
   let &1__name 'name'
   let &1__type 'type'
   symbol has_changed YES
else
   symbol has_changed NO 
   message i go "Re-using "'name'"."'type'
endif
delete /variable new__date
symbol drawn &1%
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
