#include <stdio.h>
#include <stdlib.h>

#include "user.h"

/*---------------------------------- MILIB stuff ------------------begin--*/
#include <mi.h>

#ifdef sgi
#include <strings.h>
#else
#include <string.h>
#endif

#include "query_defs.h"

/*
 * QUERYX macros are generated into the query_defs.h include file from
 * the various queryx.sql files
 */
char *Queries[] = {
    QUERY0,
    QUERY1,
    QUERY2,
    QUERY3,
    QUERY4,
    QUERY5
};

int              Ind = -1;
int              Debug  = MI_TRUE;
int              Debug2 = MI_FALSE;
int              Hydra  = MI_FALSE;
/*---------------------------------- MILIB stuff ------------------end----*/
/*---------------------------------- CGI stuff --------------------begin--*/
#define xxx "http://www.sdsc.edu/SDSC/Staff/marciano/LO/jj.cgi"

#define LF 10
#define CR 13
#define MAX_ENTRIES 10000

typedef struct {
    char *name;
    char *val;
} entry;

int          x, 
             m = 0;
int          cl;
char         w[256];
entry        entries[MAX_ENTRIES];
/*---------------------------------- CGI stuff --------------------end----*/
/*---------------------------------- PROTOTYPES--------------------begin--*/
char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);

void COVER_SCREEN();
void NALC_MAP_SCREEN();

int connection();
int send_command( MI_CONNECTION *, char * );
int get_results( MI_CONNECTION * );
int get_data( MI_CONNECTION *);
int get_composite_values( MI_ROW * );
void save_data( char *, char * );
/*---------------------------------- PROTOTYPES--------------------end----*/

/* *************************************************************** */
main(int argc, char *argv[]) 
{
    printf("Content-type: text/html%c%c",LF,LF);

    if( argc == 1 ) {
        COVER_SCREEN();
    }
    else if( !strcmp( argv[1], "nalcmap" ) ) {
        NALC_MAP_SCREEN();
    }
    else {
        printf( "NOT GOOD! argv[1] = %s<BR>%c", argv[1], LF );
    }
}
/* *************************************************************** */


/*
########################################################################
### Subprogram: COVER_SCREEN
###
###
###
#######################################################################
*/
void COVER_SCREEN() {

    printf( "<HTML>%c", LF );
    printf( "<HEAD>%c", LF );
    printf( "<TITLE>Web to Illustra to Unitree Prototype</TITLE>%c", LF );

    printf( "<BODY>%c", LF );
    printf( "<CENTER>%c", LF );
    printf( "<H1>%c", LF );
    printf( "<I>Web-Illustra-Unitree</I>- Testing Facility%c", LF );
    printf( "</H1>%c", LF );

    printf( "<HR>%c", LF );
    printf( "Richard <B>Marciano</B><BR>%c", LF );
    printf( "Michael <B>Wan</B><BR>%c", LF );
    printf( "</CENTER>%c", LF );

    printf( "<HR>%c", LF );
    printf( "<PRE>%c", LF );
    printf( "<B>create table sdbay</B>                       SAMPLE RECORD VALUES%c", LF );
    printf( "<B>(</B>                                  ---------------------------------------%c", LF );
    printf( "  <B>label</B>    varchar(20),            'Bathymetry' %c", LF );
    printf( "  <B>prog</B>     varchar(20),            'NRAD' %c", LF );
    printf( "  <B>times</B>    varchar(20),            '1994/12/01' %c", LF );
    printf( "  <B>collMeth</B> varchar(20),            'unknown' %c", LF );
    printf( "  <B>collInst</B> varchar(20),            'unknown' %c", LF );
    printf( "  <B>coords</B>   varchar(20),            'decimal_degrees' %c", LF );
    printf( "  <B>f_type</B>   varchar(4),             'gif' %c", LF );
    printf( "  <B>f_name</B>   varchar(30),            'bathymetry.raw.dx.00.gif' %c",LF);
    printf( "  <B>f_text</B>   varchar(40),            'Entrance to San Diego Bay' %c",LF);
    printf( "  <B>bboxnum</B>  integer,                1 %c", LF );
    printf( "  <B>bcoords</B>  arrayof( varchar(10) )  '[latlon]', %c", LF );
    printf( "  <B>bbox</B>     arrayof( real )         '[32.7397, -117.275, 32.6292, -117.11]'%c", LF );
    printf( "<B>);</B>                                 ---------------------------------------%c", LF );
    printf( "</PRE>%c", LF );

/*
    printf( "<FORM method=POST ACTION=
            \"http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query\">%c", xxx, LF );
*/

    printf( "<FORM method=POST ACTION=%s?nalcmap>%c", xxx, LF );
    printf( "SELECT an Illustra query <SELECT NAME=\"querylist\"SIZE=1>%c",LF);
    printf( "<OPTION VALUE=\"create\" SELECTED>create table sdbay%c", LF );
    printf( "<OPTION VALUE=insert1>insert into table sdbay values 1%c", LF );
    printf( "<OPTION VALUE=insert2>insert into table sdbay values 2%c", LF );
    printf( "<OPTION VALUE=select>select label, bboxnum, bbox from sdbay%c", LF );
    printf( "<OPTION VALUE=delete>delete from sdbay%c", LF );
    printf( "<OPTION VALUE=drop>drop table sdbay%c", LF );
    printf( "</SELECT> <P> %c", LF );
    printf( "Press this button to send query to Ilustra: <INPUT TYPE=\"submit\" VALUE=\"Invoke Database\"> <P> %c", LF );
    printf( "Press this button to reset form:  <INPUT TYPE=\"reset\" VALUE=\"Reset\">.%c", LF );
    printf( "</FORM>%c", LF );

    printf( "<HR>%c", LF );
    printf( "<CENTER>%c", LF );
    printf( "<H5>%c", LF );
    printf( "This page last modified on:  January 22, 1996%c", LF );
    printf( "</H5>%c", LF );
    printf( "</CENTER>%c", LF );

    printf( "</BODY>%c", LF );
    printf( "</HTML>%c", LF );


}


