Both Fortran and C programs create a file called gmeta upon execution. This is a "graphics metafile" containing the contour plot. You can view the gmeta file using ctrans or idt, etc.
|
Fortran version: newtest.f |
|---|
c fortran version of newtest.c
c compile with ncargf77
program newtstf
parameter(nx=50,nz=25)
dimension data(nx,nz)
character*20 plbl
plbl="Contour plot example" ! plot title
do i=1,nx
do k=1,nz
data(i,k)=float(i*k)
enddo
enddo
call opngks ! open graphics device
call pwrit(.5,.850,plbl,20,3,0,0) ! write the plot title
call cpcnrc(data,nx,nx,nz,0.,0.,0.,0,0,0) ! make the plot
call frame ! close out the plot
call clsgks ! close graphics device
stop
end
You compile this program using this command:
ncargf77 newtest.f
|
C version: newtest.c |
|---|
I barely know the C language, so this program may not be optimally written. However, it does reproduce the plot made with the Fortran program listed above. One significant difference between the programs lies in how the 2D arrays were defined. In the Fortran version, the array data was dimensioned (NX,NZ). For me to get it plotted correctly in C, however, I had to define the array as data[NZ][NX].
/* newtest.c */
/* simple contour plot -- NOTE HAVE TO DO ARRAYS NZ BY NX INSTEAD */
/* Compile with ncargcc */
#include <stdio.h>
#include <math.h>
/*
* Function prototypes for NCARG and NCARG-GKS routines
*/
#include <ncarg/ncargC.h>
#include <ncarg/gks.h>
#define NX 50
#define NZ 25
/* for gks */
#define IWTYPE 1
#define WKID 1
float data[NZ][NX]; /* to get displayed correctly needed to swap X and Z */
char *plbl = "Contour plot example"; /* Plot label */
main()
{
int i,k;
/* here is the data */
for (i = 0; i < NX; i++) {
for (k = 0; k < NZ; k++) {
data[k][i] = (double) (i+1)*(k+1);
}
}
/*
* open gks, and turn clipping off
*/
gopen_gks ("stdout",0);
gopen_ws (WKID, NULL, IWTYPE);
gactivate_ws(WKID);
gset_clip_ind(GIND_NO_CLIP);
c_cpcnrc((float *)data,NX,NX,NZ,0.,0.,0.,0,0,0); /* contour routine */
c_pwrit (.5,.850,plbl,20,3,0,0); /* write plot label 20 = num of chars */
/*
* close frame and close gks
*/
c_frame();
gdeactivate_ws(WKID);
gclose_ws(WKID);
gclose_gks();
} /* endof main */
This code is compiled using the command:
ncargcc newtest.c
Page created January, 2000, by
Robert Fovell