#include #include #include #include /* #include #include */ #include "blockstat.h" unsigned char sfslope[NST]; /* On Smith's Mac, this has to be declared outside main() to avoid a segv. Apparently, variables inside use stack memory, outside use heap. */ int main (int argc, char **argv) { char strbuf[512]; struct BM_CSYS coords; struct BM_ERR_PARAMS err_p; struct BM_DATA_IN bmdi; FILE *fp_filelist = NULL; /* file listing names of files to read */ FILE *fp_data_in = NULL; /* data file we are currently reading */ FILE *fp_slopes = NULL; /* file with seafloor slope */ int coord_choice = -1; int stdin_is_list = 0; int nerrs = 0; int nargfiles = 0; /* # of file names given in **argv */ int karg; init_default_err_p (&err_p); for (karg = 1; karg < argc; karg++) { if (argv[karg][0] == '-') { switch (argv[karg][1]) { case 'C': nerrs += ((sscanf(&argv[karg][2], "%d", &coord_choice)) != 1); break; case 'H': nerrs += ((sscanf(&argv[karg][2], "%lf/%lf", &err_p.ha, &err_p.hb)) != 2); break; case 'L': if (argv[karg][2] == '-') { stdin_is_list = 1; } else { if ((fp_filelist = fopen (&argv[karg][2], "r")) == NULL) { fprintf (stderr, "Cannot open %s\n", &argv[karg][2]); nerrs++; } } break; case 'S': if ((fp_slopes = fopen (&argv[karg][2], "r")) == NULL) { fprintf (stderr, "Cannot open %s\n", &argv[karg][2]); nerrs++; } break; case 'V': nerrs += ((sscanf(&argv[karg][2], "%lf/%lf", &err_p.va, &err_p.vb)) != 2); break; case 'Z': nerrs += ((sscanf(&argv[karg][2], "%lf", &err_p.smin)) != 1); break; default: nerrs++; break; } } else { nargfiles++; } } if (init_coords (&coords, coord_choice)) { fprintf (stderr, "You must set a coordinate system choice.\n"); nerrs++; } if (square_err_a (&err_p) ) { fprintf (stderr, "Error parameters must be non-negative.\n"); nerrs++; } if (load_slopes (fp_slopes, sfslope) ) { fprintf (stderr, "Error loading seafloor slopes.\n"); nerrs++; } if (nerrs || (argc == 1) ) { fprintf (stderr, "usage: cm2bm_prep -C<#> -S [-L] [-H/] [-V/] [-Z] [file1] [file2] ... > binary_output\n\n"); if (argc == 1) exit (EXIT_SUCCESS); fprintf (stderr, "\tUse -C<#> to set the block coordinate system, where <#> is one of:\n"); fprintf (stderr, "\t-C%d to select the SRTM30 (lon/lat, 30 arc sec, to +/- 90, pixel) system.\n", GTOPO30); fprintf (stderr, "\t-C%d to select the IMG1M72 (mercator, 1 arc min, to +/- 72.005977, pixel) system.\n", IMG1M72); fprintf (stderr, "\t-C%d to select the IMG1M81 (mercator, 1 arc min, to +/- 80.738009, pixel) system.\n", IMG1M81); fprintf (stderr, "\t-C%d to select the IMG2M72 (mercator, 2 arc min, to +/- 72.005977, pixel) system.\n", IMG2M72); fprintf (stderr, "\t-C%d to select the IMG2M81 (mercator, 2 arc min, to +/- 80.738009, pixel) system.\n\n", IMG2M81); fprintf (stderr, "\t-S required filename for 1 byte per 1 arcmin global pixel seafloor slope file bottomslope.byt\n\n"); fprintf (stderr, "\tSpecify one or more files to read in ascii cm format, in one of these ways:\n"); fprintf (stderr, "\t-L when is the name of a file with one input file name per line.\n"); fprintf (stderr, "\t-L- when standard input will be read to obtain one input file name per line.\n"); fprintf (stderr, "\tOr do not use -L and standard input will be scanned for ascii input data in cm format.\n\n"); fprintf (stderr, "\t-H/ sets horizontal uncertainty (in meters) constants. [Default %lg/%lg ].\n", SIG_H_A, SIG_H_B); fprintf (stderr, "\t-V/ sets vertical uncertainty (in meters) constants. [Default %lg/%lg ].\n", SIG_V_A, SIG_V_B); fprintf (stderr, "\tThe above are formulated as s = sqrt(a*a + (b*z)*(b*z)), with z the depth in meters.\n"); fprintf (stderr, "\tThese values are used as over-rides only when the input data gives non-positive uncertainties.\n"); fprintf (stderr, "\t-Z sets minimum uncertainty to use if computed uncertainties fall below . [Default %lg ].\n", SIG_Z_MIN); exit (EXIT_FAILURE); } /* Here begins the read over files. Use get_next_fp to find the next file to read: */ karg = 0; while ( (fp_data_in = get_next_fp (stdin_is_list, argc, argv, &nargfiles, &karg, fp_filelist, fp_data_in) ) ) { while (fgets (strbuf, 512, fp_data_in) ) { if (process_cm_record (strbuf, sfslope, &err_p, &coords, &bmdi)) continue; fwrite ((void *)&bmdi, sizeof(struct BM_DATA_IN), (size_t)1, stdout); } } exit (0); } int process_cm_record (char *strbuf, unsigned char *sfslope, struct BM_ERR_PARAMS *err_p, struct BM_CSYS *coords, struct BM_DATA_IN *bmdi) { /* returns -1 if record can't be processed, or 0 if bmdi is loaded with good data. */ double t, x, y, z, sh, sv, t_pred; int source_id, nscan; nscan = sscanf (strbuf, "%lf %lf %lf %lf %lf %lf %d %lf", &t, &x, &y, &z, &sh, &sv, &source_id, &t_pred); if (nscan < 8) { fprintf (stderr, "Read error. Input line skipped.\n"); return (-1); } if (fabs(sv) > 9998.) { return (-1); } if (fabs(y) > 90.0 || x < -190.0 || x > 360.0) { fprintf (stderr, "Bad lon or lat. Input line skipped.\n"); return (-1); } if (fabs(z) > 32767) { fprintf (stderr, "Bad z value. Input line skipped.\n"); return (-1); } if (source_id > 65536 || source_id < 1) { fprintf (stderr, "Bad sid value. Input line skipped.\n"); return (-1); } if (y <= -(coords->maxlat) || y > coords->maxlat) return (-1); if (x < 0.0) x += 360.0; if (x == 360.0) x = 0.0; dat2bmdi (x, y, z, sh, sv, source_id, sfslope, err_p, coords, bmdi); return (0); }