begin procedure odpMira 
  
!-------------------------------------------------------------------------------
! Prototype for 30m online data calibratio with MIRA                          
! 2005-11-29, H. Wiesemeyer, IRAM Grenoble
! 2006-02-09, modified for use together with driver procedure odp.mira
!  
! Input: idScan     number of scan to be processed
!        obsDate    observing date, format YYYY-MM-DD
!        idCalScan  number of calibration scan (may be 0 if no calibration 
!                   needed)
!        obsDateCal observing date of calibration (could be different from 
!                   obsDate)
! 
! For the output, we need to agree on the file location & naming conventions
!
! The following file names changed, see 
! http://mrt-lx1/mainWiki/NCSFileStructure
!
! Output : spectra.30m                CLASS file with (calibrated) data
!          myPlot.eps                 hardcopies of MIRA display (on per FeBe)
!          miraResultsPointing.xml    pointing results (one per FeBe)
!          miraResultsFocus.xml       focus results (one per FeBe)
!          miraResultsCalibration.xml calibration results (to be done)
!_______________________________________________________________________________

on error continue
 
define integer   idCalScan idScan ifb intDate nsubscan
define integer   intYear intMonth intDay
define double    max
define character append*3 xpolString*1
define character year*4 month*2 day*2
define character subString1*4 subString2*7
define character inputFile*256 obsDate*10 obsDateCal*10
define character outputFile*256 plotDir*256 plotFile*256 plotFileSubscan*256
define logical   doCal

let idScan     = &1
let obsDate    = &2
let idCalScan  = &3
let obsDateCal = &4
let ifb        = &5
let ncsData  "/ncsServer/mrt/ncs/data"
let visData  "$HOME"

if idCalScan.ne.0 then   ! a calibration scan is provided

   let subString1  'obsDateCal'
   let subString2  'obsDateCal'
   let intYear     'subString1'
   let intMonth    'subString2'-intYear
   let intDay      'obsDateCal'-intYear-intMonth
   let intMonth    -intMonth
   let intDay      -intDay

   let year  'intYear'  /format i4
   let month 'intMonth' /format i2.2
   let day   'intDay'   /format i2.2

   let inputFile  'ncsData'"/"'year''month''day'"/scans/"'idCalScan'
   if .not.file(inputFile) then
      say "W-ODP: subdirectory with calibration scan does not exist."
      say "       Omitting calibration."
      let idCalScan  0
   else
      file in 'inputFile'
      find /scan 'idCalScan' /observed 'obsDate'
      scan idCalScan
      let doCal  .true.
   endif
 
endif
 

let subString1  'obsDate'
let subString2  'obsDate'
let intYear     'subString1'
let intMonth    'subString2'-intYear
let intDay      'obsDate'-intYear-intMonth
let intMonth    -intMonth
let intDay      -intDay

let year  'intYear'  /format i4
let month 'intMonth' /format i2.2
let day   'intDay'   /format i2.2

let inputFile  'ncsData'"/"'year''month''day'"/scans/"'idscan'

if .not.file(inputFile) then
   say " "
   say "E-ODP: subdirectory with scan does not exist."
   say " "
   return
endif

if .not.file("$HOME/miraOdp") then
   sic mkdir "$HOME/miraOdp"
   sic mkdir "$HOME/miraOdp/plots"
endif
let outputFile  "$HOME/miraOdp/spectra.30m"

let plotDir  "$HOME/miraOdp/plots"
if .not.file(plotDir) then
   sic mkdir "$HOME/miraOdp/plots"
endif

file in 'inputFile'
find /scan 'idScan' /observed 'obsDate'

sic find 'outputFile'
if dir%nfile.gt.0 then
   file out 'outputFile'
else
   file out 'outputFile' new
endif
 
scan idScan
var scan
var array
var febe
var data

