aboutsummaryrefslogtreecommitdiff
path: root/src/analysis/bpm_combine.c
blob: 86ec7d071dee98cb347b65742525346f7c298744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548

/*****************************************************************************
 *	      Johns Hopkins University
 *	      Center For Astrophysical Sciences
 *	      FUSEbpm_combine
 *****************************************************************************
 *
 * Synopsis:	bpm_combine output_bpm_file combined_idf_file
 *		
 *
 * Description: Creates a bpm file associated with a combined idf file
 *              It gets the names of the idf files from the combined idf 
 *		file header. It gets the name of the bpm files from the idf 
 *		files header. These bpm files are then combined using the   
 *		offset information provided in the combined idf file header.  
 *		The BPM_CAL keyword is updated in the combined idf file 
 *		header.
 *
 * 		WARNING: all the single exposure IDF files (who took part in 
 *		the creation of the idf_file) and their associated BPM files
 *		must be in the working directory.
 *		
 *
 * History:	12/03/03	bjg	v1.0	Begin work.
 *              12/05/03        bjg             First version that compiles
 *              
 *		12/10/03 	bjg 	        Accept now only idf as parameter
 *                                              Updated NSPEC keyword and 
 *						SPECxxx, WOFFLxxx, WOFFSxxx
 *						keywords
 *						Removed paths in filenames 
 *						written into header.
 *              04/05/04        bjg             Remove unused variables
 *                                              Change formats to match arg
 *                                              types in printf
 *              05/25/04        bjg             Skip when BPM_CAL unpopulated 
 *                                              or not present.
 *              04/13/05	wvd	v2.0	combined_idf_file may be
 *						replaced by a file containing
 *						a list of BPM files.  The first
 *						line of this file must contain
 *						the number of entries that 
 *						follow.
 *
 ****************************************************************************/



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "calfuse.h"

typedef char filename[FLEN_CARD];



static char CF_PRGM_ID[]= "bpm_combine";
static char CF_VER_NUM[]= "2.0";

