N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/Vaike/linux/system-addons/drivers/raw_radiation.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by LANL/LANS pelowitz                              *
00003  *   pelowitz.lanl.gov                                                     *
00004  *                                                                         *
00005  *   Code modifications: Balakumar J. Balasubramaniam (bbalasub@gmail.com) *
00006  *                       Ernst Ingo-Esch (ernst@lanl.gov)                  *
00007  *                                                                         *
00008  *   This program is not free software; you cannot redistribute it         *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   any later version.                                                    *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  ***************************************************************************/
00023 
00024 //THIS VERSION Balakumar Balasubramaniam J
00025 // Last updated: 26 October, 2007
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <time.h>
00035 #include <sys/types.h> 
00036 #include <sys/stat.h> 
00037 //#include <signal.h>
00038 //#include <unistd.h>
00039 #include </usr/include/math.h>
00040 #include "usb.h"
00041 #include <assert.h>
00042 #include <sys/types.h>
00043 #include <sys/socket.h>
00044 #include <netinet/in.h>
00045 //#include "/usr/include/linux/sctp.h"
00046 #include <netdb.h>
00047 #include <unistd.h>
00048 #include <errno.h>
00049 
00050 #define PROGVERSION "1.0.0.1"
00051 
00052 #define BOOL int
00053 #define TRUE 1
00054 #define FALSE 0
00055 
00056 #define D0  0x01
00057 #define D1  0x02
00058 #define D2  0x04
00059 #define D3  0x08
00060 #define D4  0x10
00061 #define D5  0x20
00062 #define D6  0x40
00063 #define D7  0x80
00064 #define DFF 0xff
00065 
00066 #define LOG_FILEPATH_FLAG       0x00000001
00067 #define SPECTRA_FILEPATH_FLAG   0x00000002
00068 #define SPECTRA_SECONDS_FLAG    0x00000004
00069 #define LOG_DOLOG_FLAG          0x00000008
00070 #define PRE_ALARM_FLAG          0x00000010
00071 #define POST_ALARM_FLAG         0x00000020
00072 #define RING_SIZE_FLAG          0x00000040
00073 #define DEAD_BAND_FLAG          0x00000080
00074 #define BG_PERIOD_FLAG          0x00000100
00075 #define SIGMA_ALARM_FLAG        0x00000200
00076 #define PEAKTRACK_BIN_FLAG      0x00000400
00077 #define PEAKTRACK_ROI_FLAG      0x00000800
00078 #define PEAKTRACK_SMOOTH_FLAG   0x00001000
00079 #define ALARM_FILEPATH_FLAG     0x00002000
00080 #define SPECTRAOUT_SIZE_FLAG    0x00004000
00081 #define BG_MINIMUM_FLAG         0x00008000
00082 #define FORCE_NOALARM_FLAG      0x00010000
00083 #define PEAKTRACK_FORCE_FLAG    0x00020000
00084 #define ALL_FLAGS               0x0003ffff
00085 #define IPPROTO_SCTP    132
00086 
00087 /* the device's vendor and product id */
00088 //#define MY_VID 1234
00089 //#define MY_PID 5678
00090 #define MY_VID 0x0BD7
00091 #define MY_PID 0xA021
00092 
00093 #define REQUESTTYPEIN 0xc8
00094 #define REQUESTTYPEOUT 0x48
00095 
00096 /* the device's endpoints */
00097 #define EP_IN_STATUSA  0x81
00098 #define EP_IN_SPECTRAA 0x82
00099 #define EP_IN_STATUSB  0x83
00100 #define EP_IN_SPECTRAB 0x84
00101 #define EP_IN_CONFIGUR 0x85
00102 #define EP_OUT_CONFIGUR 0x01
00103 #define EP_OUT 0x01
00104 
00105 #define CLEARBUFF_A 0x80
00106 #define CLEARBUFF_B 0x81
00107 #define ENABLEMCA 0x82
00108 #define DISABLEMCA 0x83
00109 
00110 #define BUF_SIZE 65
00111 #define CONFIG_SIZE 64
00112 #define STATUS_SIZE 64
00113 #define SPECT_SIZE (8192*3+1)
00114 #define SPECT_FSIZE (8192)
00115 
00116 #define NOSPECTRA_TYPE          0
00117 #define UNASSIGNED_TYPE         1
00118 #define PREALARM_TYPE           2
00119 #define ALARM_TYPE              3
00120 #define POSTALARM_TYPE          4
00121 #define GUARD_TYPE              5
00122 #define BG_TYPE                 6
00123 
00124 //will probably have to comment this out
00125 //unsigned int sleep(unsigned int seconds);
00126 
00127 // The remote receiver information goes here (bjb addition)
00128 char *server="128.165.54.61";
00129 int port=2000;
00130 #define FRAGMENTSIZE 1024
00131 #define PKTDELAY 10000
00132 int fd;
00133 struct hostent *he;
00134 struct sockaddr_in their_addr;
00135 #define min(x,y) (x<y ? x:y)
00136 int tcpconnectopen=0;
00137 
00138 //data to configure the instrument
00139 struct {
00140         char *szParameter;
00141         char cIndex;
00142         char cMask;
00143         char cSigned;
00144 } config[] = {
00145         "reset_lockout=",                       0,      D7,                     0,
00146         "flat_top_width=",                      0,      D6|D5|D4|D3,            0,
00147         "decimation_factor=",                   0,      D2|D1|D0,               0,
00148         "slow_channel_threshold=",              1,      DFF,                    0,
00149         "fast_channel_threshold=",              2,      DFF,                    0,
00150         "output_dac_offset=",                   3,      D7|D6|D5|D4|D3|D2|D1,   1,
00151         "dac_enabled=",                         3,      D0,                     0,
00152         "normal_operation1=",                   4,      D7|D6,                  0,
00153         "mca_enabled=",                         4,      D5,                     0,
00154         "mcamcs_channel_mode=",                 4,      D4|D3|D2,               0,
00155         "dac_output=",                          4,      D1|D0,                  0,
00156         "pileup_reject_interval=",              5,      DFF,                    0,
00157         "peaking_time=",                        6,      D7|D6|D5|D4,            0,
00158         "detector_reset_lockout_period=",       6,      D3|D2,                  0,
00159         "auto_baseline_reset_during_detector_reset=",   6,      D1,             0,
00160         "dont_disable_mca_during_detector_reset=",      6,      D0,             0,
00161         "rtd_slow_threshold=",                  7,      DFF,                    0,
00162         "normal_operation2=",                   8,      D7,                     0,
00163         "analog_gain=",                         8,      D6|D5,                  0,
00164         "rtd_on=",                              8,      D4,                     0,
00165         "rtd_time_threshold=",                  8,      D3|D2|D1|D0,            0,
00166         "50_digital_attenuation=",              9,      D7,                     0,
00167         "blr_enabled=",                         9,      D6,                     0,
00168         "blr_down_correction=",                 9,      D5|D4,                  0,
00169         "blr_up_correction=",                   9,      D3|D2,                  0,
00170         "blr_correction_threshold=",            9,      D1|D0,                  0,
00171         "gate=",                                10,     D7|D6,                  0,
00172         "mca_buffer_select=",                   10,     D5|D4,                  0,
00173         "digital_scope_trigger=",               10,     D3,                     0,
00174         "aux_out=",                             10,     D2|D1|D0,               0,
00175         "preset_time_lsb=",                     11,     DFF,                    0,
00176         "preset_time_byte2=",                   12,     DFF,                    0,
00177         "preset_time_msb=",                     13,     DFF,                    0,
00178         "fine_grain_normalizer_lsb=",           23,     DFF,                    0,
00179         "digital_scope_trigger_position=",      24,     D7|D6,                  0,
00180         "fine_grain_normalizer_msb=",           24,     D5|D4|D3|D2|D1|D0,      0,
00181         "preset_counts_lsb=",                   25,     DFF,                    0,
00182         "preset_counts_byte2=",                 26,     DFF,                    0,
00183         "preset_counts_byte3=",                 27,     DFF,                    0,
00184         "preset_counts_msb=",                   28,     DFF,                    0,
00185         "mca_or_mcs_mode=",                     29,     D7,                     0,
00186         "mcs_enabled=",                         29,     D5,                     0,
00187         "mcs_time_base=",                       29,     D3|D2|D1|D0,            0,
00188         "sca1_lower_threshold_lsb=",            32,     DFF,                    0,
00189         "sca1_lower_threshold_msb=",            33,     DFF,                    0,
00190         "sca1_upper_threshold_lsb=",            34,     DFF,                    0,
00191         "sca1_enabled=",                        35,     D7,                     0,
00192         "sca1_upper_threshold_msb=",            35,     D6|D5|D4|D3|D2|D1|D0,   0,
00193         "sca2_lower_threshold_lsb=",            36,     DFF,                    0,
00194         "sca2_lower_threshold_msb=",            37,     DFF,                    0,
00195         "sca2_upper_threshold_lsb=",            38,     DFF,                    0,
00196         "sca2_enabled=",                        39,     D7,                     0,
00197         "sca2_upper_threshold_msb=",            39,     D6|D5|D4|D3|D2|D1|D0,   0,
00198         "sca3_lower_threshold_lsb=",            40,     DFF,                    0,
00199         "sca3_lower_threshold_msb=",            41,     DFF,                    0,
00200         "sca3_upper_threshold_lsb=",            42,     DFF,                    0,
00201         "sca3_enabled=",                        43,     D7,                     0,
00202         "sca3_upper_threshold_msb=",            43,     D6|D5|D4|D3|D2|D1|D0,   0,
00203         "sca4_lower_threshold_lsb=",            44,     DFF,                    0,
00204         "sca4_lower_threshold_msb=",            45,     DFF,                    0,
00205         "sca4_upper_threshold_lsb=",            46,     DFF,                    0,
00206         "sca4_enabled=",                        47,     D7,                     0,
00207         "sca4_upper_threshold_msb=",            47,     D6|D5|D4|D3|D2|D1|D0,   0,
00208         "sca5_lower_threshold_lsb=",            48,     DFF,                    0,
00209         "sca5_lower_threshold_msb=",            49,     DFF,                    0,
00210         "sca5_upper_threshold_lsb=",            50,     DFF,                    0,
00211         "sca5_enabled=",                        51,     D7,                     0,
00212         "sca5_upper_threshold_msb=",            51,     D6|D5|D4|D3|D2|D1|D0,   0,
00213         "sca6_lower_threshold_lsb=",            52,     DFF,                    0,
00214         "sca6_lower_threshold_msb=",            53,     DFF,                    0,
00215         "sca6_upper_threshold_lsb=",            54,     DFF,                    0,
00216         "sca6_enabled=",                        55,     D7,                     0,
00217         "sca6_upper_threshold_msb=",            55,     D6|D5|D4|D3|D2|D1|D0,   0,
00218         "sca7_lower_threshold_lsb=",            56,     DFF,                    0,
00219         "sca7_lower_threshold_msb=",            57,     DFF,                    0,
00220         "sca7_upper_threshold_lsb=",            58,     DFF,                    0,
00221         "sca7_enabled=",                        59,     D7,                     0,
00222         "sca7_upper_threshold_msb=",            59,     D6|D5|D4|D3|D2|D1|D0,   0,
00223         "sca8_lower_threshold_lsb=",            60,     DFF,                    0,
00224         "sca8_lower_threshold_msb=",            61,     DFF,                    0,
00225         "sca8_upper_threshold_lsb=",            62,     DFF,                    0,
00226         "sca8_enabled=",                        63,     D7,                     0,
00227         "sca8_upper_threshold_msb=",            63,     D6|D5|D4|D3|D2|D1|D0,   0,
00228         NULL,0,0,0
00229         };
00230         
00231 //configuration and status buffer
00232 //unsigned char tmp[BUF_SIZE];
00233 unsigned char cur_config[BUF_SIZE];
00234                 
00235 //date of last change to configfile
00236 char szConfigChange[18] = {"0000_00_00_000000"};
00237 //path to the executable -- used to find the .config file
00238 char szExecutablePath[256];
00239 //holds the .config file and path
00240 char szConfigPathFile[256];
00241 //holds the log file and path
00242 char szLogPathFile[256];
00243 //holds the spectra path and file
00244 char szSpectraPathFile[256];
00245 //holds the alarm path and file
00246 char szAlarmPathFile[256];
00247 //holds the bg written when in alarm
00248 char szAlarmBGPathFile[256];
00249 //collection duration in sec
00250 int iCollectSeconds;
00251 //flag for log 0=no log, 1 = min log, 2 = max log
00252 int iDoLog;
00253 
00254 //how often to save (seconds) back ground
00255 int bg_period = 3600;
00256 time_t bg_previous = 0;
00257 
00258 //alarm and spectra ring buffer
00259 int pre_alarm=1;
00260 int post_alarm=1;
00261 int ring_size=32;
00262 int ring_size_prev=0;
00263 int dead_band=2;
00264 int bg_minimum=20;
00265 int ring_insertion_point = 0;
00266 int ring_count = 0;
00267 double sigma_alarm = 5.0;
00268 int force_noalarm = 600;
00269 
00270 //peak tracking and rebin
00271 int spectraout_size=2500;
00272 int peaktrack_bin=750;
00273 int peaktrack_roi=50;
00274 int peaktrack_smooth=6;
00275 double fpeaktrack_offset = 0.0;
00276 double fpeaktrack_force = 1.0;
00277 float fA_TParam;
00278 float fA_Param;
00279 float fB_TParam;
00280 float fB_Param;
00281 float fC_TParam;
00282 float fC_Param;
00283 
00284 //alarm and background
00285 BOOL bInAlarm = FALSE;
00286 BOOL bHaveBG = FALSE;
00287 
00288 struct RING_ELEMENT{
00289         time_t time;    //start time of the spectra
00290         int iSum;
00291         double iSqSum; // bjb add
00292         int iType;
00293         int iSize;
00294         double fDuration;
00295         unsigned char status[BUF_SIZE]; //status from inst
00296         double fspectra[SPECT_FSIZE];//real version of spectra 
00297 }ring_element,*pring_element;
00298 
00299 struct RING_ELEMENT alarm_spectra;
00300 struct RING_ELEMENT bg_spectra;
00301 struct RING_ELEMENT alarmbg_spectra;
00302 struct RING_ELEMENT tmp_spectra;
00303 struct RING_ELEMENT rebin_spectra;
00304 struct RING_ELEMENT *ring_buffer = NULL;
00305 
00306 usb_dev_handle *open_dev(void);
00307 void print_to_log(char*);
00308 char getconfig(char* pcBuf, int iOffset, char cMask, char cSigned);
00309 double Transmogrify(double fInPut, double fPeakTrackOffset, BOOL bUseTemp);
00310 double PeakTrack(double*dOutSpectrum, int iSize, int iExpectedBinPeak, int iRegionSize, int iSmoothWindow);
00311 void ReBIN(double*dInSpectrum, int iInSize, double*dOutSpectrum, int iOutSize, double dPeakTrackOffset, BOOL bUseTemp);
00312 
00313 void zero_spectra(struct RING_ELEMENT* pring_element)
00314 {
00315         pring_element->iSum = 0;
00316         pring_element->iSqSum=0; //bjb add
00317         pring_element->fDuration = 0.0;
00318         memset(&pring_element->fspectra,0,sizeof(pring_element->fspectra));
00319 }
00320 
00321 void add_spectra(struct RING_ELEMENT* pring_elementdest, struct RING_ELEMENT* pring_elementsrc)
00322 {
00323         int i;
00324         pring_elementdest->iSum += pring_elementsrc->iSum;
00325         pring_elementdest->iSqSum += pring_elementsrc->iSqSum;  //bjb add
00326         pring_elementdest->fDuration += pring_elementsrc->fDuration;
00327         for (i = 0; i < SPECT_FSIZE; i++)
00328                 pring_elementdest->fspectra[i] += pring_elementsrc->fspectra[i]; 
00329 }
00330 
00331 void print_spectra(struct RING_ELEMENT* pring_element,char* szPath,double fPeakTrackOffset)
00332 {
00333         int i,iLength;
00334         char szFilename[512];
00335         struct tm*timeinfo;
00336         FILE *outfile;
00337         timeinfo = localtime(&pring_element->time);
00338         //build the spectra name with time stamp
00339         sprintf(szFilename,"%s_%04d_%02d_%02d_%02d%02d%02d.txt",
00340                 szPath,
00341                 timeinfo->tm_year+1900,
00342                 timeinfo->tm_mon+1,
00343                 timeinfo->tm_mday,
00344                 timeinfo->tm_hour,
00345                 timeinfo->tm_min,
00346                 timeinfo->tm_sec);
00347 
00348         //make sure we have a good file name
00349         //step around the potential "C:" if running on windoz
00350         //some linux systems have problems with space in the file name
00351         //if that is the case here then add the check for space
00352         for (i = 2; i < (signed)strlen(szFilename); i++) 
00353         {
00354                 if ((szFilename[i] == ':') || (szFilename[i] == '\n') || (szFilename[i] == '?') || (szFilename[i] == '*')) 
00355                         szFilename[i] = '_';
00356         }
00357 
00358         outfile = fopen(szFilename,"w");
00359         
00360         print_to_log(szFilename);
00361         
00362         if (outfile)
00363         {
00364                 fprintf(outfile,"Type: ");
00365                 
00366                 switch (pring_element->iType) {
00367                 case NOSPECTRA_TYPE:
00368                         fprintf(outfile,"No Spectra");
00369                         break;
00370                 case UNASSIGNED_TYPE:
00371                         fprintf(outfile,"Unassigned");
00372                         break;
00373                 case PREALARM_TYPE:
00374                         fprintf(outfile,"Pre-Alarm");
00375                         break;
00376                 case ALARM_TYPE:
00377                         fprintf(outfile,"Alarm");
00378                         break;
00379                 case POSTALARM_TYPE:
00380                         fprintf(outfile,"Post-Alarm");
00381                         break;
00382                 case GUARD_TYPE:
00383                         fprintf(outfile,"Guard");
00384                         break;
00385                 case BG_TYPE:
00386                         fprintf(outfile,"Background");
00387                         break;
00388                 default:
00389                         fprintf(outfile,"UNKNOWN");
00390                 }
00391                 fprintf(outfile,"\n");
00392                 if (pring_element->iType != NOSPECTRA_TYPE)
00393                 {
00394                         fprintf(outfile,"Time: %04d\\%02d\\%02d %02d:%02d:%02d\n",
00395                                 timeinfo->tm_year+1900,
00396                                 timeinfo->tm_mon+1,
00397                                 timeinfo->tm_mday,
00398                                 timeinfo->tm_hour,
00399                                 timeinfo->tm_min,
00400                                 timeinfo->tm_sec);
00401                 
00402                         fprintf(outfile,"Duration (mSec): %f\n",pring_element->fDuration);
00403                         fprintf(outfile,"Integral Sum: %d\n",pring_element->iSum);
00404                         fprintf(outfile,"Peak Track: %f\n",fPeakTrackOffset);
00405                 
00406                         fprintf(outfile,"CONFIGURATION");
00407                         for (i = 0; i < CONFIG_SIZE; i++)
00408                         {
00409                                 if (i%16==0)fprintf(outfile,"\n  %04d ",i);
00410                                 fprintf(outfile,"%02x ",cur_config[i]);
00411                         }
00412                         
00413                         fprintf(outfile,"\nSTATUS");
00414                         for (i = 0; i < STATUS_SIZE; i++)
00415                         {
00416                                 if (i%16==0)fprintf(outfile,"\n  %04d ",i);
00417                                 fprintf(outfile,"%02x ",pring_element->status[i]);
00418                         }
00419                 
00420                         iLength = getconfig(cur_config, 4, D2|D3|D4, 0);
00421                         fprintf(outfile,"\nSPECTRA (size:%d)\n", pring_element->iSize);
00422                         for (i = 0; i < pring_element->iSize; i++)
00423                                 fprintf(outfile,"%04d %lf\n",i,pring_element->fspectra[i]);
00424                 }
00425                 fclose(outfile);
00426         }
00427         else
00428         {
00429                 print_to_log("ERROR: Failed on write spectra file.");
00430         }
00431 }
00432 
00433 // this is where the packets are sent across the network
00434 // bjb addition send_spectra
00435 int send_spectra(struct RING_ELEMENT* pring_element,char* szPath,double fPeakTrackOffset)
00436 {
00437  char *pkt;
00438  int pktsize;
00439  char *tempstr;
00440  struct tm*timeinfo;
00441  int i, error;
00442  int remaining, total, tcnt;
00443  
00444  timeinfo = localtime(&pring_element->time);
00445  tempstr=malloc(1024); // tempstr holds the string that will be strcat-ted to pkt
00446 
00447  if (tempstr == NULL)
00448   {
00449    print_to_log("ERROR: memory allocation in send_spectra");
00450    return -1;
00451   } 
00452  // we need the size of the packet later on. so keep count of the number of characters
00453  pktsize=0;
00454  // allocate some large memory. this could be optimized depending upon the maximum size of the packet that will be sent
00455  pkt=malloc(2*SPECT_SIZE+CONFIG_SIZE+STATUS_SIZE);
00456  if (pkt == NULL)
00457   {
00458    print_to_log("ERROR: memory allocation in send_spectra");
00459    return -1;
00460   } 
00461  // initialize strings to NULL
00462  tempstr[0]='\0';
00463  pkt[0]='\0'; 
00464  // first create the pkt to be sent through the socket
00465  pktsize+=sprintf(tempstr,"PIPESTART ");
00466  strcat(pkt,tempstr);
00467 
00468  pktsize+=sprintf(tempstr,"Type: ");
00469  strcat(pkt,tempstr);
00470  switch (pring_element->iType)
00471  {
00472     case NOSPECTRA_TYPE:
00473      pktsize+=sprintf(tempstr,"No Spectra");
00474      strcat(pkt,tempstr);
00475      break;
00476     case UNASSIGNED_TYPE:
00477      pktsize+=sprintf(tempstr,"Unassigned");
00478      strcat(pkt,tempstr);
00479      break;
00480     case PREALARM_TYPE:
00481      pktsize+=sprintf(tempstr,"Pre-Alarm");
00482      strcat(pkt,tempstr);
00483      break;
00484     case ALARM_TYPE:
00485      pktsize+=sprintf(tempstr,"Alarm");
00486      strcat(pkt,tempstr);
00487      break;
00488     case POSTALARM_TYPE:
00489      pktsize+=sprintf(tempstr,"Post-Alarm");
00490      strcat(pkt,tempstr);
00491      break;
00492     case GUARD_TYPE:
00493      pktsize+=sprintf(tempstr,"Guard");
00494      strcat(pkt,tempstr);
00495      break;
00496     case BG_TYPE:
00497      pktsize+=sprintf(tempstr,"Background");
00498      strcat(pkt,tempstr);
00499      break;
00500     default:
00501      pktsize+=sprintf(tempstr,"UNKNOWN");
00502      strcat(pkt,tempstr);
00503  }     
00504  pktsize+=sprintf(tempstr," ");
00505  strcat(pkt,tempstr);
00506 
00507  if (pring_element->iType != NOSPECTRA_TYPE)
00508  {
00509     pktsize+=sprintf(tempstr,"Time: %04d\\%02d\\%02d %02d:%02d:%02d ",
00510                 timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
00511     strcat(pkt,tempstr);
00512     
00513     pktsize+=sprintf(tempstr,"Duration (mSec): %f ", pring_element->fDuration);
00514     strcat(pkt,tempstr);
00515     pktsize+=sprintf(tempstr,"Integral Sum: %d ", pring_element->iSum);
00516     strcat(pkt,tempstr);
00517     pktsize+=sprintf(tempstr,"Peak Track: %f ", fPeakTrackOffset);
00518     strcat(pkt,tempstr);
00519 
00520     pktsize+=sprintf(tempstr,"CONFIGURATION ");
00521     strcat(pkt,tempstr);
00522     for (i=0;i<CONFIG_SIZE; i++)
00523     {
00524      if (i%16==0)
00525       {
00526        pktsize+=sprintf(tempstr," %04d ",i);
00527        strcat(pkt,tempstr);
00528       }
00529        pktsize+=sprintf(tempstr," %02x ",cur_config[i]);
00530        strcat(pkt,tempstr);
00531     }
00532 
00533     pktsize+=sprintf(tempstr,"STATUS ");
00534     strcat(pkt,tempstr);
00535     for (i=0;i<STATUS_SIZE; i++)
00536     {
00537      if (i%16==0)
00538       {
00539        pktsize+=sprintf(tempstr," %04d ",i);
00540        strcat(pkt,tempstr);
00541       }
00542        pktsize+=sprintf(tempstr," %02x ",pring_element->status[i]);
00543        strcat(pkt,tempstr);
00544     }
00545 
00546     pktsize+=sprintf(tempstr,"SPECTRA (size:%d) ", pring_element->iSize);
00547     strcat(pkt,tempstr);
00548 
00549     for (i=0;i<pring_element->iSize; i++)
00550     {
00551        pktsize+=sprintf(tempstr," %04d %lf ",i, pring_element->fspectra[i]);
00552        strcat(pkt,tempstr);
00553     }
00554  }
00555  pktsize+=sprintf(tempstr,"PIPEEND");
00556  strcat(pkt,tempstr);
00557  
00558  // now send the pkt through the socket
00559  if (tcpconnectopen==0)
00560  {
00561  if ((fd = socket(PF_INET,SOCK_STREAM,0))<0) 
00562  {
00563         perror("Socket");
00564         print_to_log("ERROR: socket error during open\n");
00565         return fd;
00566  }
00567  if ((he =  gethostbyname(server))==NULL)
00568  {
00569         herror("gethostbyname");
00570         print_to_log("ERROR: gethostbyname error\n");
00571         return -1;  
00572  }
00573  
00574  their_addr.sin_family = AF_INET;
00575  their_addr.sin_port = htons(port);
00576  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
00577  memset(their_addr.sin_zero,'\0',sizeof(their_addr.sin_zero));
00578 
00579  if (connect(fd,(struct sock_addr*)(& their_addr),sizeof(struct sockaddr))<0)
00580  { 
00581   perror("connect error");
00582   print_to_log("connect error\n");
00583  }
00584  else
00585   tcpconnectopen=1;
00586  } 
00587 
00588  printf("sending packet (size = %d)\n", pktsize);
00589  // send stuff here (break up packets to match MTU)
00590 // remaining=pktsize;
00591 // total=0;
00592 // tcnt=0;
00593 // while (total<pktsize)
00594 // {
00595   //if ((error = sendto(fd,pkt+total,min(FRAGMENTSIZE,remaining),0,(struct sock_addr*)(& their_addr),sizeof(struct sockaddr))) <0)
00596   if ((error = send(fd,pkt,pktsize,MSG_NOSIGNAL)) <0)  
00597   {
00598    if (errno==EPIPE)
00599     tcpconnectopen=0;
00600    perror("sendto");
00601    print_to_log("ERROR: sendto error\n");
00602   }
00603 //  tcnt++;
00604 //  total+=error;
00605 //  remaining-=error;
00606 //  printf("%d)sendto returned %d\n",tcnt,error);  
00607 //  usleep(PKTDELAY); // need delay to mitigate packet collision
00608 // }
00609   free(pkt);
00610   free(tempstr);
00611   return 0;
00612 }
00613 
00614 //write string to the log file if iDoLog > 0
00615 void print_to_log(char* szText)
00616 {
00617   char szfilename[256];
00618   time_t rawtime;
00619   struct tm* timeinfo;
00620   FILE *outfile;
00621 
00622   if (iDoLog > 0)
00623   {
00624         time(&rawtime);
00625         timeinfo = localtime(&rawtime);
00626         sprintf(szfilename,"%s_%04d_%02d_%02d.log",
00627                 szLogPathFile,
00628                 timeinfo->tm_year+1900,
00629                 timeinfo->tm_mon+1,
00630                 timeinfo->tm_mday);
00631         outfile = fopen(szfilename,"a");
00632         if (outfile)
00633         {
00634           fprintf(outfile,"%04d\\%02d\\%02d %02d:%02d:%02d %s\n",
00635                   timeinfo->tm_year+1900,
00636                   timeinfo->tm_mon+1,
00637                   timeinfo->tm_mday,
00638                   timeinfo->tm_hour,
00639                   timeinfo->tm_min,
00640                   timeinfo->tm_sec,
00641                   szText);
00642           fclose(outfile);
00643         }
00644         if (iDoLog >= 4)
00645                 printf("%04d\\%02d\\%02d %02d:%02d:%02d %s\n",
00646                         timeinfo->tm_year+1900,
00647                         timeinfo->tm_mon+1,
00648                         timeinfo->tm_mday,
00649                         timeinfo->tm_hour,
00650                         timeinfo->tm_min,
00651                         timeinfo->tm_sec,
00652                         szText);
00653   }
00654 }
00655 
00656 double Transmogrify(double fInPut, double fPeakTrackOffset, BOOL bUseTemp)
00657 {
00658         double  dATemp,dBTemp,dCTemp;
00659         if (fPeakTrackOffset > 0.0)
00660         {
00661                 dATemp = 0.0;
00662                 dBTemp = fPeakTrackOffset;
00663                 dCTemp = 0.0;
00664         }
00665         else
00666         {
00667                 dATemp = (bUseTemp)?fA_TParam:fA_Param;
00668                 dBTemp = (bUseTemp)?fB_TParam:fB_Param;
00669                 dCTemp = (bUseTemp)?fC_TParam:fC_Param;
00670         }
00671         return (dATemp * fInPut * fInPut + dBTemp * fInPut + dCTemp);
00672 }
00673 
00674 
00675 //assume iExpectedBinPeak iRegionSize/2
00676 double PeakTrack(double*dOutSpectrum, int iSize, int iExpectedBinPeak, int iRegionSize, int iSmoothWindow)
00677 {
00678         double dShift = -1.0;
00679         int iMaxPointL = 0;
00680         int iMaxPointR = 0;
00681         int iMaxPoint = 0;
00682         double dMaxValueL = 0.0;
00683         double dMaxValueR = 0.0;
00684         int iPoint;
00685         
00686         if ((iExpectedBinPeak + iRegionSize/2) < iSize)
00687         {
00688                 double* pdSmooth = (double*)malloc(sizeof(double)*iRegionSize);
00689                 //double dSmooth[iRegionSize];
00690                 double dSmoothTemp;
00691                 int i,j,k;
00692                 int iHalfRegionSize = iRegionSize/2;
00693 
00694                 //smooth the window around the expected peak
00695                 if (iSmoothWindow)
00696                 {
00697                         for (i = iExpectedBinPeak-iHalfRegionSize,j=0; i < iExpectedBinPeak+iHalfRegionSize; i++,j++)
00698                         {
00699                                 dSmoothTemp = 0.0;
00700                                 for (k = i - iSmoothWindow/2; k < i + iSmoothWindow/2; k++)
00701                                         dSmoothTemp += dOutSpectrum[k];
00702                                 pdSmooth[j] = dSmoothTemp/iSmoothWindow;
00703                         }       
00704                 }
00705                 else
00706                 {
00707                         memcpy(pdSmooth,&dOutSpectrum[iExpectedBinPeak-iRegionSize/2],sizeof(double)*iRegionSize);
00708                 }
00709 
00710                 //find highest peak from left
00711                 for (iPoint = 0; iPoint < iRegionSize; iPoint++)
00712                 {
00713                         if (dMaxValueL < pdSmooth[iPoint])
00714                         {
00715                                 iMaxPointL = iPoint;
00716                                 dMaxValueL = pdSmooth[iPoint];
00717                         }
00718                         //TRACE("L = %d %f\r\n",iPoint,pdSmooth[iPoint]);
00719                 }
00720 
00721                 for (/*int*/ iPoint = iRegionSize-1; iPoint >= 0; iPoint--)
00722                 {
00723                         if (dMaxValueR < pdSmooth[iPoint])
00724                         {
00725                                 iMaxPointR = iPoint;
00726                                 dMaxValueR = pdSmooth[iPoint];
00727                         }
00728                         //TRACE("R = %d %f\r\n",iPoint,pdSmooth[iPoint]);
00729                 }
00730 
00731                 if (iMaxPointL != iMaxPointR)
00732                         iMaxPoint = (iMaxPointL + iMaxPointR)/2;
00733                 else
00734                         iMaxPoint = iMaxPointL;
00735 
00736                 if (iMaxPoint>=0)       //expected / observed
00737                         dShift = (double)iExpectedBinPeak/(double)(iExpectedBinPeak + (iMaxPoint - (iRegionSize/2)));
00738 
00739                 free (pdSmooth);
00740         }
00741         return dShift;
00742 }
00743 
00744 void ReBIN(double*dInSpectrum, int iInSize, double*dOutSpectrum, int iOutSize, double dPeakTrackOffset, BOOL bUseTemp)
00745 {
00746         int i;
00747         for (i = 0; i < iOutSize; i++)
00748                 dOutSpectrum[i] = 0.0;
00749 
00750         double fStart, fEnd;
00751         double fFirst, fLast;
00752         int iStart, iEnd;
00753         int   iTwixt;
00754         double fTotal;
00755         int iSourceIndex;
00756         
00757         //step across the source spectrum figuring the start KEV and end KEV for each bin
00758         //and distribute the source spectra into the destination spectra accordingly
00759 
00760         for (iSourceIndex = 0; iSourceIndex < iInSize; iSourceIndex++)
00761         {
00762                 fStart = Transmogrify((double)iSourceIndex, dPeakTrackOffset, bUseTemp);
00763                 fEnd   = Transmogrify((double)(iSourceIndex+1), dPeakTrackOffset, bUseTemp);
00764                 fTotal = fEnd - fStart;
00765                 fFirst = fStart - (int)fStart;
00766                 fLast  = fEnd - (int)fEnd;
00767                 iTwixt = (int)((int)fEnd - fStart);
00768                 iStart = (int)fStart;   
00769                 iEnd   = (int)fEnd;     
00770                 if ((iStart < iOutSize) && (iStart >= 0))
00771                 {
00772                         if (iStart == iEnd)
00773                         {
00774                                         dOutSpectrum[iStart] += dInSpectrum[iSourceIndex];
00775                         }
00776                         else
00777                         {
00778 
00779                                 //do we have any "fractional" bin to start with
00780                                 if ((fFirst > 0.0) && (fStart < iOutSize)) 
00781                                 {
00782                                         double dTemp = (double)((1.0-fFirst) * dInSpectrum[iSourceIndex])/fTotal; 
00783                                         dOutSpectrum[iStart] += dTemp;
00784                                 }
00785 
00786                                 //do all of the "whole" bins
00787                                 if (iTwixt > 0)
00788                                         for (i = 0; i < iTwixt; i++)
00789                                         {
00790                                                 double fTemp = dInSpectrum[iSourceIndex]/fTotal;
00791                                                 if ((iStart+i) < iOutSize)
00792                                                 {
00793                                                         if ((fFirst <= 0.0) && (iStart+i >= 0))
00794                                                         {
00795                                                                 dOutSpectrum[iStart+i] += fTemp;
00796                                                         }
00797                                                         else
00798                                                         {
00799                                                                 int iTemp = iStart+i+1;
00800                                                                 if ((iTemp < iOutSize) && (iTemp >= 0))
00801                                                                 {
00802                                                                         dOutSpectrum[iStart+i+1] += fTemp;
00803                                                                 }
00804                                                         }
00805                                                 }
00806                                         }
00807 
00808                                 //do the end "fractional" bin if we have any
00809                                 if ((fLast > 0.0) && (iEnd < iOutSize))
00810                                 {
00811                                         double fTemp = fLast * dInSpectrum[iSourceIndex]/fTotal;
00812                                         dOutSpectrum[iEnd] += fTemp;
00813                                 }
00814                         }
00815                 }
00816         }
00817 }
00818 
00819 //try to open the usb device
00820 usb_dev_handle *open_dev(void)
00821 {
00822   struct usb_bus *bus;
00823   struct usb_device *dev;
00824 
00825   for(bus = usb_get_busses(); bus; bus = bus->next) 
00826     {
00827       for(dev = bus->devices; dev; dev = dev->next) 
00828         {
00829           if(dev->descriptor.idVendor == MY_VID
00830              && dev->descriptor.idProduct == MY_PID)
00831             {
00832               return usb_open(dev);
00833             }
00834         }
00835     }
00836   return NULL;
00837 }
00838 
00839 //get the cMask bits from the iOffset byte in pcBuf
00840 char getconfig(char* pcBuf, int iOffset, char cMask, char cSigned)
00841 {
00842         int iCount = 0;
00843         char cResult;
00844         unsigned char cTempMask = cMask;
00845         while ((cTempMask & 0x01)==0x00)
00846         {
00847                 iCount++;
00848                 cTempMask >>= 1;
00849         }
00850         if (cSigned)
00851                 cResult = (pcBuf[iOffset] >> iCount);
00852         else
00853                 cResult = (pcBuf[iOffset] >> iCount) & cTempMask;
00854 
00855         return cResult;
00856 }
00857 
00858 //set the cMask bits in the iOffset byte in the pcBuf to the
00859 //right justified bits in cValue
00860 void setconfig(char* pcBuf, int iOffset, unsigned char cMask, char cValue)
00861 {
00862         int iCount = 0;
00863         char cTempMask = cMask;
00864         while ((cTempMask & 0x01)==0x00)
00865         {
00866                 iCount++;
00867                 cTempMask >>= 1;
00868         }
00869 //      printf("in setconfig pcBuff:%08x iOffset:%d cMask:%02x cValue:%d iCount:%d\n",
00870 //              pcBuf,iOffset,cMask,cValue,iCount);
00871         pcBuf[iOffset] = (pcBuf[iOffset] & ~cMask) | ((cValue << iCount) & cMask);
00872 }
00873 
00874 //print out to the log file the configuration information in tmp
00875 void log_configuration(char* tmp)
00876 {
00877         int n;
00878         char s[256];
00879         n = 0;
00880         while (config[n].szParameter)
00881         {
00882                 sprintf(s,"%s%d",config[n].szParameter,
00883                         getconfig(tmp,config[n].cIndex,
00884                                 config[n].cMask,
00885                                 config[n].cSigned));
00886                 print_to_log(s);
00887                 n++;
00888         }
00889 }
00890 
00891 void reconfig1(void)
00892 {
00893   //get all the "REQUIRED" elements from the config file
00894   FILE* infile;
00895   //if we reconfigure then we probably want a new bg spectra
00896   bg_previous = 0;
00897   bInAlarm = FALSE;
00898   bHaveBG = FALSE;
00899    
00900   //open the config file
00901   infile = fopen(szConfigPathFile,"r");
00902   //get the "REQUIRED" configuration from it
00903   if (infile)
00904   {
00905           char linebuffer[512];
00906           char *szAt;
00907           unsigned int iFlags = 0x00000000;
00908           fgets(linebuffer,sizeof(linebuffer),infile);
00909           while (strlen(linebuffer) && (iFlags != ALL_FLAGS))
00910           {
00911                 if (strstr(linebuffer,"REQUIRED_log_filepath="))
00912                 {
00913                         szAt = strchr(linebuffer,'=') + 1;
00914                         strcpy(szLogPathFile,szAt);
00915                         szLogPathFile[strlen(szLogPathFile)-1] = 0;
00916                         iFlags |= LOG_FILEPATH_FLAG;
00917                 }
00918                 else if (strstr(linebuffer,"REQUIRED_spectra_filepath="))
00919                 {
00920                         szAt = strchr(linebuffer,'=') + 1;
00921                         strcpy(szSpectraPathFile,szAt);
00922                         szSpectraPathFile[strlen(szSpectraPathFile)-1] = 0;
00923                         iFlags |= SPECTRA_FILEPATH_FLAG;
00924                 }
00925                 else if (strstr(linebuffer,"REQUIRED_alarm_filepath="))
00926                 {
00927                         szAt = strchr(linebuffer,'=') + 1;
00928                         strcpy(szAlarmPathFile,szAt);
00929                         szAlarmPathFile[strlen(szAlarmPathFile)-1] = 0;
00930                         strcpy(szAlarmBGPathFile,szAlarmPathFile);
00931                         strcat(szAlarmBGPathFile,"_BG");
00932                         iFlags |= ALARM_FILEPATH_FLAG;
00933                 }
00934                 else if (strstr(linebuffer,"REQUIRED_spectra_seconds="))
00935                 {
00936                         szAt = strchr(linebuffer,'=') + 1;
00937                         sscanf(szAt,"%d",&iCollectSeconds);
00938                         iFlags |= SPECTRA_SECONDS_FLAG;
00939                 }
00940                 else if (strstr(linebuffer,"REQUIRED_log_dolog="))
00941                 {
00942                         szAt = strchr(linebuffer,'=') + 1;
00943                         sscanf(szAt,"%d",&iDoLog);
00944                         iFlags |= LOG_DOLOG_FLAG;
00945                 }
00946                 else if (strstr(linebuffer,"REQUIRED_pre_alarm="))
00947                 {
00948                         szAt = strchr(linebuffer,'=') + 1;
00949                         sscanf(szAt,"%d",&pre_alarm);
00950                         iFlags |= PRE_ALARM_FLAG;
00951                 }
00952                 else if (strstr(linebuffer,"REQUIRED_post_alarm="))
00953                 {
00954                         szAt = strchr(linebuffer,'=') + 1;
00955                         sscanf(szAt,"%d",&post_alarm);
00956                         iFlags |= POST_ALARM_FLAG;
00957                 }
00958                 else if (strstr(linebuffer,"REQUIRED_ring_size="))
00959                 {
00960                         ring_size_prev = ring_size;
00961                         szAt = strchr(linebuffer,'=') + 1;
00962                         sscanf(szAt,"%d",&ring_size);                   
00963                         iFlags |= RING_SIZE_FLAG;
00964                 }
00965                 else if (strstr(linebuffer,"REQUIRED_dead_band="))
00966                 {
00967                         szAt = strchr(linebuffer,'=') + 1;
00968                         sscanf(szAt,"%d",&dead_band);
00969                         iFlags |= DEAD_BAND_FLAG;
00970                 }
00971                 else if (strstr(linebuffer,"REQUIRED_bg_period="))
00972                 {
00973                         szAt = strchr(linebuffer,'=') + 1;
00974                         sscanf(szAt,"%d",&bg_period);
00975                         iFlags |= BG_PERIOD_FLAG;
00976                 }
00977                 else if (strstr(linebuffer,"REQUIRED_sigma_alarm="))
00978                 {
00979                         szAt = strchr(linebuffer,'=') + 1;
00980                         sscanf(szAt,"%lf",&sigma_alarm);
00981                         iFlags |= SIGMA_ALARM_FLAG;
00982                 }
00983                 else if (strstr(linebuffer,"REQUIRED_peaktrack_bin="))
00984                 {
00985                         szAt = strchr(linebuffer,'=') + 1;
00986                         sscanf(szAt,"%d",&peaktrack_bin);
00987                         iFlags |= PEAKTRACK_BIN_FLAG;
00988                 }
00989                 else if (strstr(linebuffer,"REQUIRED_peaktrack_roi="))
00990                 {
00991                         szAt = strchr(linebuffer,'=') + 1;
00992                         sscanf(szAt,"%d",&peaktrack_roi);
00993                         iFlags |= PEAKTRACK_ROI_FLAG;
00994                 }
00995                 else if (strstr(linebuffer,"REQUIRED_peaktrack_smooth="))
00996                 {
00997                         szAt = strchr(linebuffer,'=') + 1;
00998                         sscanf(szAt,"%d",&peaktrack_smooth);
00999                         iFlags |= PEAKTRACK_SMOOTH_FLAG;
01000                 }
01001                 else if (strstr(linebuffer,"REQUIRED_spectraout_size="))
01002                 {
01003                         szAt = strchr(linebuffer,'=') + 1;
01004                         sscanf(szAt,"%d",&spectraout_size);
01005                         iFlags |= SPECTRAOUT_SIZE_FLAG;
01006                 }
01007                 else if (strstr(linebuffer,"REQUIRED_bg_minimum="))
01008                 {
01009                         szAt = strchr(linebuffer,'=') + 1;
01010                         sscanf(szAt,"%d",&bg_minimum);
01011                         iFlags |= BG_MINIMUM_FLAG;
01012                 }
01013                 else if (strstr(linebuffer,"REQUIRED_force_noalarm="))
01014                 {
01015                         szAt = strchr(linebuffer,'=') + 1;
01016                         sscanf(szAt,"%d",&force_noalarm);
01017                         iFlags |= FORCE_NOALARM_FLAG;
01018                 }
01019                 else if (strstr(linebuffer,"REQUIRED_peaktrack_force="))
01020                 {
01021                         szAt = strchr(linebuffer,'=') + 1;
01022                         sscanf(szAt,"%lf",&fpeaktrack_force);
01023                         iFlags |= PEAKTRACK_FORCE_FLAG;
01024                 }
01025                 fgets(linebuffer,sizeof(linebuffer),infile);
01026           }
01027           fclose(infile);
01028   }
01029   else
01030   {
01031         //couldn't open the config file so write a new one
01032         print_to_log("WARNING: Cannot find config file. Creating default file!");
01033         strcpy(szLogPathFile,"Log");
01034         strcpy(szSpectraPathFile,"Spectra");
01035         strcpy(szAlarmPathFile,"Alarm");
01036         strcpy(szAlarmBGPathFile,"Alarm_BG");
01037         iCollectSeconds=1;
01038         iDoLog=1;
01039         pre_alarm=1;
01040         post_alarm=1;
01041         ring_size=64;
01042         dead_band=2;
01043         bg_period=3600;
01044         sigma_alarm=5.0;
01045         peaktrack_bin=1460;
01046         peaktrack_smooth=6;
01047         fpeaktrack_force=1.0;
01048         spectraout_size=2500;
01049         bg_minimum=60;
01050         infile = fopen(szConfigPathFile,"w");
01051         if (infile)
01052         {
01053                 fprintf(infile,
01054                         "REQUIRED_log_dolog=1\n"
01055                         "REQUIRED_log_filepath=Log\n"
01056                         "REQUIRED_spectra_filepath=Spectra\n"
01057                         "REQUIRED_alarm_filepath=Alarm\n"
01058                         "REQUIRED_spectra_seconds=1\n"
01059                         "REQUIRED_pre_alarm=1\n"
01060                         "REQUIRED_post_alarm=1\n"
01061                         "REQUIRED_ring_size=64\n"
01062                         "REQUIRED_dead_band=2\n"
01063                         "REQUIRED_bg_period=3600\n"
01064                         "REQUIRED_sigma_alarm=5.0\n"
01065                         "REQUIRED_peaktrack_bin=1460\n"
01066                         "REQUIRED_peaktrack_roi=100\n"
01067                         "REQUIRED_peaktrack_smooth=6\n"
01068                         "REQUIRED_peaktrack_force=1.0\n"
01069                         "REQUIRED_spectraout_size=2500\n"
01070                         "REQUIRED_bg_minimum=60\n"
01071                         "REQUIRED_force_noalarm=600\n"
01072                         "reset_lockout=0\n"
01073                         "flat_top_width=6\n"
01074                         "decimation_factor=1\n"
01075                         "slow_channel_threshold=0\n"
01076                         "fast_channel_threshold=19\n"
01077                         "output_dac_offset=13\n"
01078                         "dac_enabled=1\n"
01079                         "normal_operation1=0\n"
01080                         "mca_enabled=1\n"
01081                         "mcamcs_channel_mode=5\n"
01082                         "dac_output=1\n"
01083                         "pileup_reject_interval=0\n"
01084                         "peaking_time=8\n"
01085                         "detector_reset_lockout_period=2\n"
01086                         "auto_baseline_reset_during_detector_reset=0\n"
01087                         "dont_disable_mca_during_detector_reset=0\n"
01088                         "rtd_slow_threshold=0\n"
01089                         "normal_operation2=0\n"
01090                         "analog_gain=1\n"
01091                         "rtd_on=0\n"
01092                         "rtd_time_threshold=6\n"
01093                         "50_digital_attenuation=1\n"
01094                         "blr_enabled=0\n"
01095                         "blr_down_correction=3\n"
01096                         "blr_up_correction=3\n"
01097                         "blr_correction_threshold=3\n"
01098                         "gate=0\n"
01099                         "mca_buffer_select=0\n"
01100                         "digital_scope_trigger=0\n"
01101                         "aux_out=0\n"
01102                         "preset_time_lsb=0\n"
01103                         "preset_time_byte2=0\n"
01104                         "preset_time_msb=0\n"
01105                         "fine_grain_normalizer_lsb=0\n"
01106                         "digital_scope_trigger_position=2\n"
01107                         "fine_grain_normalizer_msb=4\n"
01108                         "preset_counts_lsb=0\n"
01109                         "preset_counts_byte2=0\n"
01110                         "preset_counts_byte3=0\n"
01111                         "preset_counts_msb=0\n"
01112                         "mca_or_mcs_mode=0\n"
01113                         "mcs_enabled=1\n"
01114                         "mcs_time_base=0\n"
01115                         "sca1_lower_threshold_lsb=0\n"
01116                         "sca1_lower_threshold_msb=0\n"
01117                         "sca1_upper_threshold_lsb=0\n"
01118                         "sca1_enabled=0\n"
01119                         "sca1_upper_threshold_msb=0\n"
01120                         "sca2_lower_threshold_lsb=0\n"
01121                         "sca2_lower_threshold_msb=0\n"
01122                         "sca2_upper_threshold_lsb=0\n"
01123                         "sca2_enabled=0\n"
01124                         "sca2_upper_threshold_msb=0\n"
01125                         "sca3_lower_threshold_lsb=0\n"
01126                         "sca3_lower_threshold_msb=0\n"
01127                         "sca3_upper_threshold_lsb=0\n"
01128                         "sca3_enabled=0\n"
01129                         "sca3_upper_threshold_msb=0\n"
01130                         "sca4_lower_threshold_lsb=0\n"
01131                         "sca4_lower_threshold_msb=0\n"
01132                         "sca4_upper_threshold_lsb=0\n"
01133                         "sca4_enabled=0\n"
01134                         "sca4_upper_threshold_msb=0\n"
01135                         "sca5_lower_threshold_lsb=0\n"
01136                         "sca5_lower_threshold_msb=0\n"
01137                         "sca5_upper_threshold_lsb=0\n"
01138                         "sca5_enabled=0\n"
01139                         "sca5_upper_threshold_msb=0\n"
01140                         "sca6_lower_threshold_lsb=0\n"
01141                         "sca6_lower_threshold_msb=0\n"
01142                         "sca6_upper_threshold_lsb=0\n"
01143                         "sca6_enabled=0\n"
01144                         "sca6_upper_threshold_msb=0\n"
01145                         "sca7_lower_threshold_lsb=0\n"
01146                         "sca7_lower_threshold_msb=0\n"
01147                         "sca7_upper_threshold_lsb=0\n"
01148                         "sca7_enabled=0\n"
01149                         "sca7_upper_threshold_msb=0\n"
01150                         "sca8_lower_threshold_lsb=0\n"
01151                         "sca8_lower_threshold_msb=0\n"
01152                         "sca8_upper_threshold_lsb=0\n"
01153                         "sca8_enabled=0\n"
01154                         "sca8_upper_threshold_msb=0\n");
01155 
01156                 fclose(infile);
01157         }
01158         else
01159                 print_to_log("WARNING: Cannot create default file!");
01160   }
01161 }
01162 
01163 int reconfig2(usb_dev_handle *dev)
01164 {
01165   struct stat buf;
01166   struct tm* timeinfo;
01167   char s[512];
01168   char szTemp[512];
01169   int i,j,iResult;
01170   FILE *infile;
01171 
01172   //get the two descriptor strings from the instrument
01173   if (usb_get_string_simple(dev,1,szTemp,sizeof(szTemp)) > 0)
01174   {
01175           sprintf(s,"Descriptor 1:%s",szTemp);
01176           print_to_log(s);
01177   }
01178   if (usb_get_string_simple(dev,2,szTemp,sizeof(szTemp)) > 0)
01179   {
01180           sprintf(s,"Descriptor 2:%s",szTemp);
01181           print_to_log(s);
01182   }
01183   //get the current instrument configuration
01184   if ((iResult = usb_bulk_read(dev, EP_IN_CONFIGUR, cur_config, sizeof(cur_config), 5000)) 
01185      != (sizeof(cur_config)-1))
01186   {
01187         sprintf(s,"ERROR: bulk read configuration (%d) failed",iResult);
01188         print_to_log(s);
01189         return -1;
01190   }
01191   else
01192   {
01193         //log the previous configuration
01194         strcpy(s,"OLD CONFIGURATION");
01195         szTemp[0] = 0;
01196         for (i = 0; i < CONFIG_SIZE; i++)
01197         {
01198                 if ((i & 0x0000000f) == 0)
01199                 {
01200                         sprintf(szTemp,"\n  %04d ",i);
01201                         strcat(s,szTemp);
01202                 }
01203                 sprintf(szTemp,"%02x ",cur_config[i]);
01204                 strcat(s,szTemp);
01205         }
01206         print_to_log(s);        
01207 
01208         //print the verbose veraion only if iDoLog says so
01209         if (iDoLog > 1)
01210                 log_configuration(cur_config);
01211 
01212         //get the remaining configuration information
01213         //build the 64 byte configuration based on the 
01214         //old configuration.  Note: only changed values
01215         //need to be included in the config file
01216         infile = fopen(szConfigPathFile,"r");
01217         if (infile)
01218         {
01219           char linebuffer[256];
01220           char *szAt;
01221           char cValue;
01222           //read a line from the config file
01223           while (fgets(linebuffer,sizeof(linebuffer),infile))
01224           {
01225             int n;
01226                 BOOL bFound;
01227                 //see if any of our config strings are included
01228                 //note that this assumes the config strings are
01229                 //unique and that they start the line
01230                 n = 0;
01231                 bFound = FALSE;
01232                 if ((strlen(linebuffer)>2) && (strstr(linebuffer,"REQUIRED_")==NULL))
01233                 {
01234                         while (config[n].szParameter && !bFound)
01235                         {
01236                                 if (strstr(linebuffer,config[n].szParameter))
01237                                 {
01238                                         int dValue;
01239                                         //go to the character after the =
01240                                         szAt = strchr(linebuffer,'=')+1;
01241                                         sscanf(szAt,"%d",&dValue);
01242                                         //set the value in the config array
01243                                         cValue = (char)dValue;
01244                                         setconfig(cur_config,config[n].cIndex,config[n].cMask,cValue);
01245                                         bFound = TRUE;
01246                                 }
01247                                 n++;
01248                         }
01249                         if (!bFound)
01250                         {
01251                                 for (i = 0; i < strlen(linebuffer); i++)
01252                                         if (linebuffer[i] == '\n') linebuffer[i] = 0;
01253                                 sprintf(s,"Configuration parameter \"%s\" not found.",linebuffer);
01254                                 print_to_log(s);
01255                         }
01256                 }
01257           }
01258           fclose(infile);
01259         }
01260 
01261         //log the new configuration
01262         strcpy(s,"NEW CONFIGURATION");
01263         szTemp[0] = 0;
01264         for (i = 0; i < CONFIG_SIZE; i++)
01265         {
01266                 if ((i & 0x0000000f) == 0)
01267                 {
01268                         sprintf(szTemp,"\n  %04d ",i);
01269                         strcat(s,szTemp);
01270                 }
01271                 sprintf(szTemp,"%02x ",cur_config[i]);
01272                 strcat(s,szTemp);
01273         }
01274         print_to_log(s);
01275 
01276         //only do the verbose if iDoLog says so
01277         if (iDoLog > 2)
01278                 log_configuration(cur_config);
01279 
01280         //send new configuration to instrument
01281         iResult=usb_bulk_write(dev, EP_OUT, cur_config, sizeof(cur_config)-1, 5000); 
01282         if (iResult <= 0)
01283         {
01284                 //log any errors
01285                 sprintf(s,"ERROR: bulk write returned: %d",iResult);
01286                 print_to_log(s);
01287                 return -1;
01288         }
01289 
01290         //read back the new configuration
01291         if ((iResult = usb_bulk_read(dev, EP_IN_CONFIGUR, cur_config, sizeof(cur_config), 5000)) 
01292                 != (sizeof(cur_config)-1))
01293         {
01294                 sprintf(s,"ERROR: bulk read configuration (%d) failed",iResult);
01295                 print_to_log(s);
01296                 return -1;
01297         }
01298 
01299         //log the new configuration
01300         strcpy(s,"NEW CONFIGURATION READBACK");
01301         szTemp[0] = 0;
01302         for (i = 0; i < CONFIG_SIZE; i++)
01303         {
01304                 if ((i & 0x0000000f) == 0)
01305                 {
01306                         sprintf(szTemp,"\n  %04d ",i);
01307                         strcat(s,szTemp);
01308                 }
01309                 sprintf(szTemp,"%02x ",cur_config[i]);
01310                 strcat(s,szTemp);
01311         }
01312         print_to_log(s);
01313   }
01314   
01315   //free up any existing storage in the ring buffer
01316   if (ring_buffer)
01317   {
01318         free(ring_buffer);
01319         ring_buffer = NULL;
01320   }       
01321 
01322   //get memory for the new ring buffer
01323   ring_buffer = malloc(sizeof(ring_element)*ring_size);
01324   memset(ring_buffer,0,sizeof(ring_element)*ring_size);
01325   
01326   //start at a known point in the ring
01327   ring_insertion_point = 0;
01328   ring_count = 0;
01329   
01330   //get the config file's last modified time
01331   //so we can reconfig on the fly if the config file
01332   //gets modified
01333   stat(szConfigPathFile,&buf);
01334   timeinfo = localtime(&buf.st_mtime);
01335   sprintf(s,"%04d_%02d_%02d_%02d%02d%02d\n",
01336                 timeinfo->tm_year+1900,
01337                 timeinfo->tm_mon+1,
01338                 timeinfo->tm_mday,
01339                 timeinfo->tm_hour,
01340                 timeinfo->tm_min,
01341                 timeinfo->tm_sec);
01342   strcpy(szConfigChange,s);
01343   return 0;
01344 }
01345 
01346 void End(void)
01347 {
01348         print_to_log("Exiting");
01349 }
01350 /*
01351 void Quit(int i)
01352 {
01353         printf("Quitting\n");
01354 }
01355 */
01356 int main(int argc, char**argv)
01357 {
01358   struct stat buf;
01359   int iDuration;
01360   int iCollectUSec;
01361   int iPostAlarmCount;
01362   usb_dev_handle *dev; /* the device handle */
01363   int ret; // bjb
01364   char buffr[65535]; //bjb
01365   //configuration and status buffer
01366   //unsigned char tmp[BUF_SIZE];
01367   //spectra buffer
01368   unsigned char spectra[SPECT_SIZE];
01369   //various vars
01370   int i,j,q,iResult,iSeq;
01371   //easy translation from byte,byte,byte to int
01372   union {
01373           char c[4];
01374           int  i;
01375   } translate;
01376   //spectra file
01377   FILE* outfile;
01378   //configuration file
01379 //  FILE* infile;
01380   //file name of spectra file 
01381   char filename[256];
01382   //struct to hold time from os
01383   time_t rawtime;
01384   //struct to hold values of date, time
01385   struct tm* timeinfo;
01386   //string buffer to facilitate talking to log
01387   char s[512];
01388   char szTemp[64];
01389   float mux, sigx; // mean and sigma of the count rate from the background
01390 
01391   atexit(End);
01392 //  signal(9, Quit);
01393 
01394   //get the current path, we'll use it if 
01395   //we can't find a path in the config file
01396   //and to find the config file
01397   if (argc > 0)
01398         strcpy(szExecutablePath,argv[0]);
01399 
01400   //default to 60 second spectra
01401   iCollectSeconds = 60;
01402 
01403   //default to doing a simple log file
01404   iDoLog = 1;
01405 
01406   //correct for windows .exe
01407   if (szExecutablePath[strlen(szExecutablePath)-4] == '.')
01408     szExecutablePath[strlen(szExecutablePath)-4] = 0;
01409   //build the configuration file path
01410   strcpy(szConfigPathFile,szExecutablePath);
01411   strcat(szConfigPathFile,".config");
01412   strcpy(szLogPathFile,szExecutablePath);
01413 
01414   reconfig1();
01415  
01416   print_to_log("Starting version: "PROGVERSION);
01417 
01418   usb_init(); /* initialize the library */
01419   usb_find_busses(); /* find all busses */
01420   usb_find_devices(); /* find all connected devices */
01421   //get a handle to the device 
01422   if((dev = open_dev()) == NULL)
01423   {
01424       print_to_log("ERROR: USB device not found!");
01425       exit(-1);
01426   }
01427 // begin bjb code
01428   ret=usb_get_driver_np(dev,0,buffr,sizeof(buffr)); 
01429   if (ret==0)
01430    {
01431     print_to_log("ERROR: interface 0 already claimed by another driver... attempting to detach it\n");
01432     ret=usb_detach_kernel_driver_np(dev,0);
01433     print_to_log("ERROR: usb_detach_kernel_driver_np failed\n");
01434    }                            // check if USB device is already claimed by a driver and attempt detach if it is
01435 // end bjb code
01436 
01437   //think this isn't necessary on this type of instrument
01438   //but should do it anyway
01439 //bjb  if(usb_set_configuration(dev, 1) < 0)
01440 //bjb  {
01441 //bjb      print_to_log("ERROR: USB setting config 1 failed");
01442 //bjb      usb_close(dev);
01443 //bjb      exit(-1);
01444 //bjb  }
01445   //get control of the interface--don't let anyone else
01446   //talk to the instrument while we are
01447   if(usb_claim_interface(dev, 0) < 0)
01448   {
01449       print_to_log("ERROR: USB claiming interface 0 failed");
01450       usb_close(dev);
01451       exit(-1);
01452   }
01453 // bjb code  begin
01454  ret=usb_set_altinterface(dev,0);
01455  assert(ret>=0);
01456 // end bjb code 
01457 
01458   if (reconfig2(dev) < 0)
01459         exit(-1);
01460 
01461   //read a spectra -- having some problems with garbage in the buffer
01462   //don't use this spectra
01463 //  iResult = usb_bulk_read(dev, EP_IN_SPECTRAA, spectra, 
01464 //      sizeof(spectra), 5000);
01465         
01466   //clear buffer A
01467   //this is the actual start of the first acquisition
01468   iResult = usb_control_msg(dev, USB_TYPE_VENDOR | USB_RECIP_OTHER, 
01469         0x80, 0, 0, spectra, 0, 5000);
01470   sleep(1);
01471   iResult = usb_control_msg(dev, USB_TYPE_VENDOR | USB_RECIP_OTHER, 
01472         0x80, 0, 0, spectra, 0, 5000);
01473         
01474   if (iResult < 0) 
01475   {
01476         sprintf(s,"ERROR: Cannot clear buffer.  usb_control_msg returned %d",iResult);
01477         print_to_log(s);
01478         exit(-1);
01479   }
01480 
01481   //get time at start of spectra
01482   time(&rawtime);
01483   timeinfo = localtime(&rawtime);
01484   //get time at start of the spectra
01485   ring_buffer[ring_insertion_point].time = rawtime;
01486   
01487   bInAlarm = FALSE;
01488   bHaveBG = FALSE;
01489   
01490   //repeat forever 
01491   for(iSeq = 1; iSeq <= 2; /*iSeq++*/)
01492   //while(1)
01493   {
01494         //check time of configuration file
01495         struct stat buf;
01496         stat(szConfigPathFile,&buf);
01497         timeinfo = localtime(&buf.st_mtime);
01498         sprintf(s,"%04d_%02d_%02d_%02d%02d%02d\n",
01499                 timeinfo->tm_year+1900,
01500                 timeinfo->tm_mon+1,
01501                 timeinfo->tm_mday,
01502                 timeinfo->tm_hour,
01503                 timeinfo->tm_min,
01504                 timeinfo->tm_sec);
01505         //did it change from startup
01506         if (strcmp(s,szConfigChange)!=0)
01507         {
01508                 //reconfig if it did change
01509                 reconfig1();
01510                 print_to_log("INFO: config file changed -- reconfiguring");
01511                 if (reconfig2(dev) < 0)
01512                         exit(-1);
01513         }
01514 
01515         iDuration = 0;
01516         iCollectUSec = iCollectSeconds*500000;
01517         //Sleep(iCollectMSec-25);
01518         usleep(iCollectUSec);//wait half
01519         while (iDuration < (iCollectSeconds*1000))
01520         {
01521                 //Sleep(5);
01522                 iCollectUSec = (iCollectSeconds*1000-iDuration)>>1;
01523                 if (iCollectUSec > 5)
01524                         usleep(iCollectUSec*1000);
01525                 if ((iResult = usb_bulk_read(dev, EP_IN_STATUSA, ring_buffer[ring_insertion_point].status, BUF_SIZE, 5000))
01526                         != (BUF_SIZE-1))
01527                 {
01528                         sprintf(s,"ERROR: bulk read STATUS A (%d) failed",iResult);
01529                         print_to_log(s);
01530                         return -1;
01531                 }
01532                 else
01533                 {
01534                         translate.c[0] = ring_buffer[ring_insertion_point].status[10];
01535                         translate.c[1] = ring_buffer[ring_insertion_point].status[11];
01536                         translate.c[2] = ring_buffer[ring_insertion_point].status[12];
01537                         translate.c[3] = 0;
01538                         iDuration = translate.i * 100 + ring_buffer[ring_insertion_point].status[9];//in milliseconds
01539                 }
01540         }
01541 
01542         //read the configuration from the instrument
01543         //although we currently have the config in cur_config this ensures that
01544         //the actual configuration is present (and it only takes 1msec)
01545         if ((iResult = usb_bulk_read(dev, EP_IN_CONFIGUR, cur_config, sizeof(cur_config), 5000)) 
01546                 != (sizeof(cur_config)-1))
01547         {
01548                 //log any error
01549                 sprintf(s,"ERROR: bulk read configuration (%d) failed",iResult);
01550                 print_to_log(s);
01551                 exit(-1);
01552         }
01553 
01554         //read the status from the instrument
01555         if ((iResult = usb_bulk_read(dev, EP_IN_STATUSA, 
01556                 ring_buffer[ring_insertion_point].status, BUF_SIZE, 5000)) != (BUF_SIZE-1))
01557         {
01558                 //put errors in the log file
01559                 sprintf(s,"ERROR: bulk read STATUS A (%d) failed",iResult);
01560                 print_to_log(s);
01561                 exit(-1);
01562         }
01563         else
01564         {
01565                 //put the duration in the spectra 
01566                 translate.c[0] = ring_buffer[ring_insertion_point].status[10];
01567                 translate.c[1] = ring_buffer[ring_insertion_point].status[11];
01568                 translate.c[2] = ring_buffer[ring_insertion_point].status[12];
01569                 translate.c[3] = 0;
01570                 ring_buffer[ring_insertion_point].fDuration = translate.i * 100 + ring_buffer[ring_insertion_point].status[9];//in milliseconds
01571                 //read the spectra data
01572                 iResult = usb_bulk_read(dev, EP_IN_SPECTRAA, spectra, 
01573                         sizeof(spectra), 5000);
01574                 //put the spectra in the spectra file
01575                 if ((iResult >= 0))// && outfile)
01576                 {
01577                         //zero the buffer and start the next acquisition
01578                         //clear inst buffer A
01579                         int iResult2;
01580                         iResult2 = usb_control_msg(dev, USB_TYPE_VENDOR | USB_RECIP_OTHER, 
01581                                 0x80, 0, 0, spectra, 0, 5000);
01582                         if (iResult2 < 0) 
01583                         {//error report on failure
01584                                 sprintf(s,"ERROR: usb_control_msg returned %d",iResult);
01585                                 print_to_log(s);
01586                                 exit(-1);
01587                         }
01588                 
01589                         //s/get time at start of the next spectra
01590                         time(&ring_buffer[(ring_insertion_point+1 % ring_size)].time);
01591 
01592                         ring_buffer[ring_insertion_point].iSum = 0;
01593                         ring_buffer[ring_insertion_point].iType = UNASSIGNED_TYPE;
01594                         
01595                         for (i = 0,j=0; i < iResult; i++)
01596                         {
01597                                 switch (i%3) 
01598                                 {
01599                                 case 0:
01600                                         translate.c[0] = spectra[i];
01601                                         break;
01602                                 case 1:
01603                                         translate.c[1] = spectra[i];
01604                                         break;
01605                                 case 2:
01606                                         translate.c[2] = spectra[i];
01607                                         ring_buffer[ring_insertion_point].iSum += translate.i;
01608                                         ring_buffer[ring_insertion_point].iSqSum += (double)(translate.i)*(double)(translate.i);  //bjb add
01609                                         ring_buffer[ring_insertion_point].fspectra[j++] = (double)translate.i;
01610                                         //ring_buffer[ring_insertion_point].fspectra[j++] = (double)ring_insertion_point;
01611                                         break;
01612                                 }
01613                         }
01614                         ring_buffer[ring_insertion_point].iSize = j;
01615                         
01616                         //do any alarm processing here
01617                         //consider reading the most recent background back in along with the offset and sqrt sum at startup so that
01618                         //      after initial bg we always have a bg and bg sqrt.
01619                         
01620                         //if not in alarm and have bg and (sum sqrt/time / sum bg sqrt/time) > sigma_alarm
01621                                 //set "in alarm" flag
01622                                 //clear then add all pre alarm spectras to alarm spectra  -- that we have received(may not have enough)
01623                                 //log event
01624                         if (!bInAlarm)//start an alarm state only if not currently in an alarm
01625                         {
01626                                 if (bHaveBG)//don't do an alarm unless we have sufficient bg
01627                                 {
01628                                         //is there an actual alarm situation
01629                                         //may need to change the division to use a "normalized isum"
01630                                         //printf("%f::%f::%f\n",
01631                                         //      sqrt(ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration),
01632                                         //      sqrt(bg_spectra.iSum/bg_spectra.fDuration),
01633                                         //      sigma_alarm);   //shorty commented out
01634                                         printf("%f::%f::%f\n",
01635                                                 sqrt(ring_buffer[ring_insertion_point].iSum)/ring_buffer[ring_insertion_point].fDuration,
01636                                                 sqrt(bg_spectra.iSum)/bg_spectra.fDuration,
01637                                                 sigma_alarm);                           // shorty addition              
01638                                         printf("iSqSum = %f\n",bg_spectra.iSqSum);      
01639                                         mux=bg_spectra.iSum/bg_spectra.fDuration;       // bjb addition
01640                                         sigx=sqrt(bg_spectra.iSum)/bg_spectra.fDuration;  // bjb addition
01641                                         
01642                                         if (ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration > mux+sigma_alarm*sigx) // bjb addition
01643                                         //if ((sqrt(ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration) /
01644                                         //  sqrt(bg_spectra.iSum/bg_spectra.fDuration)) > sigma_alarm) // bjb comment out
01645                                         //bjb addition//if ((bg_spectra.iSum/bg_spectra.fDuration+sigma_alarm*sqrt(bg_spectra.iSum)/bg_spectra.fDuration) < 
01646                                         //bjb addition//ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration) //bjb addition
01647                                         {
01648                                                 //set all the flags and accumulate the pre alarms
01649                                                 bInAlarm = TRUE;
01650                                                 iPostAlarmCount = 0;
01651                                                 ring_buffer[ring_insertion_point].iType = ALARM_TYPE;
01652                                                 zero_spectra(&alarm_spectra);
01653                                                 alarm_spectra.iType = ALARM_TYPE;
01654                                                 alarm_spectra.time = ring_buffer[ring_insertion_point].time;
01655                                                 alarm_spectra.iSize = ring_buffer[ring_insertion_point].iSize;
01656                                                 memcpy(alarm_spectra.status,ring_buffer[ring_insertion_point].status,BUF_SIZE);
01657                                                 int iExtractPoint;
01658                                                 print_to_log("Start of alarm."); 
01659                                                 for (i = 1; i <= pre_alarm; i++)
01660                                                 {
01661                                                         print_to_log("Pre-alarm spectra added to alarm spectra.");
01662                                                         iExtractPoint = ring_insertion_point-i;
01663                                                         if (iExtractPoint < 0) iExtractPoint += ring_size;
01664                                                         ring_buffer[iExtractPoint].iType = PREALARM_TYPE;
01665                                                         add_spectra(&alarm_spectra,&ring_buffer[iExtractPoint]);
01666                                                 }
01667                                         }
01668                                 }
01669                                 
01670                                 //create a bg spectra
01671                                 //if not in alarm and time for new background and have enough spectra to do bg
01672                                 //generate new background spectra based on current - pre-alarm - deadband + n of older spectra
01673                                 //set have bg
01674                                 //do peak search and save offset for alarm spectra
01675                                 //do re-bin to put into kEv
01676                                 //save bg
01677                                 if (!bInAlarm)
01678                                 {
01679                                         //do we have enough background potential spectra?
01680                                         int iTestPoint = ring_insertion_point;
01681                                         BOOL bNewBackground = TRUE;
01682                                         iTestPoint -= pre_alarm;
01683                                         iTestPoint -= dead_band;
01684                                         if (iTestPoint < 0) iTestPoint += ring_size;
01685                                         //check if the minimum number of spectra are available
01686                                         for (i=0; i<bg_minimum; i++)
01687                                         {
01688                                                 int iTemp = iTestPoint - i;
01689                                                 if (iTemp < 0) iTemp += ring_size;
01690                                                 if ((ring_buffer[iTemp].iType != BG_TYPE) && (ring_buffer[iTemp].iType != UNASSIGNED_TYPE))
01691                                                 {
01692                                                         bNewBackground = FALSE;
01693                                                         break; //break out of the for loop we don't have enough
01694                                                 }
01695                                         }
01696                                         if (bNewBackground)
01697                                         {
01698                                                 int iSpectraCount = 0;
01699                                                 time_t now;
01700                                                 zero_spectra(&tmp_spectra);
01701                                                 
01702                                                 tmp_spectra.time = ring_buffer[iTestPoint].time;
01703                                                 tmp_spectra.iSize = ring_buffer[iTestPoint].iSize;
01704                                                 tmp_spectra.iType = BG_TYPE;
01705                                                 
01706                                                 bg_spectra.time = ring_buffer[iTestPoint].time;
01707                                                 bg_spectra.iSize = spectraout_size;
01708                                                 
01709                                                 for (i = 0; i < BUF_SIZE; i++) bg_spectra.status[i] = ring_buffer[iTestPoint].status[i]; 
01710                                                 while (((ring_buffer[iTestPoint].iType == BG_TYPE) || 
01711                                                         (ring_buffer[iTestPoint].iType == UNASSIGNED_TYPE)) && 
01712                                                         (iSpectraCount < ring_size) && (iTestPoint != ring_insertion_point))
01713                                                 {
01714                                                         ring_buffer[iTestPoint].iType = BG_TYPE;
01715                                                         add_spectra(&tmp_spectra,&ring_buffer[iTestPoint]);
01716                                                         iTestPoint--;
01717                                                         if (iTestPoint < 0) iTestPoint += ring_size;
01718                                                         iSpectraCount++;
01719                                                 }
01720                                                 if (!bHaveBG)
01721                                                         print_to_log("Have Background");
01722                                                 bHaveBG = TRUE;
01723                                                 //re-bin for the number of bins
01724                                                 // shorty comment out//ReBIN(tmp_spectra.fspectra,
01725                                                 //shorty comment out//  tmp_spectra.iSize,rebin_spectra.fspectra,spectraout_size,
01726                                                 //shorty comment out//          (double)spectraout_size/(double)tmp_spectra.iSize,FALSE);
01727                                                 ReBIN(tmp_spectra.fspectra,tmp_spectra.iSize,rebin_spectra.fspectra,spectraout_size,
01728                                                 (double)spectraout_size/(double)tmp_spectra.iSize,FALSE); // shorty addition
01729                                                 //peak track
01730                                                 if (peaktrack_bin > 0)
01731                                                 {
01732                                                         fpeaktrack_offset = PeakTrack(rebin_spectra.fspectra, 
01733                                                                 spectraout_size, peaktrack_bin, peaktrack_roi, peaktrack_smooth);
01734                                                 }
01735                                                 else
01736                                                         fpeaktrack_offset = fpeaktrack_force;
01737                                                 //re-bin for the peak track
01738                                                 //shorty comment out//ReBIN(rebin_spectra.fspectra, 
01739                                                 //shorty comment out//  spectraout_size, bg_spectra.fspectra, spectraout_size, fpeaktrack_offset,FALSE);
01740                                                 ReBIN(rebin_spectra.fspectra, spectraout_size,bg_spectra.fspectra,spectraout_size, fpeaktrack_offset,FALSE); // shorty addition
01741                                                 bg_spectra.iType = BG_TYPE;
01742                                                 bg_spectra.iSize = spectraout_size;
01743                                                 bg_spectra.fDuration = tmp_spectra.fDuration;
01744                                                 bg_spectra.iSum = tmp_spectra.iSum;
01745                                                 //if n time since last bg write then write bg
01746                                                 time(&now);
01747                                                 if ((now - bg_previous) > bg_period)
01748                                                 {
01749                                                         sprintf(s,"Current Peak Track Offset %f",fpeaktrack_offset);
01750                                                         print_to_log(s);
01751                                                         bg_previous = now;
01752                                                         send_spectra(&bg_spectra,szSpectraPathFile,fpeaktrack_offset);
01753                                                         print_spectra(&bg_spectra,szSpectraPathFile,fpeaktrack_offset);
01754                                                 }
01755                                         }
01756                                 }
01757                         }
01758                                 
01759                         //if in alarm and post alarm count <= post alarm desired
01760                                 //add current to alarm spectra
01761                                 //decrement post alarms 
01762                                 //if post alarm count == post alarm desired
01763                                         //re-bin to bg spectra offset using computed and saved peak offset
01764                                         //print out the spectra 
01765                                         //log event
01766                         if (bInAlarm) //we are currently in an alarm situation
01767                         {
01768                                 //add the current spectra if needed
01769                                 if (iPostAlarmCount <= post_alarm)
01770                                 {
01771                                         add_spectra(&alarm_spectra,&ring_buffer[ring_insertion_point]);
01772                                         ring_buffer[ring_insertion_point].iType = (iPostAlarmCount==0)?ALARM_TYPE:POSTALARM_TYPE;
01773                                         print_to_log("Current spectra added to alarm spectra.");
01774                                 }
01775                                 //see if we have enough to stop adding spectra
01776                                 if (iPostAlarmCount == post_alarm)
01777                                 {
01778                                         time_t temptime;
01779                                         //re-bin to the output size
01780                                         ReBIN(alarm_spectra.fspectra,
01781                                                 alarm_spectra.iSize,
01782                                                 tmp_spectra.fspectra,
01783                                                 spectraout_size,
01784                                                 (double)spectraout_size/(double)alarm_spectra.iSize,
01785                                                 FALSE); // 
01786                                         //re-bin to the peaktrack_offset
01787                                         if (peaktrack_bin <= 0)
01788                                                 fpeaktrack_offset = fpeaktrack_force; 
01789                                         // shorty comment out ReBIN(alarm_spectra.fspectra, 
01790                                         // shorty comment out   alarm_spectra.iSize, tmp_spectra.fspectra, spectraout_size, fpeaktrack_offset,FALSE);
01791                                         ReBIN(tmp_spectra.fspectra, 
01792                                                 tmp_spectra.iSize, 
01793                                                 rebin_spectra.fspectra, 
01794                                                 spectraout_size, 
01795                                                 fpeaktrack_offset,
01796                                                 FALSE); // shorty addition
01797                                         //clear out the full memory
01798                                         memset(alarm_spectra.fspectra,0,sizeof(alarm_spectra.fspectra));
01799                                         //copy the rebin data back into the alarm spectra
01800                                         // shorty comment out memcpy(alarm_spectra.fspectra,tmp_spectra.fspectra,spectraout_size*sizeof(double));
01801                                         memcpy(alarm_spectra.fspectra,rebin_spectra.fspectra,spectraout_size*sizeof(double)); // shorty addition
01802                                         alarm_spectra.iSize=spectraout_size;
01803                                         //print out the alarm spectra
01804                                         send_spectra(&alarm_spectra,szAlarmPathFile,fpeaktrack_offset);                                 
01805                                         print_spectra(&alarm_spectra,szAlarmPathFile,fpeaktrack_offset);
01806                                         //print out the background spectra
01807                                         temptime = bg_spectra.time;
01808                                         bg_spectra.time = alarm_spectra.time;
01809                                         send_spectra(&bg_spectra,szAlarmBGPathFile,fpeaktrack_offset);                                  
01810                                         print_spectra(&bg_spectra,szAlarmBGPathFile,fpeaktrack_offset);
01811                                         bg_spectra.time = temptime;
01812                                         if (peaktrack_bin > 0)
01813                                         {
01814                                                 sprintf(s,"Peak Track Offset Used: %f",fpeaktrack_offset);
01815                                                 print_to_log(s);
01816                                         }
01817                                         //print_to_log("Alarm and Background spectra printed.");        
01818                                 }
01819                                 //if past post alarms and bg is down then drop out of alarm status
01820                                 else if (iPostAlarmCount > post_alarm)
01821                                 {
01822                                         mux=bg_spectra.iSum/bg_spectra.fDuration;
01823                                         sigx=sqrt(bg_spectra.iSum)/bg_spectra.fDuration;  // bjb addition
01824 
01825                                         if (iPostAlarmCount > force_noalarm)
01826                                         {
01827                                                 bInAlarm = FALSE;
01828                                                 iPostAlarmCount = 0;
01829                                                 bHaveBG = FALSE;
01830                                                 print_to_log("Forced out of alarm status. Building new BG.");
01831                                         }       
01832                                         else if (ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration <= mux+sigma_alarm*sigx) // bjb addition
01833                                         // bjb addition// else if ((bg_spectra.iSum/bg_spectra.fDuration+sigma_alarm*sqrt(bg_spectra.iSum)/bg_spectra.fDuration) > ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration)
01834                                         // else if (sqrt((ring_buffer[ring_insertion_point].iSum/ring_buffer[ring_insertion_point].fDuration) /
01835                                         // sqrt(bg_spectra.iSum / bg_spectra.fDuration) < sigma_alarm))
01836                                         {
01837                                                 bInAlarm = FALSE;
01838                                                 iPostAlarmCount = 0;
01839                                                 print_to_log("Normal out of alarm status. Keeping old BG.");
01840                                         }       
01841                                 }       
01842                                 //make sure we've gone beyond this if -- only do it once
01843                                 iPostAlarmCount++;
01844                         }
01845                                 
01846                         //point to the next in the ring buffer
01847                         ring_insertion_point = (ring_insertion_point + 1) % ring_size;
01848                         //keep track of how many we have in the buffer
01849                         //'cause we don't want to try to use spectra that aren't there
01850                         if (ring_count < ring_size) ring_count++;
01851                         
01852                 }
01853                 else
01854                 {
01855                         //put errors in the log file
01856                         sprintf(s,"ERROR: read SPECTRA (%d) failed",iResult);
01857                         print_to_log(s);
01858                         exit(-1);
01859                 }
01860         }
01861         //close the spectra file
01862         if (outfile)
01863                 fclose(outfile);
01864   }//repeat forever
01865 
01866   //actually the os will do these for us
01867   usb_release_interface(dev, 0);
01868   usb_close(dev);
01869 
01870   return 0;
01871 }


© 2007, Los Alamos National Security, LLC.