28#include <lal/StringInput.h>
29#include <lal/AVFactories.h>
30#include <lal/LALConstants.h>
31#include <lal/LALStdio.h>
32#include <lal/LogPrintf.h>
33#include <lal/HeapToplist.h>
46#define snprintf _snprintf
61#define min(a,b) ((a)<(b)?(a):(b))
65#define SYNC_FAIL_LIMIT 5
125 for (
i = 0;
i < ( *l )->elems;
i++ ) {
167 UINT4 len, chars = 0;
187 if ( maxbytes == 0 ) {
192 while ( fgets(
line,
sizeof(
line ) - 1,
fp ) ) {
194 if ( !strncmp(
line,
"%DONE\n", strlen(
"%DONE\n" ) ) ) {
199 len = strlen(
line );
205 }
else if (
line[len - 1] !=
'\n' ) {
207 "Line %d is too long or has no NEWLINE. First %zu chars are:\n'%s'\n",
212 items = sscanf(
line,
218 &HoughFstatLine.
Freq,
219 &HoughFstatLine.
Alpha,
220 &HoughFstatLine.
Delta,
221 &HoughFstatLine.
f1dot,
229 !isfinite( HoughFstatLine.
Freq ) ||
230 !isfinite( HoughFstatLine.
f1dot ) ||
231 !isfinite( HoughFstatLine.
Alpha ) ||
232 !isfinite( HoughFstatLine.
Delta ) ||
235 HoughFstatLine.
Freq < 0.0 ||
236 HoughFstatLine.
Alpha < 0.0 - epsilon ||
244 "Line %d has invalid values.\n"
245 "First %zu chars are:\n"
247 "All fields should be finite\n"
248 "1st field should be positive.\n"
249 "2nd field should lie between 0 and %1.15f.\n"
250 "3rd field should lie between %1.15f and %1.15f.\n",
257 for (
i = 0;
i < len;
i++ ) {
258 *checksum +=
line[
i];
273 if ( chars == maxbytes ) {
274 LogPrintf(
LOG_DEBUG,
"Read exactly %d == maxbytes from HoughFstat-file, that's enough.\n",
279 if ( chars > maxbytes ) {
299 char extraFStr[256] =
"";
302 snprintf( extraFStr,
sizeof( extraFStr ),
" %.6f", fline.
sumTwoF );
305 for ( X = 0; X <
numDet ; X ++ ) {
306 snprintf( buf0,
sizeof( buf0 ),
" %7.6f", fline.
sumTwoFX->
data[X] );
307 UINT4 len1 = strlen( extraFStr ) + strlen( buf0 ) + 1;
308 if ( len1 >
sizeof( extraFStr ) ) {
309 XLALPrintError(
"%s: assembled output string too long! (%d > %zu)\n", fn, len1,
sizeof( extraFStr ) );
312 strcat( extraFStr, buf0 );
317 return ( snprintf( buf, buflen,
324 "%16.15f %16.15f %- 16.15f %- 21.15g %- 16.15f %16.15f %- 16.15f %- 8.7f %8.7f%s\n",
348 if ( length >
sizeof( linebuf ) - 1 ) {
353 for (
i = 0;
i < length;
i++ ) {
354 *checksum += linebuf[
i];
400 errno, strerror( errno ) );
431#define TEMP_EXT ".tmp"
433 tempname = (
char * )malloc(
s );
441 fpnew = fopen( tempname,
"wb" );
444 tempname, errno, strerror( errno ) );
453 if ( ( write_done ) && ( length >= 0 ) ) {
455 ret =
fprintf( fpnew,
"%%DONE\n" );
467 tempname, errno, strerror( errno ) );
475 if ( rename( tempname,
filename ) ) {
477 filename, errno, strerror( errno ) );
513#define __func__ __FUNCTION__
520#define LOGIOERROR(mess,filename) \
521 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): doserr:%d, ferr:%d, errno:%d: %s\n",\
522 mess,filename,__func__,__FILE__,__LINE__,_doserrno,((fp)?(ferror(fp)):0),errno,strerror(errno))
524#define LOGIOERROR(mess,filename) \
525 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): errno:%d: %s\n",\
526 mess,filename,__func__,__FILE__,__LINE__,errno,strerror(errno))
532#define TMP_EXT ".tmp"
537 static UINT4 sync_fail_counter = 0;
541 tmpfilename =
LALCalloc( len,
sizeof(
char ) );
542 if ( !tmpfilename ) {
547 strcat( tmpfilename,
TMP_EXT );
551 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
552 checksum += *( ( (
char * ) & ( tl->
elems ) ) + len );
554 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
555 checksum += *( ( (
char * )tl->
data ) + len );
557 for ( len = 0; len <
sizeof( counter ); len++ ) {
558 checksum += *( ( (
char * )&counter ) + len );
562 fp = fopen( tmpfilename,
"wb" );
569 len = fwrite( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
571 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
573 if ( fclose(
fp ) ) {
574 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
581 if ( len != tl->
elems ) {
582 LOGIOERROR(
"Couldn't write data to", tmpfilename );
584 if ( fclose(
fp ) ) {
585 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
591 len = fwrite( &counter,
sizeof( counter ), 1,
fp );
593 LOGIOERROR(
"Couldn't write counter to", tmpfilename );
595 if ( fclose(
fp ) ) {
596 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
602 len = fwrite( &checksum,
sizeof( checksum ), 1,
fp );
604 LOGIOERROR(
"Couldn't write checksum to", tmpfilename );
606 if ( fclose(
fp ) ) {
607 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
614 if ( fsync( fileno(
fp ) ) ) {
621 sync_fail_counter = 0;
626 if ( fclose(
fp ) ) {
632 if ( rename( tmpfilename,
filename ) ) {
633 LOGIOERROR(
"Couldn't rename\n", tmpfilename );
654 if ( errno == ENOENT ) {
666 len = fread( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
670 if ( fclose(
fp ) ) {
678 "Number of elements read larger than length of toplist: %zu > %zu\n",
680 if ( fclose(
fp ) ) {
688 if ( len != tl->
elems ) {
691 if ( fclose(
fp ) ) {
699 len = fread( counter,
sizeof( *counter ), 1,
fp );
703 if ( fclose(
fp ) ) {
711 len = fread( &checksum,
sizeof( checksum ), 1,
fp );
715 if ( fclose(
fp ) ) {
723 if ( fclose(
fp ) ) {
730 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
731 checksum -= *( ( (
char * ) & ( tl->
elems ) ) + len );
733 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
734 checksum -= *( ( (
char * )tl->
data ) + len );
736 for ( len = 0; len <
sizeof( *counter ); len++ ) {
737 checksum -= *( ( (
char * )counter ) + len );
746 for ( len = 0; len < tl->
elems; len++ ) {
int create_toplist(toplist_t **list, size_t length, size_t size, int(*smaller)(const void *, const void *))
void free_toplist(toplist_t **list)
void qsort_toplist(toplist_t *list, int(*compare)(const void *, const void *))
void * toplist_elem(toplist_t *list, size_t ind)
void qsort_toplist_r(toplist_t *list, int(*compare)(const void *, const void *))
int insert_into_toplist(toplist_t *list, void *element)
void go_through_toplist(toplist_t *list, void(*handle)(void *))
void clear_toplist(toplist_t *list)
int read_hfs_checkpoint(const char *filename, toplist_t *tl, UINT4 *counter)
tries to read a checkpoint
int create_houghFstat_toplist(toplist_t **tl, UINT8 length)
creates a toplist with length elements, returns -1 on error (usually out of memory),...
int write_houghFstat_toplist_item_to_fp(HoughFstatOutputEntry fline, FILE *fp, UINT4 *checksum)
File IO.
static int houghFstat_smaller(const void *a, const void *b)
void sort_houghFstat_toplist(toplist_t *l)
sorts the toplist with an internal sorting function, used before finally writing it
void free_houghFstat_toplist(toplist_t **l)
frees the space occupied by the toplist
#define __func__
log an I/O error, i.e.
static int _atomic_write_houghFstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum, int write_done)
int final_write_houghFstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
meant for the final writing of the toplist
int write_hfs_checkpoint(const char *filename, toplist_t *tl, UINT4 counter, BOOLEAN do_sync)
new, simpler checkpointing for HierarchicalSearch
static void reduce_houghFstat_toplist_precision(toplist_t *l)
static void reduce_houghFstatline_precision(void *line)
int read_houghFstat_toplist_from_fp(toplist_t *l, FILE *fp, UINT4 *checksum, UINT4 maxbytes)
reads a (created!) toplist from an open filepointer sets the checksum if non-NULL reads maximum maxby...
static int houghFstat_toplist_qsort_function(const void *a, const void *b)
int atomic_write_houghFstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum)
writes the given toplitst to a temporary file, then renames the temporary file to filename.
int write_houghFstat_toplist_to_fp(toplist_t *tl, FILE *fp, UINT4 *checksum)
Writes the toplist to an (already open) filepointer Returns the number of written charactes sets the ...
int insert_into_houghFstat_toplist(toplist_t *tl, HoughFstatOutputEntry elem)
Inserts an element in to the toplist either if there is space left or the element is larger than the ...
#define LOGIOERROR(mess, filename)
int write_hfs_oputput(const char *filename, toplist_t *tl)
write the final output file:
static int print_houghFstatline_to_str(HoughFstatOutputEntry fline, char *buf, int buflen)
#define XLAL_LAST_ELEM(x)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
void XLALDestroyREAL4Vector(REAL4Vector *vector)
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
Type to hold the fields that will be kept in a "toplist"
REAL8 AlphaBest
skyposition of best candidate: longitude
REAL4 sumTwoF
sum of 2F-values as recomputed in LV postprocessing
REAL8 Delta
skyposition: latitude
REAL8 VarianceSig
variance of significance values in hough map
REAL8 f1dot
spindown value f1dot = df/dt
REAL8 Alpha
Skyposition: longitude in equatorial coords, radians.
REAL4Vector * sumTwoFX
sum of 2F-values per detector, computed in LV postprocessing
REAL8 Freq
Frequency at maximum (?) of the cluster.
REAL8 DeltaBest
skyposition of best candidate: latitude
REAL8 MeanSig
mean of significance values in hough map
REAL8 HoughFstat
Hough significance.