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

int qfits_header_sort ( qfits_header **  hdr  ) 

Sort a FITS header.

Parameters:
hdr Header to sort (modified)
Returns:
-1 in error case, 0 otherwise

Definition at line 451 of file qfits_header.c.

References first, last, n, qfits_header_destroy(), and qfits_header_new().

{
    qfits_header    *   sorted ;
    keytuple        *   k ;
    keytuple        *   kbf ;
    keytuple        *   next ;
    keytuple        *   last ;

    /* Test entries */
    if (hdr == NULL) return -1 ;
    if (*hdr == NULL) return -1 ;
    if ((*hdr)->n < 2) return 0 ;
    
    /* Create the new FITS header */
    sorted = qfits_header_new() ;

    /* Move the first keytuple to the sorted empty header */
    k = (keytuple*)(*hdr)->first ;
    next = k->next ;
    sorted->first = sorted->last = k ;
    k->next = k->prev = NULL ;
    sorted->n = 1 ;
    
    /* Loop over the other tuples */
    while (next != NULL) {
        k = next ;
        next = k->next ;

        /* Find k's place in sorted */
        kbf = (keytuple*)sorted->first ;
        while (kbf!=NULL) {
            if (k->typ < kbf->typ) break ;
            kbf = kbf->next ;
        }
        
        /* Hook k into sorted list */
        if (kbf == NULL) {
            /* k is last in sorted */
            last = sorted->last ;
            sorted->last = k ;
            k->next = NULL ;
            k->prev = last ;
            last->next = k ;
        } else {
            /* k goes just before kbf */
            k->next = kbf ;
            k->prev = kbf->prev ;
            if (kbf->prev != NULL) (kbf->prev)->next = k ;
            else sorted->first = k ;
            kbf->prev = k ;
        }
        (sorted->n) ++ ;
    }

    /* Replace the input header by the sorted one */
    (*hdr)->first = (*hdr)->last = NULL ;
    qfits_header_destroy(*hdr) ;
    *hdr = sorted ;
    
    return 0 ;
}


Generated by  Doxygen 1.6.0   Back to index