25 #include <sys/types.h>
29 #define buffsize_mb 0.5
30 #define primary_hdr_size 6
32 int main(
int argc,
char *argv[]) {
36 int packet_length = 0;
40 int last_pkt_in_buffer;
48 static int pkt_len_off = 4;
50 static int time_cnt = 8;
51 static int L0_true = 0;
52 static int L0_false = 1;
53 static int day_pkt_size = 642;
54 static int night_pkt_size = 276;
55 static unsigned char time_tag[8];
64 unsigned char *buffer;
65 FILE *stream, *stream_w =
NULL;
77 printf(
"fix_L0: Version as of 07/16/07\n\n");
79 stream = fopen(argv[1],
"r");
81 printf(
"%s not found.\n", argv[1]);
84 fseek(stream, (
long) 0, SEEK_SET);
86 taitime_start = atof(argv[2]);
87 taitime_stop = atof(argv[3]);
89 if (taitime_stop == -1) {
92 stream_w = fopen(argv[4],
"wb");
96 buffer = (
unsigned char *) malloc(buffsize *
sizeof (
unsigned char));
98 fseek(stream, (
long) 0, SEEK_SET);
102 last_pkt_in_file = L0_false;
103 found_start_time = L0_false;
105 while (last_pkt_in_file == L0_false) {
106 last_pkt_in_buffer = L0_false;
108 read_cnt = fread(buffer,
sizeof (
char), buffsize, stream);
111 while (last_pkt_in_buffer == L0_false) {
112 bytes_left = read_cnt - buf_pos;
113 if (bytes_left < day_pkt_size) {
114 if (bytes_left == 0) {
115 last_pkt_in_buffer = L0_true;
117 }
else if ((bytes_left == night_pkt_size) ||
118 (bytes_left == 2 * night_pkt_size)) {
119 }
else if (feof(stream) == 0) {
120 last_pkt_in_buffer = L0_true;
121 fseek(stream, strm_pos, SEEK_SET);
126 printf(
"%d Total packets read\n", n_packets);
127 printf(
"taitime_stop: %f\n", taitime_good);
129 if (stream_w !=
NULL) fclose(stream_w);
132 if (atof(argv[2]) != -1) {
133 stat(argv[4], &filestat);
134 if (n_day * 642 + n_night * 276 != filestat.st_size) {
135 printf(
"output filesize discrepency.\n");
136 printf(
"Computed filesize: %d\n", n_day * 642 + n_night * 276);
137 printf(
"Actual filesize: %d\n", (
int) filestat.st_size);
146 packet_length = buffer[buf_pos + (pkt_len_off)]*256 +
147 buffer[buf_pos + pkt_len_off + 1];
152 if ((packet_length != 642) && (packet_length != 276)) {
154 printf(
"Packet with invalid length found %d %d\n",
155 n_packets, packet_length);
161 memcpy(time_tag, &buffer[buf_pos + time_off], time_cnt);
162 PGS_TD_EOSAMtoTAI(time_tag, &taitime_tag);
166 if (taitime_tag < taitime_start - eps || taitime_tag > taitime_stop + eps) {
167 printf(
"Bad L0 packet: %d %d\n",
168 taitime_tag < taitime_start,
169 taitime_tag > taitime_stop);
170 printf(
"Bad L0 packet: %f %f %f\n",
171 taitime_start, taitime_tag, taitime_stop);
175 if (taitime_start == -1 && found_start_time == L0_false) {
176 printf(
"taitime_start: %f\n", taitime_tag);
177 found_start_time = L0_true;
180 if (write && stream_w !=
NULL) {
181 status = fwrite(&buffer[buf_pos], packet_length, 1, stream_w);
188 taitime_good = taitime_tag;
190 strm_pos += (long) packet_length;
191 buf_pos += packet_length;
195 if (packet_length == 642) n_day++;
196 if (packet_length == 276) n_night++;
199 if ((n_packets % 100000) == 0) {
200 printf(
"%10d packets read\n", n_packets);
205 if (feof(stream) != 0) {
206 last_pkt_in_file = L0_true;
209 if (ferror(stream) != 0) {
211 fseek(stream, (
long) 0, SEEK_SET);
217 printf(
"%d Total packets read\n", n_packets);
218 printf(
"taitime_stop: %f\n", taitime_good);
220 if (stream_w !=
NULL) fclose(stream_w);
223 if (atof(argv[2]) != -1) {
224 stat(argv[4], &filestat);
225 if (n_day * 642 + n_night * 276 != filestat.st_size) {
226 printf(
"output filesize discrepency.\n");
227 printf(
"Computed filesize: %d\n", n_day * 642 + n_night * 276);
228 printf(
"Actual filesize: %d\n", (
int) filestat.st_size);