!-------------------------------------------------------------------------------
! Prototype for 30m online data calibratio with MIRA                          
! 2005-11-29, H. Wiesemeyer, IRAM Grenoble
!  
! Input: projId     project id (spelled as in $visData)
!        idScan     number of scan to be processed
!        obsDate    observing date, format YYYYMMDD
!        idCalScan  number of calibration scan (may be 0 if no calibration 
!                   needed)
!        obsDateCal observing date of calibration (could be different from 
!                   obsDate)
!        backendProcessed backend(group) processed
! 
! 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 : spectraOdp.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)
! Modifications:
!   2008-04-22 wb: include setting variable writeXML
!   2008-12-05 hw: online phase calibration of XPOL data
!   2009-06-18 hw: updated for EMIR polarimetry
!   2010-05-02 hw: works if backends with both "/mode simple" and "/mode polar"
!                  are connected.
!   2010-11-23 as: not finishes
!   2010-12-02 as: by backendProcessed
!   2013-03-05 wb: add date to parameters for mvMiraResultFiles.py
!   2015-01-21 as: tips reintroduced? 
!_______________________________________________________________________________


on error continue
say "--- onlineDataProcessing.mira - v 2015-07-22.1"
system "touch .processing"

let calCheck no      ! added by HW because for Mira version >= 2.0
                      ! default is calCheck no
                      ! 2009-05-14
		     ! changed to 'no', 'yes' calibrates part 4 using
                     ! gainarray from part 1 ... 2015-07-22 AS
let flagBSwSpikes yes ! added by HW to interpolate between beam-switch
                      ! spikes generated by abnormally short integration
	              ! times and hence normalization. Needs version 
		      ! gag_dev (edit in scanInfo.py). 2009-06-16 HW
let atmVersion 2009   ! added by HW, 2009-12-01: new ATM version valid
                      ! for all data taken after 1 Dec 2009 after maintenance
 
define integer   idCalScan idScan intDate nsubscan
define integer   intYear intMonth intDay
define double    max
define character append*3 projId*20 xpolString*3 tmpScanType*5
let xpolString = "   "
define character year*4 month*2 day*2
define character inputFile*256 obsDate*8 obsDateCal*8 obsDateMira*10
define character outputFile*100 plotDir*256 plotFile*256 plotFileSubscan*256
define logical   doCal doPolarimetry
define character ncsData*80 visData*80 visDataClass*80
define character lastPlotLink*80
define character polTemplate*64 polMode*64
define character backendProcessed*10 

let writeXML yes

let projId     = &1 
let idScan     = &2
let obsDate    = &3
let idCalScan  = &4
let obsDateCal = &5
let backendProcessed = &6
say "--- "'projId'" "'idScan'" "'obsDate'" "'idCalScan'" "'obsDateCal'" "'backendProcessed'
let ncsData = "/ncsServer/mrt/ncs/data"
let visData = "/mrt-lx1/vis/"'projId'"/observationData"
!let visDataClass = "/mrt-lx1/vis/herapool2/"'projId'"/observationData"
!let visDataClass = "/mrt-lx1/vis/herapool/"'projId'"/observationData"
!if .not.file(visDataClass) then
   let visDataClass = "/mrt-lx1/vis/"'projId'"/observationData"
!endif
!
! visDataClass reset to old default (odp data written to project account, 
! not to project subdirectory in herapool account), 2008-02-12 hwi. 
!
say "visData dir: "'visData'

