41#pragma GCC diagnostic ignored "-Wstrict-prototypes"
43#pragma GCC diagnostic pop
46#include <gsl/gsl_interp.h>
47#include <gsl/gsl_spline.h>
48#include <gsl/gsl_rng.h>
49#include <lal/LALString.h>
50#include <lal/TimeSeries.h>
51#include <lal/LALDatatypes.h>
53#include <lal/UserInput.h>
54#include <lal/LogPrintf.h>
55#include <lal/LALFrameIO.h>
56#include <lal/LALFrStream.h>
62#define MAX_DT 0.000976562
63#define MIN_DT 0.000244140625
64#define GPSMINUSTAI 441417609
65#define TIMESTAMPDELTAT 1
66#define MAXTIMESTAMPDELTAT 256
68#define STRINGLENGTH 256
69#define LONGSTRINGLENGTH 1024
71#define NUSELESSDATAMODE 5
72#define MINFRAMELENGTH 10
76#define MAXZEROCOUNT 1000
80#define XTEFITSTOFRAMEC_ENULL 1
81#define XTEFITSTOFRAMEC_ESYS 2
82#define XTEFITSTOFRAMEC_EINPUT 3
83#define XTEFITSTOFRAMEC_EMEM 4
84#define XTEFITSTOFRAMEC_ENONULL 5
85#define XTEFITSTOFRAMEC_EXLAL 6
86#define XTEFITSTOFRAMEC_MSGENULL "Arguments contained an unexpected null pointer"
87#define XTEFITSTOFRAMEC_MSGESYS "System call failed (probably file IO)"
88#define XTEFITSTOFRAMEC_MSGEINPUT "Invalid input"
89#define XTEFITSTOFRAMEC_MSGEMEM "Out of memory. Bad."
90#define XTEFITSTOFRAMEC_MSGENONULL "Output pointer is non-NULL"
91#define XTEFITSTOFRAMEC_MSGEXLAL "XLALFunction-call failed"
294const char *
APID[6] = {
"FS37",
"FS3b",
"FS3f",
"FS4f",
"XENO",
"FS46"};
299const char *
USELESSDATAMODE[5] = {
"D_1US_0_249_1024_64S_F",
"D_1US_0_249_128_1S_F",
"D_1US_0_249_128_1S_2LLD_F",
"CB",
"GoodXenon"};
303int main(
int argc,
char *argv[]);
350int main(
int argc,
char *argv[] ) {
361 gsl_set_error_handler_off();
380 LogPrintf(
LOG_CRITICAL,
"%s : User requested barycentering but no barycentered timestamps found in file %s.\n",fn,uvar.inputfile);
489 uvar->inputfile = NULL;
490 uvar->outputdir = NULL;
503 if ( should_exit ) { exit(1); }
507 for (
int i=0;
i<argc;
i++) {
508 strcat(clargs,argv[
i]);
532 if ((*fitsfiledata) != NULL) {
550 (*fitsfiledata)->array = NULL;
551 (*fitsfiledata)->event = NULL;
552 (*fitsfiledata)->gti = NULL;
553 (*fitsfiledata)->stamps = NULL;
554 header = (*fitsfiledata)->header;
557 if (fits_open_file(&fptr,filepath,READONLY,&
status)) {
558 fits_report_error(stderr,
status);
566#pragma GCC diagnostic push
567#pragma GCC diagnostic ignored "-Wstringop-truncation"
571#pragma GCC diagnostic pop
590 LogPrintf(
LOG_CRITICAL,
"%s : XLALReadFITSTimeStamps() failed to read time stamps information from FITS file %s with error = %d.\n",fn,filepath,
xlalErrno);
647 if (fits_close_file(fptr,&
status)) {
648 fits_report_error(stderr,
status);
672 REAL8 doublenull = 0.0;
681 if ((*gti) != NULL) {
691 if (fits_movabs_hdu(fptr,3,&hdutype,&
status)) {
692 fits_report_error(stderr,
status);
700 fits_report_error(stderr,
status);
713 if (fits_get_num_cols(fptr,&ngticols,&
status)) {
714 fits_report_error(stderr,
status);
725 if (fits_get_num_rows(fptr,&ngtirows,&
status)) {
726 fits_report_error(stderr,
status);
744 if (fits_read_col(fptr,TDOUBLE,1,1,1,(*gti)->length,&doublenull,(*gti)->start,&anynull,&
status)) {
745 fits_report_error(stderr,
status);
749 if (fits_read_col(fptr,TDOUBLE,2,1,1,(*gti)->length,&doublenull,(*gti)->end,&anynull,&
status)) {
750 fits_report_error(stderr,
status);
757 for (
i=0;
i<(*gti)->length;
i++) {
758 (*gti)->start[
i] += gpsoffset;
759 (*gti)->end[
i] += gpsoffset;
760 LogPrintf(
LOG_DEBUG,
"%s : found GTI range %6.12f -> %6.12f.\n",fn,(*gti)->start[
i],(*gti)->end[
i]);
801 if ((
c = strrchr(
header->file,
'/')))
c++;
809 printf(
"Warning: truncated filename %s\n",
header->filename);
824 if (fits_movabs_hdu(fptr,2,&hdutype,&
status)) {
825 fits_report_error(stderr,
status);
832 fits_report_error(stderr,
status);
853 fits_report_error(stderr,
status);
866 fits_report_error(stderr,
status);
871 fits_report_error(stderr,
status);
878 if (strstr(
header->filename,
"XENON") == NULL) {
882 fits_report_error(stderr,
status);
899 fits_report_error(stderr,
status);
907 fits_report_error(stderr,
status);
917 fits_report_error(stderr,
status);
921 if (strcmp(
type,
"ARRAY")==0)
header->type = 0;
922 else if ( (strcmp(
type,
"EVENTS") == 0 ) || (strcmp(
type,
"EVENT") == 0 ) )
header->type = 1;
953 if (fits_read_key(fptr,TSTRING,keyword,&colnamestring,
comment,&
status)) {
954 fits_report_error(stderr,
status);
958 if ( ((strstr(colnamestring,
"Cnt")!=NULL) && (strstr(colnamestring,
"MeanCnt")==NULL)
959 && (strstr(colnamestring,
"RemainingCnt")==NULL) && (strstr(colnamestring,
"VLECnt")==NULL)
960 && (strstr(colnamestring,
"VpCnt")==NULL)) || ((strncmp(colnamestring,
"Event",5)==0))) {
961 header->XeCntcolidx[idx] =
i+1;
988 fits_report_error(stderr,
status);
1001 fits_report_error(stderr,
status);
1034 if (fits_read_key_longstr(fptr,keyword,&tddes_string,
comment,&
status)) {
1035 fits_report_error(stderr,
status);
1036 LogPrintf(
LOG_CRITICAL,
"%s : fits_read_key_longstr() failed to read in long string keyword %s.\n",fn,keyword);
1061 if (fits_read_tdim(fptr,col,maxdim,&naxis,naxes,&
status)) {
1062 fits_report_error(stderr,
status);
1063 LogPrintf(
LOG_CRITICAL,
"%s : fits_read_tdim() failed to read in the number and size of dimensions in col %d.\n",fn,col);
1067 LogPrintf(
LOG_CRITICAL,
"%s : the size of dimensions in col %d = %d. Can only deal with 1 or 2 dimensional tables.\n",fn,col,naxis);
1070 if (naxis == 1) naxes[1] = 1;
1072 LogPrintf(
LOG_DEBUG,
"%s : read dim as %d nchannels as %ld and channelsize as %ld for col %d\n",fn,naxis,naxes[1],naxes[0],col);
1076 if (naxes[1] !=
header->tddes[
i]->nchannels) {
1077 LogPrintf(
LOG_CRITICAL,
"%s : The number of energy channels read from TDDES %d != %ld the number given by the TDIM keyword for col %d.\n",fn,
header->tddes[
i]->nchannels,naxes[1],col);
1089 if (fits_hdr2str(fptr,0,NULL,0,&
dummy,&nkeys,&
status)) {
1090 fits_report_error(stderr,
status);
1102 for (
i=0;
i<nkeys;
i++) {
1107 snprintf(kwpair,80,
"%s",
dummy+idx);
1108 snprintf(tempstr,81,
"\n%s",kwpair);
1109 strncat(
header->headerdump,tempstr,81);
1134 (*temp) = *(
temp+1);
1162 double doublenull = 0.0;
1171 long int numrows = 0;
1174 if ((*stamps) != NULL) {
1190 if (fits_movabs_hdu(fptr,2,&hdutype,&
status)) {
1191 fits_report_error(stderr,
status);
1192 LogPrintf(
LOG_CRITICAL,
"%s : fits_movabs_hdu() failed to move to the first extension header.\n",fn);
1199 fits_report_error(stderr,
status);
1208 fits_report_error(stderr,
status);
1219 if (fits_get_num_rows(fptr,&numrows,&
status)) {
1220 fits_report_error(stderr,
status);
1224 (*stamps)->length = numrows;
1229 fits_report_error(stderr,
status);
1240 for (
i=0;
i<ncols;
i++) {
1242 if (fits_read_key(fptr,TSTRING,keyword,×tring,
comment,&
status)) {
1243 fits_report_error(stderr,
status);
1257 if (((*stamps)->dettime = (
double *)
LALCalloc((*stamps)->length,
sizeof(
double))) == NULL) {
1263 if (fits_read_col(fptr,TDOUBLE,detidx,1,1,(*stamps)->length,&doublenull,(*stamps)->dettime,&anynull,&
status)) {
1264 fits_report_error(stderr,
status);
1270 for (
j=0;
j<(*stamps)->length;
j++) (*stamps)->dettime[
j] += gpsoffset;
1271 LogPrintf(
LOG_DEBUG,
"%s : read first detector frame timestamp as %6.12f\n",fn,(*stamps)->dettime[0]);
1283 if (((*stamps)->barytime = (
double *)
LALCalloc((*stamps)->length,
sizeof(
double))) == NULL) {
1289 if (fits_read_col(fptr,TDOUBLE,baryidx,1,1,(*stamps)->length,&doublenull,(*stamps)->barytime,&anynull,&
status)) {
1290 fits_report_error(stderr,
status);
1291 LogPrintf(
LOG_CRITICAL,
"%s : fits_read_col() failed to read in barycentric frame timestamps.\n",fn);
1296 for (
j=0;
j<(*stamps)->length;
j++) (*stamps)->barytime[
j] += gpsoffset;
1297 LogPrintf(
LOG_DEBUG,
"%s : read first barycentered time value as %6.12f\n",fn,(*stamps)->barytime[0]);
1300 else (*stamps)->barytime = NULL;
1324 INT4 totallength = 0;
1325 UINT4 *tempdata = NULL;
1326 CHAR *tempundefined = NULL;
1330 if ((*array) != NULL) {
1342 col =
header->XeCntcolidx[colidx];
1343 if ((col < 1) || (col>
header->ncols)) {
1350 tddes =
header->tddes[colidx];
1369 LogPrintf(
LOG_DEBUG,
"%s : computed total number of expected data samples as %d\n",fn,totallength);
1380 LogPrintf(
LOG_DEBUG,
"%s : allocated memory for data (approx %.1f MB)\n",fn,totallength*2
e-6);
1383 if (fits_read_colnull(fptr,TINT,col,1,1,totallength,tempdata,tempundefined,&anynull,&
status)) {
1384 fits_report_error(stderr,
status);
1385 LogPrintf(
LOG_CRITICAL,
"%s : fits_read_colnull() failed to read col %d in science array data.\n",fn,col);
1396 (*array)->channeldata[
i].energy[0] = tddes->
minenergy[
i];
1397 (*array)->channeldata[
i].energy[1] = tddes->
maxenergy[
i];
1403 snprintf(
temp,12,
"%d",
j);
1404 strcat((*array)->channeldata[
i].detconfig,
temp);
1409 (*array)->channeldata[
i].deltat = tddes->
deltat;
1410 (*array)->channeldata[
i].rowlength =
header->rowlength[colidx];
1417 if (((*array)->channeldata[
i].data = (
UINT4 *)
LALCalloc((*array)->channeldata[
i].length,
sizeof(
UINT4))) == NULL) {
1421 if (((*array)->channeldata[
i].undefined = (
CHAR *)
LALCalloc((*array)->channeldata[
i].length,
sizeof(
CHAR))) == NULL) {
1425 LogPrintf(
LOG_DEBUG,
"%s : allocated memory for data (approx %.1f MB)\n",fn,(*array)->channeldata[
i].length*2
e-6);
1435 (*array)->channeldata[
i].data[idx] = tempdata[
k];
1436 (*array)->channeldata[
i].undefined[idx] = tempundefined[
k];
1444 int M = (*array)->channeldata[
i].length > 10 ? 10 : (*array)->channeldata[
i].length;
1484 if ((*
event) != NULL) {
1496 col =
header->XeCntcolidx[colidx];
1497 if ((col < 1) || (col>
header->ncols)) {
1504 tddes =
header->tddes[colidx];
1518 (*event)->nchannels = 1;
1522 (*event)->channeldata[0].energy[0] = tddes->
minenergy[0];
1528 snprintf(
temp,12,
"%d",
j);
1529 strcat((*event)->channeldata[0].detconfig,
temp);
1533 (*event)->channeldata[0].deltat = tddes->
deltat;
1536 (*event)->channeldata[0].nevents =
header->nrows;
1537 (*event)->channeldata[0].rowlength =
header->rowlength[0];
1538 (*event)->channeldata[0].length = (
INT8)(
header->rowlength[0]*
header->nrows);
1543 if (((*event)->channeldata[0].data = (
CHAR *)
LALCalloc((*event)->channeldata[0].length,
sizeof(
CHAR))) == NULL ) {
1552 if (fits_read_col_bit(fptr,col,1,1,(*event)->channeldata[0].length,(*event)->channeldata[0].data,&
status)) {
1553 fits_report_error(stderr,
status);
1563 int M = (*event)->channeldata[0].nevents > 100 ? 100 : (*event)->channeldata[0].nevents;
1566 LogPrintf(
LOG_DEBUG,
"%d,",(*event)->channeldata[0].data[(*event)->channeldata[0].rowlength*
i]);
1590 char *Dstart,*Dend,*Estart,*Eend,*Tstart,*Tend;
1591 CHAR *Dstring,*Estring,*Tstring;
1592 INT4 Dlength,Elength,Tlength;
1609 (*params)->deltat = 0.0;
1610 (*params)->nsamples = 0;
1611 (*params)->offset = 0.0;
1621 if ((Dend = strstr(Dstart,
"]"))==NULL)
return XLAL_SUCCESS;
1622 Dlength = strlen(Dstart) - strlen(Dend) + 1;
1626 snprintf(Dstring,Dlength,
"%s",Dstart);
1627 strcat(Dstring,
",");
1632 while ((
c1 = strstr(
c2,
",")) != NULL) {
1639 (*params)->ndetconfig = Dcount;
1646 while ((
c1 = strstr(
c2,
",")) != NULL) {
1650 sublen = 1 + strcspn(
c2,
",");
1652 snprintf(subs,sublen,
"%s",
c2);
1655 if ((til = strstr(subs,
"~")) != NULL) {
1657 INT4 e1len = 1 + strcspn(subs,
"~");
1658 INT4 e2len = sublen - e1len;
1662 snprintf(e1,e1len,
"%s",subs);
1663 snprintf(e2,e2len,
"%s",til+1);
1666 for (
k=
s;
k<=
e;
k++) (*params)->detectors[Dcount][
k] = 1;
1673 if (strstr(subs,
"0") != NULL) (*params)->detectors[Dcount][0] = 1;
1674 if (strstr(subs,
"1") != NULL) (*params)->detectors[Dcount][1] = 1;
1675 if (strstr(subs,
"2") != NULL) (*params)->detectors[Dcount][2] = 1;
1676 if (strstr(subs,
"3") != NULL) (*params)->detectors[Dcount][3] = 1;
1677 if (strstr(subs,
"4") != NULL) (*params)->detectors[Dcount][4] = 1;
1680 LogPrintf(
LOG_DEBUG,
"%s : read detectors as [%d %d %d %d %d]\n",fn,(*params)->detectors[Dcount][0],
1681 (*params)->detectors[Dcount][1],(*params)->detectors[Dcount][2],
1682 (*params)->detectors[Dcount][3],(*params)->detectors[Dcount][4]);
1699 if ((Eend = strstr(Estart,
"]"))==NULL)
return XLAL_SUCCESS;
1700 Elength = strlen(Estart) - strlen(Eend) + 1;
1704 snprintf(Estring,Elength,
"%s",Estart);
1705 strcat(Estring,
",");
1710 while ((
c1 = strstr(
c2,
",")) != NULL) {
1717 (*params)->nenergy = Ecount;
1724 while ((
c1 = strstr(
c2,
",")) != NULL) {
1728 sublen = 1 + strcspn(
c2,
",");
1730 snprintf(subs,sublen,
"%s",
c2);
1733 if ((til = strstr(subs,
"~")) != NULL) {
1735 INT4 e1len = 1 + strcspn(subs,
"~");
1736 INT4 e2len = sublen - e1len;
1739 snprintf(e1,e1len,
"%s",subs);
1740 snprintf(e2,e2len,
"%s",til+1);
1741 (*params)->minenergy[Ecount] = atoi(e1);
1742 (*params)->maxenergy[Ecount] = atoi(e2);
1747 (*params)->minenergy[Ecount] = atoi(subs);
1748 (*params)->maxenergy[Ecount] = atoi(subs);
1750 LogPrintf(
LOG_DEBUG,
"%s : read energies as %d %d\n",fn,(*params)->minenergy[Ecount],(*params)->maxenergy[Ecount]);
1767 if (((*params)->nenergy == 1) || ((*params)->ndetconfig == 1)) {
1768 (*params)->nchannels = (*params)->nenergy*(*params)->ndetconfig;
1776 if ((*params)->nenergy == 1) {
1777 (*params)->minenergy = (
INT4 *)
XLALRealloc((*params)->minenergy,(*params)->nchannels*
sizeof(
INT4));
1778 (*params)->maxenergy = (
INT4 *)
XLALRealloc((*params)->maxenergy,(*params)->nchannels*
sizeof(
INT4));
1781 for (
i=1;
i<(*params)->nchannels;
i++) {
1782 (*params)->minenergy[
i] = (*params)->minenergy[0];
1783 (*params)->maxenergy[
i] = (*params)->maxenergy[0];
1787 if ((*params)->ndetconfig == 1) {
1788 (*params)->detectors = (
INT4 **)
XLALRealloc((*params)->detectors,(*params)->nchannels*
sizeof(
INT4 *));
1792 for (
i=1;
i<(*params)->nchannels;
i++) {
1794 for (
j=0;
j<
NPCU;
j++) (*params)->detectors[
i][
j] = (*params)->detectors[0][
j];
1806 if ((Tend = strstr(Tstart,
"]"))==NULL)
return XLAL_SUCCESS;
1807 Tlength = strlen(Tstart) - strlen(Tend) + 1;
1811 snprintf(Tstring,Tlength,
"%s",Tstart);
1812 strcat(Tstring,
";");
1817 while ((
c1 = strstr(
c2,
";")) != NULL) {
1830 c1 = strstr(
c2,
";");
1831 sublen = 1 + strcspn(
c2,
";");
1833 snprintf(sub,sublen,
"%s",
c2);
1834 (*params)->offset = atof(sub);
1839 c1 = strstr(
c2,
";");
1840 sublen = 1 + strcspn(
c2,
";");
1842 snprintf(sub,sublen,
"%s",
c2);
1843 (*params)->deltat = atof(sub);
1848 c1 = strstr(
c2,
";");
1849 sublen = 1 + strcspn(
c2,
";");
1851 snprintf(sub,sublen,
"%s",
c2);
1852 (*params)->nsamples = atoi(sub);
1884 if ((*
ts) != NULL) {
1892 if (fits->
header == NULL) {
1955 (*ts)->length = nts;
1985 if ((*
ts) != NULL) {
1989 if (
event == NULL) {
1993 if (stamps == NULL) {
2015 (*ts)->energy[0] =
event->energy[0];
2016 (*ts)->energy[1] =
event->energy[1];
2017 snprintf((*ts)->detconfig,
NPCU+1,
"%s",
event->detconfig);
2029 for (
i=0;
i<(*ts)->length;
i++) (*ts)->undefined[
i] = 0;
2032 for (
i=0;
i<
event->nevents;
i++) {
2035 long int k =
i*
event->rowlength;
2038 long int idx = (
long int)floor((stamps->
dettime[
i]-stamps->
dettime[0])/(*ts)->deltat);
2041 (*ts)->data[idx] += (
unsigned char)
event->data[
k];
2070 if ((*
ts) != NULL) {
2078 if (fits->
header == NULL) {
2142 (*ts)->length = nts;
2170 INT2 *temp_undefined = NULL;
2173 if ((*
ts) != NULL) {
2177 if (array == NULL) {
2181 if (stamps == NULL) {
2203 (*ts)->energy[0] = array->
energy[0];
2204 (*ts)->energy[1] = array->
energy[1];
2216 if ((temp_undefined = (
short int *)
LALCalloc((*ts)->length,
sizeof(
short int))) == NULL) {
2223 for (
i=0;
i<(*ts)->length;
i++) {
2225 (*ts)->undefined[
i] = 1;
2226 temp_undefined[
i] = 0;
2243 if ((idxin>=array->
length) || (idxout>=(*ts)->length)) printf(
"idxin = %ld (%ld) idxout = %ld (%ld)\n",idxin,(
long int)array->
length,idxout,(
long int)(*ts)->length);
2246 (*ts)->data[idxout] += array->
data[idxin];
2247 temp_undefined[idxout] += array->
undefined[idxin];
2254 for (
i=0;
i<(*ts)->length;
i++) {
2255 if (temp_undefined[
i] > 1) (*ts)->undefined[
i] = 1;
2256 else (*ts)->undefined[
i] = 0;
2279 LogPrintf(
LOG_CRITICAL,
"%s : tried to allocate an XTEUINT4TimeSeries with non-positive size.\n",fn);
2293 LogPrintf(
LOG_CRITICAL,
"%s : failed to allocate memory for an XTEUINT4TimeSeries data vector.\n",fn);
2296 if (((*x)->undefined = (
char *)
LALCalloc(
N,
sizeof(
char))) == NULL) {
2297 LogPrintf(
LOG_CRITICAL,
"%s : failed to allocate memory for an XTEUINT4TimeSeries data quality vector.\n",fn);
2322 if (
x->event != NULL) {
2323 for (
i=0;
i<
x->header->nXeCntcol;
i++) {
2324 if (
x->event[
i] != NULL) {
2325 for (
j=0;
j<
x->event[
i]->nchannels;
j++) {
2326 if (
x->event[
i]->channeldata[
j].data != NULL)
XLALFree(
x->event[
i]->channeldata[
j].data);
2327 if (
x->event[
i]->channeldata[
j].undefined != NULL)
XLALFree(
x->event[
i]->channeldata[
j].undefined);
2336 if (
x->array != NULL) {
2337 for (
i=0;
i<
x->header->nXeCntcol;
i++) {
2338 if (
x->array[
i] != NULL) {
2339 for (
j=0;
j<
x->array[
i]->nchannels;
j++) {
2340 if (
x->array[
i]->channeldata[
j].data != NULL)
XLALFree(
x->array[
i]->channeldata[
j].data);
2341 if (
x->array[
i]->channeldata[
j].undefined != NULL)
XLALFree(
x->array[
i]->channeldata[
j].undefined);
2350 if (
x->header != NULL) {
2352 for (
i=0;
i<
x->header->nXeCntcol;
i++) {
2356 for (
j=0;
j<
x->header->tddes[
i]->nchannels;
j++)
XLALFree(
x->header->tddes[
i]->detectors[
j]);
2367 if (
x->gti != NULL) {
2374 if (
x->stamps != NULL) {
2405 if (
ts->ts != NULL) {
2406 for (
i=0;
i<
ts->length;
i++) {
2407 if (
ts->ts[
i] != NULL) {
2465 if ((*
ts) == NULL) {
2475 for (
i=0;
i<(*ts)->length;
i++) {
2481 LogPrintf(
LOG_DEBUG,
"%s : applied GTI table to data from column (%d/%d).\n",fn,
i+1,(*ts)->length);
2499 INT8 newstartindex,newendindex,newN;
2503 if ((*
ts) == NULL) {
2519 bti->
start[0] = (*ts)->tstart;
2531 bti->
end[gti->
length] = (*ts)->tstart + (*ts)->T;
2539 long int sindex = (bti->
start[
i] - (*ts)->tstart)/(*ts)->deltat;
2540 long int eindex = (bti->
end[
i] - (*ts)->tstart)/(*ts)->deltat;
2541 if (sindex<0) sindex = 0;
2542 if (eindex>(*ts)->length) eindex = (*ts)->length;
2545 for (
k=sindex;
k<eindex;
k++) {
2546 (*ts)->undefined[
k]=1;
2554 newstartindex = (bti->
end[0]-(*ts)->tstart)/(*ts)->deltat;
2555 newendindex = (bti->
start[bti->
length-1]-(*ts)->tstart)/(*ts)->deltat;
2556 newN = newendindex - newstartindex;
2560 if (newstartindex>0) {
2562 for (
i=newstartindex;
i<newendindex;
i++) {
2563 (*ts)->data[
count] = (*ts)->data[
i];
2564 (*ts)->undefined[
count] = (*ts)->undefined[
i];
2582 (*ts)->length = newN;
2583 (*ts)->T = newN*(*ts)->deltat;
2584 (*ts)->tstart += newstartindex*(*ts)->deltat;
2585 LogPrintf(
LOG_DEBUG,
"%s : changed start time and duration to %f and %f.\n",fn,(*ts)->tstart,(*ts)->T);
2615 LogPrintf(
LOG_CRITICAL,
"%s : failed to allocate memory for an XTEUINT4TimeSeries data vector.\n",fn);
2619 LogPrintf(
LOG_CRITICAL,
"%s : failed to allocate memory for an XTEUINT4TimeSeries undefined vector.\n",fn);
2646 if ((*stamps) == NULL) {
2656 for (
i=1;
i<(*stamps)->length;
i++) {
2659 if ((*stamps)->dettime[
i]>thresh) {
2663 (*stamps)->dettime[
m] = (*stamps)->dettime[
i];
2664 (*stamps)->barytime[
m] = (*stamps)->barytime[
i];
2671 if ((*stamps)->dettime[
m-1]<(*stamps)->dettime[(*stamps)->length-1]) {
2672 (*stamps)->dettime[
m] = (*stamps)->dettime[(*stamps)->length-1];
2673 (*stamps)->barytime[
m] = (*stamps)->barytime[(*stamps)->length-1];
2676 (*stamps)->length =
m;
2680 if (((*stamps)->dettime = (
double *)
LALRealloc((*stamps)->dettime,(*stamps)->length*
sizeof(
double))) == NULL) {
2684 if (((*stamps)->barytime = (
double *)
LALRealloc((*stamps)->barytime,(*stamps)->length*
sizeof(
double))) == NULL) {
2691 int M = (*stamps)->length > 5 ? 5 : (*stamps)->length;
2694 fn,(*stamps)->dettime[
i],(*stamps)->barytime[
i]);
2717 if ((*gti) != NULL) {
2726 if (((*gti)->start = (
double *)
LALCalloc(
N,
sizeof(
double))) == NULL) {
2730 if (((*gti)->end = (
double *)
LALCalloc(
N,
sizeof(
double))) == NULL) {
2777 LogPrintf(
LOG_CRITICAL,
"%s : tried to allocate an barycenteredtimestamps with non-positive size.\n",fn);
2780 if ((*stamps) != NULL) {
2789 if (((*stamps)->dettime = (
double *)
LALCalloc(
N,
sizeof(
double))) == NULL) {
2793 if (((*stamps)->barytime = (
double *)
LALCalloc(
N,
sizeof(
double))) == NULL) {
2797 (*stamps)->length =
N;
2814 if (stamps == NULL) {
2837 CHAR *temp_undefined = NULL;
2847 if ((*gti) != NULL) {
2862 for (
j=0;
j<tempts->
length;
j++) temp_undefined[
j] = 0;
2863 for (
i=0;
i<
ts->length;
i++) {
2864 for (
j=0;
j<
ts->ts[
i]->length;
j++)
if (
ts->ts[
i]->undefined[
j]) temp_undefined[
j] = 1;
2869 for (
i=0;
i<
ts->length;
i++) {
2872 UINT4 zerocount = 0;
2875 while (j<ts->
ts[
i]->length) {
2878 if (
ts->ts[
i]->data[
j] == 0) zerocount++;
2881 else if (
ts->ts[
i]->data[
j] != 0) {
2885 for (
k=
j-zerocount;
k<
j;
k++) temp_undefined[
k] = 1;
2895 for (
i=0;
i<
ts->length;
i++) {
2898 UINT4 goodcount = 0;
2901 while (j<ts->
ts[
i]->length) {
2904 if (temp_undefined[
j] == 0) goodcount++;
2907 else if (temp_undefined[
j] != 0) {
2912 for (
k=
j-goodcount;
k<
j;
k++) temp_undefined[
k] = 1;
2926 if ((!flag) && (temp_undefined[
j] == 0)) flag =
TRUE;
2927 if (flag && temp_undefined[
j]) {
2950 if ((!flag) && (temp_undefined[
j] == 0)) {
2954 if (flag && temp_undefined[
j]) {
2960 if (flag) (*gti)->end[
i] = tempts->
tstart + tempts->
T;
2965 for (
i=0;
i<(*gti)->length;
i++)
LogPrintf(
LOG_DEBUG,
"%s : %6.12f -> %6.12f\n",fn,(*gti)->start[
i],(*gti)->end[
i]);
3010 for (
i=0;
i<
ts->length;
i++) {
3011 if ((
ts->ts[
i]->length != tempts->
length) ||
3012 (
ts->ts[
i]->tstart != tempts->
tstart) ||
3013 (
ts->ts[
i]->T != tempts->
T)) {
3024 else if (gti == NULL) {
3053 epoch.gpsNanoSeconds = 0;
3066 outputdir,
ts->objectname,
ts->obsid,
ts->apid,
epoch.gpsSeconds,
T))
3067 printf(
"Warning: truncated string %s\n",outputfile);
3072 outputdir,
ts->objectname,
ts->obsid,
ts->apid,
epoch.gpsSeconds,
T))
3073 printf(
"Warning: truncated string %s\n",outputfile);
3085 for (
i=0;
i<
ts->length;
i++) {
3094 printf(
"Warning: truncated channel name %s\n",channelname);
3115 LogPrintf(
LOG_DEBUG,
"%s : added timeseries from col %s for epoch %d to frame structure\n",fn,
ts->ts[
i]->colname,
epoch.gpsSeconds);
3124 CHAR *versionstring = NULL;
3176 if ((*
ts) == NULL) {
3180 if (stamps == NULL) {
3190 else if (gti == NULL) {
3197 for (
i=0;
i<(*ts)->length;
i++) {
3235 double start_det,end_det;
3236 double start_bary,end_bary;
3237 gsl_interp_accel *detbary_acc = NULL;
3238 gsl_interp *detbary_interp = NULL;
3241 if ((*
ts) == NULL) {
3245 if (stamps == NULL) {
3251 start_det = (*ts)->tstart > stamps->
dettime[0] ? (*ts)->tstart : stamps->
dettime[0];
3252 end_det = ((*ts)->tstart + (*ts)->T) < stamps->
dettime[stamps->
length-1] ? ((*ts)->tstart + (*ts)->T) : stamps->
dettime[stamps->
length-1];
3255 if (start_det>=end_det) {
3256 LogPrintf(
LOG_NORMAL,
"%s : GPS start (%f) > GPS end time (%f), unable to generate a barycentric time series. Exiting.\n",fn,start_det,end_det);
3261 if ((detbary_acc = gsl_interp_accel_alloc()) == NULL) {
3262 LogPrintf(
LOG_CRITICAL,
"%s : gsl_interp_accel_alloc() failed to allocate memory for acceleration.\n",fn);
3267 if (stamps->
length > 2) {
3268 if ((detbary_interp = gsl_interp_alloc(gsl_interp_cspline,stamps->
length)) == NULL) {
3269 LogPrintf(
LOG_CRITICAL,
"%s : gsl_spline_alloc() failed to allocate memory for acceleration.\n",fn);
3272 LogPrintf(
LOG_DEBUG,
"%s : performing nearest neighbour spline interpolation for barycentering\n",fn);
3274 else if (stamps->
length == 2) {
3275 if ((detbary_interp = gsl_interp_alloc(gsl_interp_linear,stamps->
length)) == NULL) {
3276 LogPrintf(
LOG_CRITICAL,
"%s : gsl_spline_alloc() failed to allocate memory for acceleration.\n",fn);
3279 LogPrintf(
LOG_DEBUG,
"%s : performing nearest neighbour linear interpolation for barycentering\n",fn);
3282 LogPrintf(
LOG_CRITICAL,
"%s : only one timestamp so unable to perform barycentering, exiting.\n",fn);
3293 start_bary = gsl_interp_eval(detbary_interp,stamps->
dettime,stamps->
barytime,start_det,detbary_acc);
3294 end_bary = gsl_interp_eval(detbary_interp,stamps->
dettime,stamps->
barytime,end_det,detbary_acc);
3301 INT8 N = (
INT8)floor(0.5 + (end_bary-start_bary)/(*ts)->deltat);
3311 tempts->
data[
i] = 0;
3325 gsl_interp *barydet_interp = NULL;
3326 gsl_interp_accel *barydet_acc = NULL;
3327 REAL8 tempstart_det,tempend_det;
3331 while ( ( stamps->
dettime[sidx] > gti->
start[
k] ) && ( sidx >= 0 ) ) sidx--;
3332 while ( ( stamps->
dettime[eidx] < gti->
end[
k] ) && ( eidx < stamps->length - 1 ) ) eidx++;
3335 if ( sidx < stamps->length - 1 ) {
3342 nstamps = eidx - sidx + 1;
3343 if ( (sidx > stamps->
length-1) || (eidx < 0) ) {
3344 LogPrintf(
LOG_CRITICAL,
"%s : timestamps indices for GTI range %d -> %d are out of range, exiting.\n",fn,sidx,eidx);
3347 LogPrintf(
LOG_DEBUG,
"%s : temporary timestamps have indices %d -> %d\n",fn,sidx,eidx);
3350 for (
i=sidx;
i<eidx;
i++) {
3353 LogPrintf(
LOG_NORMAL,
"%s : timestamp spacing is too large for accurate barycentering.\n",fn);
3359 if ((sidx<eidx) && (!gap)) {
3363 LogPrintf(
LOG_CRITICAL,
"%s : XLALCreateBarycentricData() failed to allocate memory for temporary timestamps.\n",fn);
3374 tempstart_det = (*ts)->tstart > tempstamps->
dettime[0] ? (*ts)->tstart : tempstamps->
dettime[0];
3375 tempend_det = ((*ts)->tstart + (*ts)->T) < tempstamps->
dettime[tempstamps->
length-1] ? ((*ts)->tstart + (*ts)->T) : tempstamps->
dettime[tempstamps->
length-1];
3378 REAL8 tempstart_bary = gsl_interp_eval(detbary_interp,stamps->
dettime,stamps->
barytime,tempstart_det,detbary_acc);
3379 REAL8 tempend_bary = gsl_interp_eval(detbary_interp,stamps->
dettime,stamps->
barytime,tempend_det,detbary_acc);
3381 LogPrintf(
LOG_DEBUG,
"%s : GTI[%d] barycentric GPS start %f (delta = %f)\n",fn,
k,tempstart_bary,tempstart_bary-start_bary);
3382 LogPrintf(
LOG_DEBUG,
"%s : GTI[%d] barycentric GPS end = %f (delta = %f)\n",fn,
k,tempend_bary,tempend_bary-start_bary);
3383 LogPrintf(
LOG_DEBUG,
"%s : GTI[%d] barycentric duration = %f\n",fn,
k,tempend_bary-tempstart_bary);
3386 if ((barydet_acc = gsl_interp_accel_alloc()) == NULL) {
3387 LogPrintf(
LOG_CRITICAL,
"%s : gsl_interp_accel_alloc() failed to allocate memory for acceleration.\n",fn);
3392 if (tempstamps->
length > 2) {
3393 if ((barydet_interp = gsl_interp_alloc(gsl_interp_cspline,tempstamps->
length)) == NULL) {
3394 LogPrintf(
LOG_CRITICAL,
"%s : gsl_spline_alloc() failed to allocate memory for acceleration.\n",fn);
3397 LogPrintf(
LOG_DEBUG,
"%s : performing nearest neighbour spline interpolation for barycentering\n",fn);
3399 else if (tempstamps->
length == 2) {
3400 if ((barydet_interp = gsl_interp_alloc(gsl_interp_linear,tempstamps->
length)) == NULL) {
3401 LogPrintf(
LOG_CRITICAL,
"%s : gsl_spline_alloc() failed to allocate memory for acceleration.\n",fn);
3404 LogPrintf(
LOG_DEBUG,
"%s : performing nearest neighbour linear interpolation for barycentering\n",fn);
3408 if (tempstamps->
length >= 2) {
3419 INT8 s = floor(0.5 + (tempstart_bary-start_bary)/(*ts)->deltat);
3420 INT8 e = floor(0.5 + (tempend_bary-start_bary)/(*ts)->deltat);
3424 REAL8 delta_dettime = gsl_interp_eval(barydet_interp,tempstamps->
barytime,tempstamps->
dettime,bt,barydet_acc) - (*ts)->tstart;
3425 INT8 idx = floor(0.5 + delta_dettime/(*ts)->deltat);
3426 tempts->
data[
i] = (*ts)->data[idx];
3427 tempts->
undefined[
i] = (*ts)->undefined[idx];
3431 LogPrintf(
LOG_DEBUG,
"%s : performed barycentering using nearest bin interpolation\n",fn);
3434 gsl_interp_free(barydet_interp);
3435 gsl_interp_accel_free(barydet_acc);
3439 LogPrintf(
LOG_NORMAL,
"%s : only one timestamp so unable to perform barycentering on this GTI segment.\n",fn);
3457 (*ts)->data[
i] = tempts->
data[
i];
3460 (*ts)->length = tempts->
length;
3461 (*ts)->T = (*ts)->length*(*ts)->deltat;
3462 (*ts)->tstart = start_bary;
3463 LogPrintf(
LOG_DEBUG,
"%s : resized original timeseries and replaced it with barycentered timeseries\n",fn);
3473 gsl_interp_free(detbary_interp);
3474 gsl_interp_accel_free(detbary_acc);
3479 int M = (*ts)->length > 10 ? 10 : (*ts)->length;
3481 for (
i=0;
i<
M;
i++)
LogPrintf(
LOG_DEBUG,
"%s : %6.12f %d (%d)\n",fn,start_bary+(
double)
i*(*ts)->deltat,(*ts)->data[
i],(*ts)->undefined[
i]);
const LALVCSInfoList lalAppsVCSInfoList
NULL-terminated list of VCS and build information for LALApps and its dependencies
void LALCheckMemoryLeaks(void)
#define XLAL_INIT_DECL(var,...)
LALFrameUFrameH LALFrameH
int XLALFrameAddFrHistory(LALFrameH *frame, const char *name, const char *comment)
int XLALFrameWrite(LALFrameH *frame, const char *fname)
int XLALFrameAddINT4TimeSeriesProcData(LALFrameH *frame, const INT4TimeSeries *series)
LALFrameH * XLALFrameNew(const LIGOTimeGPS *epoch, double duration, const char *project, int run, int frnum, INT8 detectorFlags)
void * XLALCalloc(size_t m, size_t n)
void * XLALRealloc(void *p, size_t n)
int XLALStringCaseCompare(const char *s1, const char *s2)
int XLALStringNCaseCompare(const char *s1, const char *s2, size_t n)
char * XLALVCSInfoString(const LALVCSInfoList vcs_list, const int verbose, const char *prefix)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
void XLALDestroyINT4TimeSeries(INT4TimeSeries *series)
INT4TimeSeries * XLALCreateINT4TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
const LALUnit lalDimensionlessUnit
#define XLAL_CHECK(assertion,...)
#define XLAL_CHECK_MAIN(assertion,...)
CHAR comment[LIGOMETA_COMMENT_MAX]
A structure for storing vectors of detector and barycentric frame timestamps A pre-barycentered FITS ...
REAL8 dtrow
the time steps for the timestamps
REAL8 * dettime
the detector timestamps
INT8 length
the number of timestamps
REAL8 * barytime
the barycentered timestamps
A structure containing all of the relavent information extracted from a single (R)XTE FITS PCA file.
FITSHeader * header
FITS file header information.
BarycentricData * stamps
barycentric timestamps information
XTEUINT4Array ** array
array of vectors containing array data
XTECHARArray ** event
array of vectors containing event data
GTIData * gti
good time interval information
The good time interval data read from a FITS file.
INT8 length
the number of gti segments
REAL8 * start
the start times of good data
REAL8 * end
the end times of good data
A structure containing a vector of event data information.
XTECHARVector * channeldata
pointers to individual event data vectors
INT4 nchannels
the number of channels
A structure containing event information from a single channel found in an (R)XTE FITS file.
CHAR * data
vector of data
REAL8 deltat
the sampling time
CHAR * undefined
data quality flag
INT8 nevents
the actual number of events
INT8 length
length of the vector
INT8 rowlength
the number of data per timestamp
A structure to store TDDES2 DDL data.
INT4 ndetconfig
the number of detector configurations
INT4 * minenergy
minimum energy channel (0-255) (one for each channel)
INT4 nsamples
the number of samples
INT4 ** detectors
flags indicating which detectors were being used
INT4 * maxenergy
maximum energy channel (0-255) (one for each channel)
INT4 nenergy
the number of energy channels
REAL8 deltat
the sampling time
INT4 nchannels
the number of channels (nenergy * ndetconfig)
REAL8 offset
the time offset
A structure containing a vector of array data information.
INT4 nchannels
the number of channels
XTEUINT4Vector * channeldata
a pointer to array data vectors
A structure for storing an array of integer timeseries for XTE data.
XTEUINT4TimeSeries ** ts
pointer to single timeseries vectors
INT4 bary
barycentered flag
INT4 length
number of timeseries
CHAR * comment
a comment field (used to store original clargs)
CHAR * headerdump
an ascii dump of the original FITS header
A structure for storing a timeseries of unsigned integers for XTE data.
REAL8 tstart
the GPS start time
UINT4 * data
the data (timeseries of binned photon counts)
REAL8 deltat
the time step size in seconds
CHAR * undefined
a data quality flag (0=good, 1=bad)
INT8 length
length of the timeseries
REAL8 T
the time span in seconds
A structure containing array data information from a single channel found in an (R)XTE FITS file.
INT8 rowlength
the number of data per timestamp
CHAR * undefined
data quality flag
INT8 length
length of the vector
REAL8 deltat
the sampling time
UINT4 * data
vector of data
INT4 energy[2]
the energy channel range (0-255)
CHAR detconfig[NPCU+1]
contains detector config string
int output(const char *outfile, int outtype, REAL4TimeSeries *series)
int main(int argc, char *argv[])
The main function of xtefitstoframe.c.
int XLALEventDataToXTEUINT4TimeSeriesArray(XTEUINT4TimeSeriesArray **ts, FITSData *fits, REAL8 dt)
Converts a FITS event data file to a binned timeseries.
int XLALXTEUINT4TimeSeriesArrayToFrames(XTEUINT4TimeSeriesArray *ts, CHAR *outputdir)
This function outputs an XTEUINT4TimeSeriesArray to a frame file or files.
int XLALReadFITSEventData(XTECHARArray **event, fitsfile *fptr, FITSHeader *header, int col)
int XLALCreateBarycentricData(BarycentricData **stamps, INT8 N)
Allocates memory for a barycentric timestamps structure.
int XLALCreateXTEUINT4TimeSeriesArray(XTEUINT4TimeSeriesArray **x, INT8 N)
int XLALFreeXTEUINT4TimeSeriesArray(XTEUINT4TimeSeriesArray *ts)
Frees an XTEUINT4TimeSeriesArray data structure.
const char * USELESSDATAMODE[5]
int XLALConvertTDDES(XTETDDESParams **params, char *tddes)
Extracts PCA config, energy range and sampling parameters from tddes2 DDL string.
int XLALFreeXTEUINT4TimeSeries(XTEUINT4TimeSeries *ts)
Frees an XTEUINT4TimeSeries data structure.
int XLALCreateGTIData(GTIData **gti, INT8 N)
Allocates memory for a GTI structure.
const char xtechannelname[16]
int XLALReallocXTEUINT4TimeSeries(XTEUINT4TimeSeries **ts, INT8 N)
Resizes an XTEUINT4TimeSeries data structure.
int XLALReadFITSGTI(GTIData **fitsfilegti, fitsfile *fptr)
Read in GTI (good time interval) table from FITS file.
int XLALCreateXTEUINT4TimeSeries(XTEUINT4TimeSeries **x, INT8 N)
Allocates memory for a XTEUINT4TimeSeries.
#define MAXTIMESTAMPDELTAT
int XLALReadFITSFile(FITSData **data, char *filename)
This function reads in data from a FITS file and returns a FITSdata data structure.
int XLALApplyGTIToXTEUINT4TimeSeriesArray(XTEUINT4TimeSeriesArray **ts, GTIData *gti)
Sets timeseries array samples NOT in the GTI table as undefined and zeros the corresponding data.
int XLALArrayDataToXTEUINT4TimeSeriesArray(XTEUINT4TimeSeriesArray **ts, FITSData *fits, REAL8 dt)
Converts a FITS array data file to a binned timeseries.
int XLALReadFITSTimeStamps(BarycentricData **fitsfilebary, fitsfile *fptr)
Reads in FITS file first extension header timestamps information.
int XLALFreeGTIData(GTIData *gti)
Frees memory for a GTI structure.
int XLALReadFITSArrayData(XTEUINT4Array **array, fitsfile *fptr, FITSHeader *header, int col)
int XLALXTEUINT4TimeSeriesArrayToGTI(GTIData **gti, XTEUINT4TimeSeriesArray *ts)
This function outputs an XTEUINT4TimeSeriesArray to a frame file or files.
int XLALFreeBarycentricData(BarycentricData *stamps)
Allocates memory for a barycentric timestamps structure.
int ReadUserVars(int argc, char *argv[], UserInput_t *uvar, CHAR *clargs)
Read in input user arguments.
int removechar(CHAR *p, CHAR ch)
Removes a character from a string.
int vrbflg
defined in lal/lib/std/LALError.c
int XLALBarycenterXTEUINT4TimeSeries(XTEUINT4TimeSeries **ts, BarycentricData *stamps, GTIData *gti)
This function barycenters an XTEUINT4TimeSeries using a set of barycentric timestamps.
int XLALReduceBarycentricData(BarycentricData **stamps)
Reduces the number of barycentric timestamps from a FITS data structure.
int XLALBarycenterXTEUINT4TimeSeriesArray(XTEUINT4TimeSeriesArray **ts, BarycentricData *stamps)
This function barycenters an XTEUINT4TimeSeriesArray using a set of barycentric timestamps.
int XLALArrayDataToXTEUINT4TimeSeries(XTEUINT4TimeSeries **ts, XTEUINT4Vector *event, BarycentricData *stamps, REAL8 dt)
Converts a FITS file data structure containing science array (binned) data into a timeseries.
int XLALEventDataToXTEUINT4TimeSeries(XTEUINT4TimeSeries **ts, XTECHARVector *array, BarycentricData *stamps, REAL8 dt)
Converts a FITS event data file to a binned timeseries.
int XLALReadFITSHeader(FITSHeader *fitsfileheader, fitsfile *fptr)
Reads in the FITS file first extension header information.
int XLALFreeFITSData(FITSData *x)
Frees a FITS data structure.
int XLALApplyGTIToXTEUINT4TimeSeries(XTEUINT4TimeSeries **ts, GTIData *gti)
Sets timeseries samples NOT in the GTI table as undefined and zeros the corresponding data.