Exemple #1
0
static void gc_fij2xy(void* p, double fi, double fj, double* x, double* y)
{
    gnxy_curv* nodes = (gnxy_curv*) ((grid*) p)->gridnodes_xy;

    gridmap_fij2xy(nodes->gm, fi, fj, x, y);
}
Exemple #2
0
int main(int argc, char* argv[])
{
    char* gfname = NULL;
    char* ofname = NULL;
    FILE* of = NULL;
    gridnodes* gn = NULL;
    gridmap* map = NULL;
    char buf[BUFSIZE];

    parse_commandline(argc, argv, &gfname, &ofname);
    if (nt == NT_DD) {
        gridnodes* gndd = gridnodes_read(gfname, NT_DD);

        gridnodes_validate(gndd);
        gn = gridnodes_transform(gndd, NT_COR);
        gridnodes_destroy(gndd);
    } else {
        gn = gridnodes_read(gfname, NT_COR);
        gridnodes_validate(gn);
    }

    /*
     * build grid map 
     */
    map = gridmap_build(gridnodes_getnce1(gn), gridnodes_getnce2(gn), gridnodes_getx(gn), gridnodes_gety(gn));

    if (strcmp(ofname, "stdin") == 0 || strcmp(ofname, "-") == 0)
        of = stdin;
    else
        of = gu_fopen(ofname, "r");

    /*
     * read points to be mapped, do the mapping and write results to stdout 
     */
    while (fgets(buf, BUFSIZE, of) != NULL) {
        char rem[BUFSIZE] = "";
        double xc, yc, ic, jc;

        if (sscanf(buf, "%lf %lf %[^\n]", &xc, &yc, rem) >= 2) {
            if ((!reverse && gridmap_xy2fij(map, xc, yc, &ic, &jc)) || (reverse && gridmap_fij2xy(map, xc, yc, &ic, &jc))) {
                if (!isnan(ic))
                    printf("%.15g %.15g %s\n", ic, jc, rem);
                else
                    printf("NaN NaN %s\n", rem);
            } else {
                if (!force)
                    quit("could not convert (%.15g, %.15g) from %s to %s space\n", xc, yc, (reverse) ? "index" : "physical", (reverse) ? "physical" : "index");
                else
                    printf("NaN NaN %s\n", rem);
            }
        } else
            printf("%s", buf);
    }

    if (of != stdin)
        fclose(of);
    gridmap_destroy(map);
    gridnodes_destroy(gn);

    return 0;
}
int main(int argc, char* argv[])
{
    char* bathyfname = NULL;
    int nbathy = -1;
    point* pbathy = NULL;

    char* gridfname = NULL;
    NODETYPE nt = NT_DD;
    gridnodes* gn = NULL;
    gridmap* gm = NULL;

    char* maskfname = NULL;
    int** mask = NULL;

    int ppe = PPE_DEF;
    double zmin = ZMIN_DEF;
    double zmax = ZMAX_DEF;

    delaunay* d = NULL;

    void (*interpolate_point) (void*, point *) = NULL;
    void* interpolator = NULL;

    int i = -1, j = -1;

    parse_commandline(argc, argv, &bathyfname, &gridfname, &maskfname, &nt, &ppe, &zmin, &zmax, &i, &j);

    /*
     * sanity check 
     */
    if (bathyfname == NULL)
        quit("no input bathymetry data specified");
    if (gridfname == NULL)
        quit("no input grid data specified");
    if (ppe <= 0 || ppe > PPE_MAX)
        quit("number of points per edge specified = %d greater than %d", ppe, PPE_MAX);
    if (zmin >= zmax)
        quit("min depth = %.3g > max depth = %.3g", zmin, zmax);
    if (nt != NT_DD && nt != NT_COR)
        quit("unsupported node type");

    /*
     * read bathymetry 
     */
    points_read(bathyfname, 3, &nbathy, &pbathy);

    if (gu_verbose)
        fprintf(stderr, "## %d input bathymetry values", nbathy);
    if (nbathy < 3)
        quit("less than 3 input bathymetry values");

    /*
     * read and validate grid 
     */
    gn = gridnodes_read(gridfname, nt);
    gridnodes_validate(gn);
    /*
     * read mask
     */
    if (maskfname != NULL) {
        int nx = gridnodes_getnce1(gn);
        int ny = gridnodes_getnce2(gn);

        mask = gu_readmask(maskfname, nx, ny);
    }
    /*
     * transform grid nodes to corner type 
     */
    if (nt != NT_COR) {
        gridnodes* newgn = gridnodes_transform(gn, NT_COR);

        gridnodes_destroy(gn);
        gn = newgn;
    }
    /*
     * build the grid map for physical <-> index space conversions
     */
    gm = gridmap_build(gridnodes_getnce1(gn), gridnodes_getnce2(gn), gridnodes_getx(gn), gridnodes_gety(gn));

    /*
     * convert bathymetry to index space if necessary 
     */
    if (indexspace) {
        point* newpbathy = malloc(nbathy * sizeof(point));
        int newnbathy = 0;
        int ii;

        for (ii = 0; ii < nbathy; ++ii) {
            point* p = &pbathy[ii];
            point* newp = &newpbathy[newnbathy];
            double ic, jc;

            if (gridmap_xy2fij(gm, p->x, p->y, &ic, &jc)) {
                newp->x = ic;
                newp->y = jc;
                newp->z = p->z;
                newnbathy++;
            }
        }

        free(pbathy);
        pbathy = newpbathy;
        nbathy = newnbathy;
    }

    /*
     * create interpolator 
     */
    if (rule == CSA) {          /* using libcsa */
        interpolator = csa_create();
        csa_addpoints(interpolator, nbathy, pbathy);
        csa_calculatespline(interpolator);
        interpolate_point = (void (*)(void*, point *)) csa_approximatepoint;
    } else if (rule == AVERAGE) {
        interpolator = ga_create(gm);
        ga_addpoints(interpolator, nbathy, pbathy);
        interpolate_point = (void (*)(void*, point *)) ga_getvalue;
        ppe = 1;
    } else {                    /* using libnn */
        /*
         * triangulate 
         */
        if (gu_verbose) {
            fprintf(stderr, "## triangulating...");
            fflush(stdout);
        }
        d = delaunay_build(nbathy, pbathy, 0, NULL, 0, NULL);
        if (gu_verbose) {
            fprintf(stderr, "done\n");
            fflush(stderr);
        }

        if (rule == NN_SIBSON || rule == NN_NONSIBSONIAN) {
            interpolator = nnpi_create(d);
            if (rule == NN_SIBSON)
                nn_rule = SIBSON;
            else
                nn_rule = NON_SIBSONIAN;
            interpolate_point = (void (*)(void*, point *)) nnpi_interpolate_point;
        } else if (rule == LINEAR) {
            interpolator = lpi_build(d);
            interpolate_point = (void (*)(void*, point *)) lpi_interpolate_point;
        }
    }

    /*
     * main cycle -- over grid cells 
     */
    {
        double** gx = gridnodes_getx(gn);
        int jmin, jmax, imin, imax;

        if (i < 0) {
            imin = 0;
            imax = gridnodes_getnce1(gn) - 1;
            jmin = 0;
            jmax = gridnodes_getnce2(gn) - 1;
        } else {
            if (gu_verbose)
                fprintf(stderr, "## calculating depth for cell (%d,%d)\n", i, j);
            imin = i;
            imax = i;
            jmin = j;
            jmax = j;
        }

        for (j = jmin; j <= jmax; ++j) {
            for (i = imin; i <= imax; ++i) {
                double sum = 0.0;
                int count = 0;
                int ii, jj;

                if ((mask != NULL && mask[j][i] == 0) || isnan(gx[j][i]) || isnan(gx[j + 1][i + 1]) || isnan(gx[j][i + 1]) || isnan(gx[j + 1][i])) {
                    printf("NaN\n");
                    continue;
                }

                for (ii = 0; ii < ppe; ++ii) {
                    for (jj = 0; jj < ppe; ++jj) {
                        double fi = (double) i + 0.5 / (double) ppe * (1.0 + 2.0 * (double) ii);
                        double fj = (double) j + 0.5 / (double) ppe * (1.0 + 2.0 * (double) jj);
                        point p;

                        if (!indexspace)
                            gridmap_fij2xy(gm, fi, fj, &p.x, &p.y);
                        else {
                            p.x = fi;
                            p.y = fj;
                        }

                        interpolate_point(interpolator, &p);

                        if (isnan(p.z))
                            continue;
                        else if (p.z < zmin)
                            p.z = zmin;
                        else if (p.z > zmax)
                            p.z = zmax;

                        sum += p.z;
                        count++;
                    }
                }

                if (count == 0)
                    printf("NaN\n");
                else
                    printf("%.2f\n", sum / (double) count);
                fflush(stdout);
            }
        }
    }

    /*
     * clean up, just because 
     */
    if (rule == CSA)
        csa_destroy(interpolator);
    else if (rule == AVERAGE)
        ga_destroy(interpolator);
    else {
        if (rule == NN_SIBSON || rule == NN_NONSIBSONIAN)
            nnpi_destroy(interpolator);
        else if (rule == LINEAR)
            lpi_destroy(interpolator);
        delaunay_destroy(d);
    }
    if (mask != NULL)
        gu_free2d(mask);
    gridmap_destroy(gm);
    gridnodes_destroy(gn);
    free(pbathy);

    return 0;
}