/****************************************************************************** File name: jemx_shared_imod.c Version: see below Component: Developer: Peter Kretschmar, ISDC Peter.Kretschmar@obs.unige.ch Purpose: Utility functions to access JEM-X Instrument Model (IMOD) data. *****************************************************************************/ /* * Revision 0.2.0 2004/01/23 * First draft * */ #include "jemx_shared.h" /***************************************************************************** Function: JEMXdetectorGetMap Description: Find the detector map for the given JEM-X instrument in the instrument model group and read in the data Error codes: 0 == ISDC_OK ==> function executed with no errors < 0 (various) ==> ISDC system function error code, passed on Parameter arguments: name range I/O description instModGrp !=NULL input handle to instrument model group jemxInstr JMX1 or JMX2 input instrument number detMap (array) output 2D array with DETE-MOD values onAxisArea >0.0 output active area for an on-axis source chatter 0 to 5 input verbosity control variable callingStatus any input error status on input *****************************************************************************/ int JEMXdetectorGetMap(dal_element *instModGrp, Instrument jemxInstr, unsigned short detMap[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], double *onAxisArea, int chatter, int callingStatus) { /* * Local variable definitions */ int status = ISDC_OK; /* local status flag */ dal_element *detModPtr = NULL; /* handle to JMXi-DETE-MOD */ void *buffer = NULL; long dimAxes[2]={J_NUM_X_PIXELS,J_NUM_Y_PIXELS}; long ix=0, iy=0; char detModName[14]; /* exit function immediately if status is not OK on entry */ if(callingStatus != ISDC_OK) return(callingStatus); switch(jemxInstr) { case JMX1: strcpy(detModName,"JMX1"); break; case JMX2: strcpy(detModName,"JMX1"); break; default: status = JEMX_SHARED_ILLEGAL_DETECTOR_NUMBER; RILlogMessage(NULL,Error_1, "JEM-X instrument number %i impossible!",jemxInstr); break; } strcat(detModName,J_DETECTOR_DS); status = JEMXarrayGet(instModGrp,detModName,DAL_USHORT, 2,dimAxes,&detModPtr,&buffer, chatter,status); if (ISDC_OK!=status) { return(status); } /* Map 1D transfer buffer to 2D array */ for (ix=0; ix function executed with no errors < 0 (various) ==> ISDC system function error code, passed on Parameter arguments: name range I/O description instModGrp !=NULL input handle to instrument model group jemxInstr JMX1 or JMX2 input instrument number detCorX (array) output 2D array with corrected X positions detCorY (array) output 2D array with corrected Y positions chatter 0 to 5 input verbosity control variable callingStatus any input error status on input *****************************************************************************/ int JEMXdetectorGetCorXY(dal_element *instModGrp, Instrument jemxInstr, double detCorX[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], double detCorY[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], int chatter, int callingStatus) { /* * Local variable definitions */ int status = ISDC_OK; /* local status flag */ dal_element *detCorPtr = NULL; /* handle to JMXi-DETE-MOD */ void *buffer = NULL; long dimAxes[2]={J_NUM_X_PIXELS,J_NUM_Y_PIXELS}; long ix=0, iy=0; char detCorName[14]; /* exit function immediately if status is not OK on entry */ if(callingStatus != ISDC_OK) return(callingStatus); switch(jemxInstr) { case JMX1: strcpy(detCorName,"JMX1"); break; case JMX2: strcpy(detCorName,"JMX1"); break; default: status = JEMX_SHARED_ILLEGAL_DETECTOR_NUMBER; RILlogMessage(NULL,Error_1, "JEM-X instrument number %i impossible!",jemxInstr); break; } /* Get CORX table */ strcpy(detCorName+4,J_CORX_DS); status = JEMXarrayGet(instModGrp,detCorName,DAL_DOUBLE, 2,dimAxes,&detCorPtr,&buffer, chatter,status); if (ISDC_OK!=status) { return(status); } /* Map 1D transfer buffer to 2D array */ for (ix=0; ix function executed with no errors < 0 (various) ==> ISDC system function error code, passed on Parameter arguments: name range I/O description detCorX (array) input 2D array with corrected X positions detCorY (array) input 2D array with corrected Y positions pixelArea (array) output 2D array with effective sizes of pixels pixelLowLeftX (array) output 2D array with pixel lower left X pos. pixelLowLeftY (array) output 2D array with pixel lower left Y pos. chatter 0 to 5 input verbosity control variable callingStatus any input error status on input *****************************************************************************/ int JEMXdetectorPixelAreas(double detCorX[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], double detCorY[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], double pixelArea[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], double pixelLowLeftX[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], double pixelLowLeftY[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], int chatter, int callingStatus) { /* * Local variable definitions */ int status = ISDC_OK; /* local status flag */ /* * * Pixel (x,y) with corners: * * * (x-1,y+1) (x,y+1) (x+1,y+1) * * (ulCorner) (urCorner) * * (x-1,y) (x,y) (x+1,y) * * (llCorner) (lrCorner) * * (x-1,y-1) (x,y-1) (x+1,y-1) * */ struct pixelCorner { double x; double y; } llCorner, ulCorner, lrCorner, urCorner; double height = 0.0; double width = 0.0; double rawRadius = 0.0; double sumArea = 0.0; long ix=0, iy=0; /* exit function immediately if status is not OK on entry */ if(callingStatus != ISDC_OK) return(callingStatus); /* * Loop over all detector pixels. * Set corners as mean of surrounding pixel coordinates. * Calculate pixel area assuming a rectangle */ for ( ix=1 ; ix function executed with no errors < 0 (various) ==> ISDC system function error code, passed on Parameter arguments: name range I/O description instModGrp !=NULL input handle to instrument model group jemxInstr JMX1 or JMX2 input instrument number detector (struct) output important detector data chatter 0 to 5 input verbosity control variable callingStatus any input error status on input *****************************************************************************/ int JEMXdetectorGetData(dal_element *instModGrp, Instrument jemxInstr, JEMXdetectorData *detector, int chatter, int callingStatus) { /* * Local variable definitions */ int status = ISDC_OK; /* local status flag */ /* exit function immediately if status is not OK on entry */ if(callingStatus != ISDC_OK) return(callingStatus); /* read in pixel status information */ status = JEMXdetectorGetMap(instModGrp,jemxInstr, detector->detMap,&(detector->area), chatter,status); if (ISDC_OK!=status) { RILlogMessage(NULL,Error_2, "Error %d reading detector map",status); return(status); } /* read in the corrected pixel positions */ status = JEMXdetectorGetCorXY(instModGrp,jemxInstr, detector->CorX,detector->CorY, chatter,status); if (ISDC_OK!=status) { RILlogMessage(NULL,Error_2, "Error %d reading CORX/CORY tables",status); return(status); } /* derive the areas per pixel */ status = JEMXdetectorPixelAreas(detector->CorX,detector->CorY, detector->pixelArea, detector->pixelLowerLeftX, detector->pixelLowerLeftY, chatter,status); return(status); } /***************************************************************************** Function: JEMXdetectorCalcUsedArea Description: Calculate the overall used area within the detector based on the given radius limit and excluding bad pixels. Error codes: 0 == ISDC_OK ==> function executed with no errors < 0 (various) ==> ISDC system function error code, passed on Parameter arguments: name range I/O description detector (struct) input detector description radiusLimit >=0.0 input radius in mm up to which events are used excludeFlags >=0 input holds bit flags excluding pixels area >=0.0 output effectively used area in cm^2 usedMap (array) output chatter 0 to 5 input verbosity control variable callingStatus any input error status on input *****************************************************************************/ int JEMXdetectorCalcOnAxisArea(JEMXdetectorData detector, double radiusLimit, unsigned short excludeFlags, double *area, unsigned short usedMap[J_NUM_X_PIXELS][J_NUM_Y_PIXELS], int chatter, int callingStatus) { /* * Local variable definitions */ int status = ISDC_OK; /* local status flag */ unsigned short excludePixel = 0; long ix=0, iy=0, usedPixels=0; double radius=0.0; /* exit function immediately if status is not OK on entry */ if(callingStatus != ISDC_OK) return(callingStatus); /* * Loop over all detector pixels. * Exclude those with certain bits set. */ *area = 0.0; usedPixels = 0; for ( ix=1 ; ix