static void demo2() { static int nx = 40, ny = 40; int i, j, k, lw, ci, ls; float f[1600], fmin, fmax, alev; double x, y; static float tr[6] = {0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; /* Compute a suitable function. A C array is used to emulate a 2D fortran array f(nx,ny). */ fmin = fmax = 0.0; for (j=1; j<=ny; j++) { for (i=1; i<=ny; i++) { k = (j-1)*nx + (i-1); /* Fortran convention */ x = tr[0] + tr[1]*i + tr[2]*j; y = tr[3] + tr[4]*i + tr[5]*j; f[k] = cos(0.3*sqrt(x*2)-0.13333*y)*cos(0.13333*x)+ (x-y)/(double)nx; if (f[k] < fmin) fmin = f[k]; if (f[k] > fmax) fmax = f[k]; } } /* Clear the screen. Set up window and viewport. */ cpgpage(); cpgsvp(0.05, 0.95, 0.05, 0.95); cpgswin(1.0, (float) nx, 1.0, (float) ny); cpgbox("bcts", 0.0, 0, "bcts", 0.0, 0); cpgmtxt("t", 1.0, 0.0, 0.0, "Contouring using cpgcont()"); /* Draw the map. cpgcont is called once for each contour, using different line attributes to distinguish contour levels. */ cpgbbuf(); for (i=1; i<21; i++) { alev = fmin + i*(fmax-fmin)/20.0; lw = (i%5 == 0) ? 3 : 1; ci = (i < 10) ? 2 : 3; ls = (i < 10) ? 2 : 1; cpgslw(lw); cpgsci(ci); cpgsls(ls); cpgcont(f, nx, ny, 1, nx, 1, ny, &alev, -1, tr); } cpgslw(1); cpgsls(1); cpgsci(1); cpgebuf(); return; }
static void do_pgcon_bs (double *blank, int *nc_sign) { unsigned int idim, jdim; float *a; int i_1, i_2, j_1, j_2; SLang_Array_Type *tr, *c, *at; at = NULL; if (-1 == pop_tr_vector (&tr)) return; if (-1 == pop_float_vector (&c)) goto return_error; if (-1 == pop_4_ints (&j_1, &j_2, &i_1, &i_2)) goto return_error; if (NULL == (at = pop_2d_float_array (&a, &jdim, &idim))) goto return_error; /* Convert to FORTRAN indexing */ i_1++; j_1++; i_2++; j_2++; if (blank != NULL) cpgconb (a, idim, jdim, i_1, i_2, j_1, j_2, (float *)c->data, c->num_elements, (float *)tr->data, *blank); else if (nc_sign != NULL) cpgcont (a, idim, jdim, i_1, i_2, j_1, j_2, (float *)c->data, *nc_sign * (int)c->num_elements, (float *)tr->data); else cpgcons (a, idim, jdim, i_1, i_2, j_1, j_2, (float *)c->data, c->num_elements, (float *)tr->data); return_error: free_arrays (tr, c, at, NULL); }
int plot(float ***u, const int nx, const int ny) { #ifdef PGPLOT const int ng=2; double xl, xr, yl, yr, dx, dy; float denscontours[NCONTOURS], prescontours[NCONTOURS]; float *dens, *pres; double maxd=-1e19, mind=+1e-19; double maxp=-1e19, minp=+1e-19; static int called = 0; int i, j, count; float tr[6] = {0.,0.,0.,0.,0.,0.}; xl = 0.; xr = nx-2*ng-1; yl = 0.; yr = ny-2*ng-1; dx = 1.; dy = 1.; dens = (float *)malloc((nx-2*ng)*(ny-2*ng)*sizeof(float)); pres = (float *)malloc((nx-2*ng)*(ny-2*ng)*sizeof(float)); count = 0; for (j=ny-ng; j>ng; j--) { for (i=ng; i<nx-ng; i++) { dens[count] = u[j][i][IDENS]; float vx = u[j][i][IMOMX]/dens[count]; float vy = u[j][i][IMOMY]/dens[count]; pres[count] = (u[j][i][IENER] - 0.5*(vx*vx+vy*vy)*dens[count]); if (dens[count] > maxd) maxd = dens[count]; if (dens[count] < mind) mind = dens[count]; if (pres[count] > maxp) maxp = pres[count]; if (pres[count] < minp) minp = pres[count]; count++; } } tr[0] = xl; tr[3] = yl; tr[1] = dx; tr[5] = dy; for (i=0; i<NCONTOURS; i++) { denscontours[i] = mind + (i+1)*(maxd-mind)/(NCONTOURS+1); prescontours[i] = minp + (i+1)*(maxp-minp)/(NCONTOURS+1); } if (!called) { cpgbeg(0, "/XWINDOW", 1, 1); called = 1; cpgask(0); } cpgenv(xl, xr, yl, yr, 1, 1); cpgsci(2); cpgcont(dens, nx-2*ng, ny-2*ng, 1, nx-2*ng, 1, ny-2*ng, denscontours, NCONTOURS, tr); if (minp != maxp) { cpgsci(3); cpgcont(pres, nx-2*ng, ny-2*ng, 1, nx-2*ng, 1, ny-2*ng, prescontours, NCONTOURS, tr); } cpgsci(1); free(dens); free(pres); #endif return 0; }
static PyObject * genContours_s (enum pp_contour_funcs ft, PyObject *args) { PyObject *oa=NULL, *oc=NULL; PyArrayObject *aa=NULL, *ac=NULL; float *a = NULL, *c = NULL, tr[6], x1=0.0,y1=0.0,x2=0.0,y2=0.0,blank=0.0, mn = 0.0, mx = 0.0; int rd=0, cd=0, csz=0, nc=0, ncont=0; if (!PyArg_ParseTuple(args,"Oi|Offfff:contour_s", &oa,&nc,&oc,&x1,&y1,&x2,&y2,&blank)) return(NULL); if (abs(nc)<1) { PyErr_SetString(PpgTYPEErr,"_ppgplot.error: Number of contours is 0"); return(NULL); } if (!(aa = (PyArrayObject *)tofloatmat(oa, &a, &rd, &cd))) goto fail; if (oc) { if (!(ac = (PyArrayObject *)tofloatvector(oc, &c, &csz))) goto fail; } else { if (!(c = malloc(abs(nc)*sizeof(*c)))) { PyErr_SetString(PpgTYPEErr,"_ppgplot.error: Out of mem!"); goto fail; } ncont = abs(nc); } /* Perform autocalibrations as nesecairy. */ autocal2d(a, rd, cd, &mx, &mn, ncont, c, &x1, &x2, &y1, &y2, tr); #ifdef DEBUG_CONT_S { int i; fprintf(stderr,"ncontours = %d = %d\n",nc,ncont); fprintf(stderr,"Contours:\n"); for (i=0; i<abs(nc); i++) fprintf(stderr," cont[%d] = %f\n",i,c[i]); fprintf(stderr,"blank = %f\n",blank); } #endif switch (ft) { case FUN_PGCONB: cpgconb(a,cd,rd,0+1,cd,0+1,rd,c,nc,tr,blank); break; case FUN_PGCONS: cpgcons(a,cd,rd,0+1,cd,0+1,rd,c,nc,tr); break; case FUN_PGCONT: cpgcont(a,cd,rd,0+1,cd,0+1,rd,c,nc,tr); break; default: assert(0); break; } Py_DECREF(aa); if (ac) Py_DECREF(ac); else if (c) free(c); PYRN; fail: if (aa) { Py_DECREF(aa); } if (ac) { Py_DECREF(ac); } else if (c) { free(c); } return(NULL); }
static PyObject * genContours (enum pp_contour_funcs ft, PyObject *args) { PyObject *oa=NULL, *oc=NULL, *otr=NULL; PyArrayObject *aa=NULL, *ac=NULL, *atr=NULL; float *a=NULL, *c=NULL, *tr=NULL, blank=0.0; int cd=0 ,rd=0, c1=0,c2=0,r1=0,r2=0, csz=0, trsz=0, nc=0; if (!PyArg_ParseTuple(args,"OiiiiiiOiO|f:contour", &oa, &cd, &rd, &c1, &c2, &r1, &r2, &oc, &nc, &otr, &blank)) return(NULL); if (!(aa = (PyArrayObject *)tofloatmat(oa, &a, &rd, &cd))) goto fail; if (!(ac = (PyArrayObject *)tofloatvector(oc, &c, &csz))) goto fail; if (!(atr = (PyArrayObject *)tofloatvector(otr, &tr, &trsz))) goto fail; if (abs(nc) > csz) { PyErr_SetString(PpgTYPEErr,"contour: size of cont vec < than the " "req. contours number"); goto fail; } if (trsz < 6) { PyErr_SetString(PpgTYPEErr,"contour: invalid transform. vector"); goto fail; } switch (ft) { case FUN_PGCONB: cpgconb(a,cd,rd,c1+1,c2+1,r1+1,r2+1,c,nc,tr,blank); break; case FUN_PGCONS: cpgcons(a,cd,rd,c1+1,c2+1,r1+1,r2+1,c,nc,tr); break; case FUN_PGCONT: cpgcont(a,cd,rd,c1+1,c2+1,r1+1,r2+1,c,nc,tr); break; default: assert(0); break; } Py_DECREF(aa); Py_DECREF(ac); Py_DECREF(atr); PYRN; fail: if (aa) { Py_DECREF(aa); } if (ac) { Py_DECREF(ac); } if (atr) { Py_DECREF(atr); } return(NULL); }
int main() { /* Set up a 2 x 2 lookup table. */ const int M = 2; const int K[] = {K1, K2}; const int map[] = {0, 1}; const double crval[] = {0.0, 0.0}; char text[80]; int i, j, k, l, l1, l2, l3, lstep, m, stat[NP*NP], status; float array[NP][NP], clev[31], v0, v1, w; const float scl = 2.0f/(NP-1); float ltm[6]; double x[NP][NP][2], world[NP][NP][2]; struct tabprm tab; printf("Testing WCSLIB coordinate lookup table routines (ttab2.c)\n" "---------------------------------------------------------\n"); /* List status return messages. */ printf("\nList of tab status return values:\n"); for (status = 1; status <= 5; status++) { printf("%4d: %s.\n", status, tab_errmsg[status]); } printf("\n"); /* PGPLOT initialization. */ strcpy(text, "/xwindow"); cpgbeg(0, text, 1, 1); cpgvstd(); cpgsch(0.7f); /* The viewport is slightly oversized. */ cpgwnad(-0.65f, 1.65f, -0.65f, 1.65f); for (l = 0; l <= 30; l++) { clev[l] = 0.2f*(l-10); } ltm[0] = -scl*(1.0f + (NP-1)/4.0f); ltm[1] = scl; ltm[2] = 0.0f; ltm[3] = -scl*(1.0f + (NP-1)/4.0f); ltm[4] = 0.0f; ltm[5] = scl; /* Set up the lookup table. */ tab.flag = -1; if ((status = tabini(1, M, K, &tab))) { printf("tabini ERROR %d: %s.\n", status, tab_errmsg[status]); return 1; } tab.M = M; for (m = 0; m < tab.M; m++) { tab.K[m] = K[m]; tab.map[m] = map[m]; tab.crval[m] = crval[m]; for (k = 0; k < tab.K[m]; k++) { tab.index[m][k] = (double)k; } } /* Subdivide the interpolation element. */ for (i = 0; i < NP; i++) { for (j = 0; j < NP; j++) { x[i][j][0] = j*(K1-1.0)*scl - 0.5 - crval[0]; x[i][j][1] = i*(K2-1.0)*scl - 0.5 - crval[1]; } } /* The first coordinate element is static. */ tab.coord[0] = 0.0; tab.coord[2] = 0.0; tab.coord[4] = 0.0; tab.coord[6] = 0.0; /* (k1,k2) = (0,0). */ tab.coord[1] = 0.0; /* The second coordinate element varies in three of the corners. */ for (l3 = 0; l3 <= 100; l3 += 20) { /* (k1,k2) = (1,1). */ tab.coord[7] = 0.01 * l3; for (l2 = 0; l2 <= 100; l2 += 20) { /* (k1,k2) = (0,1). */ tab.coord[5] = 0.01 * l2; cpgpage(); for (l1 = 0; l1 <= 100; l1 += 2) { /* (k1,k2) = (1,0). */ tab.coord[3] = 0.01 * l1; /* Compute coordinates within the interpolation element. */ tab.flag = 0; if ((status = tabx2s(&tab, NP*NP, 2, (double *)x, (double *)world, stat))) { printf("tabx2s ERROR %d: %s.\n", status, tab_errmsg[status]); } /* Start a new plot. */ cpgbbuf(); cpgeras(); cpgsci(1); cpgslw(3); cpgbox("BCNST", 0.0f, 0, "BCNSTV", 0.0f, 0); cpgmtxt("T", 0.7f, 0.5f, 0.5f, "-TAB coordinates: " "linear interpolation / extrapolation in 2-D"); /* Draw the boundary of the interpolation element in red. */ cpgsci(2); cpgmove(-0.5f, 0.0f); cpgdraw( 1.5f, 0.0f); cpgmove( 1.0f, -0.5f); cpgdraw( 1.0f, 1.5f); cpgmove( 1.5f, 1.0f); cpgdraw(-0.5f, 1.0f); cpgmove( 0.0f, 1.5f); cpgdraw( 0.0f, -0.5f); /* Label the value of the coordinate element in each corner. */ sprintf(text, "%.1f", tab.coord[1]); cpgtext(-0.09f, -0.05f, text); sprintf(text, "%.2f", tab.coord[3]); cpgtext( 1.02f, -0.05f, text); sprintf(text, "%.1f", tab.coord[5]); cpgtext(-0.13f, 1.02f, text); sprintf(text, "%.1f", tab.coord[7]); cpgtext( 1.02f, 1.02f, text); cpgsci(1); /* Contour labelling: bottom. */ v0 = world[0][0][1]; v1 = world[0][NP-1][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%4.1f", l*0.01f); cpgptxt(w+0.04f, -0.56f, 0.0f, 1.0f, text); } } /* Contour labelling: left. */ v0 = world[0][0][1]; v1 = world[NP-1][0][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%4.1f", l*0.01f); cpgptxt(-0.52f, w-0.02f, 0.0f, 1.0f, text); } } /* Contour labelling: right. */ v0 = world[0][NP-1][1]; v1 = world[NP-1][NP-1][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%.1f", l*0.01f); cpgptxt(1.52f, w-0.02f, 0.0f, 0.0f, text); } } /* Contour labelling: top. */ v0 = world[NP-1][0][1]; v1 = world[NP-1][NP-1][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%4.1f", l*0.01f); cpgptxt(w+0.04f, 1.52f, 0.0f, 1.0f, text); } } /* Draw contours for the second coordinate element. */ for (i = 0; i < NP; i++) { for (j = 0; j < NP; j++) { array[i][j] = world[i][j][1]; } } cpgsci(4); cpgslw(2); cpgcont(array[0], NP, NP, 1, NP, 1, NP, clev, 10, ltm); cpgsci(7); cpgcont(array[0], NP, NP, 1, NP, 1, NP, clev+10, 1, ltm); cpgsci(5); cpgcont(array[0], NP, NP, 1, NP, 1, NP, clev+11, 20, ltm); cpgebuf(); } } } cpgend(); tabfree(&tab); return 0; }