327 #include <sys/types.h>
328 #include <sys/wait.h>
331 #include <sys/stat.h>
334 #ifdef HAVE_GDS_DTT_SISTR_H
335 #include <gds/dtt/SIStr.h>
337 #error SIStr header is not available
341 #include <lal/LALMalloc.h>
342 #include <lal/XLALError.h>
343 #include <lal/LALConstants.h>
344 #include <lal/LALgetopt.h>
345 #include <lal/LALPulsarVCSInfo.h>
347 #ifdef HAVE_LIBLALFRAME
348 #include <lal/Units.h>
349 #include <lal/TimeSeries.h>
350 #include <lal/LALFrameIO.h>
353 #define MAXPULSARS 64
355 #define BLOCKSIZE 16384
409 void syserror(
int showerrno,
const char *
fmt, ... );
411 void usage( FILE *filep );
420 int minutes = seconds / 60;
421 int hours = minutes / 60;
422 int days = hours / 24;
423 int secs = seconds % 60;
425 syserror( 0,
"made %d days %d hours %d minutes %d seconds of data\n",
426 days, hours % 24, minutes % 60, secs );
428 syserror( 0,
"Received signal %d\n", sig );
430 if ( sig == SIGTERM ) {
431 syserror( 0,
"received SIGTERM, initiating graceful shutdown...\n" );
435 if ( sig != SIGCHLD ) {
439 if ( ( pid = waitpid( -1, &
status, WNOHANG | WUNTRACED ) ) > 0 ) {
441 if ( WIFSTOPPED(
status ) ) {
442 syserror( 0,
"Subprocess [PID=%d] stopped because it caught signal %d [%s]\n",
443 (
int )pid, WSTOPSIG(
status ), strsignal( WSTOPSIG(
status ) ) );
448 syserror( 0,
"Subprocess [PID=%d] is misbehaving.\n", (
int )pid );
449 if ( WIFEXITED(
status ) ) {
450 syserror( 0,
"Subprocess [PID=%d] or shell did exit(%d)\n", (
int )pid, WEXITSTATUS(
status ) );
453 if ( WIFSIGNALED(
status ) )
454 syserror( 0,
"Subprocess [PID=%d] terminated because it caught signal %d [%s]\n",
455 (
int )pid, WTERMSIG(
status ), strsignal( WTERMSIG(
status ) ) );
458 syserror( 1,
"waitpid() returned -1. Call Bruce...\n" );
466 char *thiserror = NULL;
467 pid_t pid = getpid();
468 time_t
t =
time( NULL );
473 if ( showerrno && errno && ( thiserror = strerror( errno ) ) ) {
474 fprintf( stderr,
"%s [PID=%d] %.24s: %s: ",
programname, (
int )pid, ctime( &
t ), thiserror );
478 vfprintf( stderr,
fmt,
ap );
488 "--------------------------------------------------------------------------------\n"
490 "--------------------------------------------------------------------------------\n"
492 "-h THIS help message\n"
493 "-v VCS ID information\n"
494 "-n INT Number of pulsars to simulate: 0, 1, 2, ..., %d\n"
495 "-d DIRECTORY Directory containing command line files in.0, in.1, ...\n"
496 " Default is: . (current working directory)\n"
497 "-e CHANNEL Inject excitation into CHANNEL\n"
498 "-D Turn on Debug output for routines in signal injection library\n"
499 "-G INTGPS GPS time (integer seconds) that will be passed on command line\n"
500 "-T Print human readable text rather than binary to stdout\n"
501 "-X Include X-axis in human-readable text output\n"
502 "-s Print commands that would be fork(2)ed, then exit(0)\n"
503 " ---------- --------------------------------------------------\n"
504 "-L DOUBLE | Inject calibration lines. Here L,M,H denote Low/Mid/High |\n"
505 "-M DOUBLE | frequency. The DOUBLE values specifies the corresponding |\n"
506 "-H DOUBLE | amplitude. If NOT given, the amplitude defaults to 0.0 |\n"
507 " ------------- -----------------------------------------------\n"
508 "-p Print the calibration line frequencies in Hz then exit(0)\n"
509 "-I STRING Detector: LHO, LLO, GEO, VIRGO, TAMA, CIT, ROME [REQUIRED]\n"
510 "-A STRING File containing detector actuation-function [OPTIONAL]\n"
511 #ifdef HAVE_LIBLALFRAME
512 "-F INT Keep N frame files on disk. If N==0 write all frames immediately.\n"
513 "-S INT Number of 1-second frames per frame file (default 60).\n"
515 "-r INT Sampling rate (NOTE: strain-generators must use the same!) (Default:16384)\n"
516 "-z DOUBLE Delay: shift CHANNEL signals by round[offset*samplingRate] samples forward (Default:0)\n"
517 "--------------------------------------------------------------------------------\n"
528 const char *optionlist =
"hL:M:H:n:d:e:DG:TXspI:A:F:vS:r:z:";
531 double starttime_offset_req = 0;
532 double starttime_offset_samples = 0;
539 while ( -1 != (
c =
LALgetopt( argc, argv, optionlist ) ) ) {
552 printf(
"The calibration line frequencies are:\n"
568 if ( npulsars < 0 || npulsars >
MAXPULSARS ) {
569 syserror( 0,
"%s: Number of pulsars (-n argument = %d) must be non-negative and less than %d\n",
580 syserror( 1,
"-%c %s is invalid. -%c takes a double-precision amplitude.\n",
c,
LALoptarg,
c );
586 }
else if (
c ==
'M' ) {
610 syserror( 0,
"The -e option to enable online signal injection requires compilation with -DONLINE\n" );
619 syserror( 0,
"The -D option to enable SIStr debugging requires compilation with -DONLINE\n" );
646 #ifdef HAVE_LIBLALFRAME
649 if ( how_many < 0 ) {
650 syserror( 0,
"%s: fatal error, argument -F %d must be non-negative.\n", argv[0], how_many );
656 syserror( 0,
"%s: -F specified, but this binary was built without frame support.\n", argv[0] );
670 #ifdef HAVE_LIBLALFRAME
681 syserror( 1,
"-%c %s is invalid. -%c takes a double-precision amplitude.\n",
c,
LALoptarg,
c );
688 syserror( 0,
"%s: Option argument: -%c unrecognized or missing argument.\n"
689 "\t\tPlease use the '-h' option to print usage message\n"
700 syserror( 0,
"The -X (axis) option only works with the -T (human readable) option\n" );
704 syserror( 0,
"Can't use both '-T' and '-e' together\n" );
708 syserror( 0,
"Excitation channel %s not of form CC:CCC...\n",
channel );
712 syserror( 0,
"You must specify the IFO name (-I)\n" );
719 starttime_offset_samples = floor( starttime_offset_req *
sampling_rate + 0.5 );
721 if ( starttime_offset_req ) {
722 syserror( 0,
"starttime OFFSET requested = %+.16g s (offset > 0 means a *delay*)\n", starttime_offset_req );
728 syserror( 0,
"Can't do exicitations. Code not compiled with ONLINE defined\n" );
737 int main(
int argc,
char *argv[] )
745 memset( &sis,
'\0',
sizeof( SIStream ) );
748 #ifdef HAVE_LIBLALFRAME
749 int framecounter = 0;
762 struct sigaction sig;
763 memset( &sig,
'\0',
sizeof( sig ) );
764 sig.sa_flags = SA_RESTART;
766 if ( sigaction( SIGCHLD, &sig, NULL ) ) {
767 syserror( 1,
"Unable to install signal handler for messages about troubled children\n" );
769 if ( sigaction( SIGTERM, &sig, NULL ) ) {
770 syserror( 1,
"Unable to install signal handler for logging output rate data and terminating\n" );
772 if ( sigaction( SIGUSR1, &sig, NULL ) ) {
773 syserror( 1,
"Unable to install signal handler for logging output rate data\n" );
782 char *newlineloc = NULL;
786 syserror( 0,
"%s: file name %s/in.%d has more than MAXLINE=%d characters\n",
792 if ( !( fpc = fopen(
filename,
"r" ) ) ) {
798 if ( !( fgets( command,
MAXLINE, fpc ) ) ) {
805 length = strlen( command );
807 syserror( 0,
"Command line file %s has line >= to MAXLINE=%d characters!\n",
813 if ( ( newlineloc = strchr( command,
'\n' ) ) ) {
819 length = strlen( command );
821 command[length] =
'\0';
826 length = strlen( command );
828 command[length] =
'\0';
833 length = strlen( command );
835 command[length] =
'\0';
841 length = strlen( command );
843 command[length] =
'\0';
851 printf(
"[%02d] %s\n",
i, command );
854 if ( !(
fp[
i] = popen( command,
"r" ) ) || errno ) {
855 syserror( 1,
"Unable to popen(3) %s\n", command );
864 printf(
"%s: Warning: n=0 so an infinite-length zero strength signal will be made!\n", argv[0] );
875 pid = waitpid( -1, &
status, WNOHANG | WUNTRACED );
877 syserror( 0,
"Subprocess with PID=%d is misbehaving.\n", (
int )pid );
878 if ( WIFEXITED(
status ) ) {
879 syserror( 0,
"Subprocess or shell did exit(%d)\n", WEXITSTATUS(
status ) );
882 if ( WIFSIGNALED(
status ) )
883 syserror( 0,
"Subprocess terminated because it caught signal %d [%s]\n",
892 if ( fread( &
testval,
sizeof(
float ), 1,
fp[
i] ) != 1 ) {
893 syserror( 1,
"Could not read first float 1234.5 from %d'th signal source\n",
i );
895 }
else if (
testval != 1234.5 ) {
896 syserror( 0,
"First value (%f) from %d'th signal source was not 1234.5\n",
testval,
i );
898 }
else if ( fread(
bufflen +
i,
sizeof(
int ), 1,
fp[
i] ) != 1 ) {
899 syserror( 1,
"Could not read buffer size from %d'th signal source\n",
i );
905 syserror( 0,
"Bad buffer size %d floats from %d'th signal source NOT a multiple of BLOCKSIZE=%d\n",
bufflen[
i],
i,
BLOCKSIZE );
907 }
else if ( !(
buffs[
i] = (
float * )calloc(
bufflen[
i],
sizeof(
float ) ) ) ) {
908 syserror( 1,
"Can't allocate buffer of %d floats for %d'th signal source\n",
bufflen[
i],
i );
920 cwd = getcwd( NULL, 256 );
922 sprintf(
info,
"%s %s", argv[0], cwd );
924 sprintf(
info,
"%s unknown_directory", argv[0] );
927 SIStrAppInfo(
info );
934 if (
status != SIStr_OK ) {
935 syserror( 0,
"SIStrOpen() error opening SIStream: %s\n", SIStrErrorMsg(
status ) );
940 printf(
"1234.5\n" );
943 if ( 1 != fwrite( &
testval,
sizeof(
float ), 1, stdout ) ) {
944 syserror( 1,
"Unable to output key value 1234.5\n" );
961 for ( line = 0; line < 3; line++ ) {
962 if (
calamp[line] != 0.0 ) {
975 int f_int = (
int )f_fra;
990 double cycles1, cycles2, cycles3;
991 double tlocal_fra = dt_fra + ( double )
j / ( (
double )
sampling_rate );
992 int tlocal_int = (
int )tlocal_fra;
993 tlocal_fra -= tlocal_int;
995 cycles1 = f_fra * tlocal_int;
996 cycles1 -= (
int )cycles1;
997 cycles2 = tlocal_fra * f_int;
998 cycles2 -= (
int )cycles2;
999 cycles3 = tlocal_fra * f_fra;
1000 cycles3 -= (
int )cycles3;
1014 syserror( 1,
"Only read %d floats (not %d) from %d'th signal source\n", num,
bufflen[
i],
i );
1037 #ifdef HAVE_LIBLALFRAME
1043 const char frName[] =
"CW_simulated";
1046 syserror( 1,
"XLALCreateREAL4TimeSeries() failed" );
1058 const char framename[] =
"CW_Injection";
1061 syserror( 1,
"XLALFrameNew() failed" );
1067 syserror( 1,
"!XLALFrameAddREAL4TimeSeriesSimData() failed" );
1072 char framefilename[256];
1073 snprintf( framefilename,
sizeof( framefilename ),
"%s-%d-%d.gwf", framename, framesim->
epoch.
gpsSeconds, secs_per_framefile );
1075 syserror( 1,
"Error during frame write" );
1085 int watchtime =
gpstime + secs_per_framefile * ( framecounter / secs_per_framefile -
write_frames + 1 );
1086 sprintf( listname,
"%s-%d-%d.gwf", framename, watchtime, secs_per_framefile );
1088 struct stat statbuf;
1089 while ( !
stat( listname, &statbuf ) ) {
1091 struct timespec rqtp;
1093 rqtp.tv_nsec = 100000000;
1094 nanosleep( &rqtp, NULL );
1101 syserror( 0,
"ERROR: write_frames!=0, but binary was built without Frame support\n" );
1111 if ( SIStr_debug >= 2 ) {
1114 if (
status != SIStr_OK ) {
1115 syserror( 0,
"SIStrAppend() error adding data to stream: %s\n",
1116 SIStrErrorMsg(
status ) );
1126 long long E9 = 1000000000;
1131 long long x2 =
count, x3;
1138 printf(
"%lld.%09lld %g\n", x3, x2,
total[
j] );
1144 printf(
"%g\n",
total[
j] );
1167 status = SIStrClose( &sis );
1168 if ( SIStr_debug ) {
1171 if (
status != SIStr_OK ) {
1172 syserror( 0,
"Error while closing SIStream: %s\n", SIStrErrorMsg(
status ) );
1181 struct sigaction sig;
1182 memset( &sig,
'\0',
sizeof( sig ) );
1183 sig.sa_flags = SA_RESTART | SA_NOCLDSTOP;
1184 sig.sa_handler = SIG_IGN;
1185 if ( sigaction( SIGCHLD, &sig, NULL ) ) {
1186 syserror( 1,
"Unable to install signal handler for exiting\n" );
1188 if ( sigaction( SIGPIPE, &sig, NULL ) ) {
1189 syserror( 1,
"Unable to install signal handler for exiting\n" );
1199 syserror( 0,
"The %d'th signal generator did exit(%d)\n",
i, (
int )WEXITSTATUS(
status ) );
1201 syserror( 1,
"Trouble shutting down the %d'th signal generator\n",
i );
1207 #ifdef HAVE_LIBLALFRAME
1214 syserror( 0,
"Shutdown complete, exiting cleanly\n" );
void LALCheckMemoryLeaks(void)
const LALVCSInfoList lalPulsarVCSInfoList
NULL-terminated list of VCS and build information for LALPulsar and its dependencies
int LALgetopt(int argc, char *const *argv, const char *optstring)
LALFrameUFrameH LALFrameH
int XLALFrameWrite(LALFrameH *frame, const char *fname)
int XLALFrameAddREAL4TimeSeriesSimData(LALFrameH *frame, const REAL4TimeSeries *series)
LALFrameH * XLALFrameNew(const LIGOTimeGPS *epoch, double duration, const char *project, int run, int frnum, INT8 detectorFlags)
void XLALFrameFree(LALFrameH *frame)
int XLALOutputVCSInfo(FILE *fp, const LALVCSInfoList vcs_list, const int verbose, const char *prefix)
void XLALDestroyREAL4TimeSeries(REAL4TimeSeries *series)
REAL4TimeSeries * XLALCreateREAL4TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
const LALUnit lalDimensionlessUnit
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
int main(int argc, char *argv[])
int readfrombuff[MAXPULSARS]
volatile int shutdown_pulsar_injection
void syserror(int showerrno, const char *fmt,...)
int parseinput(int argc, char **argv)
double starttime_offset_eff
float * buffs[MAXPULSARS]