next up previous contents index
Next: Examples Using the Communication Up: Examples Using the Graphics Previous: A Fortran Application

A C Application Using Graphics Interfaces

The following program plotexampl is written in C and plots a line of a frame in the current active viewport, like the standard application PLOT/ROW. This example demonstrated the usage of the high level plot interfaces: PCKRDx, PCKWRx, PCOPEN, PCAXES, PCDATA, PCGCUR and PCTEXT. This program is tied into MIDAS through the following procedure,
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!.IDENTIFICATION: plotexampl.prg
!.PURPOSE:        MIDAS procedure to plot a line of an image
!                 execute as @@ plotexampl par1 [par2] [par3] [par4] [par5] 
!                 where:
!                 par1 = input frame
!                 par2 = line number (defaulted to 1)
!                 par3 = 1st point,last point on the line (defaulted to 1,npix)
!                        this parameters is read in automatic scaling mode only
!                 par4 = sc_x,sc_y,off_x,off_y (defaults device filling)
!                 par5 = options for AG_AXES
!.AUTHOR:         R.M. van Hees
!.VERSION:        RvH 931025
! -----------------------------------------------------------------------
DEFINE/PARAM P1 ? IMA "Enter image:"                ! frame 
WRITE/KEYWORD  IN_A   {P1}                            
DEFINE/PARAM P2 @1                                  ! line number
!
IF {PLRSTAT(1)} .NE. 0.0 .AND. {PLRSTAT(2)} .NE. 0.0 THEN
   DEFINE/PARAM P3 MANUAL C
ELSE
   DEFINE/PARAM P3 <,> C
   IF P3(1:1) .EQ. "M" THEN
      WRITE/OUT "*** FATAL: X axis not manually set; use SET/GRAPHICS"
      RETURN
   ENDIF
ENDIF
!
DEFINE/PARAM P4 0.,0.,-999,-999 NUM
@ plscoff {P4}                                    ! get the scales and offsets
WRITE/KEYWORD  INPUTC {P5}                            ! options for AGL
!
DATTIM = M$TIME()                     ! time and date of the plot
RUN plotexampl                                    ! draw the plot
!
WRITE/KEYWORD PLCDATA/C/1/60  {P1}                    ! name of data structure
WRITE/KEYWORD PLCDATA/C/61/20 "FRAME       "          ! type of data structure
!
@ purgeplt {P1}
IF MID$PLOT(26:30) .EQ. "SPOOL" THEN              ! make plot if spool is on
   @ sendplot
ENDIF

To run the application execute the procedure via:
Midas 001> @@ plotexampl frame line_nr start,end sc_x,sc_y,off_x,off_y AGL_options

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.COPYRIGHT   (c) 1993 European Southern Observatory
.IDENTifer   PLOTEXMPL
.AUTHOR      R.M. van Hees IPG-ESO Garching
.KEYWORDS    Graphics, bulk data frame, one-dimensional plotting
.LANGUAGE    C
.PURPOSE     Plot a line of a FRAME
  in/output: IN_A/C/1/60  = input frame
             P2/C/72      = line number (default 1)
             P3/C/72      = first and last pixel of the line 
                            (default: the whole line).
             INPUTC/1/80  = options for PCAXES
.RETURNS     
.COMMENTS    Test program for high level plot interfaces
             
.ENVIRONment MIDAS
             #include <midas_def.h>     Prototypes for MIDAS interfaces
             #include <plot_def.h>      General symbols for Plot routines

.VERSION     1.0     09-Sep-1993   Creation R.M. van Hees
------------------------------------------------------------*/
/*
 * Define _POSIX_SOURCE to indicate
 * that this is a POSIX program
 */
#define _POSIX_SOURCE 1

/*
 * definition of the used functions
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

/*
 * define some macros and constants
 */
#include <midas_def.h>
#include <plot_def.h>

#define FOR_Y           4
#define PLDIM2          2                      /* only two dimensional plots */
#define KEY_EXIT        32            /* AGL key code for middle mouse buton */

/*
 * here starts the code of the function
 */
