- Made sinqhttprot go asynchronous properly, the library was hiding a

problem
- Cleaned up sanslirebin
This commit is contained in:
koennecke
2008-07-14 07:23:42 +00:00
parent 0eeaaaff9d
commit fa59313759
3 changed files with 235 additions and 117 deletions

167
sanslirebin.c Normal file
View File

@ -0,0 +1,167 @@
/**
* A special module for doing SANSLI rebinning. This is a fudge to make
* the data from the new detector electronics look like the old one.
*
* copyright: see file COPYRIGHT
*
* Mark Koennecke, May 2007
*/
#include <stdlib.h>
#include <assert.h>
#include <sics.h>
#include <splitter.h>
#include <math.h>
#include <sicsdata.h>
#include <motor.h>
#include "rebin.h"
/*---------------------------------------------------------------------------*/
static char correctionFilePath[512];
static float runde(float v){
float ip, rund;
rund = modff(v,&ip);
if(rund > .5){
ip += 1.;
}
return ip;
}
/*---------------------------------------------------------------------------*/
static int loadCorrectionData(SConnection *pCon, double *xPos,
double *yPos, int length){
pMotor dist = NULL;
FILE *fd = NULL;
float distance;
char filename[1024];
int i;
dist = (pMotor)FindCommandData(pServ->pSics,"dz","Motor");
if(dist == NULL){
SCWrite(pCon,"ERROR: dz motor not found!, cannot correct",eError);
return 0;
}
MotorGetSoftPosition(dist,pCon,&distance);
snprintf(filename,1023,"%s/%1.1dm-targetpos-final.txt",
correctionFilePath,(int)runde(distance));
fd = fopen(filename,"r");
if(fd == NULL){
SCWrite(pCon,"ERROR: correction file not found!",eError);
SCWrite(pCon,filename,eError);
return 0;
}
for(i = 0; i < length; i++){
fscanf(fd,"%lf %lf", xPos+i, yPos+i);
}
fclose(fd);
return 1;
}
/*---------------------------------------------------------------------------*/
int SansliRebin(SConnection *pCon,SicsInterp *pSics, void *pData,
int argc, char *argv[]){
pSICSData target = NULL, hmData = NULL;
pNXDS dataset = NULL, weights = NULL;
int iDim[2], ix, iy, pos, ival, xDetDim[2], yDetDim[2], nDetX, nDetY, posIdx;
long totalCounts = 0;
double x, y, val, *xPos = NULL, *yPos = NULL, corrSum = .0, doubleCounts;
float detectorDistance;
if(argc < 2){
SCWrite(pCon,"ERROR: Not enough arguments",eError);
return 0;
}
xDetDim[0] = 108;
xDetDim[1] = 413;
nDetX = xDetDim[1] - xDetDim[0];
yDetDim[0] = 155;
yDetDim[1] = 365;
nDetY = yDetDim[1] - yDetDim[0];
hmData = (pSICSData)FindCommandData(pSics,argv[1],"SICSData");
if(hmData == NULL){
SCWrite(pCon,"ERROR: histogram memory data not found",eError);
return 0;
}
target = (pSICSData)FindCommandData(pSics,argv[2],"SICSData");
if(target == NULL){
SCWrite(pCon,"ERROR: target sicsdata not found",eError);
return 0;
}
iDim[0] = 128;
iDim[1] = 128;
dataset = createNXDataset(2,NX_FLOAT64, iDim);
weights = createNXDataset(2,NX_FLOAT64, iDim);
xPos = malloc(nDetX*nDetY*sizeof(double));
yPos = malloc(nDetX*nDetY*sizeof(double));
if(dataset == NULL || weights == NULL || xPos == NULL || yPos == NULL){
SCWrite(pCon,"ERROR: out of memory allocating temporary data",eError);
return 0;
}
memset(xPos,0,nDetX*nDetY*sizeof(double));
memset(yPos,0,nDetX*nDetY*sizeof(double));
if(!loadCorrectionData(pCon,xPos, yPos,nDetX*nDetY)){
dropNXDataset(dataset);
dropNXDataset(weights);
free(xPos);
free(yPos);
return 0;
}
for(ix = xDetDim[0]; ix < xDetDim[1]; ix++){
for(iy = yDetDim[0]; iy < yDetDim[1]; iy++){
posIdx = (iy-yDetDim[0])*nDetX + (ix - xDetDim[0]);
x = yPos[posIdx];
y = xPos[posIdx];
getSICSDataInt(hmData,512*iy + ix,&ival);
totalCounts += ival;
val = (double)ival;
rebinPoint2D(dataset,x,y,val,TRILINEAR);
rebinPoint2D(weights,x,y,1.0,TRILINEAR);
}
}
pos = 0;
clearSICSData(target);
if(argc > 3) {
setSICSDataInt(target,0,128);
setSICSDataInt(target,1,128);
pos = 2;
}
/*
* apply weights
*/
for(ix = 0; ix < 128*128; ix++){
if(weights->u.dPtr[ix] > .0){
dataset->u.dPtr[ix] /= weights->u.dPtr[ix];
}
corrSum += dataset->u.dPtr[ix];
}
doubleCounts = (double)totalCounts;
for(ix = 0; ix < 128*128; ix++, pos++){
if(corrSum > .01){
val = floor(dataset->u.dPtr[ix]*doubleCounts/corrSum);
} else {
val = .0;
}
setSICSDataInt(target,pos,(int)val);
}
dropNXDataset(dataset);
dropNXDataset(weights);
free(xPos);
free(yPos);
SCSendOK(pCon);
return 1;
}
/*---------------------------------------------------------------------------*/
int MakeSansliRebin(SConnection *pCon,SicsInterp *pSics, void *pData,
int argc, char *argv[]){
if(argc < 2){
SCWrite(pCon,"ERROR: need path to correction files as a parameter",eError);
return 0;
}
strncpy(correctionFilePath,argv[1],512);
AddCommand(pSics,"sanslirebin", SansliRebin,NULL,NULL);
SCSendOK(pCon);
return 1;
}