/*
########################################################################
### Subprogram: NALC_MAP_SCREEN 
###
###
###
#######################################################################
*/
void NALC_MAP_SCREEN()
{
    int              i, j;
    char            *cmd;

    /*---- Get parameters from POST ----*/
    cl = atoi( getenv("CONTENT_LENGTH") );
    for( x=0; cl && (!feof(stdin)); x++ ) 
    {
        m=x;
        entries[x].val = fmakeword( stdin, '&', &cl );
        plustospace( entries[x].val );
        unescape_url( entries[x].val );
        entries[x].name = makeword( entries[x].val, '=' );
    }

    printf( "<HTML>%c", LF );
    printf( "<HEAD>%c", LF );
    printf( "<TITLE>Web to Illustra to Unitree Prototype</TITLE>%c", LF );

    printf( "<BODY>%c", LF );
    printf( "<H1>%c", LF );

    if( !strcmp( entries[0].name, "querylist" ) )
    {
        if( !strcmp( entries[0].val, "create" ) ) 
        {
            printf( "<I>You selected:</I> create table sdbay%c", LF );
            printf( "</H1>%c", LF );
            printf( "<HR>%c", LF );

            printf( "About to try send the following command to the <B>Illustra</B> database%c", LF );
            printf( "<PRE>%c", LF );
            printf( "   <B>create table sdbay</B>%c", LF );
            printf( "   <B>(</B>%c", LF );
            printf( "           <B>label</B>            varchar( 20 ),%c", LF );
            printf( "           <B>prog</B>             varchar( 20 ),%c", LF );
            printf( "           <B>times</B>            varchar( 20 ),%c", LF );
            printf( "           <B>collMeth</B>	    varchar( 20 ),%c", LF );
            printf( "           <B>collInst</B>	    varchar( 20 ),%c", LF );
            printf( "           <B>coords</B>           varchar( 20 ),%c", LF );
            printf( "           <B>f_type</B>           varchar( 20 ),%c", LF );
            printf( "           <B>f_name</B>           varchar( 20 ),%c",LF);
            printf( "           <B>f_text</B>           varchar( 20 ),%c",LF);
            printf( "           <B>bboxnum</B>          integer,%c", LF );
            printf( "           <B>bcoords</B>          arrayof( varchar ),%c", LF );
            printf( "           <B>bbox</B>             arrayof( real )%c", LF );
            printf( "   <B>);</B>%c", LF );
            printf( "</PRE>%c", LF );


            printf( "<HR>%c", LF );
            printf( "<HR SIZE=4 WIDTH=75\%> %c", LF );
            printf( "<CENTER>%c", LF );
            printf( "<B>-- STATUS INFO --</B><BR>%c", LF );
            printf( "<B>__________________________________________</B><BR>%c", LF );
            printf( "Invoking database...<BR>%c", LF );
            /* create table */
            connect1( Queries[ 0 ] );
            printf( "</CENTER>%c", LF );
        }
        else if( !strcmp( entries[0].val, "insert1" ) ) 
        {
            printf( "<I>You selected:</I> insert into table sdbay 1%c", LF );
            printf( "</H1>%c", LF );
            printf( "<HR>%c", LF );
            printf( "Invoking database...<BR>%c", LF );
            /* insert  row */
            connect1( Queries[ 1 ] );
        }
        else if( !strcmp( entries[0].val, "insert2" ) ) 
        {
            printf( "<I>You selected:</I> insert into table sdbay 2%c", LF );
            printf( "</H1>%c", LF );
            printf( "<HR>%c", LF );
            printf( "Invoking database...<BR>%c", LF );
            /* insert  row */
            connect1( Queries[ 2 ] );
        }
        else if( !strcmp( entries[0].val, "select" ) ) 
        {
            printf( "<I>You selected:</I> select label, bboxnum, bbox from sdbay%c", LF );
            printf( "</H1>%c", LF );
            printf( "<HR>%c", LF );
            printf( "Invoking database...<BR>%c", LF );
            /* select the rows */
            connect1( Queries[ 3 ] );
            printf( "</CENTER>%c", LF );
            printf( "<PRE>%c", LF );
            for( j = 0; j <= Ind; j++ )
            {
                printf( "%c", LF );
                printf( "sdbay[%1d].label = \t<B>%s</B>%c", j, sdbay[ j ].label, LF );
                printf( "sdbay[%1d].bboxnum = \t<B>%d</B>%c", j, sdbay[ j ].bboxnum, LF );
                for( i = 0; i < sdbay[j].bboxnum * 4; i++ )
                {
                    printf( "sdbay[%1d].bbox[%1d] = \t<B>%f</B>%c", 
	  	     	     j, i, sdbay[ j ].bbox[ i ], LF );
                }
            }
            printf( "</PRE>%c", LF );
        }
        else if( !strcmp( entries[0].val, "delete" ) ) 
        {
            printf( "<I>You selected:</I> delete from sdbay%c", LF );
            printf( "</H1>%c", LF );
            printf( "<HR>%c", LF );
            printf( "Invoking database...<BR>%c", LF );
            /* delete all the rows */
            cmd = "delete from sdbay;";
            connect1( Queries[ 4 ] );
        }
        else if( !strcmp( entries[0].val, "drop" ) ) 
        {
            printf( "<I>You selected:</I> drop table sdbay%c", LF );
            printf( "</H1>%c", LF );
            printf( "<HR>%c", LF );
            printf( "Invoking database...<BR>%c", LF );
            /* drop table */
            cmd = "drop table sdbay;"; 
            connect1( Queries[ 5 ] );
        }
    }

    /* FREE memory allocated on previous SELECT statement */
/*
    for( i = 0; i < count; i++ )
    {
        free( sdbay[ i ].bbox );
    }
    free( sdbay );
*/

    printf( "<HR>%c", LF );
    printf( "<CENTER>%c", LF );
    printf( "<H5>%c", LF );

    printf( " ...click <A HREF=%s> HERE </A>to return to main screen...%c",
		xxx, LF );

    printf( "</H5>%c", LF );
    printf( "</CENTER>%c", LF );

    printf( "</BODY>%c", LF );
    printf( "</HTML>%c", LF );
}


