/* COPYRIGHT (c) 1998 Kapteyn Astronomical Institute University of Groningen, The Netherlands All Rights Reserved. #> getval.dc1 Program: GETVAL Purpose: Get image values at given positions in a set Category: UTILITY File: getval.c Author: M.G.R. Vogelaar Keywords: INSET= Give set (,subset): Maximum number of subsets is 1. The set/subset can have any dimension. OUTFILE= Name of output file: [GETVAL_output.txt] ASCII file on disk which contains the positions in POSITION= and the corresponding image values. The image values are formatted according the string entered in FORMAT= APPEND= Ok to append? [Y]/N ASCII file on disk exists. Is it ok to append? FORMAT= Give format for output: [fffffffff.fffff] Print numbers on screen or save numbers in file in user given output format. See description for possible formats POSITION= Enter position in (......): [STOP] Positions are entered as grids or physical coordinates (or mixed) according to the GIPSY rules for coordinate input. Each position consists of n numbers where n is the dimension of INSET= You can abort the loop by pressing [enter]. If you want to read coordinates from file then this file must have extension '.rcl' and you can use it in the following way: POSITION= (b) ? (a) : (b) ) #define MYMIN(a,b) ( (a) > (b) ? (b) : (a) ) #define NINT(a) ( (a) < 0 ? (int)((a)-.5) : (int)((a)+.5) ) #define ABS(a) ( (a) < 0 ? (-(a)) : (a) ) #define PI 3.141592653589793 #define RAD(a) ( (a) * 0.017453292519943295769237 ) #define DEG(a) ( (a) * 57.295779513082320876798155 ) #define RELEASE "1.0" /* Version number */ #define MAXAXES 10 /* Max. axes in a set */ #define MAXSUBSETS 1 /* Max. allowed subsets */ #define MAXBUF 4096 /* Buffer size for I/O */ #define STRLEN 256 /* Max length of strings */ #define FILENAMELEN 256 /* Max length of file names */ #define FITSLEN 20 /* Max length of header items etc.*/ #define NONE 0 /* Default levels in userxxx routines */ #define REQUEST 1 #define HIDDEN 2 #define EXACT 4 #define YES 1 /* C versions of .TRUE. and .FALSE. */ #define NO 0 /* Miscellaneous globals */ static fint setlevel = 0; /* To get header items at set level. */ static float blank; /* Global value for BLANK. */ static char message[STRLEN]; /* All purpose character buffer. */ /* Defines for in/output routines etc.*/ #define KEY_INSET tofchar("INSET=") #define MES_INSET tofchar("Give input set (,subset):") #define KEY_FILENAME tofchar("OUTFILE=") #define KEY_APPEND tofchar("APPEND=") static void clearstr( char *str, int len ) /*------------------------------------------------------------*/ /* PURPOSE: Avoid garbage in strings. */ /*------------------------------------------------------------*/ { int i; for (i = 0; i < len; i++) str[i] = ' '; } FILE *fopenC( char *filename ) /*---------------------------------------------*/ /* Open file to write data extern. The */ /* macro 'fmake' must be available. */ /* If file exists, ask user if it is ok */ /* to append. 'filename' is the default name. */ /* It is also the filename on return. */ /*---------------------------------------------*/ { fchar Filename; bool append; fint dfault; fint n; fint nitems; fint agreed; FILE *fp; char mes1[STRLEN]; dfault = REQUEST; Filename.l = STRLEN; Filename.a = filename; sprintf( mes1, "Name of output file: [%s]", filename ); do { append = toflog(YES); /* Default APPEND=Y */ n = usertext_c( Filename, &dfault, KEY_FILENAME, tofchar( mes1 ) ); if (n == 0) n = strlen( filename ); if (n == 0) return NULL; sprintf( filename, "%.*s", nelc_c(Filename), Filename.a ); fp = fopen(filename, "r"); if (fp != NULL) { /* The file exists */ nitems = 1; n = userlog_c( &append, &nitems, &dfault, KEY_APPEND, tofchar("Ok to append? [Y]/N") ); append = tobool( append ); fclose( fp ); cancel_c( KEY_APPEND ); } if (!append) { cancel_c( KEY_FILENAME ); agreed = NO; } else { fp = fopen(filename, "a"); agreed = (fp != NULL); if (!agreed) { reject_c( KEY_FILENAME, tofchar("Cannot open, try another!") ); } } } while (!agreed); return( fp ); } static void findaxnames( char *axnames, fchar Setin, fint subdim, fint *axnum ) /* *------------------------------------------------------------------------------ * Find names of axes in format (RA,DEC) etc. *------------------------------------------------------------------------------ */ { int n; char axis_b[20+1]; fchar Ctype; fint err = 0; char dummystr[STRLEN]; (void) sprintf( axnames, "(" ); for (n = 0; n < subdim; n++ ) { Ctype.a = axis_b; Ctype.l = 20; axis_b[20] = '\0'; err = 0; gdsc_name_c( Ctype, Setin, &axnum[n], &err ); if (( n + 1 ) == subdim) { /* Space and hyphen! */ sprintf( dummystr, "%s", strtok( axis_b, " -" ) ); } else { /* Comma added */ sprintf( dummystr, "%s,", strtok( axis_b, " -" ) ); } strcat( axnames, dummystr ); } strcat( axnames, ")" ); } MAIN_PROGRAM_ENTRY /*-------------------------------------------------------------------------*/ /* The macro MAIN_PROGRAM_ENTRY replaces the C-call main() to start the */ /* main body of your GIPSY application. Variables defined as 'fchar' start */ /* with a capital. */ /*-------------------------------------------------------------------------*/ { fint maxsubs = MAXSUBSETS; fint maxaxes = MAXAXES; /* Max num. of axes the program can deal with.*/ fint class = 1; /* Class 1 is for applications which repeat */ fint showdev = 1; fint nsubs; /* Number of input subsets */ fint dfault; /* Default option for input etc */ fint nitems; fint r1, r2; FILE *fp; char outfilename[STRLEN]; char axnames[STRLEN]; char postxt[STRLEN]; fchar Formatstr; fchar Prusingstr; fchar Setin; /* Name of input set */ fint subin[MAXSUBSETS]; /* Subset coordinate words */ fint axnum[MAXAXES]; /* Array of size MAXAXES containing the */ /* axes numbers. The first elements (upto */ /* the dimension of the subset) contain the */ /* axes numbers of the subset, the other */ /* ones ontain the axes numbers outside the */ /* the subset ordered according to the */ /* specification by the user. */ fint axcount[MAXAXES]; /* Array of size MAXAXES containing the */ /* number of grids along an axes as */ /* specified by the user. The first elements */ /* (upto the dimension of the subset) contain */ /* the length of the subset axes, the other */ /* ones contain the the number of grids along */ /* an axes outside the subset. */ /* the operation for each subset, Class 2 */ /* is for applications for which the operation */ /* requires an interaction between the different */ /* subsets. */ fint subdim; /* Dimensionality of the subsets for class 1 applications */ fint setdim; /* Dimension of set. */ fint flo[MAXAXES]; /* Low edge of frame in grids */ fint fhi[MAXAXES]; /* High edge of frame in grids */ fint blo[MAXAXES]; /* Low edge of box in grids */ fint bhi[MAXAXES]; /* High edge of box in grids */ init_c(); /* contact Hermes */ /* Task identification */ { static fchar Task; /* Name of current task */ fmake( Task, 20 ); /* Macro 'fmake' must be available */ myname_c( Task ); /* Get task name */ Task.a[nelc_c(Task)] = '\0'; /* Terminate task name with null char. */ IDENTIFICATION( Task.a, RELEASE ); /* Show task and version */ } setfblank_c( &blank ); /*--------------------------------------------------*/ /* Get the input set. Documentation can be found in */ /* $gip_sub/gdsinp.dc2 */ /*--------------------------------------------------*/ { fmake( Setin, STRLEN ); dfault = NONE; subdim = 0; /* Allow only all-dim structures */ nsubs = gdsinp_c( Setin, /* Name of input set. */ subin, /* Array containing subsets coordinate words. */ &maxsubs, /* Maximum number of subsets in 'subin'.*/ &dfault, /* Default code as is USERxxx. */ KEY_INSET, /* Keyword prompt. */ MES_INSET, /* Keyword message for the user. */ &showdev, /* Device number (as in ANYOUT). */ axnum, /* Array of size 'maxaxes' containing the axes numbers. */ /* The first elements (upto the dimension of the subset) */ /* contain the axes numbers of the subset, */ /* the other ones contain the axes numbers */ /* outside the subset ordered according to the */ /* specification by the user. */ axcount, /* Number of grids on axes in 'axnum' */ &maxaxes, /* Max. number of axes. */ /* the operation for each subset. */ &class, /* Class 1 is for applications which repeat */ &subdim ); /* Dimensionality of the subsets for class 1 */ } setdim = gdsc_ndims_c( Setin, &setlevel ); /*-------------------------------*/ /* Determine edges of this frame */ /*-------------------------------*/ { fint cwlo, cwhi; /* Local coordinate words */ int m; r1 = 0; gdsc_range_c( Setin, &setlevel, &cwlo, &cwhi, &r1 ); r1 = r2 = 0; for (m = 0; m < (int) setdim; m++) { flo[m] = gdsc_grid_c( Setin, &axnum[m], &cwlo, &r1 ); fhi[m] = gdsc_grid_c( Setin, &axnum[m], &cwhi, &r2 ); } } strcpy( outfilename, "GETVAL_output.txt" ); fp = fopenC( outfilename ); fmake( Prusingstr, STRLEN ); fmake( Formatstr, STRLEN ); dfault = REQUEST; nitems = 1; strcpy( Formatstr.a, "fffffffff.fffff" ); sprintf( message, "Give format for output: [%s]", Formatstr.a ); r1 = userchar_c( Formatstr, &nitems, &dfault, tofchar("FORMAT="), tofchar(message) ); findaxnames( axnames, Setin, subdim, axnum ); sprintf( postxt, "Enter position in %s: [STOP]", axnames ); do { fint r; fint dfault = REQUEST; char mes2[STRLEN]; char mes3[STRLEN]; char mes4[STRLEN]; fint maxpos = 1; fint cwlo, cwhi; fint tid = 0; int ok; int i; double pos[MAXAXES]; fchar Mes; Mes.a = message; Mes.l = STRLEN+1; clearstr( message, STRLEN ); r = usertext_c( Mes, &dfault, tofchar("POSITION="), tofchar(postxt) ); if (!r) break; strcpy( mes2, "BOX=" ); strcat( mes2, message ); wkey_c( tofchar(mes2) ); dfault = HIDDEN; gdspos_c( pos, &maxpos, &dfault, tofchar("POSITION="), tofchar(" "), Setin, &subin[0] ); cancel_c(tofchar("POSITION=")); sprintf( mes3, "" ); ok = YES; for (i = 0; i < subdim; i++) { blo[i] = (float) pos[i]; bhi[i] = (float) pos[i]; if (blo[i] < flo[i] || bhi[i] > fhi[i]) { ok = NO; } sprintf( mes4, "%6f ", pos[i] ); strcat( mes3, mes4 ); } if (!ok) { anyoutf( 3, "%s ==> Outside frame", mes3 ); } else { float imval; fint maxIObuf = 1; fint pixelsread; tid = 0; cwlo = gdsc_fill_c( Setin, &subin[0], blo ); cwhi = gdsc_fill_c( Setin, &subin[0], bhi ); gdsi_read_c( Setin, &cwlo, &cwhi, &imval, &maxIObuf, &pixelsread, &tid ); if (imval == blank) { sprintf( mes4, " blank" ); strcat( mes3, mes4 ); anyoutf( 3, mes3 ); } else { fint l; double x = (double) imval; l = printusing_c( Formatstr, &x, Prusingstr ); sprintf( mes4, "%.*s\n", nelc_c(Prusingstr), Prusingstr.a ); strcat( mes3, mes4 ); fprintf( fp, mes3 ); anyoutf( 1, mes3 ); } } } while ( 1 ); fclose( fp ); anyoutf( 3, "Data written to file [%s] on disk!", outfilename ); finis_c(); return(EXIT_SUCCESS); /* Dummy return */ }