- Changed FOCUS to writing data files through nxscript
SKIPPED: psi/faverage.c psi/fowrite.c psi/psi.c psi/swmotor2.c
This commit is contained in:
398
fomerge.c
398
fomerge.c
@ -15,11 +15,20 @@
|
||||
|
||||
|
||||
Mark Koennecke, March 2000
|
||||
|
||||
extended to support nxscripted file writing: Mark Koennecke, May 2004
|
||||
--------------------------------------------------------------------------*/
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "sics.h"
|
||||
#include "nxscript.h"
|
||||
#include "HistMem.h"
|
||||
#include "fortify.h"
|
||||
#include "scan.h"
|
||||
#include "fitcenter.h"
|
||||
|
||||
static pFit fitter = NULL;
|
||||
|
||||
/* change this in line with HistMem.h */
|
||||
|
||||
@ -402,5 +411,394 @@ int getFMdim(int which)
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
int InstallFocusMerge(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int status;
|
||||
char pBueffel[256];
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Insufficient arguments to InstallFocusMerge",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
status = initializeFM(argv[1]);
|
||||
if(!status)
|
||||
{
|
||||
snprintf(pBueffel,255,"ERROR: failed to read mergefile %s",
|
||||
argv[1]);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Install command */
|
||||
AddCommand(pSics,"focusmerge",FocusMergeAction,NULL,NULL);
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static pNXScript checkNXScript(SicsInterp *pSics, char *name)
|
||||
{
|
||||
pNXScript result = NULL;
|
||||
|
||||
result = FindCommandData(pSics,name,"NXScript");
|
||||
if(result == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if(result->fileHandle == NULL || result->dictHandle == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static int updateHMFMData(SicsInterp *pSics, SConnection *pCon)
|
||||
{
|
||||
int status, iTime;
|
||||
const float *fTimeBin = NULL;
|
||||
HistInt *data = NULL;
|
||||
pHistMem pMem = NULL;
|
||||
|
||||
pMem = (pHistMem)FindCommandData(pSics,"hm2","HistMem");
|
||||
if(pMem == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
fTimeBin = GetHistTimeBin(pMem,&iTime);
|
||||
setFMDataPointer(GetHistogramPointer(pMem,pCon),iTime,MIDDLE);
|
||||
|
||||
pMem = (pHistMem)FindCommandData(pSics,"hm1","HistMem");
|
||||
if(pMem == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
setFMDataPointer(GetHistogramPointer(pMem,pCon),iTime,LOWER);
|
||||
|
||||
pMem = (pHistMem)FindCommandData(pSics,"hm3","HistMem");
|
||||
if(pMem == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
setFMDataPointer(GetHistogramPointer(pMem,pCon),iTime,UPPER);
|
||||
setFMconfiguration(1,1,1);
|
||||
return 1;
|
||||
}
|
||||
/*-------------------------------------------------------------------*/
|
||||
static int *calculateSum(HistInt *data, int iDet, int iTime)
|
||||
{
|
||||
int i, j, iIndex;
|
||||
int *sum = NULL;
|
||||
|
||||
sum = (int *)malloc(iDet*sizeof(int));
|
||||
if(!sum)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memset(sum,0,iDet*sizeof(int));
|
||||
|
||||
for(i = 0; i < iDet; i++)
|
||||
{
|
||||
iIndex = i * iTime;
|
||||
for(j = 0; j < iTime; j++)
|
||||
{
|
||||
sum[i] += data[iIndex+j];
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static int putSum(SicsInterp *pSics, SConnection *pCon,
|
||||
pNXScript nxscript, char *name, char *alias)
|
||||
{
|
||||
HistInt *data = NULL;
|
||||
HistInt *sum = NULL;
|
||||
int iDet, iTime, i, j, iIndex, status;
|
||||
|
||||
iTime = getFMdim(TIMEBIN);
|
||||
if(strcmp(name,"upper") == 0)
|
||||
{
|
||||
iDet = getFMdim(UPPER);
|
||||
data = getFMBankPointer(UPPER);
|
||||
}
|
||||
else if(strcmp(name,"middle") == 0)
|
||||
{
|
||||
iDet = getFMdim(MIDDLE);
|
||||
data = getFMBankPointer(MIDDLE);
|
||||
}
|
||||
else if(strcmp(name,"lower") == 0)
|
||||
{
|
||||
iDet = getFMdim(LOWER);
|
||||
data = getFMBankPointer(LOWER);
|
||||
}
|
||||
else if(strcmp(name,"merged") == 0)
|
||||
{
|
||||
iDet = getFMdim(MERGED);
|
||||
data = getFMBankPointer(MERGED);
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: detector bank to sum not recognised",eError);
|
||||
return NX_ERROR;
|
||||
}
|
||||
|
||||
sum = calculateSum(data,iDet,iTime);
|
||||
if(!sum)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: out of memory summing bank",eError);
|
||||
return NX_ERROR;
|
||||
}
|
||||
|
||||
status = NXDputalias(nxscript->fileHandle,nxscript->dictHandle,
|
||||
alias,sum);
|
||||
free(sum);
|
||||
return status;
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static int putElastic(SicsInterp *pSics, SConnection *pCon,
|
||||
pNXScript pNexus, char *alias, float fElastic)
|
||||
{
|
||||
|
||||
int status, iTime, iDet;
|
||||
const float *fTimeBin = NULL;
|
||||
int *sum = NULL;
|
||||
pHistMem pMem = NULL;
|
||||
float fCenter, fFWHM, fStdDev, fVal;
|
||||
|
||||
pMem = (pHistMem)FindCommandData(pSics,"hm2","HistMem");
|
||||
if(pMem == NULL)
|
||||
{
|
||||
SCWrite(pCon,
|
||||
"ERROR: need middle detector bank for elastic peak calculation",
|
||||
eError);
|
||||
return NX_ERROR;
|
||||
}
|
||||
fTimeBin = GetHistTimeBin(pMem,&iTime);
|
||||
iDet = getFMdim(MIDDLE);
|
||||
sum = calculateSum(GetHistogramPointer(pMem,pCon),iDet,iTime);
|
||||
if(!sum)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: out of memory calculating elastic peak position",
|
||||
eError);
|
||||
return NX_ERROR;
|
||||
}
|
||||
if(fitter == NULL)
|
||||
{
|
||||
fitter = CreateFitCenter(NULL);
|
||||
if(!fitter)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: cannot allocate fitting structure",eError);
|
||||
return NX_ERROR;
|
||||
}
|
||||
}
|
||||
status = CalculateFitFromData(fitter,fTimeBin,sum,iTime);
|
||||
if(status != 1)
|
||||
{
|
||||
SCWrite(pCon,"WARNING: problem locating elastic peak",eWarning);
|
||||
}
|
||||
GetFitResults(fitter,&fCenter,&fStdDev,&fFWHM,&fVal);
|
||||
fVal = fCenter - fElastic;
|
||||
if(fVal < 0.)
|
||||
fVal = - fVal;
|
||||
/* bad value, leave at theoretical value */
|
||||
if(fVal > 10.)
|
||||
{
|
||||
SCWrite(pCon,
|
||||
"WARNING: bad fit result, using theoretical elastic peak position",
|
||||
eWarning);
|
||||
}
|
||||
else
|
||||
{
|
||||
fElastic = fCenter;
|
||||
}
|
||||
free(sum);
|
||||
|
||||
status = NXDputalias(pNexus->fileHandle, pNexus->dictHandle,alias,
|
||||
&fElastic);
|
||||
return status;
|
||||
|
||||
}
|
||||
/*-----------------------------------------------------------------------
|
||||
Usage:
|
||||
focusmerge puttwotheta nxscriptmod bankname alias
|
||||
focusmerge putmerged nxscriptmod alias
|
||||
focusmerge putsum nxscriptmod bankname alias
|
||||
focusmerge putelastic nxscriptmod alias theoelastic
|
||||
|
||||
nxscriptmod = name of the nxscript module used for writing, must be open
|
||||
alias = The alias under which to write the data item
|
||||
theoelastic = theoretical elastic peak position
|
||||
------------------------------------------------------------------------*/
|
||||
int FocusMergeAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int status;
|
||||
pNXScript pNexus = NULL;
|
||||
float fElastic;
|
||||
char pNum[20];
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Insufficient arguments to focusmerge",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
strtolower(argv[1]);
|
||||
if(strcmp(argv[1],"puttwotheta") == 0)
|
||||
{
|
||||
if(argc < 4)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Insufficient arguments to focusmerge puttwotheta",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNexus = checkNXScript(pSics, argv[2]);
|
||||
if(pNexus == NULL)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: bad nxscript name or NeXus file not open",eError);
|
||||
return 0;
|
||||
}
|
||||
strtolower(argv[3]);
|
||||
if(strcmp(argv[3],"upper") == 0)
|
||||
{
|
||||
status = NXDputalias(pNexus->fileHandle,pNexus->dictHandle,
|
||||
argv[4],getFMBankTheta(UPPER));
|
||||
}
|
||||
else if(strcmp(argv[3],"middle") == 0)
|
||||
{
|
||||
status = NXDputalias(pNexus->fileHandle,pNexus->dictHandle,
|
||||
argv[4],getFMBankTheta(MIDDLE));
|
||||
}
|
||||
else if(strcmp(argv[3],"lower") == 0)
|
||||
{
|
||||
status = NXDputalias(pNexus->fileHandle,pNexus->dictHandle,
|
||||
argv[4],getFMBankTheta(LOWER));
|
||||
}
|
||||
else if(strcmp(argv[3],"merged") == 0)
|
||||
{
|
||||
status = NXDputalias(pNexus->fileHandle,pNexus->dictHandle,
|
||||
argv[4],getFMBankTheta(MERGED));
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: requested two_theta for invalid detector bank",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
if(status == NX_OK)
|
||||
{
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: failed to write two theta array to file",eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if(strcmp(argv[1],"putmerged") == 0 )
|
||||
{
|
||||
if(argc < 4)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Insufficient arguments to focusmerge",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNexus = checkNXScript(pSics, argv[2]);
|
||||
if(pNexus == NULL)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: bad nxscript name or NeXus file not open",eError);
|
||||
return 0;
|
||||
}
|
||||
if(!updateHMFMData(pSics, pCon))
|
||||
{
|
||||
SCWrite(pCon,"ERROR: not enough HM's to merge or bad names in fomerge.c",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
snprintf(pNum,19,"%d",getFMdim(MERGED));
|
||||
NXDupdate(pNexus->dictHandle,"noofdetectors",pNum);
|
||||
snprintf(pNum,19,"%d",getFMdim(TIMEBIN));
|
||||
NXDupdate(pNexus->dictHandle,"timebin",pNum);
|
||||
status = NXDputalias(pNexus->fileHandle,pNexus->dictHandle,
|
||||
argv[3],getFMBankPointer(MERGED));
|
||||
if(status == NX_OK)
|
||||
{
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: failed to write merged data to file",eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
else if(strcmp(argv[1],"putsum") == 0 )
|
||||
{
|
||||
if(argc < 4)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Insufficient arguments to focusmerge putsum",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNexus = checkNXScript(pSics, argv[2]);
|
||||
if(pNexus == NULL)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: bad nxscript name or NeXus file not open",eError);
|
||||
return 0;
|
||||
}
|
||||
updateHMFMData(pSics, pCon);
|
||||
status = putSum(pSics,pCon,pNexus,argv[3],argv[4]);
|
||||
if(status == NX_OK)
|
||||
{
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: failed to write summed data to file",eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if(strcmp(argv[1],"putelastic") == 0 )
|
||||
{
|
||||
if(argc < 4)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Insufficient arguments to focusmerge putelastic",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
pNexus = checkNXScript(pSics, argv[2]);
|
||||
if(pNexus == NULL)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: bad nxscript name or NeXus file not open",eError);
|
||||
return 0;
|
||||
}
|
||||
fElastic = atof(argv[4]);
|
||||
status = putElastic(pSics,pCon,pNexus,argv[3],fElastic);
|
||||
if(status == NX_OK)
|
||||
{
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: failed to write elastic peak position",eError);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SCWrite(pCon,"ERROR: subcommand to focusmerge not understood",eError);
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user