int main(int argc,char *argv[]){

  char  date[FLEN_CARD]={'\0'};      
  char     rootname[FLEN_CARD];
  char *corrected_filename;
  char *string_pointer;

  int      felem_hdu2;

  char     stime[FLEN_CARD],keyword[FLEN_CARD];

 

  time_t   vtime;

  fitsfile *infits,*outfits,*idffits;
  char     *has_bpm_list;
  filename *filelist;
  double   *expstartlist;
  double   *expendlist;
  long     *neventslist;
  double   *sicshiftlist;
  double   *lifshiftlist;
  double   *exptimelist;
  
  double delta_t;
  
  filename tempstring,tempstring2;
  double   tempdouble;
  long     templong;
  float    tempfloat;
  char     tempchar;
  
  double   minexpstart;
  int      minindex;
  
  int      nfiles;

  int      intnull=0,anynull;
  int      ncol;
  
  int      status=0;
  int      hdutype=0;
  int      tref = 0;

  long     nevents=0;
  long     n_real_events=0;	
  long     i,j,istart;

  
  float   * xfield,*yfield,*weightfield,*lambdafield;
  char   *channelfield;
  
  char     fmt_byte[FLEN_CARD],fmt_float[FLEN_CARD],fmt_short[FLEN_CARD];  
  
  double   totalexptime=0, rawtime=0;
  long     neventscreened=0, neventscreenedpha=0;
  float    timescreened=0, timesaa=0, timelowlimbangle=0, timeburst=0, timejitter=0,timenight=0;

  

  int  hdu2_tfields=5;
  
  char hdu2_extname[]="POTHOLE_DATA";   /* Name of this extension */
  
  char *hdu2_ttype[]={"X", "Y", "CHANNEL", "WEIGHT", "LAMBDA"};

  char *hdu2_tform[5];  /* We'll assign values when we know 
			    the number of elements in the data set. */


  char *hdu2_tunit[]={"PIXELS", "PIXELS", "UNITLESS", "UNITLESS", "ANGSTROMS"};

  
  FILE *fp=NULL;
  char line[FLEN_FILENAME];
  int  maxline=FLEN_FILENAME;

  
  if (argc != 3) {
    printf("Incorrect number of arguments.\n");
    printf("Calling sequence:bpm_combine bpm_file combined_idf_file\n");
    printf("Final argument may be the name of a file containing a list of BPM files.\n");
    printf("First line must be number of BPM files in list.\n");
    exit(1);
  }
  
  /* Initialize error checking. */
  cf_error_init(CF_PRGM_ID, CF_VER_NUM, stderr);
    
  cf_timestamp(CF_PRGM_ID, CF_VER_NUM, "Started execution.");
  
  /* get and display time */
  vtime = time(NULL) ;
  strcpy(stime,ctime(&vtime));
  
  fits_open_file(&idffits,argv[2],READONLY,&status);
  if (status) {
	status = 0;
	if ((fp = fopen(argv[2], "r")) == NULL) {
	    printf("Can't open file %s\n", argv[2]);
	    return 1;
	}
	if ((fgets(line, maxline, fp)) == NULL) {
	    printf("Error reading file %s\n", argv[2]);
	    return 1;
	}
        sscanf(line,"%d",&nfiles);
  }
  else FITS_read_key(idffits,TINT,"NSPEC",&nfiles,NULL,&status);
  
  filelist     = (filename *)malloc(nfiles*sizeof(filename));
  neventslist  = (long *)malloc(nfiles*sizeof(long));     
  sicshiftlist  = (double *)calloc((size_t)nfiles,sizeof(double)); 
  lifshiftlist  = (double *)calloc((size_t)nfiles,sizeof(double)); 
  expstartlist = (double *)malloc(nfiles*sizeof(double));
  expendlist   = (double *)malloc(nfiles*sizeof(double));
  exptimelist   = (double *)malloc(nfiles*sizeof(double));
  has_bpm_list     = (char *)malloc(nfiles*sizeof(char));

  for (i=0; i<nfiles;i++) {

    has_bpm_list[i]=1;
    
    if (fp != NULL) {
	if((fgets(line, maxline, fp)) == NULL) {
	   printf("Error reading %s\n", argv[2]);
           return 1;
	}
        sscanf(line, "%s", filelist[i]);
    }
    else {
	/* Read name of individual IDF file from combined file header. */
      sprintf(tempstring, "SPEC%.3ld", i+1);
      FITS_read_key(idffits,TSTRING,tempstring,tempstring2,NULL,&status);
	/* Open individual IDF file and read name of associated BPM file. */
      FITS_open_file(&infits,tempstring2,READONLY,&status);
      fits_read_key(infits,TSTRING,"BPM_CAL",filelist[i],NULL,&status);
      if (status) {
        status=0;
        FITS_close_file(infits,&status);
        printf("BPM_CAL keyword not found in IDF %s; skipping.\n",tempstring2);
        has_bpm_list[i]=0;
        filelist[i][0]='\0';
        continue;
      }
	/* Close individual IDF file. */
      FITS_close_file(infits,&status);

	/* These keywords don't apply to a user-provided list of BPM files. */
      sprintf(tempstring, "WOFFL%.3ld", i+1);
      FITS_read_key(idffits,TDOUBLE,tempstring,&lifshiftlist[i],NULL,&status);
    
      sprintf(tempstring, "WOFFS%.3ld", i+1);
      FITS_read_key(idffits,TDOUBLE,tempstring,&sicshiftlist[i],NULL,&status);
    }

    /* Open individual BPM file. */
    fits_open_file(&infits,filelist[i],READONLY,&status);
    if (status) {
      status=0;
      printf("Could not open BPM %s for IDF %s; BPM_CAL keyword may be incorrectly populated; skipping.\n",filelist[i],tempstring2);
      has_bpm_list[i]=0;
      filelist[i][0]='\0';
      continue;
    }

    FITS_read_key(infits,TDOUBLE,"EXPSTART",&(expstartlist[i]),NULL,&status);
    FITS_read_key(infits,TDOUBLE,"EXPEND",&(expendlist[i]),NULL,&status);
   
    FITS_read_key(infits,TDOUBLE,"EXPTIME",&(exptimelist[i]),NULL,&status); 
    totalexptime+=exptimelist[i];

    FITS_read_key(infits,TDOUBLE,"RAWTIME",&(tempdouble),NULL,&status);
    rawtime+=tempdouble;
    
    FITS_read_key(infits,TLONG,"NBADEVNT",&templong,NULL,&status);
    neventscreened+=templong;
    FITS_read_key(infits,TLONG,"NBADPHA",&templong,NULL,&status);
    neventscreenedpha+=templong;
    
    FITS_read_key(infits,TFLOAT,"EXP_BAD",&tempfloat,NULL,&status);
    timescreened+=tempfloat;
    FITS_read_key(infits,TFLOAT,"EXP_SAA",&tempfloat,NULL,&status);
    timesaa+=tempfloat;
    FITS_read_key(infits,TFLOAT,"EXP_LIM",&tempfloat,NULL,&status);
    timelowlimbangle+=tempfloat;
    FITS_read_key(infits,TFLOAT,"EXP_BRST",&tempfloat,NULL,&status);
    timeburst+=tempfloat;
    FITS_read_key(infits,TFLOAT,"EXP_JITR",&tempfloat,NULL,&status);
    timejitter+=tempfloat;
    FITS_read_key(infits,TFLOAT,"EXPNIGHT",&tempfloat,NULL,&status);
    timenight+=tempfloat;
    
    FITS_read_key(infits,TFLOAT,"NEVENTS",&templong,NULL,&status);
    n_real_events+=templong;

    
    FITS_movabs_hdu(infits,2,&hdutype,&status);
    
    FITS_read_key(infits,TSTRING,"TFORM1",&tempstring,NULL,&status);
    sscanf(tempstring,"%ld%c",&neventslist[i],&tempchar);
    nevents+=neventslist[i];
    
    FITS_close_file(infits,&status);
    
  }
  
  if (fp != NULL) fclose(fp);
  else FITS_close_file(idffits,&status); 
  
  
  
  

  
  printf("---SORTING INPUT FILES IN TIME ORDER---\n") ;
  
  for (i=0; i<nfiles-1;i++) {
    if (!(has_bpm_list[i])) continue;
    minexpstart=expstartlist[i];
    minindex=i;
    for (j=i+1; j<nfiles;j++) {
      if (!(has_bpm_list[j])) continue;
      if (expstartlist[j]<minexpstart){
	minexpstart=expstartlist[j];
	minindex=j;
      }
    }
    
    strcpy(tempstring,filelist[minindex]);
    strcpy(filelist[minindex],filelist[i]);
    strcpy(filelist[i],tempstring);
    
    tempdouble=expstartlist[minindex];
    expstartlist[minindex]=expstartlist[i];
    expstartlist[i]=tempdouble;
    
    tempdouble=expendlist[minindex];
    expendlist[minindex]=expendlist[i];
    expendlist[i]=tempdouble;

    tempdouble=exptimelist[minindex];
    exptimelist[minindex]=exptimelist[i];
    exptimelist[i]=tempdouble;
    
    templong=neventslist[minindex];
    neventslist[minindex]=neventslist[i];
    neventslist[i]=templong;
		    
    tempdouble=lifshiftlist[minindex];
    lifshiftlist[minindex]=lifshiftlist[i];
    lifshiftlist[i]=tempdouble;
    
    tempdouble=sicshiftlist[minindex];
    sicshiftlist[minindex]=sicshiftlist[i];
    sicshiftlist[i]=tempdouble;
    
  }

  istart=-1;

  for (i=0; i<nfiles;i++) {
    if (!(has_bpm_list[i])) continue; 
      printf("%s %7.1f %7.1f %4ld %f %f\n",filelist[i],expstartlist[i],expendlist[i],neventslist[i],lifshiftlist[i],sicshiftlist[i]);
      if (istart==-1) istart=i;
  }
  printf("\n");

  if (istart==-1) {
    printf("No BPM files found. Exiting\n");
    exit(0);
  }
 
 
  
  
  printf("--------CREATING OUTPUT FILE-----\n");
  
  FITS_open_file(&infits,filelist[istart],READONLY,&status);
  FITS_create_file(&outfits,argv[1],&status);
	
  
	
	
  printf("--------WRITING MAIN HEADER-----\n");
  
  FITS_copy_hdu(infits,outfits,0,&status);
	
  FITS_read_key(infits,TSTRING,"ROOTNAME",rootname,NULL,&status);
	
  rootname[8]='9';
  rootname[9]='9';
  rootname[10]='9';
	
  FITS_update_key(outfits,TSTRING,"ROOTNAME",rootname,NULL,&status);

  string_pointer=strrchr(argv[1],'/');
  if (string_pointer==NULL) corrected_filename=argv[1];
    else corrected_filename=&(string_pointer[1]); 
	
  FITS_update_key(outfits,TSTRING,"FILENAME",corrected_filename,NULL,&status);
	
  FITS_update_key(outfits,TSTRING,"EXP_ID","999",NULL,&status);
	
  string_pointer=strrchr(argv[2],'/');
  if (string_pointer==NULL) corrected_filename=argv[2];
    else corrected_filename=&(string_pointer[1]); 
	

  if (fp == NULL) 
  FITS_update_key(outfits,TSTRING,"IDF_FILE",corrected_filename,NULL,&status);




  fits_get_system_time(date, &tref, &status);
	
  FITS_update_key(outfits,TSTRING,"DATE",date,NULL,&status);
	
  FITS_update_key(outfits,TDOUBLE,"EXPEND",&expendlist[nfiles-1],NULL,&status);	
  FITS_update_key(outfits,TDOUBLE,"EXPTIME",&totalexptime,NULL,&status);
  FITS_update_key(outfits,TDOUBLE,"RAWTIME",&rawtime,NULL,&status);	
  FITS_update_key(outfits,TLONG,"NEVENTS",&n_real_events,NULL,&status);	
  FITS_update_key(outfits,TLONG,"NBADEVNT",&neventscreened,NULL,&status);
  FITS_update_key(outfits,TLONG,"NBADPHA",&neventscreenedpha,NULL,&status);
  FITS_update_key(outfits,TFLOAT,"EXP_BAD",&(timescreened),NULL,&status);
  FITS_update_key(outfits,TFLOAT,"EXP_SAA",&(timesaa),NULL,&status);
  FITS_update_key(outfits,TFLOAT,"EXP_LIM",&(timelowlimbangle),NULL,&status);
  FITS_update_key(outfits,TFLOAT,"EXP_BRST",&(timeburst),NULL,&status);		
  FITS_update_key(outfits,TFLOAT,"EXP_JITR",&(timejitter),NULL,&status);
  FITS_update_key(outfits,TFLOAT,"EXPNIGHT",&(timenight),NULL,&status);

    fits_write_history(outfits," COMBINED WITH BPM_COMBINE ",&status);
    
  FITS_update_key(outfits,TINT,"NSPEC",&nfiles,NULL,&status);
  
  for (i=0;i<nfiles;i++){

    if (!(has_bpm_list[i])) continue; 
    
    string_pointer=strrchr(filelist[i],'/');
    if (string_pointer==NULL) corrected_filename=filelist[i];
    else corrected_filename=&(string_pointer[1]); 
    
    sprintf(keyword, "SPEC%.3ld", i+1);
    FITS_update_key(outfits,TSTRING,keyword,corrected_filename,NULL,&status);
    sprintf(keyword, "WOFFL%.3ld", i+1);
    FITS_update_key(outfits,TFLOAT,keyword,&lifshiftlist[i],NULL,&status);
    sprintf(keyword, "WOFFS%.3ld", i+1);
    FITS_update_key(outfits,TFLOAT,keyword,&sicshiftlist[i],NULL,&status);
  
      

  }



  
  FITS_close_file(infits,&status);
  
  printf("--------PREPARING EVENTS LIST HDU-----\n");
  
  /* Generate the tform array */
  sprintf(fmt_byte,  "%ldB", nevents);
  sprintf(fmt_float, "%ldE", nevents);
  sprintf(fmt_short, "%ldI", nevents);
  
  hdu2_tform[0] = fmt_float;
  hdu2_tform[1] = fmt_float;
  hdu2_tform[2] = fmt_byte;
  hdu2_tform[3] = fmt_float;
  hdu2_tform[4] = fmt_float;
  
  

  /* Append a new empty binary table to the output file */
  FITS_create_tbl(outfits, BINARY_TBL, 1, hdu2_tfields, hdu2_ttype, hdu2_tform,
		  hdu2_tunit, hdu2_extname, &status);
  
 
  
  
  
 
  
  printf("--------COMBINING HDU-----\n");
  felem_hdu2=1;
  for (i=0;i<nfiles;i++){
    if (!(has_bpm_list[i])) continue;
    
    
    delta_t=(expstartlist[i]-expstartlist[0])*3600*24;
    
    FITS_open_file(&infits,filelist[i],READONLY,&status);

   
    FITS_movabs_hdu(infits, 2, &hdutype, &status);
    FITS_movabs_hdu(outfits, 2, &hdutype, &status);
    printf("Processing file %s\n",filelist[i]);
    
    xfield=(float *)malloc(neventslist[i]*sizeof(float));
    yfield=(float *)malloc(neventslist[i]*sizeof(float));
    channelfield=(char *)malloc(neventslist[i]*sizeof(char));
    weightfield=(float *)malloc(neventslist[i]*sizeof(float));
    lambdafield=(float *)malloc(neventslist[i]*sizeof(float));
    
    
    FITS_get_colnum(infits, TRUE, "X", &ncol, &status);
    FITS_read_col(infits, TFLOAT, ncol, 1, 1, neventslist[i], &intnull,
		  xfield, &anynull, &status);
    
    
    FITS_get_colnum(infits, TRUE, "Y", &ncol, &status);
    FITS_read_col(infits, TFLOAT, ncol, 1, 1, neventslist[i], &intnull,
		  yfield, &anynull, &status);
   

    FITS_get_colnum(infits, TRUE, "CHANNEL", &ncol, &status);
    FITS_read_col(infits, TBYTE, ncol, 1, 1, neventslist[i], &intnull,
		  channelfield, &anynull, &status);
    

    FITS_get_colnum(infits, TRUE, "WEIGHT", &ncol, &status);
    FITS_read_col(infits, TFLOAT, ncol, 1, 1, neventslist[i], &intnull,
		  weightfield, &anynull, &status);
   
		
    FITS_get_colnum(infits, TRUE, "LAMBDA", &ncol, &status);
    FITS_read_col(infits, TFLOAT, ncol, 1, 1, neventslist[i], &intnull,
		  lambdafield, &anynull, &status);
    
    

    
    for (j=0;j<neventslist[i];j++){
	if (channelfield[j]<5) lambdafield[j]=lambdafield[j]+lifshiftlist[i];  
        if (channelfield[j]>4) lambdafield[j]=lambdafield[j]+sicshiftlist[i]; 
	weightfield[j]=weightfield[j]*exptimelist[i]/totalexptime;
    }


    
    FITS_write_col(outfits, TFLOAT, 1, 1, felem_hdu2, neventslist[i], xfield, &status);
    FITS_write_col(outfits, TFLOAT, 2, 1, felem_hdu2, neventslist[i], yfield, &status);
    FITS_write_col(outfits, TBYTE, 3, 1, felem_hdu2, neventslist[i], channelfield, &status);
    FITS_write_col(outfits, TFLOAT, 4, 1, felem_hdu2, neventslist[i], weightfield, &status);
    FITS_write_col(outfits, TFLOAT, 5, 1, felem_hdu2, neventslist[i], lambdafield, &status);
    
    
    free(xfield);
    free(yfield);
    free(channelfield);
    free(weightfield);
    free(lambdafield);
    
    felem_hdu2+=neventslist[i];
    
    FITS_close_file(infits,&status);
    
    
  }
  
  printf("--------CLOSING OUTPUT FILE-----\n");

  
  FITS_close_file(outfits,&status);
  
  
  if (fp == NULL) {
    FITS_open_file(&idffits,argv[2],READWRITE,&status);

    string_pointer=strrchr(argv[1],'/');
    if (string_pointer==NULL) corrected_filename=argv[1];
    else corrected_filename=&(string_pointer[1]);  

    FITS_update_key(idffits,TSTRING,"BPM_CAL",corrected_filename,NULL,&status);
    FITS_close_file(idffits,&status);
  }
  

  cf_timestamp(CF_PRGM_ID, CF_VER_NUM, "Finished execution.");
  return EXIT_SUCCESS;
  
}