7 #include <sys/utsname.h>
16 #define PROGRAM "czl1merge"
19 int main(
int argc,
char *argv[])
45 struct stat file_stat;
46 l1_data_struc l1_data;
49 timqual_struc init_info;
50 int n_mstr,
i, mstr_last, in_ptr, mstr_ptr, nfiles, ifil, new_start,
51 new_end, rstat, n_ctl_pt, mtch_st, olap_end, nscans,
init = 1,
52 orb_st_day, num_used[
MAXFILES], scans_per_file[50];
54 char infiles[
MAXFILES][FILENAME_MAX];
56 char outfile[FILENAME_MAX],
outpath[FILENAME_MAX];
57 char inflist[FILENAME_MAX];
58 char seadas_vs[64], prog_version[64];
64 int get_current_time();
78 }
else if (argc == 3) {
88 printf(
"-E- %s: error reading list file %s\n", argv[0], inflist);
96 qsort((
char *) infiles, nfiles,
sizeof ( *infiles),
97 (
int (*)(
const void*,
const void*))strcmp);
99 printf(
"\nordered list of input L1 CZCS files:\n");
100 printf(
"numb name\n");
101 for (ifil = 0; ifil < nfiles; ifil++)
102 printf(
"%4d %s\n", ifil, infiles[ifil]);
106 n_mstr = nfiles * 970;
107 mstr.msec = (int32_t *) malloc(n_mstr *
sizeof ( int32_t));
108 mstr.exist = (
short *) calloc(n_mstr,
sizeof (
short));
109 mstr.qual = (
short *) malloc(n_mstr *
sizeof (
short));
110 mstr.ds_num = (
short *) malloc(n_mstr *
sizeof (
short));
111 mstr.out_scan = (int32_t *) malloc(n_mstr *
sizeof ( int32_t));
112 mstr.scan = (
short *) malloc(n_mstr *
sizeof (
short));
113 mstr.in_msec = (int32_t *) malloc(n_mstr *
sizeof ( int32_t));
114 mstr.in_exist = (
short *) malloc(n_mstr *
sizeof (
short));
115 mstr.in_qual = (
short *) malloc(n_mstr *
sizeof (
short));
116 mstr.in_scan = (
short *) malloc(n_mstr *
sizeof (
short));
124 for (ifil = 0; ifil < nfiles; ifil++) {
125 printf(
"czl1merge: processing input file %d: %s\n", ifil, infiles[ifil]);
129 if ((rstat =
cztimqual(infiles[ifil], &init_info, &orb_st_day)) == 0) {
130 scans_per_file[ifil] = init_info.nscan;
136 n_ctl_pt = init_info.n_ctl_pt;
138 if ((mstr_last == -1) || (mstr.msec[mstr_last] < init_info.msec[0])) {
139 fill_mstr(&mstr_last, &mstr, &init_info, ifil, 0,
146 mstr_ptr = mstr_last;
148 while (mtch_st == 0) {
149 if (mstr.msec[mstr_ptr] <= init_info.msec[0])
154 printf(
"czl1merge: new file matches at index %d, current end at %d\n",
155 mstr_ptr, mstr_last);
162 while (olap_end == 0) {
163 mmsec = mstr.msec[mstr_ptr];
164 imsec = init_info.msec[in_ptr];
165 if (imsec == mmsec) {
171 new_start = mstr_ptr;
174 mstr.in_msec[mstr_ptr] = init_info.msec[in_ptr];
175 mstr.in_exist[mstr_ptr] = 1;
176 mstr.in_qual[mstr_ptr] = init_info.qual[in_ptr];
177 mstr.in_scan[mstr_ptr] = in_ptr;
181 }
else if ((mstr.exist[mstr_ptr] == 0) &&
182 (imsec - mmsec < 40) && (imsec - mmsec > -40)) {
188 new_start = mstr_ptr;
191 mstr.in_msec[mstr_ptr] = init_info.msec[in_ptr];
192 mstr.in_exist[mstr_ptr] = 1;
193 mstr.in_qual[mstr_ptr] = init_info.qual[in_ptr];
194 mstr.in_scan[mstr_ptr] = in_ptr;
198 }
else if (imsec > mmsec) {
199 mstr.in_exist[mstr_ptr] = 0;
201 printf(
"czl1merge: gap found in new data vs current at mstr_ptr: %d\n",
203 }
else if (mmsec > imsec)
209 if ((mstr_ptr > mstr_last) || (in_ptr >= init_info.nscan))
215 printf(
"czl1merge: resolving overlap from index %d to %d\n",
217 olap_resolve(&mstr, mstr_last, new_start, new_end, ifil);
221 if (in_ptr < init_info.nscan) {
222 fill_mstr(&mstr_last, &mstr, &init_info, ifil, in_ptr,
232 free(init_info.qual);
233 free(init_info.msec);
236 printf(
"\nFinished set-up of best data to merge, Outputting data\n\n");
238 if (mstr_last == -1) {
239 printf(
"No suitable data exists for this set of files\n");
251 for (
i = 0;
i <= mstr_last;
i++) {
252 if (mstr.exist[
i] != 0) {
253 mstr.out_scan[
i] = nscans;
255 file_used[ mstr.ds_num[
i] ] = 1;
256 num_used[ mstr.ds_num[
i] ]++;
269 for (
i = 0;
i < nfiles;
i++)
270 if (file_used[
i] != 0) {
273 &gattr, &l1_data) != 0) exit(-1);
277 gattr.scan_lines = nscans;
289 if (stat(
outpath, &file_stat) == 0 && S_ISDIR(file_stat.st_mode)) {
292 strcpy(outfile + 19,
"MLAC");
296 strcpy(gattr.proc_ctl, argv[0]);
297 for (
i = 1;
i < argc;
i++) {
298 strcat(gattr.proc_ctl,
" ");
299 strcat(gattr.proc_ctl, argv[
i]);
304 for (
i = 1;
i < nfiles;
i++) {
305 strcat(gattr.input_files,
" ");
306 strcat(gattr.input_files,
basename(infiles[
i]));
320 struct utsname osname;
322 sprintf(gattr.soft_id,
"%s, %s, %s %s", seadas_vs, prog_version, osname.sysname,
325 strcpy(gattr.datatype,
"MLAC");
326 get_time((
char*) &gattr.process_time);
330 printf(
"\n Writing data to output file...\n");
332 printf(
"\n\nCreated output file: %s \n",
outpath);
333 printf(
"With %d lines\n", nscans);
334 printf(
"\nContributions from input files:\n\n");
335 printf(
"seq # file name # lines # lines contributed\n");
336 for (ifil = 0; ifil < nfiles; ifil++) {
339 printf(
"%5d %22s %5d %5d\n",
i, outfile, scans_per_file[ifil],
343 printf(
" output file not created\n");
353 printf(
"%s %s (%s %s)\n",
355 printf(
"\nUsage: %s input-CZCS-listfile [output-file-name or dir]\n",