View File

@ -1,71 +0,0 @@
/**
* A special module for doing SANSLI rebinning. This is a fudge to make
* the data from the new detector electronics look like the old one.
*
* copyright: see file COPYRIGHT
*
* Mark Koennecke, May 2007
*/
<%! source ../sicstemplates.tcl %>
<% stdIncludes %>
#include <math.h>
#include <sicsdata.h>
#include "rebin.h"
/*---------------------------------------------------------------------------*/
<% makeSicsFunc SansliRebin%>{
pSICSData target = NULL, hmData = NULL;
pNXDS dataset = NULL;
int iDim[2], ix, iy, pos, ival;
double x, y, val;
<% testNoPar 2 4 %>
hmData = (pSICSData)FindCommandData(pSics,argv[1],"SICSData");
if(hmData == NULL){
SCWrite(pCon,"ERROR: histogram memory data not found",eError);
return 0;
}
target = (pSICSData)FindCommandData(pSics,argv[2],"SICSData");
if(target == NULL){
SCWrite(pCon,"ERROR: target sicsdata not found",eError);
return 0;
}
iDim[0] = 128;
iDim[1] = 128;
dataset = createNXDataset(2,NX_FLOAT64, iDim);
if(dataset == NULL){
SCWrite(pCon,"ERROR: out of memory allocating temporary data",eError);
return 0;
}
for(ix = 113; ix < 415; ix++){
for(iy = 153; iy < 363; iy++){
x = ((double)ix - 113.)/2.359;
y = ((double)iy - 153.)/1.640;
getSICSDataInt(hmData,512*iy + ix,&ival);
val = (double)ival;
rebinPoint2D(dataset,x,y,val,TRILINEAR);
}
}
pos = 0;
clearSICSData(target);
if(argc > 3) {
setSICSDataInt(target,0,128);
setSICSDataInt(target,1,128);
pos = 2;
}
for(ix = 0; ix < 128*128; ix++, pos++){
setSICSDataInt(target,pos,(int)floor(dataset->u.dPtr[ix] +.5));
}
dropNXDataset(dataset);
SCSendOK(pCon);
return 1;
}
/*---------------------------------------------------------------------------*/
<% makeSicsFunc MakeSansliRebin%>{
AddCommand(pSics,"sanslirebin", SansliRebin,NULL,NULL);
SCSendOK(pCon);
return 1;
}

View File

