!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ p_load.ima Default [Actual]
!
! Load a GILDAS image, FITS file
!		in image-like variable of name Default (or Actual).
!
! If Actual is present, the script also allows to load a SIC
! Image variable (in case TYPE = " " or TYPE = "%") and return
! its name in the (character) variable Actual
! 
! If not, only files can be loaded
!
!	Extrema are computed if needed
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!
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
!
define logical has_data_array 
!
define logical change 
let change &1__date.eq.-1
!
! Blank or % types indicate SIC variables (if found) or files
! without extension (which can happen)
if (type.eq." ".or.type.eq."%") then
  if (type.eq."%").and.(pro%narg.ne.2) then
    message e GO "This script does not allow loading SIC Image variables"
    return base
  endif
  ! The simpled minded command below can crash on odd names.  
  !!  if (exist('name')) then
  ! Use a less elegant but more robust method to test whether
  ! 'name'%data or 'name' is an existing SIC variable 
  compute is_var is_a_sic_var 'name'
  !
  if is_var then
    if exist('name'%dim) then
      if typeof('name').lt.0 then
        continue
      else if exist('name'%data) then
        if typeof('name'%data).lt.0 then
          has_data_array = yes
          is_var = no
          continue
        else
          message e GO "This script does not support such SIC structure"
          return base
        endif
      endif
    else
      is_var = no
    endif
  else if (type.eq."%") then
    message e GO "No such Image variable "'name'
    return base
  endif
else
  let is_var no /new logical
endif
!
if (is_var) then
  if pro%narg.ne.2 then
    message e GO "This script does not allow loading SIC Image variables"
    return base
  endif
  ! 
  ! Variables are fully Dynamic, with no attached dates,
  let &2 'name'
  message i GO "Using SIC structure "'name'
  let &1__name 'name'
  let &1__type %
  let &1__date -1
  symbol has_changed YES
  symbol drawn 'name'% ! Needed for "GO FLUX"
  return
else if (has_data_array) then
  ! No optimization is possible -- no way to know if variable
  ! has been changed...
  if exist(&1) delete /var &1
  !
  ! OK, now use a Virtual Image
  define image &1 *  real /like 'name'%data /global
  !
  let &1% 'name'%     ! Copy the header 
  let &1  'name'%data ! Now copy the data
  SAY "Copied SIC variable "'name'"%data"
  return
endif
!
if exist(&1_name) delete /var &1_name
!
symbol has_changed NO 
!
on error "return base"
define integer new__date
define character fichier*512
if (type.eq." ") then
  let fichier 'name'
else
  let fichier 'name'"."'type'
endif
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 'fichier' !! '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
!
! No change: re-use
if (.not.change) then
  message i go "Re-using "'fichier' !! 'name'.'type'
  if pro%narg.ge.2 then
    if ("&2none".ne."writenone") then
      let &2 &1
    endif
  endif
  RETURN
endif
!
! Try to figure out the type of File: FITS, GILDAS or something wrong
define char long_head*8 short_head*6 
accept long_head /binary 'fichier'
let short_head 'long_head'
if (short_head.eq."GILDAS") then
  !
  message I go "Changing to new (or updated) file "'fichier'
  if exist(&1)  delete /variable &1
  define header &1 'fichier' 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) 
    delete /var &1
    header 'fichier' /extrema
  else
    delete /var &1
  endif
  !
  ! This is to support the Old  "write" supplemental argument
  !
  if ("&2none".eq."writenone").or.("&3none".eq."writenone") then
    define image &1 'fichier' write /global
    message I go "Image "'fichier'" opened in write mode"
  else
    if do_trim then
      define image &1 'fichier' read /global /trim 3
    else
      define image &1 'fichier' read /global
    endif
  endif
  !
else if (long_head.eq."SIMPLE  ") then
  !
  ! First check the header to find if any data
  define header a 'fichier' read
  if .not.exist(a%ndim) then
    message e go "FITS file "'fichier'" has no data"
    pause
    return error
  else if a%ndim.eq.0 then
    message e go "FITS file "'fichier'" has no data"
    pause
    return error  
  endif
  if exist(&1) delete /var &1
  !
  ! Next define it
  define image &1 'fichier' read /global /TRIM 3  ! Now possible
  !
  ! Minor patch - should be in code in fact...
  if exist(a%restfrq) then
    let &1%restfre a%restfrq*1d-6
    let &1%velres -299792.458d0/&1%restfre*&1%freqres
    ! Do we need the ALTRVAL here ?
  endif  
  delete /var a
  !
  ! Switch to velocity axis if possible 
  ! (and not already done in DEFINE FITS...)
  ! Can be done through MODIFY /SPECUNIT since &1 is now
  ! a Virtual image
  !
  if (&1%extrema.eq.0) then
    let &1 /status write
    compute &1%min min &1
    compute &1%max max &1
    let &1 /status read
  endif
  message I go "File "'fichier'" is a FITS file"
  !
else
  message e go ""'fichier'" is not a GILDAS image, FITS file, or SIC structure"
  return base
endif
let &1__date new__date
let &1__name 'name'
let &1__type 'type'" "
symbol has_changed YES
symbol drawn &1%
if pro%narg.ge.2 then
  if ("&2none".ne."writenone") then
    let &2 &1
  endif
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
