34 #define BLOCKSIZE 8192 * 8
93 UINT4 firstbin, lastbin;
101 char *
fname = ∅
106 #define XLALLOADSFTSERROR(eno) { \
110 XLALFree(segments); \
112 XLALFree(locatalog.data); \
114 XLALDestroySFT(thisSFT); \
116 XLALDestroySFTVector(sftVector); \
117 XLAL_ERROR_NULL(eno); \
121 locatalog.
data = NULL;
139 maxbin = lastbin = firstbin + catalog->
data[0].
numBins - 1;
140 for ( catPos = 1; catPos < catalog->
length; catPos++ ) {
142 lastbin = firstbin + catalog->
data[catPos].
numBins - 1;
143 if ( firstbin < minbin ) {
146 if ( lastbin > maxbin ) {
155 XLALPrintInfo(
"%s: fMin: %f, fMax: %f, deltaF: %f, minbin: %u, maxbin: %u\n",
__func__,
fMin,
fMax,
deltaF, minbin, maxbin );
167 if ( ( lastbin == 0 ) && (
fMax != 0 ) ) {
181 if ( !( thisSFT =
XLALCreateSFT( lastbin + 1 - firstbin ) ) ) {
209 for ( catPos = 0; catPos < catalog->
length; catPos++ ) {
221 UINT4 firstSFTbin = lround( tmp );
223 UINT4 firstBin2read = firstbin;
224 UINT4 lastBin2read = lastbin;
225 UINT4 numBins2read, offsetBins;
228 if ( firstBin2read < firstSFTbin ) {
229 firstBin2read = firstSFTbin;
231 if ( lastBin2read > lastSFTbin ) {
232 lastBin2read = lastSFTbin;
236 if ( firstBin2read <= lastBin2read ) {
241 firstBinRead = firstBin2read;
242 lastBinRead = lastBin2read;
243 offsetBins = firstBin2read - firstSFTbin;
244 numBins2read = lastBin2read - firstBin2read + 1;
253 numBins2read *
sizeof(
COMPLEX8 ) );
256 thisSFT->
f0 = 1.0 * firstBin2read * thisSFT->
deltaF;
284 if ( fseek(
fp, locator->
offset, SEEK_SET ) == -1 ) {
285 XLALPrintError(
"ERROR: Couldn't seek to position %ld in file '%s'\n",
299 if ( segments[
isft].last == 0 ) {
302 if ( firstBinRead != firstbin ) {
303 XLALPrintError(
"ERROR: data gap or overlap at first bin of SFT#%u (GPS %lf)"
304 " expected bin %u, bin %u read from file '%s'\n",
306 firstbin, firstBinRead,
fname );
313 }
else if ( firstBinRead != segments[
isft].last + 1 ) {
315 " between bin %u read from file '%s' and bin %u read from file '%s'\n",
318 firstBinRead,
fname );
324 XLALPrintError(
"ERROR: deltaF mismatch (%f/%f) in SFT read from file '%s'\n",
329 XLALPrintError(
"ERROR: GPS epoch mismatch (%f/%f) in SFT read from file '%s'\n",
341 ( lastBinRead - firstBinRead + 1 ) *
sizeof(
COMPLEX8 ) );
343 }
else if ( !firstBinRead ) {
351 XLALPrintError(
"ERROR: GPS epoch mismatch (%f/%f) in SFT read from file '%s'\n",
372 if ( segments[
isft].last == lastbin ) {
377 if ( segments[
isft].last )
379 " expected bin %u, bin %u read from file '%s'\n",
381 lastbin, segments[
isft].last,
382 segments[
isft].lastfrom->fname );
384 XLALPrintError(
"ERROR: no data could be read for SFT#%u (GPS %lf)\n",
395 return ( sftVector );
465 for (
UINT4 X = 0; X < numIFOs; X++ ) {
491 const CHAR *SFTwindowtype,
492 const REAL8 SFTwindowparam,
493 const CHAR *SFTcomment
496 UINT4 comment_len = 0;
499 CHAR pad[] = {0, 0, 0, 0, 0, 0, 0};
524 comment_len = strlen( sft->
name ) + 1;
526 comment_len += strlen( SFTcomment ) + 1;
529 if ( ( _SFTcomment =
XLALCalloc( comment_len,
sizeof(
CHAR ) ) ) == NULL ) {
532 strcpy( _SFTcomment, sft->
name );
534 strcat( _SFTcomment,
"\n" );
535 strcat( _SFTcomment, SFTcomment );
541 pad_len = ( 8 - ( comment_len % 8 ) ) % 8;
558 rawheader.
crc64 =
crc64( (
const unsigned char * )&rawheader,
sizeof( rawheader ), ~( 0ULL ) );
560 rawheader.
crc64 =
crc64( (
const unsigned char * )_SFTcomment, comment_len, rawheader.
crc64 );
566 if ( 1 != fwrite( &rawheader,
sizeof( rawheader ), 1,
fp ) ) {
571 if ( comment_len != fwrite( _SFTcomment, 1, comment_len,
fp ) ) {
574 if ( pad_len != fwrite(
pad, 1, pad_len,
fp ) ) {
600 const CHAR *SFTfilename,
601 const CHAR *SFTwindowtype,
602 const REAL8 SFTwindowparam,
603 const CHAR *SFTcomment
612 if ( !( sft->
data ) ) {
615 if ( !( SFTfilename ) ) {
627 if ( (
fp = fopen( SFTfilename,
"wb" ) ) == NULL ) {
628 XLALPrintError(
"\nFailed to open file '%s' for writing: %s\n\n", SFTfilename, strerror( errno ) );
659 const CHAR *SFTcomment
667 if ( !( sft->
data ) ) {
714 const CHAR *SFTfilename,
715 const CHAR *SFTwindowtype,
716 const REAL8 SFTwindowparam,
717 const CHAR *SFTcomment
729 XLAL_CHECK( (
fp = fopen( SFTfilename,
"wb" ) ) != NULL,
XLAL_EIO,
"Failed to open '%s' for writing: %s\n\n", SFTfilename, strerror( errno ) );
758 const CHAR *SFTcomment,
853 for (
i = 0;
i < nelements;
i++ ) {
855 for (
j = 0;
j < dsize / 2;
j++ ) {
859 pdata[indx] = tempbyte;
892 if ( (
fp = fopen(
fname,
"rb" ) ) == NULL ) {
893 XLALPrintError(
"\nFailed to open SFT '%s' for reading: %s\n\n",
fname, strerror( errno ) );
897 if ( fseek(
fp, locator->
offset, SEEK_SET ) == -1 ) {
898 XLALPrintError(
"\nFailed to set fp-offset to '%ld': %s\n\n", locator->
offset, strerror( errno ) );
922 if ( ( save_filepos = ftell(
fp ) ) == -1 ) {
928 if ( 1 != fread( &ver,
sizeof( ver ), 1,
fp ) ) {
930 XLALPrintError(
"\nCould not read version-number from file\n\n" );
939 if ( ! memcmp( &ver, &vertest,
sizeof( ver ) ) ) {
943 endian_swap( (
char * )( &vertest ),
sizeof( vertest ), 1 );
944 if ( ! memcmp( &ver, &vertest,
sizeof( ver ) ) ) {
951 unsigned char *v = (
unsigned char * )( &ver );
952 XLALPrintError(
"\nERROR: illegal SFT-version (%X %X %X %X %X %X %X %X) not within [%.0f, %.0f]\n",
953 v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7],
960 if ( fseek(
fp, save_filepos, SEEK_SET ) == -1 ) {
961 XLALPrintError(
"\nfseek() failed to return to intial fileposition: %s\n\n", strerror( errno ) );
968 fseek(
fp, save_filepos, SEEK_SET );
1006 XLALPrintError(
"\nERROR read_sft_header_from_fp(): called with NULL input\n\n" );
1009 if ( SFTcomment && ( ( *SFTcomment ) != NULL ) ) {
1010 XLALPrintError(
"\nERROR: Comment-string passed to read_sft_header_from_fp() is not empty!\n\n" );
1015 if ( ( save_filepos = ftell(
fp ) ) == -1 ) {
1038 if ( (
head.epoch.gpsSeconds < 0 ) || (
head.epoch.gpsNanoSeconds < 0 ) || (
head.epoch.gpsNanoSeconds >= 1000000000 ) ) {
1040 head.epoch.gpsSeconds,
head.epoch.gpsNanoSeconds );
1044 if (
head.deltaF <= 0 ) {
1049 if (
head.f0 < 0 ) {
1059 ( *SFTcomment ) = comm;
1060 }
else if ( comm ) {
1064 ( *swapEndian ) = need_swap;
1065 ( *crc64 ) = ref_crc;
1066 ( *numBins ) = nsamples;
1068 if ( SFTwindowspec ) {
1069 ( *SFTwindowspec ) = windowspec;
1077 if ( fseek(
fp, save_filepos, SEEK_SET ) == -1 ) {
1078 XLALPrintError(
"\nfseek() failed to return to intial fileposition: %s\n\n", strerror( errno ) );
1111 if ( !
fp || !
header || !nsamples || !SFTcomment ) {
1112 XLALPrintError(
"\nERROR read_header_from_fp(): called with NULL input!\n\n" );
1115 if ( SFTcomment && ( *SFTcomment != NULL ) ) {
1116 XLALPrintError(
"\nERROR: Comment-string passed to read_header_from_fp() is not NULL!\n\n" );
1121 if ( ( save_filepos = ftell(
fp ) ) == -1 ) {
1122 XLALPrintError(
"\nERROR: ftell() failed: %s\n\n", strerror( errno ) );
1127 if ( fread( &rawheader,
sizeof( rawheader ), 1,
fp ) != 1 ) {
1129 XLALPrintError(
"\nCould not read header. %s\n\n", strerror( errno ) );
1140 rawheader.
crc64 = 0;
1142 crc =
crc64( (
const unsigned char * )&rawheader,
sizeof( rawheader ), ~( 0ULL ) );
1144 rawheader.crc64 = save_crc;
1172 XLALPrintError(
"\nNon-positive number of samples in SFT!\n\n" );
1182 XLALPrintError(
"\nComment-length must be multiple of 8 bytes!\n\n" );
1192 if ( rawheader.
version == 2 ) {
1216 ptr = strchr( comm, 0 );
1218 if ( *
ptr++ != 0 ) {
1219 XLALPrintError(
"\nNon-NULL bytes found after comment-end!\n\n" );
1228 CHAR pad[] = {0, 0, 0, 0, 0, 0, 0};
1229 UINT4 comment_len = strlen( comm ) + 1;
1230 UINT4 pad_len = ( 8 - ( comment_len % 8 ) ) % 8;
1232 crc =
crc64( (
const unsigned char * )comm, comment_len, crc );
1233 crc =
crc64( (
const unsigned char * )
pad, pad_len, crc );
1249 ( *nsamples ) = rawheader.
nsamples;
1250 ( *ref_crc64 ) = rawheader.
crc64;
1251 ( *SFTcomment ) = comm;
1252 ( *header_crc64 ) = crc;
1259 if ( fseek(
fp, save_filepos, SEEK_SET ) == -1 ) {
1260 XLALPrintError(
"\nfseek() failed to return to intial fileposition: %s\n\n", strerror( errno ) );
1288 UINT4 firstSFTbin, lastSFTbin, numSFTbins;
1293 if ( !firstBinRead ) {
1294 XLALPrintError(
"read_sft_bins_from_fp(): got passed NULL *firstBinRead\n" );
1295 return ( (
UINT4 ) - 1 );
1300 if ( ( ret == NULL ) ||
1301 ( ret->
data == NULL ) ||
1303 XLALPrintError(
"read_sft_bins_from_fp(): got passed NULL SFT*\n" );
1309 XLALPrintError(
"read_sft_bins_from_fp(): got passed NULL FILE*\n" );
1314 if ( firstBin2read > lastBin2read ) {
1315 XLALPrintError(
"read_sft_bins_from_fp(): Empty frequency-interval requested [%d, %d] bins\n",
1316 firstBin2read, lastBin2read );
1324 XLALPrintError(
"read_sft_bins_from_fp(): Failed to read SFT-header!\n" );
1332 firstSFTbin = lround( tmp );
1333 lastSFTbin = firstSFTbin + numSFTbins - 1;
1336 if ( firstBin2read < firstSFTbin ) {
1337 firstBin2read = firstSFTbin;
1339 if ( lastBin2read > lastSFTbin ) {
1340 lastBin2read = lastSFTbin;
1345 if ( firstBin2read > lastBin2read ) {
1350 *firstBinRead = firstBin2read;
1352 offsetBins = firstBin2read - firstSFTbin;
1353 offsetBytes = offsetBins * 2 *
sizeof(
REAL4 );
1354 numBins2read = lastBin2read - firstBin2read + 1;
1357 XLALPrintError(
"read_sft_bins_from_fp(): passed SFT has not enough bins (%u/%u)\n",
1364 if ( fseek(
fp, offsetBytes, SEEK_CUR ) != 0 ) {
1365 XLALPrintError(
"read_sft_bins_from_fp(): Failed to fseek() to first frequency-bin %d: %s\n",
1366 firstBin2read, strerror( errno ) );
1372 if ( numBins2read != fread( ret->
data->
data,
sizeof(
COMPLEX8 ), numBins2read,
fp ) ) {
1373 XLALPrintError(
"read_sft_bins_from_fp(): Failed to read %d bins from SFT!\n", numBins2read );
1379 ret->
f0 = 1.0 * firstBin2read * ret->
deltaF;
1385 for (
i = 0;
i < numBins2read;
i ++ ) {
1399 return ( lastBin2read );
1412 UINT8 computed_crc, ref_crc;
1416 CHAR *SFTcomment = NULL;
1424 XLALPrintError(
"\nhas_valid_crc64() was called with NULL filepointer!\n\n" );
1429 if ( ( save_filepos = ftell(
fp ) ) == -1 ) {
1430 XLALPrintError(
"\nERROR: ftell() failed: %s\n\n", strerror( errno ) );
1457 while ( data_len > 0 ) {
1460 if ( toread != (
int )fread( block, 1, toread,
fp ) ) {
1461 XLALPrintError(
"\nFailed to read all frequency-bins from SFT.\n\n" );
1467 computed_crc =
crc64( (
const unsigned char * )block, toread, computed_crc );
1472 return ( computed_crc == ref_crc );
#define __func__
log an I/O error, i.e.
#define GPS2REAL8(gps)
convert GPS-time to REAL8
#define XLALLOADSFTSERROR(eno)
#define BLOCKSIZE
blocksize used in SFT-reading for the CRC-checksum computation (has to be multiple of 8 !...
Internal SFT types and functions.
unsigned long long crc64(const unsigned char *data, unsigned int length, unsigned long long crc)
#define GPSEQUAL(gps1, gps2)
#define XLAL_INIT_DECL(var,...)
void * XLALCalloc(size_t m, size_t n)
void * XLALMalloc(size_t n)
const char * SFTErrorMessage(int errorcode)
int ValidateSFTFile(const char *fname)
Verify that the contents of a SFT file are valid.
SFTtype * XLALCreateSFT(UINT4 numBins)
XLAL function to create one SFT-struct.
MultiSFTVector * XLALLoadMultiSFTs(const SFTCatalog *inputCatalog, REAL8 fMin, REAL8 fMax)
Function to load a catalog of SFTs from possibly different detectors.
void XLALDestroyMultiSFTCatalogView(MultiSFTCatalogView *multiView)
Destroys a MultiSFTCatalogView, without freeing the original catalog that the 'view' was referring to...
UINT4 read_sft_bins_from_fp(SFTtype *ret, UINT4 *firstBinRead, UINT4 firstBin2read, UINT4 lastBin2read, FILE *fp)
void XLALDestroyMultiSFTVector(MultiSFTVector *multvect)
Destroy a multi SFT-vector.
static int read_header_from_fp(FILE *fp, SFTtype *header, UINT4 *nsamples, UINT8 *header_crc64, UINT8 *ref_crc64, UINT2 *SFTwindowspec, CHAR **SFTcomment, BOOLEAN swapEndian)
MultiSFTCatalogView * XLALGetMultiSFTCatalogView(const SFTCatalog *catalog)
Return a MultiSFTCatalogView generated from an input SFTCatalog.
REAL8 TSFTfromDFreq(REAL8 dFreq)
void endian_swap(CHAR *pdata, size_t dsize, size_t nelements)
int build_sft_windowspec(UINT2 *windowspec, CHAR(*windowspec_str)[9], const char *window_type, REAL8 window_param)
Build an SFT 2-byte 'windowspec' or filename field 'windowspec_str' for the window given by 'window_t...
int XLALWriteSFT2FilePointer(const SFTtype *sft, FILE *fp, const CHAR *SFTwindowtype, const REAL8 SFTwindowparam, const CHAR *SFTcomment)
Write the given SFTtype to a FILE pointer.
MultiSFTVector * XLALLoadMultiSFTsFromView(const MultiSFTCatalogView *multiCatalogView, REAL8 fMin, REAL8 fMax)
This function loads a MultiSFTVector from a given input MultiSFTCatalogView, otherwise the documentat...
char * XLALBuildSFTFilenameFromSpec(const SFTFilenameSpec *spec)
Build an SFT file name from the given specification.
UINT4 XLALRoundFrequencyUpToSFTBin(const REAL8 freq, const REAL8 df)
Round a REAL8 frequency up to the nearest integer SFT bin number.
SFTVector * XLALLoadSFTs(const SFTCatalog *catalog, REAL8 fMin, REAL8 fMax)
Load the given frequency-band [fMin, fMax) (half-open) from the SFT-files listed in the SFT-'catalogu...
int read_sft_header_from_fp(FILE *fp, SFTtype *header, UINT4 *version, UINT8 *crc64, UINT2 *SFTwindowspec, BOOLEAN *swapEndian, CHAR **SFTcomment, UINT4 *numBins)
UINT4 XLALRoundFrequencyDownToSFTBin(const REAL8 freq, const REAL8 df)
Round a REAL8 frequency down to the nearest integer SFT bin number.
void XLALDestroySFT(SFTtype *sft)
Destructor for one SFT.
FILE * fopen_SFTLocator(const struct tagSFTLocator *locator)
Open an "SFT" defined by the SFT-locator, return a FILE-pointer to the beginning of this SFT.
int XLALWriteSFTVector2NamedFile(const SFTVector *sftVect, const CHAR *SFTfilename, const CHAR *SFTwindowtype, const REAL8 SFTwindowparam, const CHAR *SFTcomment)
Write the given SFTVector to a single merged SFT file with the supplied filename.
int XLALWriteSFTVector2StandardFile(const SFTVector *sftVect, SFTFilenameSpec *SFTfnspec, const CHAR *SFTcomment, const BOOLEAN merged)
Write the given SFTVector to SFT file(s) with a standard () filename(s).
int XLALWriteSFT2StandardFile(const SFTtype *sft, SFTFilenameSpec *SFTfnspec, const CHAR *SFTcomment)
Write the given SFTtype to a SFT file with a standard () filename.
int compareSFTloc(const void *ptr1, const void *ptr2)
int XLALWriteSFT2NamedFile(const SFTtype *sft, const CHAR *SFTfilename, const CHAR *SFTwindowtype, const REAL8 SFTwindowparam, const CHAR *SFTcomment)
Write the given SFTtype to a SFT file with the supplied filename.
BOOLEAN has_valid_crc64(FILE *fp)
Check the SFT-block starting at fp for valid crc64 checksum.
SFTVector * XLALCreateSFTVector(UINT4 numSFTs, UINT4 numBins)
XLAL function to create an SFTVector of numSFT SFTs with SFTlen frequency-bins (which will be allocat...
int XLALCheckSFTFileIsValid(const char *fname)
Verify that the contents of a SFT file are valid.
int read_SFTversion_from_fp(UINT4 *version, BOOLEAN *need_swap, FILE *fp)
Read valid SFT version-number at position fp, and determine if we need to endian-swap the data.
BOOLEAN XLALIsValidCWDetector(const CHAR *name)
Determine if 'name' is a valid detector name or prefix.
#define XLAL_ERROR_NULL(...)
int int int XLALPrintInfo(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
#define XLAL_CHECK(assertion,...)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
#define XLAL_CHECK_NULL(assertion,...)
A vector of COMPLEX8FrequencySeries.
COMPLEX8FrequencySeries * data
Pointer to the data array.
UINT4 length
Number of elements in array.
A multi-SFT-catalogue "view": a multi-IFO vector of SFT-catalogs.
SFTCatalog * data
array of SFT-catalog pointers
UINT4 length
number of detectors
A collection of SFT vectors – one for each IFO in a multi-IFO search.
An "SFT-catalogue": a vector of SFTdescriptors, as returned by XLALSFTdataFind()
SFTDescriptor * data
array of data-entries describing matched SFTs
UINT4 length
number of SFTs in catalog
SFTtype header
SFT-header info.
struct tagSFTLocator * locator
internal description of where to find this SFT [opaque!]
UINT4 numBins
number of frequency-bins in this SFT
Structure specifying an SFT file name, following the convention in .
UINT4 SFTtimebase
Timebase in seconds of the SFT; set by XLALWriteSFT[Vector]2StandardFile()
UINT4 SFTspan
Total time interval in seconds covered by SFT file; set by XLALWriteSFT[Vector]2StandardFile()
UINT4 gpsStart
GPS time in seconds at the beginning of the first SFT in the file; set by XLALWriteSFT[Vector]2Standa...
CHAR detector[3]
2-character detector prefix (e.g.
UINT4 numSFTs
Number of SFTs in the file; set by XLALWriteSFT[Vector]2StandardFile()
CHAR window_type[32]
window function applied to SFT
REAL8 window_param
parameter of window function, if required
segments read so far from one SFT
UINT4 last
last bin in this segment
struct tagSFTLocator * lastfrom
last bin read from this locator
LIGOTimeGPS epoch
timestamp of this SFT
UINT4 first
first bin in this segment