Logo Search packages:      
Sourcecode: qfits version File versions  Download package

int qfits_replace_card ( const char *  filename,
const char *  keyword,
const char *  substitute 
)

Replace a card in a FITS (main) header by a given card.

Parameters:
filename Name of the FITS file to modify.
keyword Where to substitute a card in the header.
substitute What to replace the line with.
Returns:
int 0 if Ok, -1 otherwise
Replaces a whole card (80 chars) in a FITS header by a given FITS line (80 chars). The replacing line is assumed correctly formatted and containing at least 80 characters. The file is modified: it must be accessible in read/write mode.

The input keyword is first converted to upper case and expanded to the HIERARCH scheme if given in the shortFITS notation.

Returns 0 if everything worked Ok, -1 otherwise.

Definition at line 578 of file qfits_tools.c.

References qfits_expand_keyword(), and qfits_get_hdrinfo().

Referenced by qfits_save_table_hdrdump().

{
    char    *   exp_key ;
    int         fd ;
    char    *   buf ;
    char    *   buf2 ;
    char    *   where ;
    int         hs ;


    /* Bulletproof entries */
    if (filename==NULL || keyword==NULL || substitute==NULL) return -1 ;

    /* Expand keyword */
    exp_key = qfits_expand_keyword(keyword);
    /*
     * Memory-map the FITS header of the input file 
     */

    qfits_get_hdrinfo(filename, 0, NULL, &hs) ;
    if (hs < 1) {
        qfits_error("error getting FITS header size for %s", filename);
        return -1 ;
    }
    fd = open(filename, O_RDWR) ;
    if (fd == -1) {
        return -1 ;
    }
    buf = (char*)mmap(0,
                      hs,
                      PROT_READ | PROT_WRITE,
                      MAP_SHARED,
                      fd,
                      0) ;
    if (buf == (char*)-1) {
        perror("mmap") ;
        close(fd) ;
        return -1 ;
    }

    /* Apply search and replace for the input keyword lists */
    buf2 = qfits_malloc(hs+1) ;
    memcpy(buf2, buf, hs) ;
    buf2[hs] = (char)0 ;
    where = buf2 ;
    do {
        where = strstr(where, exp_key);
        if (where == NULL) {
            close(fd);
            munmap(buf,hs);
            qfits_free(buf2) ;
            return -1 ;
        }
        if ((where-buf2)%80) where++ ;
    } while ((where-buf2)%80) ;
       
    where = buf + (int)(where - buf2) ;
    
    /* Replace current placeholder by blanks */
    memset(where, ' ', 80);
    /* Copy substitute into placeholder */
    memcpy(where, substitute, strlen(substitute));

    close(fd) ;
    munmap(buf, hs) ;
    qfits_free(buf2) ;
    return 0 ;
}


Generated by  Doxygen 1.6.0   Back to index