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"); }
static void gridnodes_tweaknpolar(gridnodes* gn) { int nx = gridnodes_getnx(gn); int ny = gridnodes_getny(gn); double** gx = gridnodes_getx(gn); double** gy = gridnodes_gety(gn); int i, j; for (i = 0; i < nx; ++i) for (j = 1; j < ny; ++j) if (fabs(gx[j][i] - gx[j - 1][i]) > 180.0) gx[j][i] += 360.0; for (j = 0; j < ny; ++j) for (i = 1; i < nx; ++i) if (fabs(gx[j][i] - gx[j][i - 1]) > 180.0) { gx[j][i - 1] = NaN; gy[j][i - 1] = NaN; } for (j = 0; j < ny; ++j) for (i = 1; i < nx; ++i) if (fabs(gy[j][i] - gy[j][i - 1]) > 90.0) { gx[j][i - 1] = NaN; gy[j][i - 1] = NaN; } }
void grid_ij2xy(grid* g, int i, int j, double* x, double* y) { if (g->htype == GRIDHTYPE_LATLON_REGULAR || g->htype == GRIDHTYPE_LATLON_IRREGULAR) { *x = ((gnxy_simple*) g->gridnodes_xy)->x[i]; *y = ((gnxy_simple*) g->gridnodes_xy)->y[j]; } #if !defined(NO_GRIDUTILS) else if (g->htype == GRIDHTYPE_CURVILINEAR) { *x = gridnodes_getx(((gnxy_curv*) g->gridnodes_xy)->gn)[j][i]; *y = gridnodes_gety(((gnxy_curv*) g->gridnodes_xy)->gn)[j][i]; } #endif else enkf_quit("programming error"); }
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; }
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; }
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; }
int main(int argc, char* argv[]) { char* fname1 = NULL; char* fname2 = NULL; gridnodes* grid1 = NULL; gridnodes* grid2 = NULL; gridnodes* grid3 = NULL; NODETYPE type = NT_NONE; int i0 = INT_MAX; int j0 = INT_MAX; int nx1 = 0; int ny1 = 0; int nx2 = 0; int ny2 = 0; int nx3 = 0; int ny3 = 0; double** gx1 = NULL; double** gy1 = NULL; double** gx2 = NULL; double** gy2 = NULL; double** gx3 = NULL; double** gy3 = NULL; int i1start, i2start, j1start, j2start; int i1, i2, j1, j2, i3, j3; parse_commandline(argc, argv, &fname1, &fname2, &i0, &j0, &type); grid1 = gridnodes_read(fname1, NT_NONE); grid2 = gridnodes_read(fname2, NT_NONE); nx1 = gridnodes_getnx(grid1); ny1 = gridnodes_getny(grid1); nx2 = gridnodes_getnx(grid2); ny2 = gridnodes_getny(grid2); gx1 = gridnodes_getx(grid1); gy1 = gridnodes_gety(grid1); gx2 = gridnodes_getx(grid2); gy2 = gridnodes_gety(grid2); nx3 = (i0 + nx2 > nx1) ? i0 + nx2 : nx1; ny3 = (j0 + ny2 > ny1) ? j0 + ny2 : ny1; grid3 = gridnodes_create(nx3, ny3, type); gx3 = gridnodes_getx(grid3); gy3 = gridnodes_gety(grid3); if (i0 < 0) { i1start = i0; i2start = 0; } else { i1start = 0; i2start = -i0; } if (j0 < 0) { j1start = j0; j2start = 0; } else { j1start = 0; j2start = -j0; } if (!merge) { for (j3 = 0, j1 = j1start, j2 = j2start; j3 < ny3; ++j3, ++j1, ++j2) { for (i3 = 0, i1 = i1start, i2 = i2start; i3 < nx3; ++i3, ++i1, ++i2) { int ok2 = (j2 >= 0 && j2 < ny2 && i2 >= 0 && i2 <= nx2); if (ok2) { gx3[j3][i3] = gx2[j2][i2]; gy3[j3][i3] = gy2[j2][i2]; } else { int ok1 = (j1 >= 0 && j1 < ny1 && i1 >= 0 && i1 <= nx1); if (ok1) { gx3[j3][i3] = gx1[j1][i1]; gy3[j3][i3] = gy1[j1][i1]; } else { gx3[j3][i3] = NaN; gy3[j3][i3] = NaN; } } } } } else { for (j3 = 0, j1 = j1start, j2 = j2start; j3 < ny3; ++j3, ++j1, ++j2) { for (i3 = 0, i1 = i1start, i2 = i2start; i3 < nx3; ++i3, ++i1, ++i2) { int ok2 = (j2 >= 0 && j2 < ny2 && i2 >= 0 && i2 <= nx2); int ok1 = (j1 >= 0 && j1 < ny1 && i1 >= 0 && i1 <= nx1); if (!ok2 && !ok1) { gx3[j3][i3] = NaN; gy3[j3][i3] = NaN; } else if (ok2 && !ok1) { gx3[j3][i3] = gx2[j2][i2]; gy3[j3][i3] = gy2[j2][i2]; } else if (!ok2 && ok1) { gx3[j3][i3] = gx1[j1][i1]; gy3[j3][i3] = gy1[j1][i1]; } else { if (!isnan(gx2[j2][i2])) { gx3[j3][i3] = gx2[j2][i2]; gy3[j3][i3] = gy2[j2][i2]; } else { gx3[j3][i3] = gx1[j1][i1]; gy3[j3][i3] = gy1[j1][i1]; } } } } } gridnodes_validate(grid3); gridnodes_write(grid3, "stdout", CT_XY); gridnodes_destroy(grid1); gridnodes_destroy(grid2); gridnodes_destroy(grid3); 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; }
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; }