#define EXTERN #define MAIN 1 /*-------------------------- ** include files **-------------------------- */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include /*----------------------------- * non standard includes *----------------------------- */ #define IXON 1 #if IXON #include "atmcdLXd.h" // Andor SDK include file #endif #include "moris_lib.h" /*--------------------------- ** function prototypes **--------------------------- */ int cmd_execute( char * cmd_buf); int do_g1( char *par ); int do_q1( char *par ); int do_q2( char *par ); int do_t1( char *par ); int do_help( char *par ); int do_quit( char *par ); ulong get_ulong( char *prompt, ulong cur_val, ulong min, ulong max ); long get_long ( char *prompt, long cur_val, long min, long max ); double get_dbl( char *prompt, double cur_val, double min, double max ); int buf_2_dv( char * host, int dvport, int naxis1, int naxis2, long *data ); char * Gbuild_card2( char * cptr, char * keyword, char * value, char * comment ); /*------------------------------------- * Global variables *------------------------------------- */ long Data[NAXIS1*NAXIS2]; // space for an image + extra FITS record size. int Quit; struct PARSE_TABLE { char * kw; /* The opcode string */ int (* pfi)(char*); /* pfi is pointer to function returning an integer */ char * desc; /* command description */ }; struct PARSE_TABLE parse_table[] = { { "g1", do_g1, "g1 - GO #1" }, { "q1", do_q1, "q1 - Query the Camera #1" }, { "q2", do_q2, "q2 - Query the Camera #2" }, { "t1", do_t1, "t1 - test 1" }, { "help", do_help, "Help - List commands" }, { "Quit", do_quit, "Quit - quits app" }, }; int num_parse_item = { sizeof(parse_table) / sizeof(struct PARSE_TABLE) }; /*--------------------------------- ** main() **--------------------------------- */ int main( int argc, char * argv[]) { int i, err; extern char *optarg; extern int optind; char buf[80]; /* parse options */ err = 0; while((i = getopt(argc, argv, "")) != EOF) { switch(i) { case '?': err++; break; } } if( err ) { fprintf(stderr, "Usage: %s [] \n", argv[0]); exit(0); } /*-------------------------------------- ** Initialize CCD */ #if IXON printf("Calling Andor Initialize() .. takes a few seconds\n"); err = Initialize("/usr/local/etc/andor"); if( err != DRV_SUCCESS ) { printf("Andor Initialize() error %d\n", err); return (0); } sleep(2); //sleep to allow initialization to complete SetTemperature( -20 ); CoolerON(); SetShutter( 1, 2/*close*/, 0, 0 ); #endif /* main loop for commands */ Quit = FALSE; while( !Quit ) { /* Get command */ printf("Enter Command: "); fgets( buf, sizeof(buf), stdin ); unpad( buf, '\n'); cmd_execute( buf ); } #if IXON SetShutter( 1, 2/*close*/, 0, 0 ); CoolerOFF(); printf("Calling Andor ShutDown().. takes a few seconds\n"); sleep(1); ShutDown(); #endif return 0; } /*------------------------------------------------------------- ** cmd_execute() - Executes a command line instruction **------------------------------------------------------------- */ int cmd_execute( cmd_buf ) char * cmd_buf; { char copy_buf[80]; int error, found, i; char * kw, * par; /* ** Search table for keyword */ strxcpy( copy_buf, cmd_buf, sizeof(copy_buf)); kw = strtok(copy_buf, " "); if( (kw == NULL) || (*kw=='#') ) return( 0 ); if( (par = strtok( (char*)NULL, "")) != NULL ) unpad( par, ' ' ); else par = ""; /* ** Search table for keyword. If found call function. */ error = ERR_INV_KW; for(i=0, found = FALSE; i= 1 ) cur_val = atol(buf); if( INRANGE(min, cur_val, max)) { return cur_val; } else printf("\007INVALID Input. Range is %ld to %ld.\n", min, max); } } /*------------------------------------------------------------ ** buf_2_dv() - Send data to dv as a FITS file. **------------------------------------------------------------ */ int buf_2_dv( char * host, // I: dv's socket on this host int dvport, // I: dv's socket port number. int naxis1, // I: naxis1 - size of array x axis. int naxis2, // I: naxis2 - size of array y axis long *data // I: array of longs. ) { char *cptr, *buf; long *sp, *dp; char tstr[80]; int i, j, n, loop, one, npixels; int fd_socket; #define FITS_RECORD_LEN 2880 if( NULL == (buf=(char*)malloc( FITS_RECORD_LEN )) ) { return ERR_MEM_ALLOC; } if( (fd_socket = sock_open( host, dvport, SOCK_STREAM, 0)) > 0) { one = 1; setsockopt( fd_socket , SOL_SOCKET, SO_REUSEADDR, &one, sizeof( one )); /* Give command to read a fits file from socket */ strcpy( tstr, "ReadSock A "); sock_write_msg( fd_socket, tstr, TRUE, SOCK_TIMEOUT_MS ); /* write header */ cptr = buf; memset( buf, ' ', 2880); cptr = Gbuild_card2( cptr, "SIMPLE", "T", "DATA IS IN FITS FORMAT"); cptr = Gbuild_card2( cptr, "BITPIX", "32", "32 BITS TWOS COMPLEMENT INT"); cptr = Gbuild_card2( cptr, "NAXIS", "2", "NUMBER OF AXIS"); sprintf( tstr, "%d", naxis1); cptr = Gbuild_card2( cptr, "NAXIS1", tstr, "1st MOST VARYING AXIS"); sprintf( tstr, "%d", naxis2); cptr = Gbuild_card2( cptr, "NAXIS2", tstr, "2nd MOST VARYING AXIS"); cptr = Gbuild_card2( cptr, "END", " ", "End of header"); sock_write_data( fd_socket, (char*) buf, 2880, TRUE, SOCK_TIMEOUT_MS); npixels = naxis1 * naxis2; n = sizeof(long) * naxis1 * naxis2; loop = n / FITS_RECORD_LEN; if( n % FITS_RECORD_LEN ) loop++; sp = data; for( i=0; i 0 ) { *dp++ = htonl(*sp); sp++; npixels--; } else { *dp++ = 0; // ran out of pixels, set destination to 0. } } sock_write_data( fd_socket, (char*) buf, FITS_RECORD_LEN, TRUE, SOCK_TIMEOUT_MS); } close( fd_socket ); } free( buf ); return ERR_NONE; } /*-------------------------------------------------------------------- ** Gbuild_card2 () - creates a FITS header card in a memory area. **-------------------------------------------------------------------- */ char * Gbuild_card2( char * cptr, char * keyword, char * value, char * comment ) { char outbuf[85]; int l; if( strlen(value) > 18 ) sprintf(outbuf, "%-8.8s= %30.30s / %-s ", keyword, value, comment); else sprintf(outbuf, "%-8.8s= %20.20s / %-s ", keyword, value, comment); l = strlen(outbuf); if( l < 80 ) memset( &outbuf[l], ' ', 80-l); outbuf[80] = '\0'; strncpy( cptr, outbuf, 80); return ( cptr + 80 ); }