!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ l-old-layout
!
! &1, intent(in)  :: aaa
! &2, intent(in)  :: "1d" or "2d"
! &3, intent(out) :: true%first[1]  (1d case only)
! &4, intent(out) :: true%last[1]   (1d case only)
! &5, intent(out) :: true%step[1]   (1d case only)
!
! Definition of true%first, true%last, true%step and true%nbox or true%nxybox
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
define character layout*2
let layout "&2"
!
if (layout.eq."1d") then
   ! Case: p-old-over.cube, p-old-pv.cube => Always use 3rd axis
   let true%first[2] 0
   let true%last[2]  0
   let true%step[2]  0
   if ((first.ne.0).or.(last.ne.0)) then
      ! Old mode: User input are pixels
      if (first.ne.0) then
         let true%first[1] max(1,min(&1%dim[3],first))
      else
         let true%first[1] 1
      endif
      if (last.ne.0) then
         let true%last[1] max(1,min(&1%dim[3],last))
      else
         let true%last[1] &1%dim[3]
      endif
      if (step.ne.0) then
         let true%step[1] max(1,min(abs(true%last[1]-true%first[1]),step))
      else
         let true%step[1] &5
      endif
   else
      ! New mode: User input are physical ranges
      let true%first[1] &3
      let true%last[1]  &4
      let true%step[1]  &5
   endif
   ! Sign of step depend on first and last order
   if (true%first[1].gt.true%last[1]) then
      let true%step[1] -true%step[1]
   endif
   ! Compute number of boxes (both absolute values are needed!)
   let true%nbox abs((abs(true%last[1]-true%first[1])+1)/true%step[1])
else if (layout.eq."2d") then
   ! Case: p-old-spectrum.cube => Always use 1st and 2nd axes
   define integer xypix[3,2]
   let xypix[1] xpix
   let xypix[2] ypix
   for iaxis 1 to 2
      if ((xypix[1,iaxis].ne.0).or.(xypix[2,iaxis].ne.0).or.(xypix[3,iaxis].ne.0)) then
         ! Old mode: User input are pixels
         if (xypix[1,iaxis].ne.0) then
            let true%first[iaxis] max(1,min(&1%dim[2],xypix[1,iaxis]))
         else
            let true%first[iaxis] 1
         endif
         if (xypix[2,iaxis].ne.0) then
            let true%last[iaxis] max(1,min(&1%dim[2],xypix[2,iaxis]))
         else
            let true%last[iaxis] &1%dim[2]
         endif
         if (xypix[3,iaxis].ne.0) then
            let true%step[iaxis] max(1,min(&1%dim[2],xypix[3,iaxis]))
         else
            let true%step[iaxis] true%sampling%p[iaxis]
         endif
      else
         ! New mode: User input are physical ranges
         let true%first[iaxis] true%prange%p[1,iaxis]
         let true%last[iaxis]  true%prange%p[2,iaxis]
         let true%step[iaxis]  true%sampling%p[iaxis]
      endif
      ! Sign of step depend on first and last order
      if (true%first[iaxis].gt.true%last[iaxis]) then
         let true%step[iaxis] -true%step[iaxis]
      endif
      ! Define number of boxes on iaxis: Never use user defined layout because
      ! it probably do not make any sense here
      ! Both absolute values are needed!
      let true%nxybox[iaxis] abs((abs(true%last[iaxis]-true%first[iaxis])+1)/true%step[iaxis])
      if (true%nxybox[iaxis].eq.0) then
         let true%nxybox[iaxis] 1
      endif
   next iaxis
else
   message e layout "Unknown layout type: "'layout'
   return base
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
