Пример #1
0
void grid_ij2xy(grid* g, int i, int j, double* x, double* y)
{
    if (g->htype == GRIDHTYPE_LATLON) {
        gnxy_simple* gs = (gnxy_simple*) g->gridnodes_xy;

        if (i < 0 || j < 0 || i >= gs->nx || j >= gs->ny) {
            *x = NaN;
            *y = NaN;
        } else {
            *x = gs->x[i];
            *y = gs->y[j];
        }
    }
#if !defined(NO_GRIDUTILS)
    else if (g->htype == GRIDHTYPE_CURVILINEAR) {
        gridnodes* gn = ((gnxy_curv*) g->gridnodes_xy)->gn;

        if (i < 0 || j < 0 || i >= gridnodes_getnce1(gn) || j >= gridnodes_getnce2(gn)) {
            *x = NaN;
            *y = NaN;
        } else {
            *x = gridnodes_getx(gn)[j][i];
            *y = gridnodes_gety(gn)[j][i];
        }
    }
#endif
    else
        enkf_quit("programming error");
}
Пример #2
0
int main(int argc, char* argv[])
{
    char* fname = NULL;
    int ij = 0;
    int compact = 0;
    NODETYPE nt = NT_DD;
    gridnodes* gn = NULL;
    poly* pl = NULL;

    parse_commandline(argc, argv, &fname, &compact, &ij, &nt);

    if (nt == NT_DD) {
        /*
         * read DD grid nodes 
         */
        gridnodes* gndd = gridnodes_read(fname, NT_DD);

        gridnodes_validate(gndd);
        /*
         * get corner grid nodes from DD grid nodes 
         */
        gn = gridnodes_transform(gndd, NT_COR);
        gridnodes_destroy(gndd);
    } else {
        gn = gridnodes_read(fname, NT_COR);
        gridnodes_validate(gn);
    }

    /*
     * build boundary polygon 
     */
    if (!ij)
        pl = poly_formbound(gridnodes_getnce1(gn), gridnodes_getnce2(gn), gridnodes_getx(gn), gridnodes_gety(gn));
    else
        pl = poly_formboundij(gridnodes_getnce1(gn), gridnodes_getnce2(gn), gridnodes_getx(gn));

    if (compact)
        poly_compact(pl, 1.0e-10);

    poly_write(pl, stdout);

    poly_destroy(pl);
    gridnodes_destroy(gn);

    return 0;
}
Пример #3
0
static void grid_setlonbase(grid* g)
{
    double xmin = DBL_MAX;
    double xmax = -DBL_MAX;

    if (g->htype == GRIDHTYPE_LATLON_REGULAR || g->htype == GRIDHTYPE_LATLON_IRREGULAR) {
        double* x = ((gnxy_simple*) g->gridnodes_xy)->x;
        int nx = ((gnxy_simple*) g->gridnodes_xy)->nx;

        if (xmin > x[0])
            xmin = x[0];
        if (xmin > x[nx - 1])
            xmin = x[nx - 1];
        if (xmax < x[0])
            xmax = x[0];
        if (xmax < x[nx - 1])
            xmax = x[nx - 1];
#if !defined(NO_GRIDUTILS)
    } else if (g->htype == GRIDHTYPE_CURVILINEAR) {
        double** x = gridnodes_getx(((gnxy_curv*) g->gridnodes_xy)->gn);
        int nx = gridnodes_getnce1(((gnxy_curv*) g->gridnodes_xy)->gn);
        int ny = gridnodes_getnce2(((gnxy_curv*) g->gridnodes_xy)->gn);
        int i, j;

        for (j = 0; j < ny; ++j) {
            for (i = 0; i < nx; ++i) {
                if (xmin > x[j][i])
                    xmin = x[j][i];
                if (xmax < x[j][i])
                    xmax = x[j][i];
            }
        }
#endif
    }
    if (xmax - xmin >= 360.0)
        g->lonbase = xmin;
    else if (xmin < 0.0 && xmax < 180.0)
        g->lonbase = -180.0;
    else if (xmin >= 0.0 && xmax < 360.0)
        g->lonbase = 0.0;
    else
        g->lonbase = xmin;
}
Пример #4
0
gridmap* gridmap_build2(gridnodes* gn)
{
    gridmap* gm = malloc(sizeof(gridmap));
    int nce1 = gridnodes_getnce1(gn);
    int nce2 = gridnodes_getnce2(gn);
    double** gx = gridnodes_getx(gn);
    double** gy = gridnodes_gety(gn);
    int type = gridnodes_getmaptype(gn);

    gm->type = type;
    if (gm->type == GRIDMAP_TYPE_BINARY)
        gm->map = gridbmap_build(nce1, nce2, gx, gy);
    else if (gm->type == GRIDMAP_TYPE_KDTREE)
        gm->map = gridkmap_build(nce1, nce2, gx, gy);
    else
        gu_quit("grid map type = %d: unknown type", type);
    gm->sign = 0;

    return gm;
}
Пример #5
0
static gnxy_curv* gnxy_curv_create(int nodetype, int nx, int ny, double** x, double** y)
{
    gnxy_curv* nodes = malloc(sizeof(gnxy_curv));

    if (nodetype == NT_CEN) {
        gridnodes* gn_new;

        nodes->gn = gridnodes_create2(nx, ny, NT_CEN, x, y);
        gn_new = gridnodes_transform(nodes->gn, NT_COR);
        gridnodes_destroy(nodes->gn);
        nodes->gn = gn_new;
    } else if (nodetype == NT_COR)
        nodes->gn = gridnodes_create2(nx, ny, NT_COR, x, y);
    else
        enkf_quit("unknown node type for horizontal curvilinear grid");
#if defined(ENKF_PREP)
    gridnodes_validate(nodes->gn);
    nodes->gm = gridmap_build(gridnodes_getnce1(nodes->gn), gridnodes_getnce2(nodes->gn), gridnodes_getx(nodes->gn), gridnodes_gety(nodes->gn));
#else
    nodes->gm = NULL;
#endif
    return nodes;
}
Пример #6
0
int main(int argc, char* argv[])
{
    char* gridfname = NULL;
    char* maskfname = NULL;
    NODETYPE ntin = NT_DD;
    NODETYPE ntout = NT_DD;
    COORDTYPE ct = CT_XY;
    int tweaknpolar = 0;
    gridnodes* gn = NULL;

    parse_commandline(argc, argv, &gridfname, &maskfname, &ntin, &ntout, &ct, &tweaknpolar);
    gn = gridnodes_read(gridfname, ntin);
    gridnodes_validate(gn);
    if (ntin != ntout) {
        gridnodes* gnnew = gridnodes_transform(gn, ntout);

        gridnodes_destroy(gn);
        gn = gnnew;
    }
    if (maskfname != NULL) {
        int nx = gridnodes_getnce1(gn);
        int ny = gridnodes_getnce2(gn);
        int** mask = gu_readmask(maskfname, nx, ny);

        gridnodes_applymask(gn, mask);
        gu_free2d(mask);
    }
    if (tweaknpolar)
        gridnodes_tweaknpolar(gn);
    if (gu_verbose)
        gridnodes_calcstats(gn);
    gridnodes_write(gn, "stdout", ct);
    gridnodes_destroy(gn);

    return 0;
}
Пример #7
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;
}
Пример #8
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;
}