!
! procedure reduce.mira for online data processing
!
on error continue
!
file in ~/data
!
define integer iref iscan found lastScan 
define logical writeClass
!
let iref       &1 ! FEBE number of reference receiver for pointing and focus
let writeClass &2 ! write or do not write CLASS spectra
!
if writeClass then
   if file("~/spectraOdp.30m") then
      file out ~/spectraOdp.30m
   else
      file out ~/spectraOdp.30m new
   endif
endif
!
define integer nsubscan
define double max
!
found = 0
find
var list
if exist(list) then
   found = list%found
else
   for /while found.eq.0
      sic wait 10
      find
      var list
      if exist(list) then
         found = list%found
      endif
   next
endif
!
scan list%attribute'found'%scan
var data 
var scan
!
lastScan = 0
!
for /while .true.
    if (list%attribute'found'%scan.ne.lastScan) then
       lastScan = list%attribute'found'%scan
       for i 1 to list%found
           iscan = list%attribute'i'%scan
	   if list%attribute'i'%backend.eq."CONTINUU" then
              scan 'iscan' 
!
              define double aux /like data'iref'%data%subscan
              let aux data'iref'%data%subscan
              compute max max aux
              nsubscan = int(max)
              del /var aux
!
              if scan%data%be[iref].eq.10 then
	         if scan%header%scantype.eq."calibrate" then
	            view 'iref' /cal
	         else
	            cal ALL
	            if scan%header%scantype.eq."pointing".or.scan%header%scantype.eq."focus" then
	               solve 'iref'
                    endif
                 endif
	         if writeClass then
                    if scan%header%scantype.eq."pointing" then 
                       for j 1 to nsubscan 
                           for k 1 to scan%header%nfebe
                               m\write /subscan 'j' /febe 'k'
                           next
                       next
                    else if scan%header%scantype.ne."focus" then
                       m\write /febe all
                    endif
	         endif
              endif
           endif
       next
    endif
    sic wait 10
    find /new
    found = list%found
next
!
