/*$Id: sum_2d_array.c,v 1.2 1999/10/22 17:20:11 ali Exp $ ** ** ** NAME: ** ** sum_2d_array.c ** ** PURPOSE: ** This C function is used to demonstrate how to read a 2 dimensional ** IDL array. It calculates the sum of a subsection of the input array. ** The subsection is specified like this: ** ** 0,0--------------------------------------------x_size-1,0 ** | ** | x_start,y_start------------x_end,y_start ** | | | ** | | this region will be summed | ** | | | ** | x_start,y_end--------------x_end,y_end ** | ** | ** 0,y_size-1 ** ** this is equivalent to the IDL statement: ** IDL>r = TOTAL(arr[x_start:x_end,y_start:y_end],/DOUBLE) ** ** CATEGORY: ** Dynamic Link ** ** CALLING SEQUENCE: ** This function is called in IDL by using the following commands: ** ** IDL>arr = DINDGEN(20,20) ** IDL>x_start = 5L ** IDL>x_end = 10L ** IDL>x_size = 20L ** IDL>y_start = 5L ** IDL>y_end = 10L ** IDL>y_size = 20L ** IDL>r = CALL_EXTERNAL('sum_2d_array.so','sum_2d_array',$ arr,x_start,x_end,x_size,$ y_start,y_end,y_size,/D_VALUE) ** ** See sum_2d_array.pro for a more complete calling sequence. ** ** INPUTS: ** arr - a 2 dimensional IDL array (type is double) ** x_start - X index of the start of the subsection (type is long) ** x_end - X index of the end of the subsection (type is long) ** x_size - size of the X dimension of arr (type is long) ** y_start - Y index of the start of the subsection (type is long) ** y_end - Y index of the end of the subsection (type is long) ** y_size - size of the Y dimension of arr (type is long) ** ** OUTPUTS: ** The function returns the sum of all of the elements of the ** subsection of the array. ** ** SIDE EFFECTS: ** None. ** ** RESTRICTIONS: ** ** None. ** ** MODIFICATION HISTORY: ** Written May, 1998 JJG ** */ #include #include "export.h" /* make sure that this routine is exported on the Macintosh */ #if defined(__POWERPC__) __declspec(export) double sum_2d_array(int argc,void* argv[]); #endif double IDL_STDCALL sum_2d_array(int argc,void* argv[]) { /* since we didn't know the dimensions of the array at compile time, we must treat the input array as if it were a one dimensional vector. */ double* arr; IDL_LONG x_start,x_end,x_size,y_start,y_end,y_size,x,y; double result = 0.0; if (argc != 7) return 0.0; arr = (double*)argv[0]; x_start = *(int*)argv[1]; x_end = *(int*)argv[2]; x_size = *(int*)argv[3]; y_start = *(int*)argv[4]; y_end = *(int*)argv[5]; y_size = *(int*)argv[6]; /* make sure that we don't go outside the array. strictly speaking, this is redundant since identical checks are performed in the IDL wrapper routine. IDL_MIN() and IDL_MAX() are macros from export.h */ x_start = IDL_MAX(x_start,0); y_start = IDL_MAX(y_start,0); x_end = IDL_MIN(x_end,x_size-1); y_end = IDL_MIN(y_end,y_size-1); /* loop through the subsection */ for (y = y_start;y <= y_end;y++) for (x = x_start;x <= x_end;x++) result += arr[x + y*x_size]; /* build the 2d index: arr[x,y] */ return result; }