/* img_grab_constraints -B > output * * WHFS 23 Feb 2009 * * Read an imgfile of topography/bathymetry * that has even/odd constraint encoding, and * grab the odd numbered points. * * This is done to make an input for testing * the Nettleton routines. Eventually the * correct way to do the Nettleton input will * be by starting from the edited cruise and xyz * data, and computing correct error bars from * that. * Here, we fake the error bars by assigning * EPCT of the depth as the error. * */ #include "img_predict.h" #define EPCT 0.01 /* Error, relative to depth */ static unsigned char maskset[8] = {128, 64, 32, 16, 8, 4, 2, 1}; int main (int argc, char **argv) { struct BLOCK_CONSTRAINT output; unsigned char *b; short int h; int verbose = 0, error = 0, k, nx, ny, nt, nc = 0, h_abs; size_t nbytes; char *bfile, *tfile; FILE *fp; bfile = tfile = (char *)NULL; /* Parse arg list: */ for (k = 1; k < argc; k++) { if (argv[k][0] == '-') { switch (argv[k][1]) { case 'B': bfile = &argv[k][2]; break; case 'V': verbose = 1; break; default: fprintf (stderr, "img_grab_constraints: Unsupported option: %s\n", argv[k]); error ++; break; } } else { tfile = argv[k]; } } if (bfile == (char *)NULL) { fprintf (stderr, "img_grab_constraints: You must supply -B name for constraint bitmask.\n"); error ++; } if (tfile == (char *)NULL) { fprintf (stderr, "img_grab_constraints: You must supply of topography with constraints marked.\n"); error ++; } if (error) { fprintf (stderr, "usage: img_grab_constraints -B [-V] > output\n\n"); fprintf (stderr, "\tInput tfile.img is an img file of bathy/topo with constraints marked odd.\n"); fprintf (stderr, "\tOutput -B is a bit mask indicating which pixels were constrained.\n"); fprintf (stderr, "\tOutput to stderr is binary constraint structures.\n"); fprintf (stderr, "\tOption -V reports verbose operations to stderr.\n"); exit (EXIT_FAILURE); } if (check_imgfile_size (tfile, &nx, &ny) ) { fprintf (stderr, "img_grab_constraints: FATAL ERROR cannot get file size of %s\n", tfile); exit (EXIT_FAILURE); } if ( (fp = fopen (tfile, "r") ) == NULL) { fprintf (stderr, "img_grab_constraints: FATAL ERROR cannot open %s\n", tfile); exit (EXIT_FAILURE); } nt = nx * ny; nbytes = nt / 8; if ( (b = (unsigned char *) calloc (nbytes, (size_t)1) ) == NULL) { fprintf (stderr, "img_grab_constraints: FATAL ERROR cannot calloc %d bytes for bitmask.\n", (int)nbytes); fclose (fp); exit (EXIT_FAILURE); } for (k = 0; k < nt; k++) { if ( (fread ((void *)&h, (size_t)2, (size_t)1, fp) ) != 1) { fprintf (stderr, "img_grab_constraints: FATAL ERROR reading input at k = %d\n", k); fclose (fp); free ((void *)b); exit (EXIT_FAILURE); } h_abs = abs ( (int)h); if (h_abs%2) { /* h is an odd number */ output.index = k; output.source_id = (h > 0) ? 1 : 2; /* This is faked, of course. */ output.z = h; output.s = (short int) ceil (EPCT * h_abs); b[k/8] |= maskset[k%8]; fwrite ((void *)&output, (size_t)12, (size_t)1, stdout); nc++; } } fclose (fp); if ( (fp = fopen (bfile, "w") ) == NULL) { fprintf (stderr, "img_grab_constraints: FATAL ERROR cannot open %s\n", bfile); exit (EXIT_FAILURE); } if ( (fwrite ((void *)b, (size_t)1, nbytes, fp) ) != nbytes) { fprintf (stderr, "img_grab_constraints: FATAL ERROR cannot write %s\n", bfile); fclose (fp); free ((void *)b); exit (EXIT_FAILURE); } fclose (fp); free ((void *)b); if (verbose) fprintf (stderr, "img_grab_constraints: Found %d constraints in %d pixels.\n", nc, nt); exit (EXIT_SUCCESS); }