intDate = 'obsDateCal'
intYear    = int(intDate/10000)
intMonth   = int((intDate-intYear*10000)/100)
intDay     = intDate-10000*intYear-100*intMonth
let year  = 'intYear'
let month = 'intMonth' /format i2.2
let day   = 'intDay' /format i2.2
let obsDateMira 'year'"-"'month'"-"'day'
!
! NEW: check polarimetry mode
!
inputFile = 'ncsData'"/"'obsDate'"/scans/"'idScan'"/iram30m-scan-"'obsDate'"s"'idScan'".xml"
sic output .pol1
say 'inputFile'" > .pol3"
sic output
system "paste .leaveMe .pol1 > .pol2"
system "chmod u+x .pol2"
system "./.pol2"
!
accept polmode /column .pol3 /format a64
polTemplate = "<TD>polarization</TD>"
!
doPolarimetry = polmode.eq.polTemplate
!
sic delete .pol1 .pol2 .pol3
!
if idCalScan.ne.0 then   ! a calibration scan is provided
 
   inputFile = 'ncsData'"/"'obsDateCal'"/scans/"'idCalScan'
   if .not.file(inputFile) then
      say "W-ODP: subdirectory with calibration scan does not exist."
      say "       Omitting calibration."
      idCalScan = 0
   else
      file in 'inputFile'
      if doPolarimetry then
         find /scan 'idCalScan' /observed 'obsDateMira' /backend vespa cont
      else
	 !! Find all the backends for calibration scan
!!         find /scan 'idCalScan' /observed 'obsDateMira' 
         find /scan 'idCalScan' /observed 'obsDateMira' /backend 'backendProcessed'
      endif
      scan idCalScan
      var scan
      var febe
      doCal = .true.
      if scan%header%nfebe.ge.4 then
         xpolString = 'febe3%header%febe'
         if xpolString.eq."E0R".or.xpolString.eq."e0r".or.xpolString.eq."E1R".or.xpolString.eq."e1r".or.xpolString.eq."E2R".or.xpolString.eq."e2r".or.xpolString.eq."E3R".or.xpolString.eq."e3r" then
            cal all
         endif
      endif
   endif
 
endif
 
inputFile = 'ncsData'"/"'obsDate'"/scans/"'idScan'
if .not.file(inputFile) then
   say " "
   say "E-ODP: subdirectory with scan does not exist: "
   say 'inputFile'
   say " "
   exit 
endif

! say "sleep 5s"
! system "sleep 5"

outputFile = 'visDataClass'"/mira"
if .not.file(outputFile) then
   say " "
   say "W-ODP: subdirectory for CLASS output file does not exist."
   say "       Reset to default (working directory)."
   say " "
   outputFile = "."
endif
!
! Output file with date and backend name
!
outputFile = 'outputFile'"/"'backendProcessed'"Odp"'obsDate'".30m"
say "output file:"'outputFile'

!! plotDir = 'visData'"/mira/plots"
plotDir = "/ncsServer/mrt/ncs/monitor/"'projId'"/observationData/mira/plots"
if .not.file(plotDir) then
   say " "
   say "W-ODP: subdirectory for plot files does not exist."
   say "       Reset to default (working directory)."
   say " "
   plotDir = "."
endif

intDate = 'obsDate'
intYear    = int(intDate/10000)
intMonth   = int((intDate-intYear*10000)/100)
intDay     = intDate-10000*intYear-100*intMonth
let year  = 'intYear'
let month = 'intMonth' /format i2.2
let day   = 'intDay' /format i2.2
let obsDateMira 'year'"-"'month'"-"'day'

file in 'inputFile'
if doPolarimetry then
   find /scan 'idScan' /observed 'obsDateMira' /backend vespa cont
else
   find /scan 'idScan' /observed 'obsDateMira' /backend 'backendProcessed' 
endif
!
! replaced file(outputFile) which did not work for big files
!
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

let tmpScanType 'scan%header%scantype'
for i 1 to scan%header%nfebe                          ! start of FeBe loop

    @ identFeBe 'i'
!    plotFile = 'plotDir'"/iram30m-"'beIdPlot'"-"'obsDate'"s"'idScan'".eps"
    plotFile = 'plotDir'"/iram30m-plot-"'obsDate'"s"'idScan'"-"'backendProcessed'"-"'i'".png"
    if file(plotFile) then
       sic delete 'plotFile'
    endif
 
    if scan%data%be[i].ge.10.and.scan%data%be[i].le.19 then  ! continuum data
 
       if tmpScanType.eq."calib" then           ! only display data

          view i /cal