/*************************************** ILLUSTRA ***************************/
/*************************************** ILLUSTRA ***************************/
/*************************************** ILLUSTRA ***************************/


/*******************************************************************************
 *
 * make connection
 *
 *******************************************************************************/
int connect1( cmd )
char    *cmd;
{
    MI_CONNECTION_INFO  cinfo;
    MI_DATABASE_INFO    dinfo;
    MI_PARAMETER_INFO   pinfo;
    MI_CONNECTION      *conn;

/* ---------------------------------------------------------------- */
/* SET  illustra stuff here */

/* MIGHT WANT TO READ THESE IN FROM A FILE HERE */
    if( Hydra )
    {
        putenv( "MI_HOME=/usr/tmp/" );
        putenv( "MI_SYSPARAMS=/usr/tmp/MiParams" );

        putenv( "MI_LOGDIR=/var/tmp/marciano/Illustra/rosebud" );
        putenv( "MI_SYSPARAMS=/var/tmp/marciano/Illustra/rosebud/MiParams" );
        putenv( "MI_SERVER=default" );
        putenv( "MI_DATABASE=vlado" );
        putenv( "MI_LICENSE=hydra.sdsc.edu" );
        putenv( "MI_PASSWORD=richardm1" );

        dinfo.database_name = "vlado";
        dinfo.password = "richardm1";
    }

    putenv( "MI_HOME=/var/tmp/marciano/Illustra/pulsar" );
    putenv( "MI_SYSPARAMS=/var/tmp/marciano/Illustra/pulsar/MiParams" );

    putenv( "MI_SERVER=pulsar" );
    putenv( "MI_LICENSE=zippy" );
    putenv( "MI_DATABASE=vlado" );
    putenv( "MI_USER=marciano" );
    putenv( "MI_PASSWORD=richardm1" );

    /* assign values to the connection info */
    cinfo.server_name = NULL;
    cinfo.server_host = NULL;
    cinfo.server_port = 7531;

    /* assign values to the connection info */
    dinfo.user_name = "marciano";
    dinfo.database_name = "vlado";
    dinfo.password = "richardm1";

    /* set the application defaults */
    if( mi_set_default_connection_info( &cinfo ) == MI_ERROR )
        printf( "FAILED: set_def_conn_info%c<BR>", LF );
    if( mi_set_default_database_info( &dinfo ) == MI_ERROR )
        printf( "FAILED: set_def_db_info%c<BR>", LF );

    /* get the application defaults */
    mi_get_default_connection_info( &cinfo );

    /* make sure we have the right server */
    if( strcmp( "pulsar", cinfo.server_name ) != 0 )
        printf( "FAILED: got server_name %s, should be pulsar<BR>%c", 
                cinfo.server_name, LF ); 
    mi_get_default_database_info( &dinfo );

    conn = mi_server_connect( &cinfo );
    if( conn == NULL )
           printf( "FAIL: CONNECT to default<BR>%c", LF ); 
    else
    {
        if( Debug )
            printf( "OK: connected to %s<BR>%c", cinfo.server_name, LF );
        /* log in to `template1' as `marciano' */
        if( mi_login( conn, &dinfo ) != MI_ERROR )
        {
            if( Debug )
                printf( "OK: connected to db %s%<BR>%c", dinfo.database_name, LF );
        }
        else
            printf( "FAILED: connect to db %s<BR>%c", dinfo.database_name, LF );
    }

/************************************************************************/
    send_command( conn, cmd );
/************************************************************************/

    mi_close( conn );
/*
    exit( 0 );
*/
/* ---------------------------------------------------------------- */
}


