Sophie

Sophie

distrib > Fedora > 13 > i386 > media > os > by-pkgid > ecbd8c5b7568e331fe6ea5b9a07a78a0 > files > 33

CCfits-docs-2.2-2.fc13.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>CCfits: Creating and Writing to an Ascii Table Extension</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.1 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">


<h1><a class="anchor" id="ascii">Creating and Writing to an Ascii Table Extension </a></h1><p>In this section of the program we create a new Table extension of type AsciiTbl, and write three columns with 6 rows. Then we add another copy of the data two rows down (starting from row 3) thus overwriting values and creating new rows. We test the use of null values, and writing a date string. Implicit data conversion, as illustrated for images above, is supported. However, writing numeric data as character data, supported by cfitsio, is <b><em>not</em></b> supported by CCfits.</p>
<p>Note the basic pattern of CCfits operations: they are performed on an object of type FITS. Access to HDU extension is provided by FITS:: member functions that return references or pointers to objects representing HDUs. Extension are never created directly (all extension ctors are protected), but only through the functions FITS::addTable and FITS::addImage which add extensions to an existing FITS object, performing the necessary cfitsio calls.</p>
<p>The FITS::addTable function takes as one of its last arguments a HDU Type parameter, which needs to be AsciiTbl or BinTbl. The default is to create a BinTable (see next function).</p>
<p>Similarly, access to column data is provided through the functions <em>ExtHDU::Column</em>, which return references to columns specified by name or index number - see the documentation for the class ExtHDU for details.</p>
<p>addTable returns a pointer to Table, which is the abstract immediate superclass of the concrete classes AsciiTable and BinTable, whereas addImage returns a pointer to ExtHDU, which is the abstract base class of all FITS extensions. These base classes implement the public interface necessary to avoid the user of the library needing to downcast to a concrete type.</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> writeAscii ()

    <span class="comment">//******************************************************************</span>
    <span class="comment">// Create an ASCII Table extension containing 3 columns and 6 rows *</span>
    <span class="comment">//******************************************************************</span>
{
    <span class="comment">// declare auto-pointer to FITS at function scope. Ensures no resources</span>
    <span class="comment">// leaked if something fails in dynamic allocation.</span>
    std::auto_ptr&lt;FITS&gt; pFits(0);
      
    <span class="keywordflow">try</span>
    {                
    
            
        <span class="keyword">const</span> std::string fileName(<span class="stringliteral">&quot;atestfil.fit&quot;</span>);
        
        <span class="comment">// append the new extension to file created in previous function call.   </span>
        <span class="comment">// CCfits writing constructor. </span>
        
        <span class="comment">// if this had been a new file, then the following code would create</span>
        <span class="comment">// a dummy primary array with BITPIX=8 and NAXIS=0.</span>

 
        pFits.reset( <span class="keyword">new</span> FITS(fileName,Write) );
    }
    <span class="keywordflow">catch</span> (<a class="code" href="classCCfits_1_1FITS_1_1CantOpen.html" title="thrown on failure to open existing file">CCfits::FITS::CantOpen</a>)
    {
          <span class="comment">// ... or not, as the case may be.</span>
          <span class="keywordflow">return</span> -1;       
    }
        
    <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> rows(6); 
    <span class="keywordtype">string</span> hduName(<span class="stringliteral">&quot;PLANETS_ASCII&quot;</span>);  
    std::vector&lt;string&gt; colName(3,<span class="stringliteral">&quot;&quot;</span>);
    std::vector&lt;string&gt; colForm(3,<span class="stringliteral">&quot;&quot;</span>);
    std::vector&lt;string&gt; colUnit(3,<span class="stringliteral">&quot;&quot;</span>);
    
    <span class="comment">/* define the name, datatype, and physical units for the 3 columns */</span>    
    colName[0] = <span class="stringliteral">&quot;Planet&quot;</span>;
    colName[1] = <span class="stringliteral">&quot;Diameter&quot;</span>;
    colName[2] = <span class="stringliteral">&quot;Density&quot;</span>;

    colForm[0] = <span class="stringliteral">&quot;a8&quot;</span>;
    colForm[1] = <span class="stringliteral">&quot;i6&quot;</span>;
    colForm[2] = <span class="stringliteral">&quot;f4.2&quot;</span>;

    colUnit[0] = <span class="stringliteral">&quot;&quot;</span>;
    colUnit[1] = <span class="stringliteral">&quot;km&quot;</span>;
    colUnit[2] = <span class="stringliteral">&quot;g/cm^-3&quot;</span>;    

    std::vector&lt;string&gt; planets(rows);
    
    <span class="keyword">const</span> <span class="keywordtype">char</span> *planet[] = {<span class="stringliteral">&quot;Mercury&quot;</span>, <span class="stringliteral">&quot;Venus&quot;</span>, <span class="stringliteral">&quot;Earth&quot;</span>, 
                            <span class="stringliteral">&quot;Mars&quot;</span>,<span class="stringliteral">&quot;Jupiter&quot;</span>,<span class="stringliteral">&quot;Saturn&quot;</span>};
    <span class="keyword">const</span> <span class="keywordtype">char</span> *mnemoy[] = {<span class="stringliteral">&quot;Many&quot;</span>, <span class="stringliteral">&quot;Volcanoes&quot;</span>, <span class="stringliteral">&quot;Erupt&quot;</span>, 
                            <span class="stringliteral">&quot;Mulberry&quot;</span>,<span class="stringliteral">&quot;Jam&quot;</span>,<span class="stringliteral">&quot;Sandwiches&quot;</span>,<span class="stringliteral">&quot;Under&quot;</span>,
                                <span class="stringliteral">&quot;Normal&quot;</span>,<span class="stringliteral">&quot;Pressure&quot;</span>};
                    
    <span class="keywordtype">long</span> diameter[] = {  4880,    12112,      12742,   6800,    143000,   121000};
    <span class="keywordtype">float</span> density[]  = { 5.1f,     5.3f,      5.52f,   3.94f,    1.33f,    0.69f};
    
    <span class="comment">// append a new ASCII table to the fits file. Note that the user</span>
    <span class="comment">// cannot call the Ascii or Bin Table constructors directly as they</span>
    <span class="comment">// are protected.</span>
    
    Table* newTable = pFits-&gt;addTable(hduName,rows,colName,colForm,colUnit,AsciiTbl);
        <span class="keywordtype">size_t</span> j = 0;    
    <span class="keywordflow">for</span> ( ; j &lt; rows; ++j) planets[j] = <span class="keywordtype">string</span>(planet[j]);    
    
    <span class="comment">// Table::column(const std::string&amp; name) returns a reference to a Column object</span>
    
    <span class="keywordflow">try</span>
    {                
    
        newTable-&gt;column(colName[0]).write(planets,1);  
        newTable-&gt;column(colName[1]).write(diameter,rows,1);
        newTable-&gt;column(colName[2]).write(density,rows,1);
    
    }
    <span class="keywordflow">catch</span> (FitsException&amp;)
    {
         <span class="comment">// ExtHDU::column could in principle throw a NoSuchColumn exception,</span>
         <span class="comment">// or some other fits error may ensue.</span>
         std::cerr &lt;&lt; <span class="stringliteral">&quot; Error in writing to columns - check e.g. that columns of specified name &quot;</span>
                        &lt;&lt; <span class="stringliteral">&quot; exist in the extension \n&quot;</span>;
                               
    }
    
    <span class="comment">//  FITSUtil::auto_array_ptr&lt;T&gt; is provided to counter resource leaks that</span>
    <span class="comment">//  may arise from C-arrays. It is a std::auto_ptr&lt;T&gt; analog that calls</span>
    <span class="comment">//  delete[] instead of delete.</span>
    
    FITSUtil::auto_array_ptr&lt;long&gt;  pDiameter(<span class="keyword">new</span> <span class="keywordtype">long</span>[rows]);
    FITSUtil::auto_array_ptr&lt;float&gt;  pDensity(<span class="keyword">new</span> <span class="keywordtype">float</span>[rows]);
    <span class="keywordtype">long</span>* Cdiameter = pDiameter.get();
    <span class="keywordtype">float</span>*  Cdensity = pDensity.get();
    
    Cdiameter[0] = 4880; Cdiameter[1] = 12112; Cdiameter[2] = 12742; Cdiameter[3] = 6800;
    Cdiameter[4] = 143000; Cdiameter[5] = 121000;
    
    Cdensity[0] = 5.1f;  Cdensity[1] = 5.3f;  Cdensity[2] = 5.52f;
        Cdensity[3] = 3.94f; Cdensity[4] = 1.33f; Cdensity[5] = 0.69;
    
    <span class="comment">// this &lt;&lt; operator outputs everything that has been read.</span>
    
    std::cout &lt;&lt; *newTable &lt;&lt; std::endl;
    
    pFits-&gt;pHDU().addKey(<span class="stringliteral">&quot;NEWVALUE&quot;</span>,42,<span class="stringliteral">&quot; Test of adding keyword to different extension&quot;</span>);
    
        pFits-&gt;pHDU().addKey(<span class="stringliteral">&quot;STRING&quot;</span>,std::string(<span class="stringliteral">&quot; Rope &quot;</span>),<span class="stringliteral">&quot;trailing blank test 1 &quot;</span>); 
    
        pFits-&gt;pHDU().addKey(<span class="stringliteral">&quot;STRING2&quot;</span>,std::string(<span class="stringliteral">&quot;Cord&quot;</span>),<span class="stringliteral">&quot;trailing blank test 2 &quot;</span>); 
    <span class="comment">// demonstrate increaing number of rows and null values.</span>
    <span class="keywordtype">long</span> ignoreVal(12112); 
    <span class="keywordtype">long</span> nullNumber(-999);    
    <span class="keywordflow">try</span>
    {      
        <span class="comment">// add a TNULLn value to column 2. </span>
        newTable-&gt;column(colName[1]).addNullValue(nullNumber);    
        <span class="comment">// test that writing new data properly expands the number of rows</span>
        <span class="comment">// in both the file]).write(planets,rows-3);  </span>
        newTable-&gt;column(colName[2]).write(density,rows,rows-3);    
        <span class="comment">// test the undefined value functionality. Undefineds are replaced on</span>
        <span class="comment">// disk but not in the memory copy.</span>
        newTable-&gt;column(colName[1]).write(diameter,rows,rows-3,&amp;ignoreVal);
    }
    <span class="keywordflow">catch</span> (FitsException&amp;) 
    { 
            <span class="comment">// this time we&apos;re going to ignore problems in these operations </span>
            
    }

    <span class="comment">// output header information to check that everything we did so far</span>
    <span class="comment">// hasn&apos;t corrupted the file.    </span>
            
    std::cout &lt;&lt; pFits-&gt;pHDU() &lt;&lt; std::endl;
    
    
    std::vector&lt;string&gt; mnemon(9);
    <span class="keywordflow">for</span> ( j = 0; j &lt; 9; ++j) mnemon[j] = <span class="keywordtype">string</span>(mnemoy[j]);
    
    <span class="comment">// Add a new column of string type to the Table.</span>
    <span class="comment">// type,  columnName, width, units. [optional - decimals, column number]</span>
    <span class="comment">// decimals is only relevant for floatingpoint data in ascii columns.</span>
    newTable-&gt;addColumn(Tstring,<span class="stringliteral">&quot;Mnemonic&quot;</span>,10,<span class="stringliteral">&quot; words &quot;</span>);
    newTable-&gt;column(<span class="stringliteral">&quot;Mnemonic&quot;</span>).write(mnemon,1);  
    
    <span class="comment">// write the data string.</span>
    newTable-&gt;writeDate();
   
    <span class="comment">// and see if it all worked right.</span>
    std::cout &lt;&lt; *newTable &lt;&lt; std::endl;
    
    <span class="keywordflow">return</span> 0;
}        
</pre></div> </div>
<hr size="1"/><address style="text-align: right;"><small>Generated on Wed Sep 9 11:59:41 2009 for CCfits by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>
</html>