!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Load a GILDAS image, FITS file or SIC Structure in an image-like variable,
! whose name is given by &1. The image is defined by variable 'NAME' and 'TYPE'.
! Extrema are sic\computed if needed.
!
!!$ Cube limits should be found and stored one way or another!
!!$if (&1%extrema.eq.0.or.(&1%min.eq.&1%max)) then
!!$   ! No extrema section
!!$   sic\compute tmin min &1
!!$   sic\compute tmax max &1
!!$else
!!$   ! From extrema section
!!$   let tmin &1%min
!!$   let tmax &1%max
!!$endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
on error "return base"
!
! Sanity check
if ("&1none".eq."none") then
   message e load "A variable name must be given as input parameter"
   return base
endif
!
! Define useful variables, in particular: name, type and date associated to variable &1.
if .not.(exist(&1_date)) then
   define character &1_name*64 &1_type*12 /global
   define integer &1_date /global
   let &1_date -1
endif
define logical reload
!
! Check kind of object to be loaded
if (name.eq."wifi") then
   wifisyn\load 'type' &1
   message i load "Load "'type'" into &1"
   let &1_name 'name'
   let &1_type 'type'
else
   ! Check whether this is a file
   define character filename*512
   let filename 'name'"."'type'
   if .not.file(filename) then
      message e load "No such file "'filename'
      return base
   endif
   if (type.eq."vlm") then
      let filename 'name'".lmv"
      if .not.file(filename) then
         message i load "Transposing from "'name'".vlm to "'filename'
         transpose 'name'".vlm" 'filename' 231
      else
         define integer lmvdate vlmdate
         sic\compute lmvdate date 'filename'
         sic\compute vlmdate date 'name'".vlm"
         if (vlmdate.gt.lmvdate) then
            message i load "Retransposing from "'name'".vlm to "'filename'
            transpose 'name'".vlm" 'filename' 231
         endif
      endif
   endif
   !
   ! Check its modification date and its name
   define integer new_date
   sic\compute new_date date 'filename'
   let reload new_date.ne.&1_date
   if (.not.reload) then
      let reload reload.or.name.ne.&1_name
      let reload reload.or.type.ne.&1_type
   endif
   !
   ! The file may need to be reloaded
   if (reload) then
      ! User feedback
      say "Changing to new (or updated) file "'name'"."'type'
      ! Check file type
      define character head*6
      accept head /binary 'filename'
      if (head.eq."GILDAS") then
         ! This is a GILDAS image => Simple case
      else if (head.eq."SIMPLE") then
         ! This is a FITS file => Translation is needed
         say "File "'filename'" is a FITS file"
         vector\fits 'filename' to "gag_scratch:p-old-load-&1.gdf"
         let filename "gag_scratch:p-old-load-&1.gdf"
         vector\header 'filename' /extrema
      else
         ! Unknown type of file => Error
         message e load ""'filename'" is not a GILDAS image, FITS file, or SIC structure"
         return base
      endif
      !
      ! Remove old variable if needed
      if (exist(&1)) then
         delete /variable &1
      endif
      !
      ! Compute extrema if needed
      define header &1 'filename' read
      if (&1%extrema.eq.0) then
         vector\header 'filename' /extrema
      endif
      delete /var &1
      !
      ! Reload
      if ("&2none".eq."writenone") then
         ! In write mode
         define image &1 'filename' write /global
         message w load "Image "'name'"."'type'" opened in write mode"
      else
         ! In read-only mode
         define image &1 'filename' read /global
      endif
      !
      ! Reload succeeded => Update associated parameters
      let &1_date new_date
      let &1_name 'name'
      let &1_type 'type'
   endif
   !
   ! Clean up
   delete /variable new_date
   !
endif
!
symbol drawn &1% ! Needed by "GO FLUX"
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
