28 #define buffsize_mb 0.5
29 #define primary_hdr_size 6
32 #define O_STRING "nf:"
34 #define USAGE "write_constructor_file (%s %s)\n\
36 Usage: %s [-n] [-f outfile] infile\n\
39 If outfile is not specified, it will be infile.constr\n\
41 -n : do not generate a constructor file but print statistics\n\
45 printf(
USAGE, __DATE__, __TIME__, prog);
48 int main(
int argc,
char *argv[]) {
52 int packet_length = 0;
57 int last_pkt_in_buffer;
67 static int pkt_len_off = 4;
69 static int time_cnt = 8;
70 static int L0_true = 0;
71 static int L0_false = 1;
72 static int day_pkt_size = 642;
73 static int night_pkt_size = 276;
74 static unsigned char start_time_tag[8];
75 static unsigned char stop_time_tag[8];
76 char *cptr = (
char *) &n_packets;
83 char outfile[FILENAME_MAX];
87 FILE *stream, *outfp =
NULL;
97 while ((
c = getopt(argc, argv,
O_STRING)) != -1) {
103 len = snprintf(outfile,
sizeof (outfile),
"%s", optarg);
104 if (strlen(outfile) == len) {
107 printf(
"specified output file name too long!\n");
119 if (optind < argc && !errflag) {
120 printf(
"write_constructor_file: Version as of %s %s\n", __DATE__, __TIME__);
126 if (!fopt && !nopt) {
127 len = snprintf(outfile, FILENAME_MAX,
"%s.constr",
infile);
128 if (strlen(outfile) != len) {
129 printf(
"derived output file name is too long!\n");
135 stream = fopen(
infile,
"r");
137 if (stream !=
NULL) {
140 outfp = fopen(outfile,
"w");
142 if (outfp !=
NULL || nopt) {
144 fseek(stream, (
long) 0, SEEK_SET);
147 buffer = (
unsigned char *) malloc(buffsize *
sizeof (
unsigned char));
148 if (buffer ==
NULL) {
149 fseek(stream, (
long) 0, SEEK_SET);
153 last_pkt_in_file = L0_false;
154 found_start_time = L0_false;
156 while (last_pkt_in_file == L0_false) {
157 last_pkt_in_buffer = L0_false;
159 read_cnt = fread(buffer,
sizeof (
char), buffsize, stream);
163 while (last_pkt_in_buffer == L0_false) {
164 bytes_left = read_cnt - buf_pos;
165 if (bytes_left < day_pkt_size) {
166 if (bytes_left == 0) {
167 last_pkt_in_buffer = L0_true;
169 }
else if ((bytes_left == night_pkt_size) ||
170 (bytes_left == 2 * night_pkt_size)) {
171 }
else if (feof(stream) == 0) {
172 last_pkt_in_buffer = L0_true;
173 fseek(stream, strm_pos, SEEK_SET);
176 printf(
"Corrupt record found at end of file.\n");
178 fseek(stream, (
long) 0, SEEK_SET);
183 packet_length = buffer[buf_pos + (pkt_len_off)]*256 +
184 buffer[buf_pos + pkt_len_off + 1];
188 if ((packet_length != 642) && (packet_length != 276)) {
191 printf(
"Packet with invalid length found: (%d).\n", packet_length);
194 fseek(stream, (
long) 0, SEEK_SET);
198 if (packet_length == 642) n_day++;
199 if (packet_length == 276) n_night++;
201 if (found_start_time == L0_false) {
202 memcpy(start_time_tag, &buffer[buf_pos + time_off], time_cnt);
203 found_start_time = L0_true;
205 memcpy(stop_time_tag, &buffer[buf_pos_last + time_off], time_cnt);
206 PGS_TD_EOSAMtoTAI(stop_time_tag, &taitime_stop);
207 PGS_TD_TAItoUTC(taitime_stop, (
char*) outbuf);
211 strm_pos += (long) packet_length;
212 buf_pos_last = buf_pos;
213 buf_pos += packet_length;
215 if ((n_packets % 100000) == 0) {
216 printf(
"%10d packets read\n", n_packets);
220 if (feof(stream) != 0) {
221 last_pkt_in_file = L0_true;
224 if (ferror(stream) != 0) {
226 fseek(stream, (
long) 0, SEEK_SET);
234 printf(
"%d Total packets read\n", n_packets);
236 printf(
"%d day packets\n", n_day);
237 printf(
"%d night packets\n", n_night);
243 printf(
"Writing constuctor record for %s to %s\n",
infile, outfile);
247 fseek(outfp, 0, SEEK_SET);
249 memset(&outbuf[0x33], 1, 1);
251 memcpy(&outbuf[0x50], start_time_tag, 8);
252 memcpy(&outbuf[0x58], stop_time_tag, 8);
254 memcpy(&outbuf[0x16c], start_time_tag, 8);
255 memcpy(&outbuf[0x174], stop_time_tag, 8);
257 memcpy(&outbuf[0x74], cptr + 3, 1);
258 memcpy(&outbuf[0x75], cptr + 2, 1);
259 memcpy(&outbuf[0x76], cptr + 1, 1);
260 memcpy(&outbuf[0x77], cptr + 0, 1);
262 memset(&outbuf[0x93], 1, 1);
263 memset(&outbuf[0xa3], 1, 1);
264 memset(&outbuf[0xf7], 2, 1);
266 memset(&outbuf[0x167], 1, 1);
268 if (fwrite(outbuf,
OUTBUFLEN, 1, outfp) == 0) {
269 printf(
"fwrite failed: %s\n", strerror(
errno));
281 PGS_TD_EOSAMtoTAI(start_time_tag, &taitime_start);
282 PGS_TD_TAItoUTC(taitime_start, (
char*) outbuf);
283 printf(
"starttime=%s\n", outbuf);
285 PGS_TD_EOSAMtoTAI(stop_time_tag, &taitime_stop);
286 PGS_TD_TAItoUTC(taitime_stop, (
char*) outbuf);
287 printf(
"stoptime =%s\n", outbuf);
289 printf(
"granule length =%f\n", taitime_stop - taitime_start);
292 printf(
"failed to open output file: %s\n", strerror(
errno));
297 printf(
"failed to open input file: %s\n", strerror(
errno));