Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
SFTwrite.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004 Bruce Allen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20/*
21 * Author: Bruce Allen
22 *
23 * Makes a test SFT (called SFT-test)
24 *
25 * You can do this on little-endian and big-endian machines to generate
26 * both flavors. This produces a set of good and a set of bad SFTs. The
27 * good SFTs are:
28 * SFT-test[12345678] and SFT-good
29 * and the bad SFTs are
30 * SFT-bad[123456789] and SFT-bad1[0-4]
31 */
32
33#include <stdio.h>
34#include <string.h>
35#include <stdlib.h>
36#include <errno.h>
37#include <lal/SFTReferenceLibrary.h>
38
39/* some local prototypes */
40FILE *openfile( const char *name );
41void printerror( int err );
42void dosystem( const char *command );
43void modify_bytes( const char *filename, int byte_offset, const char *new_value, int nbytes );
44int isbigendian( void );
45void modify_checksum( const char *filename, unsigned long long newchecksumle, unsigned long long newchecksumbe );
46
47/* function definitions */
48int isbigendian( void )
49{
50 short i = 0x0100;
51 char *tmp = ( char * )&i;
52 return *tmp;
53}
54
55FILE *openfile( const char *name )
56{
57 FILE *fp = fopen( name, "w" );
58 if ( !fp ) {
59 if ( errno ) {
60 perror( "Unable to open file for writing" );
61 }
62 fprintf( stderr, "Unable to open file %s for writing\n", name );
63 exit( SFTENULLFP );
64 }
65 return fp;
66}
67
68void printerror( int err )
69{
70 if ( err ) {
71 fprintf( stderr, "WriteSFT failed with return value %d\n", err );
72 exit( err );
73 }
74 return;
75}
76
77void dosystem( const char *command )
78{
79 int returnval = system( command );
80 if ( returnval ) {
81 fprintf( stderr, "Unable to execute the command: %s (exit status %d)\n", command, returnval );
82 exit( returnval );
83 }
84}
85
86void modify_bytes( const char *filename, int byte_offset, const char *new_value, int nbytes )
87{
88 FILE *fp = fopen( filename, "rb+" );
89 if ( !new_value ) {
90 fprintf( stderr, "modify_byes() called with null pointer for new data!\n" );
91 exit( SFTENULLFP );
92 }
93 if ( !fp ) {
94 if ( errno ) {
95 perror( "Unable to open file for writing" );
96 }
97 fprintf( stderr, "Unable to open file %s for reading/writing\n", filename );
98 exit( SFTENULLFP );
99 }
100 if ( fseek( fp, byte_offset, SEEK_SET ) ) {
101 if ( errno ) {
102 perror( "Failed fseek()" );
103 }
104 fprintf( stderr, "Failed seek in file %s\n", filename );
105 exit( SFTESEEK );
106 }
107 if ( 1 != fwrite( ( const void * )new_value, nbytes, 1, fp ) ) {
108 if ( errno ) {
109 perror( "Failed fwrite()" );
110 }
111 fprintf( stderr, "Failed write to file %s\n", filename );
112 exit( SFTESEEK );
113 }
114 fclose( fp );
115
116 return;
117}
118
119void modify_checksum( const char *filename, unsigned long long newchecksumle, unsigned long long newchecksumbe )
120{
121 if ( isbigendian() ) {
122 modify_bytes( filename, 32, ( char * )&newchecksumbe, 8 );
123 } else {
124 modify_bytes( filename, 32, ( char * )&newchecksumle, 8 );
125 }
126 return;
127}
128
129
130/* define some detectors for convenience */
131#define DET1 "H1"
132#define DET2 "L1"
133
134int main( void )
135{
136 FILE *fp;
137 float data[] = {1.0, 0.0, 2.0, -1.0, 3.0, -2.0, 4.0, -3.0};
138 int tmp;
139 double dtmp;
140
141 printerror( WriteSFT( fp = openfile( "SFT-test1" ), 12345, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 1, "test1", data ) );
142 fclose( fp );
143 printerror( WriteSFT( fp = openfile( "SFT-test2" ), 12345 + 60, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 1, "test2", data ) );
144 fclose( fp );
145 printerror( WriteSFT( fp = openfile( "SFT-test3" ), 12345 + 60 + 60, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 1, "test3", data ) );
146 fclose( fp );
147 printerror( WriteSFT( fp = openfile( "SFT-test4" ), 12345 + 60 + 60 + 60, 6789, 50, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 1, "test4", data ) );
148 fclose( fp );
149 printerror( WriteSFT( fp = openfile( "SFT-test5" ), 12345 + 60 + 60 + 60, 6789, 60, 1100, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 1, "test5", data ) );
150 fclose( fp );
151 printerror( WriteSFT( fp = openfile( "SFT-test6" ), 12345 + 60 + 60 + 60, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ) - 1, DET1, 1, "test6", data ) );
152 fclose( fp );
153 printerror( WriteSFT( fp = openfile( "SFT-test7" ), 12345 + 60 + 60 + 60, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET2, 1, "test7", data ) );
154 fclose( fp );
155
156 dosystem( "cat SFT-test[123] > SFT-good" );
157 /* GPS times not increasing */
158 dosystem( "cat SFT-test[123] SFT-test3 > SFT-bad1" );
159 /* Different time baselines */
160 dosystem( "cat SFT-test[124] > SFT-bad2" );
161 /* Different first frequency indexes */
162 dosystem( "cat SFT-test[125] > SFT-bad3" );
163 /* Different numbers of samples */
164 dosystem( "cat SFT-test[126] > SFT-bad4" );
165 /* Different detectors */
166 dosystem( "cat SFT-test[127] > SFT-bad5" );
167 /* Inconsistent checksum (corrupted comment, change test1 to Test1) */
168 dosystem( "cat SFT-test1 > SFT-bad6" );
169 modify_bytes( "SFT-bad6", 48, "T", 1 );
170 /* Nonexistent detector (change H1 to I1) checksum */
171 dosystem( "cat SFT-test1 > SFT-bad7" );
172 modify_bytes( "SFT-bad7", 40, "I", 1 );
173 modify_checksum( "SFT-bad7", 2594301065140926588ULL, 1040429337927860515ULL );
174 /* SFT with comment containing hidden data */
175 dosystem( "cat SFT-test1 > SFT-bad8" );
176 modify_bytes( "SFT-bad8", 49, "", 1 );
177 modify_checksum( "SFT-bad8", 9546122005026447583ULL, 12540893872916896128ULL );
178 /* SFT with comment no terminating null character containing hidden data */
179 printerror( WriteSFT( fp = openfile( "SFT-bad9" ), 12345, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 0, "test567", data ) );
180 fclose( fp );
181 modify_bytes( "SFT-bad9", 55, "8", 1 );
182 modify_checksum( "SFT-bad9", 8104828364422822013ULL, 6488197905334075682ULL );
183 /* GPS nsec too big/small */
184 tmp = 1000000000;
185 dosystem( "cat SFT-test1 > SFT-bad10" );
186 modify_bytes( "SFT-bad10", 12, ( char * )&tmp, 4 );
187 modify_checksum( "SFT-bad10", 14553276594141125530ULL, 13919267759677155442ULL );
188 tmp = -1;
189 dosystem( "cat SFT-test1 > SFT-bad11" );
190 modify_bytes( "SFT-bad11", 12, ( char * )&tmp, 4 );
191 modify_checksum( "SFT-bad11", 9905878389211410491ULL, 12381252028459463649ULL );
192 /* first frequency index negative */
193 tmp = -1;
194 dosystem( "cat SFT-test1 > SFT-bad12" );
195 modify_bytes( "SFT-bad12", 24, ( char * )&tmp, 4 );
196 modify_checksum( "SFT-bad12", 5605290681458522985ULL, 7307622666058252853ULL );
197 /* number of samples negative */
198 tmp = -1;
199 dosystem( "cat SFT-test1 > SFT-bad13" );
200 modify_bytes( "SFT-bad13", 28, ( char * )&tmp, 4 );
201 modify_checksum( "SFT-bad13", 14214363586458317283ULL, 6130423751169108856ULL );
202 /* time base not positive */
203 dtmp = -60.0;
204 dosystem( "cat SFT-test1 > SFT-bad14" );
205 modify_bytes( "SFT-bad14", 16, ( char * )&dtmp, 8 );
206 modify_checksum( "SFT-bad14", 9944972421627148413ULL, 15720824585133081082ULL );
207
208 for ( size_t i = 0; i < 8; i += 2 ) {
209 data[i] += 1e-5;
210 }
211 for ( size_t i = 1; i < 8; i += 2 ) {
212 data[i] -= 1e-5;
213 }
214 printerror( WriteSFT( fp = openfile( "SFT-test8" ), 12345, 6789, 60, 1000, sizeof( data ) / ( 2 * sizeof( float ) ), DET1, 0, "test8", data ) );
215 fclose( fp );
216
217 printf( "Wrote good SFTs: SFT-test[12345678] SFT-good\n" );
218 printf( "Wrote bad SFTs: SFT-bad[123456789] SFT-bad1[0-4]\n" );
219
220 return 0;
221}
#define DET2
Definition: SFTwrite.c:132
void dosystem(const char *command)
Definition: SFTwrite.c:77
void modify_checksum(const char *filename, unsigned long long newchecksumle, unsigned long long newchecksumbe)
Definition: SFTwrite.c:119
void modify_bytes(const char *filename, int byte_offset, const char *new_value, int nbytes)
Definition: SFTwrite.c:86
FILE * openfile(const char *name)
Definition: SFTwrite.c:55
int main(void)
Definition: SFTwrite.c:134
void printerror(int err)
Definition: SFTwrite.c:68
#define DET1
Definition: SFTwrite.c:131
int isbigendian(void)
Definition: SFTwrite.c:48
const char * name
Definition: SearchTiming.c:93
#define fprintf
double e
#define SFTESEEK
#define SFTENULLFP
int WriteSFT(FILE *fp, int gps_sec, int gps_nsec, double tbase, int firstfreqindex, int nsamples, const char *detector, unsigned short windowspec, const char *comment, float *data)
float data[BLOCKSIZE]
Definition: hwinject.c:360