/*******************************************************************************
 *
 * send a command
 *
 *******************************************************************************/
int send_command( conn, cmd )
MI_CONNECTION    *conn;
char             *cmd;
{

    int    count;
  
    /* send the command */
    if( mi_exec( conn, cmd, 0 ) == MI_ERROR )
    {
        printf( "cannot send: %s<BR>%c", cmd, LF );
        return( -1 );
    }

    count = get_results( conn );

    /* make sure it finished */
    if( mi_query_finish( conn ) == MI_ERROR )
    {
        printf( "command %s could not complete<BR>%c", cmd, LF );
        return( -1 );
    }

    return( 0 );
} 


/*******************************************************************************
 *
 * get results   
 *
 *******************************************************************************/
int get_results( conn )
MI_CONNECTION *conn;
{
  int     count;
  int     result;
  char    *cmd;

  while( ( result = mi_get_result( conn ) ) != MI_NO_MORE_RESULTS )
  {
    switch( result )
    {
        case MI_ERROR:
            printf( "could not get results<BR>%c", LF );
            return( -1 );
        case MI_DDL:
            break;
        case MI_DML:
            /* what kind of command was it? */
            cmd = mi_result_command_name( conn );

              /* get # rows affected by last command */
            if( ( count = mi_result_row_count( conn ) ) == MI_ERROR )
            {
                printf( "cannot get row count<BR>%c", LF );
                return( -1 );
            }
            else
            {
                if( Debug )
                    printf( "%d rows affected by %s command<BR>%c", 
                                   count, cmd, LF );
            }
            break;
        case MI_ROWS:
            result = get_data( conn );
            break;
        default:
            printf( "unknown results<BR>%c", LF );
    }
  }
  return( count );
}            


/*******************************************************************************
 *
 * get data   
 *
 *******************************************************************************/