!          hardcopy 'plotFile' /dev eps color /fitpage
          hardcopy 'plotFile' /dev png /fitpage
      
       else
 
          if doCal then
             cal i /gains /tcal /off aver ! 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 tmpScanType.eq."point" then
             define double aux /like data'i'%data%subscan
             let aux data'i'%data%subscan
             compute max max aux
             nsubscan = int(max)
             del /var aux
             solve i 
             hardcopy 'plotFile' /dev png /fitpage
!
! the following j-loop had to be introduced due to corresponding 
! changes in MIRA's subroutine SOLVE_POINTING_MIRA (and TelCal's pcross
! structure). The loop is for having the subscan plots even if there 
! are more than 4 subscans. 2006-05-30, H.W.
!
             for j 1 to nsubscan by 4
                 @p_plot_pointBySubscan 'i' 'j'
             next
             plotFileSubscan = -
        'plotDir'"/iram30m-plot-"'obsDate'"s"'idScan'"s"'nsubscan'"-"'i'".png"
             if file(plotFileSubscan) then
                sic delete 'plotFileSubscan'
             endif
             hardcopy 'plotFileSubscan' /dev png /fitpage
!	     lastPlotLink = 'plotDir'"/lastPlot.eps"
!	     system "rm -v "'lastPlotLink'
!	     system "ln -s "'plotFileSubscan'" "'lastPlotLink'             
             for j 1 to nsubscan 
                 m\write /febe i /subscan j
             next
         else if tmpScanType.eq."focus" then
             solve i 
             hardcopy 'plotFile' /dev png /fitpage
!	     lastPlotLink = 'plotDir'"/lastPlot.eps"
!	     system "rm -v "'lastPlotLink'
!	     system "ln -s "'plotFile'" "'lastPlotLink'             
         else if tmpScanType.eq."tip"   then
             solve i 
             hardcopy 'plotFile' /dev png /fitpage
         else
             view i
             hardcopy 'plotFile' /dev png /fitpage
!	     lastPlotLink = 'plotDir'"/lastPlot.eps"
!	     system "rm -v "'lastPlotLink'
!	     system "ln -s "'plotFile'" "'lastPlotLink'             
             m\write /febe i
         endif
 
       endif
 
    else                                                 ! spectroscopic data

      if tmpScanType.eq."calib" then        ! only display data
                                            ! if not xpol mode
         if i.eq.3 then
            xpolString = 'febe3%header%febe'
         else if i.eq.7 then
            xpolString = 'febe7%header%febe'
         else
            xpolString = "   "
         endif
	 if xpolString.eq."E0R".or.xpolString.eq."e0r".or.xpolString.eq."E1R".or.xpolString.eq."e1r".or.xpolString.eq."E2R".or.xpolString.eq."e2r".or.xpolString.eq."E3R".or.xpolString.eq."e3r" then
	    cal i-2 /gains /tcal /off
	    cal i-1 /gains /tcal /off
	    cal i /gains /tcal /off
	    cal i+1 /gains /tcal /off
            m\write /febe i-2 /pixel all 
            m\write /febe i-1 /pixel all 
            m\write /febe i /pixel all 
            m\write /febe i+1 /pixel all 
         endif
         view i
         hardcopy 'plotFile' /dev png /fitpage
      else
         if doCal then
	    if xpolString.eq."E0R".or.xpolString.eq."e0r".or.xpolString.eq."E1R".or.xpolString.eq."e1r".or.xpolString.eq."E2R".or.xpolString.eq."e2r".or.xpolString.eq."E3R".or.xpolString.eq."e3r" then
               cal all /gains /tcal /off /phase 
	    else
!               cal i /gains /tcal /off aver    !calibrate frontend-backend combination i (This uses all off-positions,not good for OTF?)
               cal i /gains /tcal /off equal    !calibrate frontend-backend combination i
	    endif
         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 'plotFile' /dev png /fitpage
         m\write /febe i /pixel all 
      endif
 
    endif

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

say "--- processing done, will move result files (if any)"
system "mvMiraResultFiles.py "'obsDate'" "'idScan'" "'backendProcessed'

say "--- onlineDataProcessing.mira: reached normal end"
exit
 
