"Fossies" - the Fresh Open Source Software Archive

Member "SAOImageDS9/funtools/faq/faq2.html" (13 Nov 2019, 10541 Bytes) of package /linux/misc/ds9.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <html><head><title>Funtools FAQ: Programming</title></head>
    2 
    3 <ol>
    4 <li><a name="faq1"><b>What are the compile/link commands for various platforms?</b></a>
    5 <P>
    6 Assuming, for example, that funtools is installed in /soft/saord and that
    7 gcc is the compiler being used:
    8 <ol>
    9 <li> Linux:
   10 <BLOCKQUOTE><CODE>
   11 <PRE>
   12 gcc -g -I/soft/saord/include -c -o foo.o foo.c
   13 gcc -g foo.o -o foo -L/soft/saord/lib -lfuntools -ldl -lm
   14 </PRE>
   15 </CODE></BLOCKQUOTE>
   16 <li> Apple OS X:
   17 <BLOCKQUOTE><CODE>
   18 <PRE>
   19 gcc -g -no-cpp-precomp -fno-common -I/soft/saord/include -c -o foo.o foo.c
   20 gcc -g foo.o -o foo -L/soft/saord/lib -lfuntools -lm
   21 </PRE>
   22 </CODE></BLOCKQUOTE>
   23 <li> Sun Solaris:
   24 <BLOCKQUOTE><CODE>
   25 <PRE>
   26 gcc -g -no-cpp-precomp -fno-common -I/soft/saord/include -c -o foo.o foo.c
   27 gcc -g foo.o -o foo -L/soft/saord/lib -lfuntools -lsocket -lnsl -ldl -lm
   28 </PRE>
   29 </CODE></BLOCKQUOTE>
   30 
   31 </ol>
   32 
   33 <li><a name="faq2"><b>What is the simplest possible program?</b></a>
   34 <P>
   35 <BLOCKQUOTE><CODE>
   36 <PRE>
   37 #include &lt;stdlib.h&gt;
   38 #include &lt;funtools.h&gt;
   39 
   40 int main(int argc, char **argv)
   41 {
   42   Fun fun;
   43 
   44   /* sanity check */
   45   if( argc &lt; 2 ) return 1;
   46   /* open file for reading */
   47   if( !(fun=FunOpen(argv[1], "r", NULL)) ){
   48     fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
   49     return 1;
   50   }
   51   /* close file */
   52   FunClose(fun);
   53   return 0;
   54 }
   55 </PRE>
   56 </CODE></BLOCKQUOTE>
   57 
   58 <li><a name="faq1.1"><b>How do I read and display events?</b></a>
   59 <P>
   60 <BLOCKQUOTE><CODE>
   61 <PRE>#
   62 #include &lt;stdlib.h&gt;
   63 #include &lt;funtools.h&gt;
   64 
   65 typedef struct evstruct{
   66   double x, y;
   67   int pi, pha;
   68 } *Ev, EvRec;
   69 
   70 int main(int argc, char **argv)
   71 {
   72   int i, got;
   73   int maxrow=1024;
   74   Ev ev, ebuf=NULL;
   75   Fun fun;
   76 
   77   /* sanity check */
   78   if( argc &lt; 2 ) return 1;
   79   /* open file for reading */
   80   if( !(fun=FunOpen(argv[1], "r", NULL)) ){
   81     fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
   82     return 1;
   83   }
   84   /* select columns to read (and data types to convert to) */
   85   got = FunColumnSelect(fun, sizeof(EvRec), NULL,
   86             "x",       "D", "r", FUN_OFFSET(Ev, x),
   87             "y",       "D", "r", FUN_OFFSET(Ev, y),
   88             "pha",     "J", "r", FUN_OFFSET(Ev, pha),
   89             "pi",      "J", "r", FUN_OFFSET(Ev, pi),
   90             NULL);
   91   /* read and process events */
   92   while( (ebuf=(void *)FunTableRowGet(fun, NULL, maxrow, NULL, &got)) && got ){
   93     for(i=0; i&lt;got; i++){
   94       ev = (Ev)ebuf+i;
   95       fprintf(stdout, "%.1f %.1f %d %d\n", ev->x, ev->y, ev->pha, ev->pi);
   96     }
   97     if( ebuf) free(ebuf);
   98   }
   99   /* close file */
  100   FunClose(fun);
  101   return 0;
  102 }
  103 </PRE>
  104 </CODE></BLOCKQUOTE>
  105 
  106 <li><a name="faq1.2"><b>How do I change the value of a single column in all events?</b></a>
  107 <P>
  108 <BLOCKQUOTE><CODE>
  109 <PRE>
  110 #include &lt;stdlib.h&gt;
  111 #include &lt;funtools.h&gt;
  112 
  113 typedef struct evstruct{
  114   int pi;
  115 } *Ev, EvRec;
  116 
  117 int main(int argc, char **argv)
  118 {
  119   int i, got;
  120   int maxrow=1024;
  121   Ev ev, ebuf=NULL;
  122   Fun fun, ofun;
  123 
  124   /* sanity check */
  125   if( argc &lt; 3 ) return 1;
  126   /* open file for reading */
  127   if( !(fun=FunOpen(argv[1], "r", NULL)) ){
  128     fprintf(stderr, "ERROR: can't open input funtools file: %s\n", argv[1]);
  129     return 1;
  130   }
  131   /* open output file and inherit header params, columns, etc. from input */
  132   if( !(ofun=FunOpen(argv[2], "w", fun)) ){
  133     fprintf(stderr, "ERROR: can't open output funtools file: %s\n", argv[2]);
  134     return 1;
  135   }
  136   /* select columns to read (and data types to convert to) */
  137   /* use "merge=update" to change value while keeping original data type */
  138   /* use "merge=replace" to change boh the value and data type */
  139   got = FunColumnSelect(fun, sizeof(EvRec), "merge=update",
  140             "pi",     "J", "rw", FUN_OFFSET(Ev, pi),
  141             NULL);
  142   /* read and process events */
  143   while( (ebuf=(void *)FunTableRowGet(fun, NULL, maxrow, NULL, &got)) && got ){
  144     for(i=0; i&lt;got; i++){
  145       ev = (Ev)ebuf+i;
  146       ev->pi = ev->pi + 1;
  147     }
  148     /* write rows to output file */
  149     if( FunTableRowPut(ofun, ebuf, got, 0, NULL) != got ){
  150       fprintf(stderr, "ERROR: writing to funtools file: %s\n", argv[2]);
  151       return 1;
  152     }
  153     if( ebuf) free(ebuf);
  154   }
  155   /* close files */
  156   FunClose(ofun);
  157   FunClose(fun);
  158   return 0;
  159 }
  160 </PRE>
  161 </CODE></BLOCKQUOTE>
  162 
  163 <li><a name="faq1.3"><b>How do I process events based on the region each is in?</b></a>
  164 <P>
  165 <BLOCKQUOTE><CODE>
  166 <PRE>
  167 #include &lt;stdlib.h&gt;
  168 #include &lt;funtools.h&gt;
  169 
  170 typedef struct evstruct{
  171   int x, y;
  172   int pi, pha;
  173   int region;
  174 } *Ev, EvRec;
  175 
  176 int main(int argc, char **argv)
  177 {
  178   int i, got;
  179   int maxrow=1024;
  180   Ev ev, ebuf=NULL;
  181   Fun fun;
  182 
  183   /* sanity check */
  184   if( argc &lt; 2 ) return 1;
  185   /* open file for reading */
  186   if( !(fun=FunOpen(argv[1], "r", NULL)) ){
  187     fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
  188     return 1;
  189   }
  190   /* select columns to read (and data types to convert to) */
  191   /* specifying $REGION will retrieve the one-indexed region number */
  192   /* events passing the filter but not in a region will have value -1 */
  193   got = FunColumnSelect(fun, sizeof(EvRec), NULL,
  194             "x",       "J", "r", FUN_OFFSET(Ev, x),
  195             "y",       "J", "r", FUN_OFFSET(Ev, y),
  196             "pha",     "J", "r", FUN_OFFSET(Ev, pha),
  197             "pi",      "J", "r", FUN_OFFSET(Ev, pi),
  198             "$REGION", "J", "r", FUN_OFFSET(Ev, region),
  199             NULL);
  200   /* read and process events */
  201   while( (ebuf=(void *)FunTableRowGet(fun, NULL, maxrow, NULL, &got)) && got ){
  202     for(i=0; i&lt;got; i++){
  203       ev = (Ev)ebuf+i;
  204       fprintf(stdout, "%4d %4d %3d %3d %4d\n", 
  205           ev->x, ev->y, ev->pha, ev->pi, ev->region);
  206     }
  207     if( ebuf) free(ebuf);
  208   }
  209   /* close file */
  210   FunClose(fun);
  211   return 0;
  212 }
  213 </PRE>
  214 </CODE></BLOCKQUOTE>
  215 
  216 <li><a name="faq1.4"><b>How do I make a FITS event file from a non-FITS source (and add my own params)?</b></a>
  217 <P>
  218 <BLOCKQUOTE><CODE>
  219 <PRE>
  220 #include &lt;stdlib.h&gt;
  221 #include &lt;funtools.h&gt;
  222 
  223 typedef struct evstruct{
  224   int x, y;
  225   int pha;
  226   float pi;
  227 } *Ev, EvRec;
  228 
  229 int main(int argc, char **argv)
  230 {
  231   int i, nev;
  232   int pmod=16, put=1;
  233   double pinc=0.1234;
  234   char xbuf[32], ybuf[32];
  235   Ev ev;
  236   Fun ofun;
  237 
  238   /* sanity check */
  239   if( argc &lt; 3 ) return 1;
  240   /* open new file for writing */
  241   if( !(ofun=FunOpen(argv[1], "w", NULL)) ){
  242     fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
  243     return 1;
  244   }
  245   if( (nev = atoi(argv[2])) <=0 ) return 1;
  246   ev = (Ev)calloc(1, sizeof(EvRec));
  247   /* The pair of numeric values specify the data value range, used to bin
  248      x, y into an image. They are permitted but not needed for pi and pha */
  249   sprintf(xbuf, "J:1:%d", nev);
  250   sprintf(ybuf, "J:1:%d", nev);
  251   /* select columns to write */
  252   FunColumnSelect(ofun, sizeof(EvRec), NULL,
  253           "x",   xbuf,      "w", FUN_OFFSET(Ev, x),
  254           "y",   ybuf,      "w", FUN_OFFSET(Ev, y),
  255           "pha", "J:1:16",  "w", FUN_OFFSET(Ev, pha),
  256           "pi",  "E",       "w", FUN_OFFSET(Ev, pi),
  257           NULL);
  258   /* write params to header; generally added before first event is written */
  259   FunParamPuti(ofun, "PMOD",  0, pmod,  "modulus for pha generation", 1);
  260   FunParamPutd(ofun, "PINC",  0, pinc,  7, "increment for pi generation", 1);
  261   /* make up events */
  262   for(i=1; i<=nev; i++){
  263     ev->x = i; ev->y = nev-i+1; ev->pha = i % pmod; ev->pi = ev->pha + pinc;
  264     /* write rows to output file -- this can be done in batches, of course */
  265     if( FunTableRowPut(ofun, ev, put, 0, NULL) != put ){
  266       fprintf(stderr, "ERROR: writing to funtools file: %s\n", argv[1]);
  267       return 1;
  268     }
  269   }
  270   if( ev) free(ev);
  271   /* close file */
  272   FunClose(ofun);
  273   return 0;
  274 }
  275 </PRE>
  276 </CODE></BLOCKQUOTE>
  277 
  278 <li><a name="faq2.1"><b>How do I process an image in double float format?</b></a>
  279 <P>
  280 <BLOCKQUOTE><CODE>
  281 <PRE>
  282 #include &lt;stdlib.h&gt;
  283 #include &lt;funtools.h&gt;
  284 
  285 int main(int argc, char **argv)
  286 {
  287   int i, j, dim1, dim2;
  288   double *buf;
  289   Fun fun;
  290 
  291   /* sanity check */
  292   if( argc &lt; 2 ) return 1;
  293   /* open file for reading */
  294   if( !(fun=FunOpen(argv[1], "r", NULL)) ){
  295     fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
  296     return 1;
  297   }
  298   /* extract (and bin, if necessary) data into a double prec. image buffer */
  299   if( !(buf = FunImageGet(fun, NULL, "bitpix=-64")) ){
  300     fprintf(stderr, "ERROR: can't get image: %s\n", argv[1]);
  301     return 1;
  302   }
  303   /* get image dimensions after FunImageGet, in case an image section
  304      was specified on the command line, which changes image dimensions */
  305   FunInfoGet(fun, FUN_SECT_DIM1, &dim1, FUN_SECT_DIM2, &dim2, 0);
  306   /* loop through image */
  307   for(i=0; i&lt;dim2; i++){
  308     for(j=0; j&lt;dim1; j++){
  309       fprintf(stdout, "%.1f ", buf[i*dim1+j]);
  310     }
  311     fprintf(stdout, "\n");
  312   }
  313   /* close file */
  314   FunClose(fun);
  315   return 0;
  316 }
  317 </PRE>
  318 </CODE></BLOCKQUOTE>
  319 
  320 <li><a name="faq2.1"><b>How do I process an image in its native format?</b></a>
  321 <BLOCKQUOTE><CODE>
  322 <PRE>
  323 #include &lt;funtools.h&gt;
  324 
  325 int main(int argc, char **argv)
  326 {
  327   int i, j, bitpix, dim1, dim2;
  328   double *buf;
  329   unsigned char *cbuf;
  330   short *sbuf;
  331   int *ibuf;
  332   float *fbuf;
  333   double *dbuf;
  334   Fun fun;
  335 
  336   /* sanity check */
  337   if( argc &lt; 2 ) return 1;
  338   /* open file for reading */
  339   if( !(fun=FunOpen(argv[1], "r", NULL)) ){
  340     fprintf(stderr, "ERROR: can't open funtools file: %s\n", argv[1]);
  341     return 1;
  342   }
  343   /* extract (and bin, if necessary) data into a buffer whose
  344      data type is not specified and therefore is that of the file */
  345   if( !(buf = FunImageGet(fun, NULL, NULL)) ){
  346     fprintf(stderr, "ERROR: can't get image: %s\n", argv[1]);
  347     return 1;
  348   }
  349   /* get image dimensions after FunImageGet, in case an image section
  350      was specified on the command line, which changes image dimensions */
  351   FunInfoGet(fun, FUN_SECT_BITPIX, &bitpix, 
  352          FUN_SECT_DIM1, &dim1, FUN_SECT_DIM2, &dim2, 0);
  353   /* set appropriate data type buffer to point to image buffer */
  354   switch(bitpix){
  355     case 8:   cbuf = (unsigned char *)buf; break;
  356     case 16:  sbuf = (short *)buf;         break;
  357     case 32:  ibuf = (int *)buf;           break;
  358     case -32: fbuf = (float *)buf;         break;
  359     case -64: dbuf = (double *)buf;        break;
  360   }
  361   /* loop through image */
  362   for(i=0; i&lt;dim2; i++){
  363     for(j=0; j&lt;dim1; j++){
  364       switch(bitpix){
  365         case 8:   fprintf(stdout, "%4d ",  cbuf[i*dim1+j]);  break;
  366         case 16:  fprintf(stdout, "%6d ",  sbuf[i*dim1+j]);  break;
  367         case 32:  fprintf(stdout, "%9d ",  ibuf[i*dim1+j]);  break;
  368         case -32: fprintf(stdout, "%.2f ", fbuf[i*dim1+j]);  break;
  369         case -64: fprintf(stdout, "%.6f ", dbuf[i*dim1+j]);  break;
  370       }
  371     }
  372     fprintf(stdout, "\n");
  373   }
  374   /* close file */
  375   FunClose(fun);
  376   return 0;
  377 }
  378 </PRE>
  379 </CODE></BLOCKQUOTE>
  380 
  381 </ol>