Illustration of Low-Level I/O Routines



next up previous
Next: About this document Up: Example Programs Previous: Reading an Image

Illustration of Low-Level I/O Routines

      1:    # include <hstio.h>
      2:    # include <stdio.h>
      3:    
      4:    /***************************************************************************
      5:    
      6:            Illustration of the low-level I/O routines
      7:                            using
      8:            Data Structures and I/O Function Declarations for
      9:            STScI Calibration Pipeline Software for STIS and NICMOS
     10:    
     11:    Input data structures, such as FloatTwoDArray and Hdr, each have
     12:    initilization, allocation, and free routines: initFloatData,
     13:    allocFloatData, freeFloatData. 
     14:    
     15:    On input, if the data structures already have storage allocated and that
     16:    storage is the same size as the requested data, that data storage is
     17:    reused.  If it is not the same size, the old storage is freed and new
     18:    storage is allocated.  If storage has not been allocated, then the input
     19:    routines allocate it. 
     20:    
     21:    It is the responsibility of the application program to free the
     22:    allocated storage. 
     23:    
     24:    ***************************************************************************/
     25:    
     26:    void detect_err() {
     27:            printf("HSTIO Error (%d): %s\n",hstio_err(),hstio_errmsg());
     28:            exit(0);
     29:    }
     30:    
     31:    int main(int argc, char **argv) {
     32:            IODescPtr in;
     33:            FloatTwoDArray x = IFloatData;
     34:            Hdr h = IHdr;
     35:            IODescPtr out;
     36:            FloatTwoDArray z = IFloatData;
     37:            int i, j;
     38:    
     39:            /* install the error handler */
     40:            push_hstioerr(detect_err);
     41:    
     42:            initFloatData(&x); /* Initialize the data structures.  Failure to */
     43:            initFloatData(&z); /* initilize the data structures increases the */
     44:            initHdr(&h);     /* probablilty of getting a core dump.         */
     45:    
     46:            /* This section illustrates the input case. */
     47:            in = openInputImage("infile.fit","",0);
     48:            getFloatData(in,&x);
     49:            getHeader(in,&h);
     50:            closeImage(in);
     51:            for (j = x.ny - 1; j >= 0; --j) {       /* Do something with    */
     52:                for (i = 0; i < x.nx; ++i)          /* the data,            */
     53:                    printf("%g ", Pix(x,i,j));
     54:                printf("\n");
     55:            }
     56:            for (i = 0; i < h.nlines; ++i)          /* and the header.      */
     57:                    printf("%s\n", h.array[i]);
     58:            freeFloatData(&x);                      /* The application must */
     59:            freeHdr(&h);                            /* free storage.        */
     60:    
     61:            /* This section illustrates the output case. */
     62:            allocFloatData(&z,5,5);                 /* Storage must be      */
     63:            allocHdr(&h,25);                        /* allocated.           */
     64:            for (j = z.ny - 1; j >= 0; --j)         /* Fill the data array  */
     65:                for (i = 0; i < z.nx; ++i)          
     66:                    Pix(z,i,j) = (i + 1) * 1000.0 + j + 1;
     67:            /* And, also fill the header array */
     68:            addFloatKw(&h,"CRVAL1",1.5," "); 
     69:            addFloatKw(&h,"CRVAL2",2.5," "); 
     70:            addFloatKw(&h,"CRPIX1",3.5," "); 
     71:            addFloatKw(&h,"CRPIX2",4.5," "); 
     72:            addFloatKw(&h,"CD1_1",5.5," "); 
     73:            addFloatKw(&h,"CD1_2",6.5," "); 
     74:            addFloatKw(&h,"CD2_1",7.5," "); 
     75:            addFloatKw(&h,"CD2_2",8.5," "); 
     76:            addIntKw(&h,"TEST1",34," "); 
     77:            addBoolKw(&h,"TEST2",True," "); 
     78:    
     79:            out = openOutputImage("outfile1.fit","sci",0,&h,5,5,0);
     80:            /* the openOutputImage routine writes the header */
     81:            putFloatData(out,&z);                   /* write the data.      */
     82:            closeImage(out);
     83:            freeFloatData(&z);                      /* Then, free the       */
     84:            freeHdr(&h);                            /* storage.             */
     85:    
     86:            /* This section illustrates copying data from one file to another */
     87:            in = openInputImage("infile.fit","sci",0);
     88:            getHeader(in,&h);
     89:            getFloatData(in,&x);
     90:            closeImage(in);
     91:            out = openOutputImage("outfile2.fit","sci",0,&h,
     92:                    getNaxis1(in),getNaxis2(in),0);
     93:            putFloatData(out,&x);
     94:            closeImage(out);
     95:            freeFloatData(&x);
     96:            freeHdr(&h);
     97:    
     98:            return 0;
     99:    }
    100:



Sdas Group
Wed Aug 21 10:27:26 EDT 1996