int get_data( conn )
MI_CONNECTION *conn;
{
    MI_ROW         *row;
    MI_ROW_DESC    *rowdesc;
    int            error;
    int            numcols;
    int            i;
    char           *colname;
    int            collen;
    char           *colval;
    mi_boolean     iscomposite;

    /* get the description of the row */
    rowdesc = mi_get_row_desc_without_row( conn );

    /* get the number of columns in the row */
    numcols = mi_column_count( rowdesc );

    /* print the column names */
    if( Debug2 )
    {
        for( i = 0; i < numcols; i++ )
        {
            colname = mi_column_name( rowdesc, i );
            printf( "%s\t%c", colname, LF );
        }
        printf( "<BR><BR>%c", LF );
    }
        
    /* for each row */
    while( ( row = mi_next_row( conn, &error ) ) != NULL )
    {    
        Ind++;

        /* for each column */
        for( i = 0; i < numcols; i++ )
        {
            iscomposite = MI_FALSE;
            switch( mi_value( row, i, &colval, &collen ) )
            {
              case MI_ERROR:
                printf( "<BR>cannot get value<BR>%c", LF );
                return( -1 );
              case MI_NULL_VALUE:
                colval = "NULL";
                break;
              case MI_NORMAL_VALUE:
                break;
              case MI_ROW_VALUE:
                if( ( mi_column_is_composite( rowdesc, i ) ) == 1 )
                {
                    iscomposite = MI_TRUE;
                    get_composite_values( (MI_ROW *) colval );
                }
                break;
              default:
                printf( "<BR>unknown value<BR>%c", LF );
                return( -1 );
            }
            if( !iscomposite )
            {
                colname = mi_column_name( rowdesc, i );
                if( Debug2 )
                    printf( "%s\t%c", colval, LF );

/* ------------------------------------------------- */
                save_data( colname, colval );
/* ------------------------------------------------- */
            }
        }
        if( Debug2 )
            printf( "<BR>%c", LF );
    }
    if( Debug2 )
        printf( "<BR>%c", LF );
    return( 1 );
}


/*******************************************************************************
 *
 * get composite values
 *
 *******************************************************************************/
int get_composite_values( row )
MI_ROW *row;
{
    int numcols;
    MI_ROW_DESC    *rowdesc;
    int            i;
    char           *colname;
    int            collen;
    char           *colval;

printf( " IN GET_COMPOSITE_VALUES \n" );
    rowdesc = mi_get_row_desc( row );

    /* get the number of columns in the row */
    numcols = mi_column_count( rowdesc );

    /* print the column names */
    for( i = 0; i < numcols; i++ )
    {
        colname = mi_column_name( rowdesc, i );
        fprintf( stderr, "%s\t", colname );
    }
    fprintf( stderr, "\n" );

    /* for each column */
    for( i = 0; i < numcols; i++ )
    {
        switch( mi_value( row, i, &colval, &collen ) )
        {
          case MI_ERROR:
            fprintf( stderr, "\ncannot get value\n" );
            return( -1 );
          case MI_NULL_VALUE:
            colval = "NULL";
            break;
          case MI_NORMAL_VALUE:
            break;
          case MI_ROW_VALUE:
            get_composite_values( colval );
            break;
          default:
            fprintf( stderr, "\nunknown value\n" );
            return( -1 );
        }
        fprintf( stderr, "%s\t", colval );
    }
}



/*******************************************************************************
 *
 * user defined function
 *
 *******************************************************************************/