int main()
{
int     actvals, binmod, ii, imf, key, ltype, naxis, nrprow, stat, 
        stype, npix[PLDIM2];

float   xpos, ypos, *xdata, *ydata, area[4], image[4], wcfram[8];

double  start[PLDIM2], step[PLDIM2];

char    *bin, *cmnd, *name, *cunit, *ident, *input, *buff, *text, *option, 
        *label[PLDIM2], *pntr, *cpntr;

/*
 * initialised variables
 */
int   access =  0,                    /* parameter for PCOPEN: NEW plot mode */
      plmode = -1;                     /* plot mode taken from keyword PMODE */
float y_off  = 0.0;              /* no offset in the Y direction of the plot */

char  *err_1dim  = "*** WARNING: Image column contains only one point!",
      *err_flat  = "*** WARNING: zero dynamic range in data at %13.8g";
/*
 * allocate memory for different character pointers and initialise a few
 */
bin    = osmmget(4);
cmnd   = osmmget(21);
ident  = osmmget(33);
name   = osmmget(61);
input  = osmmget(73);
option = osmmget(81);
buff   = osmmget(81);
text   = osmmget(81);
cunit  = osmmget(PLDIM2 * 16 + 1);
for ( ii = 0; ii < PLDIM2; ii++ ) label[ii] = osmmget(81);

strcpy( label[0], "Position (" );
strcpy( label[1], "Pixel value (" );
strcpy( ident, "                                " );    /* SCIGET needs this */
*cunit = '\0';
for ( ii = 0; ii < PLDIM2; ii++ ) strncat( cunit, "                ", 16 );

/*
 * start of executable code
 */
stat = SCSPRO( "PLEXMPL" );                  /*contact with the MIDAS monitor*/

/*
 * find file name and read header information
 */
stat = SCKGETC( "IN_A", 1, 60, &actvals, name );
stat = SCIGET( name, D_R4_FORMAT, F_I_MODE,F_IMA_TYPE, PLDIM2, 
               &naxis, npix, start, step, ident, cunit, &cpntr, &imf );

if ( npix[0] == 1 ) stat = SCTPUT( err_1dim );

/*
 * line number (Y-coordinate)
 */
image[2] = image[3] = 1;                          /* default line number = 1 */
if ( naxis > 1 )
   { stat = SCKGETC( "P2", 1, 72, &actvals, input );
     image[2] = image[3] = GETSIN( input, npix[1], start[1], step[1] );
     BOXPTW( image+2, npix[1], start[1], step[1], area+2 );
   }
/*
 * find first and last pixel along the row (X-coordinate)
 */
stat = SCKGETC( "P3", 1, 72, &actvals, input );
strcpy( buff, input );
image[0] = GETSIN( strtok( buff,"," ), npix[0], start[0], step[0] ); 
pntr = strchr( input, ',' );
image[1] = GETSIN( pntr+1, npix[0], start[0], step[0] );
BOXPTW( image, npix[0], start[0], step[0], area );
/*
 * Store the size of the extracted data in PLRGRAP
 */
PCKWRR( "PIXEL", 4, image );

/*
 * allocate virtual memory for a single line of the frame
 */
nrprow =(int) fabs(image[1] - image[0]) + 1;    /*number of points in the row*/
xdata = (float *) osmmget( nrprow * sizeof(float));
ydata = (float *) osmmget( nrprow * sizeof(float));

/*
 * copy the data of the row
 */
GETBDF( cpntr, image, npix, start, step, xdata, ydata );

/*
 * Get the manual setting for the axes
 */
PCKRDR( "XAXIS", 4, &actvals, wcfram );
PCKRDR( "YAXIS", 4, &actvals, wcfram+FOR_Y );

/*
 * If the X axis is not defined by the user we calculate frame along X axis
 */
if ( fabs( *wcfram ) < PLT_EPS && fabs( *(wcfram+1) ) < PLT_EPS )
   { wcfram[0] = area[0];
     wcfram[1] = area[1]; 
   }
/*
 * If the Y axis is not defined by the user we calculate frame along Y axis
 */
if ( fabs( *(wcfram+FOR_Y)) < PLT_EPS && fabs( *(wcfram+FOR_Y+1)) < PLT_EPS )
   { MINMAX( ydata, nrprow, wcfram + FOR_Y, wcfram + FOR_Y + 1 );
     if ( wcfram[FOR_Y] == wcfram[FOR_Y+1] )
        { sprintf( buff, err_flat, wcfram[FOR_Y] );
          SCTPUT( buff );
        }
   }
/*
 * the tick marks will be set by PCAXES
 */
wcfram[2] = wcfram[3] = wcfram[FOR_Y+2] = wcfram[FOR_Y+3] = 0.0;

/*
 * still we have to save the coordinate frame in PLRGRAP
 */
PCKWRR( "XWNDL", 4, wcfram );
PCKWRR( "YWNDL", 4, wcfram+FOR_Y );

/*
 * setup graphics device according to MIDAS settings
 * default graphics display & meta-file name
 */
PCOPEN( " ", " ", access, &plmode );

/*
 * plot the axes
 */
if ( strlen( cunit ) > 0 )
   { strncat( label[0], cunit+17, 16 );
     strncat( label[1], cunit, 16 );
   }
for ( ii = 0; ii < PLDIM2; ii++ ) 
    { strcat( label[ii], ")" );
      LABSTR( label[ii] );
    }
/*
 * draw the axes and define the clipping area
 */
stat = SCKGETC( "INPUTC", 1, 80, &actvals, option );
PCAXES( wcfram, wcfram+FOR_Y, label[0], label[1], option );

/*
 * get the symbol type, line type and binmode
 */
PCKRDI( "STYPE", 1, &actvals, &stype );
PCKRDI( "LTYPE", 1, &actvals, &ltype );
PCKRDC( "BINMOD", 4, &actvals, bin );
binmod = (strncmp( bin, "ON", 2 ) == 0) ? 1 : 0;

/* 
 * plot the data
 */
PCDATA( stype, ltype, binmod, xdata, ydata, y_off, nrprow );

/*
 * draw some additional information
 */
SCTPUT( "Go with the cursor to plot info about the frame (left mouse button)");
stat = PCGCUR( &xpos, &ypos, &key );
if ( key != KEY_EXIT )
   { sprintf( text, "Frame: %s", name );
     PCTEXT( text, xpos, ypos, 0.0, 0.75, 0 );
   }
SCTPUT( "Go with the cursor to plot info about the drawn row" );
stat = PCGCUR( &xpos, &ypos, &key );
if ( key != KEY_EXIT )
   { sprintf( text, "Row: %.0f", image[2] );
     PCTEXT( text, xpos, ypos, 0.0, 0.75, 0 );
   }
/*
 * close plot file and terminate graphic operations
 */
PCCLOS();

return SCSEPI();
}


Send comments to web@eso.org
Last update: 1998-10-23