begin procedure stack-init
   if (exist(mystack)) delete /var mystack
   sic message cube s-i
   define structure mystack /global
   define character*512 mystack%name mystack%stacked mystack%mask /global
   define character*12 mystack%xaxis mystack%yaxis /global
   define character*12 mystack%blank /global
   define logical mystack%donoise /global
   !
   let mystack%name 'name'
   let mystack%blank ""
   ! let mystack%xaxis 'stack%xaxis'
   ! let mystack%yaxis 'stack%yaxis'
   ! let mystack%mask stack%mask
   ! let mystack%donoise stack%donoise
   let mystack%mask "" !themask
   let mystack%xaxis velocity
   !let mystack%xaxis frequency
   let mystack%yaxis temperature
   let mystack%donoise no
end procedure stack-init
!
begin procedure stack-exec
   define character*256 cmd
   let cmd "cube\stack "'name'
   if (mystack%mask.ne.mystack%blank) then
      let cmd 'cmd'" /mask "'mystack%mask'
   endif
   if (mystack%donoise) then
      let cmd 'cmd'" /weight noise"
   endif
   execute 'cmd'
   let mystack%stacked 'name'"-stack"
end procedure stack-exec
!
begin procedure stack-load
   go\oldload 'mystack%stacked' /in mystack%cube
   ! VVV Currently load only charges the header so I am loading the
   ! data in a separate variable
   define image mystack%data tmp/'mystack%stacked'.vlm read /global
end procedure stack-load
!
begin procedure stack-plot
   gtvl\clear
   set\panel 1 /def
   define real yaxis[mystack%cube%gen%nc]
   let yaxis mystack%data[,1,1]
   if (mystack%xaxis.eq."velocity") then
      greg1\limits /var mystack%cube%spe%v%coord yaxis
      greg1\box
      greg1\label "Velocity [km/s]" /x
      greg1\label 'mystack%cube%gen%unit' /y
      histogram\histogram? mystack%cube%spe%v%coord yaxis
   else if (mystack%xaxis.eq."frequency")
      greg1\limits /var mystack%cube%spe%f%coord yaxis
      greg1\box
      greg1\label "Frequency [MHz]" /x
      greg1\label 'mystack%cube%gen%unit' /y
      histogram\histogram? mystack%cube%spe%f%coord yaxis
   else
      message e stack "Unrecognised X axis "'mystack%xaxis'
      return base
   endif
   !
end procedure stack-plot
!
begin procedure stack-main
   @ stack-init
   @ stack-exec
   @ stack-load
   @ stack-plot
   @ stack-destroy
end procedure stack-main
!
begin procedure stack-destroy
   delete /var mystack
   sic message cube s+i
end procedure stack-destroy
!
@ stack-main