void save_data( colname, colval )
char *colname;
char *colval;
{
    int            i;
    char           sdbay_bbox_fmt[ 256 ];
    static int     NotFirstTime;

    if( NotFirstTime == MI_FALSE )
    {
        sdbay = (sdbay_type *) calloc( 10,  sizeof(sdbay_type ) );
        NotFirstTime = MI_TRUE; 
    }

    /* SELECT statement DECODING BLOCK */
    if( strcmp( colname, "label") == 0 )
    {
        sdbay[Ind].label = colval;
    }
    else if( strcmp( colname, "prog") == 0 )
    {
        sdbay[Ind].prog = colval;
    }
    else if( strcmp( colname, "times") == 0 )
    {
        sdbay[Ind].times = colval;
    }
    else if( strcmp( colname, "collMeth") == 0 )
    {
        sdbay[Ind].collMeth = colval;
    }
    else if( strcmp( colname, "collInst") == 0 )
    {
        sdbay[Ind].collInst = colval;
    }
    else if( strcmp( colname, "coords") == 0 )
    {
        sdbay[Ind].coords = colval;
    }
    else if( strcmp( colname, "f_type") == 0 )
    {
        sdbay[Ind].f_type = colval;
    }
    else if( strcmp( colname, "f_name") == 0 )
    {
        sdbay[Ind].f_name = colval;
    }
    else if( strcmp( colname, "f_text") == 0 )
    {
        sdbay[Ind].f_text = colval;
    }
    else if( strcmp( colname, "bboxnum") == 0 )
    {
        sdbay[Ind].bboxnum = (int) atoi( colval );
    }
    else if( strcmp( colname, "bcoords") == 0 )
    {
/* NEEDS FIXING !!! */
        /* example: [latlon, latlon, latlon, latlon, latlon, latlon] */
        sdbay[Ind].bcoords = colval;
        sdbay[Ind].bcoords = (char *) malloc(sdbay[Ind].bboxnum);
        strcpy( sdbay_bbox_fmt , "[%s" );
        for( i = 1; i < sdbay[Ind].bboxnum; i++ )
            strcat( sdbay_bbox_fmt, ",%s" );
        strcat( sdbay_bbox_fmt, "]" );
        sscanf( colval, sdbay_bbox_fmt,  
                &sdbay[Ind].bcoords[0], &sdbay[Ind].bcoords[1], 
                &sdbay[Ind].bcoords[2], &sdbay[Ind].bcoords[3], 
                &sdbay[Ind].bcoords[4], &sdbay[Ind].bcoords[5], 
                &sdbay[Ind].bcoords[6], &sdbay[Ind].bcoords[7], 
                &sdbay[Ind].bcoords[8], &sdbay[Ind].bcoords[9], 
                &sdbay[Ind].bcoords[10], &sdbay[Ind].bcoords[11], 
                &sdbay[Ind].bcoords[12], &sdbay[Ind].bcoords[13], 
                &sdbay[Ind].bcoords[14], &sdbay[Ind].bcoords[15], 
                &sdbay[Ind].bcoords[16], &sdbay[Ind].bcoords[17], 
                &sdbay[Ind].bcoords[18], &sdbay[Ind].bcoords[19], 
                &sdbay[Ind].bcoords[20], &sdbay[Ind].bcoords[21], 
                &sdbay[Ind].bcoords[22], &sdbay[Ind].bcoords[23], 
                &sdbay[Ind].bcoords[24], &sdbay[Ind].bcoords[25], 
                &sdbay[Ind].bcoords[26], &sdbay[Ind].bcoords[27], 
                &sdbay[Ind].bcoords[28], &sdbay[Ind].bcoords[29], 
                &sdbay[Ind].bcoords[30], &sdbay[Ind].bcoords[31] );
    }
    else if( strcmp( colname, "bbox") == 0 )
    {
        sdbay[Ind].bbox = (float *) calloc( sdbay[Ind].bboxnum * 4,
					                sizeof( float ) );
        strcpy( sdbay_bbox_fmt , "[%f" );
        for( i = 1; i < sdbay[Ind].bboxnum * 4; i++ )
            strcat( sdbay_bbox_fmt, ",%f" );
        strcat( sdbay_bbox_fmt, "]" );

        sscanf( colval, sdbay_bbox_fmt,  
                &sdbay[Ind].bbox[0], &sdbay[Ind].bbox[1], 
                &sdbay[Ind].bbox[2], &sdbay[Ind].bbox[3], 
                &sdbay[Ind].bbox[4], &sdbay[Ind].bbox[5], 
                &sdbay[Ind].bbox[6], &sdbay[Ind].bbox[7],
                &sdbay[Ind].bbox[8], &sdbay[Ind].bbox[9],
                &sdbay[Ind].bbox[10], &sdbay[Ind].bbox[11],
                &sdbay[Ind].bbox[12], &sdbay[Ind].bbox[13],
                &sdbay[Ind].bbox[14], &sdbay[Ind].bbox[15],
                &sdbay[Ind].bbox[16], &sdbay[Ind].bbox[17],
                &sdbay[Ind].bbox[18], &sdbay[Ind].bbox[19],
                &sdbay[Ind].bbox[20], &sdbay[Ind].bbox[21],
                &sdbay[Ind].bbox[22], &sdbay[Ind].bbox[23],
                &sdbay[Ind].bbox[24], &sdbay[Ind].bbox[25],
                &sdbay[Ind].bbox[26], &sdbay[Ind].bbox[27],
                &sdbay[Ind].bbox[28], &sdbay[Ind].bbox[29],
                &sdbay[Ind].bbox[30], &sdbay[Ind].bbox[31] );
    }
}
