--- ray/src/util/rcollate.c 2019/11/08 05:39:05 2.32 +++ ray/src/util/rcollate.c 2020/09/07 17:08:08 2.36 @@ -1,12 +1,11 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.32 2019/11/08 05:39:05 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.36 2020/09/07 17:08:08 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) */ #include -#include #include #include "platform.h" #include "rtio.h" @@ -294,6 +293,7 @@ int i_header = 1; /* input header? */ int o_header = 1; /* output header? */ int outArray[MAXLEVELS][2]; /* output block nesting */ int outLevels = 0; /* number of blocking levels */ +int check = 0; /* force data check? */ /* parse RxCx... string */ static int @@ -387,9 +387,9 @@ get_input_pos(int r, int c) if (outLevels > 1) { /* block reordering */ n = get_block_pos(r, c, outArray, outLevels); if (transpose) { - r = n/no_columns; - c = n - r*no_columns; - n = (long)r*ni_columns + c; + r = n/ni_rows; + c = n - r*ni_rows; + n = (long)c*ni_columns + r; } } else if (transpose) /* transpose only */ n = (long)c*ni_columns + r; @@ -408,9 +408,9 @@ do_reorder(const MEMLOAD *mp) int i, j; /* propogate sizes */ if (ni_rows <= 0) - ni_rows = no_columns; + ni_rows = transpose ? no_columns : no_rows; if (ni_columns <= 0) - ni_columns = no_rows; + ni_columns = transpose ? no_rows : no_columns; /* get # records (& index) */ if (!comp_size) { if ((rp = index_records(mp, n_comp)) == NULL) @@ -421,8 +421,8 @@ do_reorder(const MEMLOAD *mp) } else if ((ni_rows > 0) & (ni_columns > 0)) { nrecords = ni_rows*ni_columns; if (nrecords > mp->len/(n_comp*comp_size)) { - fprintf(stderr, - "Input too small for specified size and type\n"); + fputs("Input too small for specified size and type\n", + stderr); return(0); } } else @@ -430,7 +430,7 @@ do_reorder(const MEMLOAD *mp) /* check sizes */ if ((ni_rows <= 0) & (ni_columns > 0)) ni_rows = nrecords/ni_columns; - if ((ni_columns <= 0) & (ni_rows > 0)) + else if ((ni_columns <= 0) & (ni_rows > 0)) ni_columns = nrecords/ni_rows; if (nrecords != ni_rows*ni_columns) goto badspec; @@ -439,13 +439,33 @@ do_reorder(const MEMLOAD *mp) no_columns = ni_rows; if (no_rows <= 0) no_rows = ni_columns; - if ((no_rows != ni_columns) | (no_columns != ni_rows)) + if (outLevels <= 1 && + (no_rows != ni_columns) | (no_columns != ni_rows)) goto badspec; + } else { + if (no_columns <= 0) + no_columns = ni_columns; + if (no_rows <= 0) + no_rows = ni_rows; } + if (ni_rows*ni_columns != no_rows*no_columns) { + fputs("Number of input and output records do not match\n", + stderr); + return(0); + } + if (o_header) { /* finish header? */ + printf("NROWS=%d\n", no_rows); + printf("NCOLS=%d\n", no_columns); + fputc('\n', stdout); + } /* reorder records */ for (i = 0; i < no_rows; i++) { for (j = 0; j < no_columns; j++) { long n = get_input_pos(i, j); + if (n >= nrecords) { + fputs("Index past end-of-file\n", stderr); + return(0); + } if (rp != NULL) { /* ASCII output */ print_record(rp, n); putc(tabEOL[j >= no_columns-1], stdout); @@ -474,8 +494,17 @@ do_resize(FILE *fp) long records2go = ni_rows*ni_columns; int columns2go = no_columns; char word[256]; + + if (o_header) { /* finish header? */ + if (no_rows > 0) + printf("NROWS=%d\n", no_rows); + if (no_columns > 0) + printf("NCOLS=%d\n", no_columns); + fputc('\n', stdout); + } /* sanity checks */ - if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows)) + if (comp_size || !check & + (no_columns == ni_columns) & (no_rows == ni_rows)) return(output_stream(fp)); /* no-op -- just copy */ if (no_columns <= 0) { fprintf(stderr, "Missing -oc specification\n"); @@ -485,7 +514,7 @@ do_resize(FILE *fp) records2go = no_rows*no_columns; else if (no_rows*no_columns != records2go) { fprintf(stderr, - "Input and output data sizes disagree (%dx%d != %dx%d)\n", + "Number of input and output records disagree (%dx%d != %dx%d)\n", ni_rows, ni_columns, no_rows, no_columns); return(0); } @@ -652,6 +681,9 @@ main(int argc, char *argv[]) case 'w': /* warnings on/off */ warnings = !warnings; break; + case 'c': /* force check operation */ + check = 1; + break; default: goto userr; } @@ -671,7 +703,7 @@ main(int argc, char *argv[]) SET_FILE_BINARY(stdout); } /* check for no-op */ - if (!transpose & (outLevels <= 1) & (i_header == o_header) && + if (!transpose & !check & (outLevels <= 1) & (i_header == o_header) && (no_columns == ni_columns) & (no_rows == ni_rows)) { if (warnings) fprintf(stderr, "%s: no-op -- copying input verbatim\n", @@ -695,15 +727,10 @@ main(int argc, char *argv[]) if (!i_header) newheader("RADIANCE", stdout); printargs(a, argv, stdout); - if (no_rows > 0) - printf("NROWS=%d\n", no_rows); - if (no_columns > 0) - printf("NCOLS=%d\n", no_columns); printf("NCOMP=%d\n", n_comp); fputformat(fmtid, stdout); - fputc('\n', stdout); /* finish new header */ } - if (transpose | (outLevels > 1)) { /* moving stuff around? */ + if (transpose | check | (outLevels > 1) || (o_header && no_rows <= 0)) { MEMLOAD myMem; /* need to map into memory */ if (a == argc-1) { if (load_file(&myMem, stdin) <= 0) { @@ -719,12 +746,12 @@ main(int argc, char *argv[]) if (!do_reorder(&myMem)) return(1); /* free_load(&myMem); about to exit, so don't bother */ - } else if (!do_resize(stdin)) /* reshaping input */ + } else if (!do_resize(stdin)) /* just reshaping input */ return(1); return(0); userr: fprintf(stderr, -"Usage: %s [-h[io]][-w][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", +"Usage: %s [-h[io]][-w][-c][-f[afdb][N]][-t][-ic in_col][-ir in_row][-oc out_col][-or out_row][-o RxC[xR1xC1..]] [input.dat]\n", argv[0]); return(1); }