You are on page 1of 3

NCAR Graphics (C/Fortran) examples

Here are example C and Fortran 77 programs that make a simple contour plot of data from a twodimensional array called data. The array data has horizontal dimension of NX and vertical dimension of NZ. The data are created using a simple function. The plot is shown below:

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" do i=1,nx do k=1,nz data(i,k)=float(i*k) enddo enddo device title call opngks call pwrit(.5,.850,plbl,20,3,0,0) call cpcnrc(data,nx,nx,nz,0.,0.,0.,0,0,0) call frame call clsgks stop end ! open graphics ! write the plot ! make the plot ! close out the plot ! close graphics ! plot title

device

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

You might also like