Example #1
0
File: bin.c Project: bitursa/maos
/**
   A unified header reading routine for .bin and .fits files. It read the array
   information and string header if any.  Return non zero value if reading failed*/
int read_header2(header_t *header, file_t *fp) {
    int ans;
    header->str=NULL;
    if(fp->isfits) {
        ans=read_fits_header(header, fp);
    } else {
        ans=read_bin_header(header, fp);
    }
    return ans;
}
Example #2
0
bool
FitsInput::set_spec_info ()
{
    keys.clear ();
    // FITS spec doesn't say anything about color space or
    // number of channels, so we read all images as if they
    // all were one-channel images
    m_spec = ImageSpec(0, 0, 1, TypeDesc::UNKNOWN);

    // reading info about current subimage
    if (! read_fits_header ())
        return false;

    // we don't deal with one dimension images
    // it's some kind of spectral data
    if (! m_spec.width || ! m_spec.height) {
        m_spec.width = m_spec.full_width = 0;
        m_spec.height = m_spec.full_height = 0;
    }

    // now we can get the current position in the file
    // this is the start of the image data
    // we will need it in the read_native_scanline method
    fgetpos(m_fd, &m_filepos);

    if (m_bitpix == 8)
        m_spec.set_format (TypeDesc::UCHAR);
    else if (m_bitpix == 16)
        m_spec.set_format (TypeDesc::USHORT);
    else if (m_bitpix == 32)
        m_spec.set_format (TypeDesc::UINT);
    else if (m_bitpix == -32)
        m_spec.set_format (TypeDesc::FLOAT);
    else if (m_bitpix == -64)
        m_spec.set_format (TypeDesc::DOUBLE);
    return true;
}
Example #3
0
bool
FitsInput::read_fits_header (void)
{
    std::string fits_header (HEADER_SIZE, 0);

    // we read whole header at once
    if (fread (&fits_header[0], 1, HEADER_SIZE, m_fd) != HEADER_SIZE) {
        if (feof (m_fd))
            error ("Hit end of file unexpectedly");
        else
            error ("read error");
        return false;   // Read failed
    }

    for (int i = 0; i < CARDS_PER_HEADER; ++i) {
        std::string card (CARD_SIZE, 0);
        // reading card number i
        memcpy (&card[0], &fits_header[i*CARD_SIZE], CARD_SIZE);

        std::string keyname, value;
        fits_pvt::unpack_card (card, keyname, value);

        // END means that this is end of the FITS header
        // we can now add to the ImageSpec COMMENT, HISTORY and HIERARCH keys
        if (keyname == "END") {
            // removing white spaces that we use to separate lines of comments
            // from the end ot the string
            m_comment = m_comment.substr (0, m_comment.size() - m_sep.size ());
            m_history = m_history.substr (0, m_history.size() - m_sep.size ());
            m_hierarch = m_hierarch.substr (0, m_hierarch.size() - m_sep.size ());
            add_to_spec ("Comment", m_comment);
            add_to_spec ("History", m_history);
            add_to_spec ("Hierarch", m_hierarch);
            return true;
        }

        if (keyname == "SIMPLE" || keyname == "XTENSION")
            continue;

        // setting up some important fields
        // m_bitpix - format of the data (eg. bpp)
        // m_naxes - number of axes
        // width, height and depth of the image 
        if (keyname == "BITPIX") {
            m_bitpix = atoi (&card[10]);
            continue;
        }
        if (keyname == "NAXIS") {
            m_naxes = atoi (&card[10]);
            continue;
        }
        if (keyname == "NAXIS1") {
            m_spec.width = atoi (&card[10]);
            m_spec.full_width = m_spec.width;
            continue;
        }
        if (keyname == "NAXIS2") {
            m_spec.height = atoi (&card[10]);
            m_spec.full_height = m_spec.height;
            continue;
        }
        // ignoring other axis
        if (keyname.substr (0,5) == "NAXIS") {
            continue;
        }
        if (keyname == "ORIENTAT") {
            add_to_spec ("Orientation", value);
            continue;
        }
        if (keyname == "DATE") {
            add_to_spec ("DateTime", convert_date (value));
            continue;
        }
        if (keyname == "COMMENT") {
            m_comment += (value + m_sep);
            continue;
        }
        if (keyname == "HISTORY") {
            m_history += (value + m_sep);
            continue;
        }
        if (keyname == "HIERARCH") {
            m_hierarch += (value + m_sep);
            continue;
        }

        add_to_spec (pystring::capitalize(keyname), value);
    }
    // if we didn't found END keyword in current header, we read next one
    return read_fits_header ();
}