00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef MLN_IO_FITS_LOAD_HH
00027 # define MLN_IO_FITS_LOAD_HH
00028 
00033 
00034 # include <iostream>
00035 # include <fstream>
00036 # include <string>
00037 
00038 # include <mln/core/image/image2d.hh>
00039 # include <mln/value/int_u8.hh>
00040 
00041 # include <fitsio.h>
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace io
00048   {
00049 
00050 
00051     namespace fits
00052     {
00053 
00060       void load(image2d<float>& ima,
00061                 const std::string& filename);
00062 
00069       image2d<float> load(const std::string& filename);
00070 
00071 # ifndef MLN_INCLUDE_ONLY
00072 
00073       inline
00074       void fits_exit(int status)
00075       {
00076         if (status)
00077         {
00078           fits_report_error(stderr, status);
00079           exit(status);
00080         }
00081         return;
00082       }
00083 
00084       inline
00085       image2d<float> load(const std::string& filename)
00086       {
00087         trace::entering("mln::io::fits::load");
00088 
00089         fitsfile *fptr;
00090         int status,  nfound, anynull;
00091         long naxes[2];
00092         float nullval;
00093 
00094         status = 0;
00095         if (fits_open_file(&fptr, filename.c_str(), READONLY, &status))
00096           fits_exit(status);
00097 
00098         char NAXIS[] = "NAXIS";
00099         if (fits_read_keys_lng(fptr, NAXIS, 1, 2, naxes, &nfound, &status))
00100           fits_exit(status);
00101 
00102         const int ncols = naxes[0], nrows = naxes[1];
00103 
00104         image2d<float> output(nrows, ncols);
00105 
00106         nullval  = 0; 
00107 
00108         point2d p(point2d(0, 0));
00109 
00110         for (p.row() = 0; p.row() < nrows; ++p.row())
00111         {
00112           if (fits_read_img(fptr,
00113                             TFLOAT,
00114                             1 + p.row() * ncols,
00115                             ncols,
00116                             &nullval,
00117                             &(output(p)),
00118                             &anynull,
00119                             &status))
00120             fits_exit(status);
00121         }
00122 
00123         if (fits_close_file(fptr, &status))
00124           fits_exit(status);
00125 
00126         trace::exiting("mln::io::fits::load");
00127 
00128         return output;
00129       }
00130 
00131       inline
00132       void load(image2d<float>& ima,
00133                 const std::string& filename)
00134       {
00135         ima = load(filename);
00136       }
00137 
00138 # endif // ! MLN_INCLUDE_ONLY
00139 
00140     } 
00141 
00142   } 
00143 
00144 } 
00145 
00146 
00147 #endif // ! MLN_IO_FITS_LOAD_HH