@ -55,12 +55,47 @@ static int configRequest(Ascon *a){
return 1;
}
/*---------------------------------------------------------------------*/
static void handleReply(Ascon *a){
char *pPtr = NULL, *pType = NULL;
int len, i, *dataPtr = NULL;
HistInt *hmData = NULL;
pHttpProt pHttp = (pHttpProt)a->private;
pPtr = ghttp_get_body(pHttp->request);
len = ghttp_get_body_len(pHttp->request);
if(strstr(pPtr,"ERROR") != NULL){
AsconError(a,pPtr, a->state);
DynStringConcat(a->rdBuffer,pPtr);
} else if(strstr(pPtr,"Authentication Error") != NULL){
DynStringConcat(a->rdBuffer,pPtr);
AsconError(a,pPtr, a->state);
} else {
pType = (char *)ghttp_get_header(pHttp->request,"Content-Type");
if(strstr(pType,"sinqhm") == NULL){
/* text data */
for(i = 0; i < len; i++){
DynStringConcatChar(a->rdBuffer, pPtr[i]);
}
} else {
hmData = (HistInt *)pPtr;
clearSICSData(pHttp->binData);
len = len/sizeof(HistInt);
dataPtr = getSICSDataPointer(pHttp->binData, 0, len);
for(i = 0; i < len; i++){
dataPtr[i] = htonl(hmData[i]);
}
assignSICSType(pHttp->binData, 0, len, INTTYPE);
DynStringCopy(a->rdBuffer,"SICSDATA");
}
}
}
/*---------------------------------------------------------------------*/
static int HttpHandler(Ascon *a) {
ghttp_status status;
pHttpProt pHttp = (pHttpProt)a->private;
char *pPtr = NULL, *pType = NULL;
HistInt *hmData = NULL;
int i, len, *dataPtr;
int socke, selStat;
fd_set rmask;
struct timeval tmo = {0,0};
switch (a->state) {
case AsconConnectStart:
@ -92,19 +127,38 @@ static int HttpHandler(Ascon *a) {
DynStringClear(a->rdBuffer);
break;
case AsconReadStart:
socke = ghttp_get_socket(pHttp->request);
FD_ZERO(&rmask);
FD_SET(socke,&rmask);
selStat = select(socke+1,&rmask, NULL, NULL, &tmo);
if(selStat != 0){
status = ghttp_process(pHttp->request);
a->state = AsconReading;
} else {
if(DoubleTime() > a->start + a->timeout){
AsconError(a," read timeout", 0);
a->state = AsconTimeout;
/* this to clear the line */
ghttp_close(pHttp->request);
}
}
return 1;
break;
case AsconReading:
status = ghttp_process(pHttp->request);
switch(status){
case ghttp_not_done:
if(DoubleTime() > a->start + a->timeout){
AsconError(a," read timeout", 0);
a->state = AsconTimeout;
/* this to clear the line */
ghttp_close(pHttp->request);
}
return 1;
if(DoubleTime() > a->start + a->timeout){
AsconError(a," read timeout", 0);
a->state = AsconTimeout;
/* this to clear the line */
ghttp_close(pHttp->request);
}
return 1;
case ghttp_done:
a->state = AsconReading;
break;
handleReply(a);
a->state = AsconReadDone;
break;
case ghttp_error:
/*
* A first error may not be an error but a
@ -118,42 +172,10 @@ static int HttpHandler(Ascon *a) {
DynStringConcat(a->rdBuffer,"Server error");
a->state = AsconReadDone;
}
return 1;
break;
default:
printf("Hugo\n");
break;
}
break;
case AsconReading:
pPtr = ghttp_get_body(pHttp->request);
len = ghttp_get_body_len(pHttp->request);
if(strstr(pPtr,"ERROR") != NULL){
AsconError(a,pPtr, a->state);
DynStringConcat(a->rdBuffer,pPtr);
} else if(strstr(pPtr,"Authentication Error") != NULL){
DynStringConcat(a->rdBuffer,pPtr);
AsconError(a,pPtr, a->state);
} else {
pType = (char *)ghttp_get_header(pHttp->request,"Content-Type");
if(strstr(pType,"sinqhm") == NULL){
/* text data */
for(i = 0; i < len; i++){
DynStringConcatChar(a->rdBuffer, pPtr[i]);
}
} else {
hmData = (HistInt *)pPtr;
clearSICSData(pHttp->binData);
len = len/sizeof(HistInt);
dataPtr = getSICSDataPointer(pHttp->binData, 0, len);
for(i = 0; i < len; i++){
dataPtr[i] = htonl(hmData[i]);
}
assignSICSType(pHttp->binData, 0, len, INTTYPE);
DynStringCopy(a->rdBuffer,"SICSDATA");
}
}
a->state = AsconReadDone;
break;
break;
default:
return AsconStdHandler(a);
}