for i 1 to scan%header%nfebe                          ! start of FeBe loop

    @ identFeBe 'i'
    let plotFile 'plotDir'"/iram30m-"'feIdPlot'"-"'beIdPlot'"-"'year''month''day'
    let plotFile 'plotFile'"s"'idScan'".eps"
    if file(plotFile) then
       sic delete 'plotFile'
    endif

    let xpolString = 'febe3%header%febe'
 
    if scan%data%be[i].ge.10.and.scan%data%be[i].le.19 then  ! continuum data
 
       if scan%header%scantype.eq."calibrate" then           ! only display data

          view i /cal
          hardcopy .tmp.eps /dev eps color
	  sic rename .tmp.eps 'plotFile'
      
       else
 
          if doCal then
             cal i              ! calibrate frontend-backend combination i
          else
             say "W-ODP: no calibration provided."
             cal i /off         ! do not calibrate them, simply subtract on-off
                                ! in order to write CLASS file
          endif
 
          if scan%header%scantype.eq."pointing" then
             define double aux /like data'i'%data%subscan
             let aux data'i'%data%subscan
             compute max max aux
             let nsubscan int(max)
             del /var aux
	     if i.eq.ifb then
                solve i 
		pause "type c to continue"
	     endif
             hardcopy .tmp.eps /dev eps color
	     sic rename .tmp.eps 'plotFile'
             @p_plot_pointBySubscan 'i'
             let plotFileSubscan -
             'plotDir'"/iram30m-"'feIdPlot'"-"'beIdPlot'"-"'year''month''day'
	     let plotFileSubscan 'plotFileSubscan'"s"'idScan'"s"'nsubscan'".eps"
             if file(plotFileSubscan) then
                sic delete 'plotFileSubscan'
             endif
             hardcopy .tmp.eps /dev eps color
	     sic rename .tmp.eps 'plotFileSubscan'
             
             for j 1 to nsubscan 
                 m\write /febe i /subscan j /pixel all
             next
         else if scan%header%scantype.eq."focus" then
	     if i.eq.ifb then
                solve i 
		pause "type c to continue"
	     endif
             hardcopy .tmp.eps /dev eps color
	     sic rename .tmp.eps 'plotFile'
         else
             view i
             hardcopy .tmp.eps /dev eps color
	     sic rename .tmp.eps 'plotFile'
             m\write /febe i /pixel all
         endif
 
       endif
 
    else                                                 ! spectroscopic data
 
      if scan%header%scantype.eq."calibrate" then        ! only display data
          if xpolString.eq."r".or.xpolString.eq."R" then
	     cal i-2
	     cal i-1
	     cal i
	     cal i+1
          endif
         view i
         hardcopy .tmp.eps /dev eps color
	 sic rename .tmp.eps 'plotFile'
      else
         if doCal then
            cal i               ! calibrate frontend-backend combination i
         else
            say "W-ODP: no calibration provided."
            cal i /off          ! do not calibrate them, simply subtract on-off
                                ! in order to write CLASS file
         endif
         view i
         hardcopy .tmp.eps /dev eps color
	 sic rename .tmp.eps 'plotFile'
         m\write /febe i  /pixel all
      endif
 
    endif

    say " "  
    say "   --> FeBe "'i'" done"
    say " "  
 
next                                                  ! end of FeBe loop

return
 
end procedure odpMira 

! Driver for odpMira.mira
! 2008-02-09, Helmut Wiesemeyer

define integer     ifb obsScan

let ifb     = &1
let obsScan = &2


define integer     calScan ifirst
define character   calDate*10 obsDate*10
define character   inputFile*256 visData*256

visData = "$HOME"
inputFile = 'visData'"/observationData/imbfits/"
file in 'inputFile' 
find

var list

if .not.exist(list) then
   return
endif

if list%found.eq.0 then
   return
endif

list /output .odp.lis

for i list%found to 1 by -1
    if list'i'%scan.eq.obsScan then
       accept * * * * * obsDate * /column .odp.lis /line 'i'
       ifirst = i
       break
    endif
next

for i ifirst to 1 by -1
    if list'i'%procedure.eq."calibrat" then
       calScan = list'i'%scan
       accept * * * * * calDate * /column .odp.lis /line 'i'
       break
    endif
next

@odpMira 'obsScan' 'obsDate' 'calScan' 'calDate' 'ifb'

file in 'inputFile' 

