home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $Id: double_array.c,v 1.1 1993/11/16 23:36:16 idl Exp $
- **
- ** NAME:
- ** double_array
- **
- ** PURPOSE:
- ** This C function is used to demonstrate how to pass a IDL array
- ** of type double to a C function using the IDL function CALL_EXTERNAL.
- **
- ** CATEGORY:
- ** Dynamic Link
- **
- ** CALLING SEQUENCE:
- ** This function is called in IDL by using the following command
- **
- ** IDL> result = CALL_EXTERNAL('double_array.so', '_double_array', $
- ** IDL> array_var, array_size)
- **
- ** INPUTS:
- ** array_var: A IDL array of type double
- **
- ** array_size: A IDL scalar long varaible that contains the number
- ** of elements in array_var. This is the number that
- ** is returned from the IDL function N_ELEMENTS.
- **
- ** OUTPUTS:
- ** This function will return the maximum value of the passed in
- ** array. This value is an IDL double scalar value.
- **
- ** SIDE EFFECTS:
- ** The values contained in the array and the number of elements
- ** are written to stdout.
- **
- ** RESTRICTIONS:
- ** This example assumes that the length value is long (4 bytes) and not
- ** a short integer. An IDL integer is only 2 bytes long, so the variables
- ** should be delcared in IDL at type long.
- **
- ** EXAMPLE:
- **----------------------------------------------------------------------------
- ;; The following are the commands that would be used to call this
- ;; routine in IDL.
- ;;
- array_var = dindgen(10)*!dpi ;create a double array with values in it.
- array_size = long(n_elements(array_var))
-
- max = CALL_EXTERNAL('double_array.so', '_double_array', $
- array_var, array_size)
-
- **----------------------------------------------------------------------------
- **
- ** MODIFICATION HISTORY:
- ** Written October, 1993 KDB
- **
- ** Declare header file.
- */
-
- #include <stdio.h>
-
- /*
- ** Declare a macro for determining the maximum of two values.
- ** This is taken from the IDL header file $IDL_DIR/source/export.h
- */
-
- #ifndef MAX
- #define MAX(x,y) (((x) > (y)) ? (x) : (y))
- #endif
-
- /*
- ** Declare the function
- */
-
- double
- double_array(argc, argv)
- int argc;
- void *argv[];
- {
- /*
- ** Declare local variables
- */
- double *double_array; /* pointer to the double array */
- short n_elements; /* number of elements in array */
- double max_value; /* used to hold max value */
- short i; /* Counter */
-
- /*
- ** Insure that the correct number of arguments were passed in (argc = 2).
- */
- if(argc != 2)
- {
- /*
- ** Print an error message and return.
- */
- fprintf(stderr, "double_array: Incorrect number of arguments\r\n");
- return(-1.0);
- }
- /*
- ** Cast the pointers in argv to local variables.
- */
- double_array = (double *) argv[0];
- n_elements = (short)(*(long *)argv[1]); /* deref pointer, cast 2 short */
-
- /*
- ** Check the size of the array passed in. n_elements should be > 0.
- */
- if( n_elements < 1)
- {
- /*
- ** Print an error message and return
- */
- fprintf(stderr, "double_array: Array elements is less that one\r\n");
- return(-1.0);
- }
- /*
- ** Set max to the first value in the array
- */
- max_value = *double_array;
-
- /*
- ** Print out the passed in information
- */
- fprintf(stdout,
- "\r\n-----------------------------------------------------\r\n");
- fprintf(stdout,"Inside C function double_array ");
- fprintf(stdout,"(Called from IDL using CALL_EXTERNAL)\r\n\r\n");
-
- /*
- ** Now print the values of each variable that was passed in from IDL
- */
- fprintf(stdout,"Number of elements in the Double array: %d\r\n",n_elements);
-
- fprintf(stdout, "Double Array Contents:\r\n");
- for(i=0; i < n_elements; i++)
- fprintf(stdout,"\tElement %3d: %f\r\n",i, double_array[i]);
-
- fprintf(stdout,"\n-----------------------------------------------------\n");
-
- /*
- ** Now lets find the max value in the array. Just use a for loop.
- */
- for(i=1; i < n_elements; i++)
- max_value = MAX(max_value, double_array[i] );
-
- /*
- ** Now return the max value of the array to IDL (Call_External).
- */
- return(max_value);
- }
-
-