|
ocssw
1.0
|
00001 /*---------------------------------------------------------------------- 00002 Copyright (C) 2000, Space Science and Engineering Center, University 00003 of Wisconsin-Madison, Madison WI. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 ------------------------------------------------------------------------*/ 00019 00020 00021 #include <stdio.h> 00022 #include <stdlib.h> 00023 #include <string.h> 00024 #include <PGS_TD.h> 00025 #include <sys/types.h> 00026 #include <sys/stat.h> 00027 #include <unistd.h> 00028 00029 #define buffsize_mb 0.5 00030 #define primary_hdr_size 6 00031 00032 00033 int main (int argc, char *argv[]) 00034 { 00035 int n_packets = 0; 00036 int read_cnt; 00037 long strm_pos = 0; 00038 int packet_length = 0; 00039 int buf_pos = 0; 00040 int buf_pos_last; 00041 int bytes_left; 00042 int last_pkt_in_file; 00043 int last_pkt_in_buffer; 00044 int found_start_time; 00045 int buffsize; 00046 int write; 00047 int n_night=0; 00048 int n_day=0; 00049 struct stat filestat; 00050 00051 static int pkt_len_off = 4; 00052 static int time_off = primary_hdr_size; 00053 static int time_cnt = 8; 00054 static int L0_true = 0; 00055 static int L0_false = 1; 00056 static int day_pkt_size = 642; 00057 static int night_pkt_size = 276; 00058 static unsigned char time_tag[8]; 00059 00060 char *cptr = (char *) &n_packets; 00061 00062 double taitime_start; 00063 double taitime_stop; 00064 double taitime_tag; 00065 double taitime_good; 00066 double eps=1e-6; 00067 size_t status; 00068 00069 char utc_str[28]; 00070 unsigned char *buffer, outbuf[384]; 00071 FILE *stream, *stream_w=NULL; 00072 00073 /* 00074 1) Get start and stop times 00075 00076 ./fix_L0 L0file -1 -1 00077 00078 2) fix L0 00079 00080 ./fix_L0 L0file starttime stoptime outputL0 00081 */ 00082 00083 printf("fix_L0: Version as of 07/16/07\n\n"); 00084 00085 stream = fopen( argv[1], "r"); 00086 if (stream == NULL) { 00087 printf("%s not found.\n", argv[1]); 00088 exit (-1); 00089 } 00090 fseek( stream, (long) 0, SEEK_SET); 00091 00092 taitime_start = atof(argv[2]); 00093 taitime_stop = atof(argv[3]); 00094 00095 if (taitime_stop == -1) { 00096 taitime_stop=1e30; 00097 } else { 00098 stream_w = fopen( argv[4], "wb"); 00099 } 00100 00101 buffsize = buffsize_mb*1000000; 00102 buffer = (unsigned char *) malloc( buffsize*sizeof(unsigned char) ); 00103 if ( buffer == NULL ) 00104 { 00105 fseek( stream, (long) 0, SEEK_SET); 00106 return 1; 00107 } 00108 00109 last_pkt_in_file = L0_false; 00110 found_start_time = L0_false; 00111 00112 while ( last_pkt_in_file == L0_false ) 00113 { 00114 last_pkt_in_buffer = L0_false; 00115 00116 read_cnt = fread( buffer, sizeof(char), buffsize, stream ); 00117 00118 buf_pos = 0; 00119 while ( last_pkt_in_buffer == L0_false ) 00120 { 00121 bytes_left = read_cnt - buf_pos; 00122 if ( bytes_left < day_pkt_size ) 00123 { 00124 if ( bytes_left == 0 ) { 00125 last_pkt_in_buffer = L0_true; 00126 continue; 00127 } 00128 else if ((bytes_left == night_pkt_size) || 00129 (bytes_left == 2*night_pkt_size)) { 00130 } 00131 else if ( feof(stream) == 0 ) { 00132 last_pkt_in_buffer = L0_true; 00133 fseek( stream, strm_pos, SEEK_SET ); 00134 continue; 00135 } else { 00136 free( buffer ); 00137 00138 printf("%d Total packets read\n", n_packets); 00139 printf("taitime_stop: %f\n", taitime_good); 00140 00141 if (stream_w != NULL) fclose(stream_w); 00142 fclose(stream); 00143 00144 if (atof(argv[2]) != -1) { 00145 stat(argv[4], &filestat); 00146 if (n_day*642+n_night*276 != filestat.st_size) { 00147 printf("output filesize discrepency.\n"); 00148 printf("Computed filesize: %d\n", n_day*642+n_night*276); 00149 printf("Actual filesize: %d\n", (int)filestat.st_size); 00150 return 3; 00151 } 00152 } 00153 00154 return 0; 00155 } 00156 } 00157 00158 packet_length = buffer[buf_pos + (pkt_len_off)]*256 + 00159 buffer[buf_pos + pkt_len_off+1]; 00160 packet_length += 1; 00161 packet_length += primary_hdr_size; 00162 00163 write = 1; 00164 if (( packet_length != 642 ) && ( packet_length != 276 )) 00165 { 00166 if ( n_packets > 0 ) { 00167 printf("Packet with invalid length found %d %d\n", 00168 n_packets, packet_length); 00169 write = 0; 00170 } 00171 } 00172 00173 00174 memcpy( time_tag, &buffer[buf_pos + time_off], time_cnt ); 00175 PGS_TD_EOSAMtoTAI( time_tag, &taitime_tag); 00176 00177 // if (n_packets == 300000) printf("%f\n", taitime_tag); 00178 00179 if ( taitime_tag < taitime_start-eps || taitime_tag > taitime_stop+eps) 00180 { 00181 printf("Bad L0 packet: %d %d\n", 00182 taitime_tag < taitime_start, 00183 taitime_tag > taitime_stop); 00184 printf("Bad L0 packet: %f %f %f\n", 00185 taitime_start, taitime_tag, taitime_stop); 00186 write = 0; 00187 } 00188 00189 if (taitime_start == -1 && found_start_time == L0_false) { 00190 printf("taitime_start: %f\n", taitime_tag); 00191 found_start_time = L0_true; 00192 } 00193 00194 if (write && stream_w != NULL) { 00195 status = fwrite(&buffer[buf_pos], packet_length, 1, stream_w); 00196 if (status != 1) { 00197 exit(4); 00198 } 00199 } 00200 00201 if (write) 00202 taitime_good = taitime_tag; 00203 00204 strm_pos += (long) packet_length; 00205 buf_pos_last = buf_pos; 00206 buf_pos += packet_length; 00207 n_packets++; 00208 00209 if (write) { 00210 if (packet_length == 642) n_day++; 00211 if (packet_length == 276) n_night++; 00212 } 00213 00214 if ((n_packets % 100000) == 0) { 00215 printf("%10d packets read\n", n_packets); 00216 } 00217 00218 } 00219 00220 if ( feof(stream) != 0 ) 00221 { 00222 last_pkt_in_file = L0_true; 00223 } 00224 00225 if ( ferror(stream) != 0 ) 00226 { 00227 free( buffer ); 00228 fseek( stream, (long) 0, SEEK_SET); 00229 return 2; 00230 } 00231 } 00232 free( buffer ); 00233 00234 printf("%d Total packets read\n", n_packets); 00235 printf("taitime_stop: %f\n", taitime_good); 00236 00237 if (stream_w != NULL) fclose(stream_w); 00238 fclose(stream); 00239 00240 if (atof(argv[2]) != -1) { 00241 stat(argv[4], &filestat); 00242 if (n_day*642+n_night*276 != filestat.st_size) { 00243 printf("output filesize discrepency.\n"); 00244 printf("Computed filesize: %d\n", n_day*642+n_night*276); 00245 printf("Actual filesize: %d\n", (int)filestat.st_size); 00246 return 3; 00247 } 00248 } 00249 00250 return 0; 00251 }
1.7.6.1