From d54fe7c1f704a63824c5bfa0ece65245572e9b27 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 4 Mar 2015 21:21:30 -0500 Subject: Initial commit --- src/analysis/bpm_combine.c | 548 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 548 insertions(+) create mode 100644 src/analysis/bpm_combine.c (limited to 'src/analysis/bpm_combine.c') diff --git a/src/analysis/bpm_combine.c b/src/analysis/bpm_combine.c new file mode 100644 index 0000000..86ec7d0 --- /dev/null +++ b/src/analysis/bpm_combine.c @@ -0,0 +1,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 +#include +#include +#include +#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; i4) 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; + +} + + -- cgit