/*-------------------------------------------------------------------------
                             A M O R S C A N

 An adaption of the general scan routine to deal with special issues at
 the reflectometer AMOR at SINQ.

 copyright: see copyright.h
 
 Mark Koennecke, September 1999
--------------------------------------------------------------------------*/
#include <stdlib.h>
#include <assert.h>
#include "fortify.h"
#include "sics.h"
#include "scan.h"
#include "scan.i"
#include "HistMem.h"
#include "nxamor.h"
#include "amorscan.h"

/*--------------------------------------------------------------------*/ 
  static int AmorHeader(pScanData self)
  {
     return WriteAmorHeader(self->pFile, self->pCon);
  }
/*--------------------------------------------------------------------*/
  static int AmorPoints(pScanData self, int iP)
  {
    /* write only at last scan point */
    if((iP+1) >= self->iNP)
    {
        return WriteAmorScan(self->pFile,self->pCon,self);
    }
  } 
/*--------------------------------------------------------------------*/
  static int AmorCollect(pScanData self, int iP)
  {
     pVarEntry pVar = NULL;
     void *pDings;
     int i, iRet, status;
     float fVal;
     char pStatus[512], pItem[20];
     char pHead[512];
     CountEntry sCount;

     assert(self);
     assert(self->pCon);
     
     /* prepare output header */
     sprintf(pHead,"%-5.5s","NP");
     sprintf(pStatus,"%-5d",iP);
 
     /* loop over all scan variables */
     status = 1;
     for(i = 0; i < self->iScanVar; i++)
     {
        DynarGet(self->pScanVar,i,&pDings);
        pVar = (pVarEntry)pDings;
        if(pVar)
        {
           fVal = pVar->pInter->GetValue(pVar->pObject,self->pCon);
           pVar->fData[iP] = fVal;
           sprintf(pItem,"%-10.10s",pVar->Name);
           strcat(pHead,pItem);
           sprintf(pItem,"%-10.3f",fVal);
           strcat(pStatus,pItem);
        }
     }
  
     /* store counter data */
     /* monitors */
     for(i = 1; i < 10; i++)
     {
        sCount.Monitors[i-1] = GetMonitor((pCounter)self->pCounterData,i,
                                        self->pCon);
     }
     if( self->iChannel != 0 && self->iChannel != -10 )
     {
       sCount.Monitors[self->iChannel - 1] = 
           GetCounts((pCounter)self->pCounterData,
                                                   self->pCon);     
     }
     /* counter1 */
     strcat(pHead,"Counter1      ");
     sCount.lCount = GetCounts((pCounter)self->pCounterData,self->pCon);
     sprintf(pItem,"%-15d",sCount.lCount);
     strcat(pStatus,pItem);

     /*
           WARNING
           Assignements have to be checked when the Schlumpfes are
           ready putting the counter box together.
     */

     /* counter2 */
     strcat(pHead,"Counter2      ");
     sCount.Monitors[0] = GetMonitor((pCounter)self->pCounterData,
                                    1,self->pCon);
     sprintf(pItem,"%-15d",sCount.Monitors[0]);
     strcat(pStatus,pItem);

     /* monitors */
     sCount.Monitors[3] = GetMonitor((pCounter)self->pCounterData,
                                     2,self->pCon);
     sCount.Monitors[4] = GetMonitor((pCounter)self->pCounterData,
                                     3,self->pCon);

     /* get time */
     sCount.fTime = GetCountTime((pCounter)self->pCounterData,
                                 self->pCon);
     strcat(pHead,"Monitor1    ");
     sprintf(pItem,"%-12d",sCount.Monitors[3]);
     strcat(pStatus,pItem);
     strcat(pHead,"Monitor2    ");
     sprintf(pItem,"%-12d",sCount.Monitors[4]);
     strcat(pStatus,pItem);
     strcat(pHead,"Time  ");
     sprintf(pItem,"%-6.1f",sCount.fTime);
     strcat(pStatus,pItem);

     /* write progress */
     strcat(pHead,"\n");
     strcat(pStatus,"\n");
     SCWrite(self->pCon,pHead,eWarning);
     SCWrite(self->pCon,pStatus,eWarning);
     
     /* stow away */
     DynarReplace(self->pCounts,self->iCounts,&sCount,sizeof(CountEntry));
     self->iCounts++;
     return 1;
  }
/*-----------------------------------------------------------------------*/
  int ConfigureAmor(pScanData self)
  {
      self->WriteHeader = AmorHeader;
      self->WriteScanPoints = AmorPoints;
      self->CollectScanData = AmorCollect;
      strcpy(self->ext,".hdf");
      return 1; 
  }
