Index: trunk/psLib/src/collections/psMetadata.c
===================================================================
--- trunk/psLib/src/collections/psMetadata.c	(revision 1381)
+++ trunk/psLib/src/collections/psMetadata.c	(revision 1385)
@@ -11,6 +11,6 @@
 *  @author Ross Harman, MHPCC
 *
-*  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2004-08-04 21:05:24 $
+*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-08-04 23:37:39 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -65,15 +65,15 @@
 {
     psMetadataType type;
-    
+
     type = metadataItem->type;
-    
+
     if ( metadataItem == NULL ) {
-            return ;
-        }
-        
+        return ;
+    }
+
     psFree( metadataItem->name );
     psFree( metadataItem->comment );
     psFree( metadataItem->items );
-    
+
     if ( type == PS_META_STR ||
             type == PS_META_IMG ||
@@ -82,6 +82,6 @@
             type == PS_META_ASTROM ||
             type == PS_META_UNKNOWN ) {
-            psFree( metadataItem->data.V );
-        }
+        psFree( metadataItem->data.V );
+    }
 }
 
@@ -89,6 +89,6 @@
 {
     if ( metadata == NULL ) {
-            return ;
-        }
+        return ;
+    }
     psFree( metadata->list );
     psFree( metadata->table );
@@ -103,14 +103,14 @@
     va_list argPtr;
     psMetadataItem *metadataItem = NULL;
-    
+
     // Get the variable list parameters to pass to allocation function
     va_start( argPtr, comment );
-    
+
     // Call metadata item allocation
     metadataItem = psMetadataItemAllocV( name, type, comment, argPtr );
-    
+
     // Clean up stack after variable arguement has been used
     va_end( argPtr );
-    
+
     return metadataItem;
 }
@@ -119,67 +119,67 @@
 {
     psMetadataItem * metadataItem = NULL;
-    
+
     if ( name == NULL ) {
-            psError( __func__, "Null value for string not allowed" );
-            return NULL;
-        }
-        
+        psError( __func__, "Null value for string not allowed" );
+        return NULL;
+    }
+
     // Allocate metadata item
     metadataItem = ( psMetadataItem * ) psAlloc( sizeof( psMetadataItem ) );
     if ( metadataItem == NULL ) {
-            psAbort( __func__, "Failed to allocate memory" );
-        }
-        
+        psAbort( __func__, "Failed to allocate memory" );
+    }
+
     // Set deallocator
     p_psMemSetDeallocator( metadataItem, ( psFreeFcn ) metadataItemFree );
-    
+
     // Allocate and set metadata item comment
     metadataItem->comment = ( char * ) psAlloc( sizeof( char ) * MAX_STRING_LENGTH );
     if ( comment == NULL ) {
-            // Per SDRS, null isn't allowed, must use "" instead
-            strncpy( metadataItem->comment, "", MAX_STRING_LENGTH );
-        } else {
-            strncpy( metadataItem->comment, comment, MAX_STRING_LENGTH );
-        }
-        
+        // Per SDRS, null isn't allowed, must use "" instead
+        strncpy( metadataItem->comment, "", MAX_STRING_LENGTH );
+    } else {
+        strncpy( metadataItem->comment, comment, MAX_STRING_LENGTH );
+    }
+
     // Set metadata item unique id
     *( int* ) ( &metadataItem->id ) = ++metadataId;
-    
+
     // Set metadata item type
     metadataItem->type = type;
-    
+
     // Set metadata item value
     switch ( type ) {
-            case PS_META_BOOL:
-            metadataItem->data.B = ( bool ) va_arg( argPtr, int );
-            break;
-            case PS_META_S32:
-            metadataItem->data.S32 = va_arg( argPtr, psS32 );
-            break;
-            case PS_META_F32:
-            metadataItem->data.F32 = ( psF32 ) va_arg( argPtr, psF64 );
-            break;
-            case PS_META_F64:
-            metadataItem->data.F64 = va_arg( argPtr, psF64 );
-            break;
-            case PS_META_STR:
-            metadataItem->data.V = psStringNCopy( va_arg( argPtr, char* ), MAX_STRING_LENGTH );
-            break;
-            case PS_META_IMG:
-            case PS_META_JPEG:
-            case PS_META_PNG:
-            case PS_META_ASTROM:
-            case PS_META_UNKNOWN:
-            default:
-            psError( __func__, "Invalid psMetadataType: %d", type );
-        }
-        
+    case PS_META_BOOL:
+        metadataItem->data.B = ( bool ) va_arg( argPtr, int );
+        break;
+    case PS_META_S32:
+        metadataItem->data.S32 = va_arg( argPtr, psS32 );
+        break;
+    case PS_META_F32:
+        metadataItem->data.F32 = ( psF32 ) va_arg( argPtr, psF64 );
+        break;
+    case PS_META_F64:
+        metadataItem->data.F64 = va_arg( argPtr, psF64 );
+        break;
+    case PS_META_STR:
+        metadataItem->data.V = psStringNCopy( va_arg( argPtr, char* ), MAX_STRING_LENGTH );
+        break;
+    case PS_META_IMG:
+    case PS_META_JPEG:
+    case PS_META_PNG:
+    case PS_META_ASTROM:
+    case PS_META_UNKNOWN:
+    default:
+        psError( __func__, "Invalid psMetadataType: %d", type );
+    }
+
     // Allocate and set metadata item name
     metadataItem->name = ( char * ) psAlloc( sizeof( char ) * MAX_STRING_LENGTH );
     vsprintf( metadataItem->name, name, argPtr );
-    
+
     // Allocate metadata items with same name.
     metadataItem->items = psListAlloc( NULL );
-    
+
     return metadataItem;
 }
@@ -190,28 +190,28 @@
     psHash *table = NULL;
     psMetadata *metadata = NULL;
-    
+
     // Allocate metadata
     metadata = ( psMetadata * ) psAlloc( sizeof( psMetadata ) );
     if ( metadata == NULL ) {
-            psAbort( __func__, "Failed to allocate metadata" );
-        }
-        
+        psAbort( __func__, "Failed to allocate metadata" );
+    }
+
     // Set deallocator
     p_psMemSetDeallocator( metadata, ( psFreeFcn ) metadataFree );
-    
+
     // Allocate metadata's internal containers
     list = ( psList * ) psListAlloc( NULL );
     if ( list == NULL ) {
-            psAbort( __func__, "Failed to allocate list" );
-        }
-        
+        psAbort( __func__, "Failed to allocate list" );
+    }
+
     table = ( psHash * ) psHashAlloc( 10 );
     if ( table == NULL ) {
-            psAbort( __func__, "Failed to allocate table" );
-        }
-        
+        psAbort( __func__, "Failed to allocate table" );
+    }
+
     metadata->list = list;
     metadata->table = table;
-    
+
     return metadata;
 }
@@ -224,48 +224,49 @@
     psMetadataItem *value = NULL;
     psMetadataType type = PS_META_ITEM_SET;
-    
+
     if ( md == NULL ) {
-            psError( __func__, "Null metadata collection not allowed" );
-            return false;
-        }
-        
+        psError( __func__, "Null metadata collection not allowed" );
+        return false;
+    }
+
     if ( metadataItem == NULL ) {
-            psError( __func__, "Null metadata item not allowed" );
-            return false;
-        }
-        
+        psError( __func__, "Null metadata item not allowed" );
+        return false;
+    }
+
     type = metadataItem->type;
-    
+
     mdTable = md->table;
     if ( mdTable == NULL ) {
-            psError( __func__, "Null metadata table not allowed" );
-            return false;
-        }
-        
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return false;
-        }
-        
+        psError( __func__, "Null metadata table not allowed" );
+        return false;
+    }
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return false;
+    }
+
     key = metadataItem->name;
     if ( key == NULL ) {
-            psError( __func__, "Null key item not allowed" );
-            return false;
-        }
-        
+        psError( __func__, "Null key item not allowed" );
+        return false;
+    }
+
     // Check if key is already in table
     value = ( psMetadataItem* ) psHashLookup( mdTable, key );
     if ( value != NULL && type != PS_META_ITEM_SET ) {
-    
-            // The key was found and the new metadata item is a leaf node (its type isn't PS_META_ITEM_SET), so
-            // add the new metadata item to hash as a child of the existing metadata item folder node.
-            if ( !psListAdd( value->items, metadataItem, where ) ) {
-                    psError( __func__, "Couldn't add metadata item to items list. Name: %s",
-                             metadataItem->name );
-                    return false;
-                }
-        } else if ( value != NULL ) {
-        
+
+        // The key was found and the new metadata item is a leaf node (its type isn't PS_META_ITEM_SET), so
+        // add the new metadata item to hash as a child of the existing metadata item folder node.
+        if ( !psListAdd( value->items, metadataItem, where ) ) {
+            psError( __func__, "Couldn't add metadata item to items list. Name: %s",
+                     metadataItem->name );
+            return false;
+        }
+    } else
+        if ( value != NULL ) {
+
             // The key was found and the new metadata item is a folder node. Don't add new metadata item, since
             // it will wipe out existing node.
@@ -274,20 +275,20 @@
             return false;
         } else {
-        
+
             // Duplicate key not found. Add new metadata item to metadata collection's hash
             if ( !psHashAdd( mdTable, key, metadataItem ) ) {
-                    psError( __func__, "Couldn't add metadata item to metadata collection table. Name: %s",
-                             metadataItem->name );
-                    return false;
-                }
-        }
-        
+                psError( __func__, "Couldn't add metadata item to metadata collection table. Name: %s",
+                         metadataItem->name );
+                return false;
+            }
+        }
+
     // Add all items to metadata collection's list, even if they have the same metadata item names
     if ( !psListAdd( md->list, metadataItem, where ) ) {
-            psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
-                     metadataItem->name );
-            return false;
-        }
-        
+        psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
+                 metadataItem->name );
+        return false;
+    }
+
     return true;
 }
@@ -298,19 +299,19 @@
     va_list argPtr;
     psMetadataItem *metadataItem = NULL;
-    
+
     va_start( argPtr, comment );
     metadataItem = psMetadataItemAllocV( name, type, comment, argPtr );
     va_end( argPtr );
-    
+
     if ( !psMetadataAddItem( md, where, metadataItem ) ) {
-            psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
-                     metadataItem->name );
-            psFree( metadataItem );
-            return false;
-        }
-        
+        psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
+                 metadataItem->name );
+        psFree( metadataItem );
+        return false;
+    }
+
     // Decrement reference count, since the metadata item is now in metadata collection and no longer needed here
     psMemDecrRefCounter( metadataItem );
-    
+
     return true;
 }
@@ -323,72 +324,72 @@
     psMetadataItem *entry = NULL;
     psMetadataItem *entryChild = NULL;
-    
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return false;
-        }
-        
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return false;
+    }
+
     mdTable = md->table;
     if ( mdTable == NULL ) {
-            psError( __func__, "Null metadata table not allowed" );
-            return false;
-        }
-        
+        psError( __func__, "Null metadata table not allowed" );
+        return false;
+    }
+
     // Select removal by key or index
     if ( key != NULL ) {
-    
-            // Remove by key name
-            entry = ( psMetadataItem* ) psHashLookup( mdTable, key );
-            if ( entry == NULL ) {
-                    psError( __func__, "Couldn't find metadata item remove. Name: %s", key );
-                    return false;
-                }
-                
-            numChildren = entry->items->size;
-            if ( entry->type == PS_META_ITEM_SET && numChildren > 0 ) {
-            
-                    // Table entry has children. Entry and children must be removed from metadata collection's list
-                    psListSetIterator( mdList, PS_LIST_HEAD );
-                    entryChild = psListGetCurrent( mdList );
-                    while ( entryChild != NULL ) {
-                            if ( !psListRemove( entry->items, entryChild, PS_LIST_UNKNOWN ) ) {
-                                    psError( __func__, "Couldn't remove metadata item from list. Name: %s", key );
-                                    return false;
-                                }
-                            entryChild = psListGetNext( entry->items );
-                        }
-                }
-                
-            // Remove entry from metadata collection's list
-            if ( !psListRemove( mdList, entry, PS_LIST_UNKNOWN ) ) {
+
+        // Remove by key name
+        entry = ( psMetadataItem* ) psHashLookup( mdTable, key );
+        if ( entry == NULL ) {
+            psError( __func__, "Couldn't find metadata item remove. Name: %s", key );
+            return false;
+        }
+
+        numChildren = entry->items->size;
+        if ( entry->type == PS_META_ITEM_SET && numChildren > 0 ) {
+
+            // Table entry has children. Entry and children must be removed from metadata collection's list
+            psListSetIterator( mdList, PS_LIST_HEAD );
+            entryChild = psListGetCurrent( mdList );
+            while ( entryChild != NULL ) {
+                if ( !psListRemove( entry->items, entryChild, PS_LIST_UNKNOWN ) ) {
                     psError( __func__, "Couldn't remove metadata item from list. Name: %s", key );
                     return false;
                 }
-                
-            // Remove entry from metadata collection's table
-            if ( !psHashRemove( mdTable, key ) ) {
-                    psError( __func__, "Couldn't remove metadata item from table. Name: %s", key );
-                    return false;
-                }
-        } else {
-        
-            // Remove by index
-            entry = psListGet( mdList, where );
-            if ( entry == NULL ) {
-                    psError( __func__, "Couldn't find metadata item from list. Index: %d", where );
-                    return false;
-                }
-                
-            key = entry->name;
-            if ( key == NULL ) {
-                    psError( __func__, "Null key name not allowed. Index: %d", where );
-                    return false;
-                }
-                
-            // Use recursive remove, now that key is known
-            psMetadataRemove( md, PS_LIST_UNKNOWN, key );
-        }
-        
+                entryChild = psListGetNext( entry->items );
+            }
+        }
+
+        // Remove entry from metadata collection's list
+        if ( !psListRemove( mdList, entry, PS_LIST_UNKNOWN ) ) {
+            psError( __func__, "Couldn't remove metadata item from list. Name: %s", key );
+            return false;
+        }
+
+        // Remove entry from metadata collection's table
+        if ( !psHashRemove( mdTable, key ) ) {
+            psError( __func__, "Couldn't remove metadata item from table. Name: %s", key );
+            return false;
+        }
+    } else {
+
+        // Remove by index
+        entry = psListGet( mdList, where );
+        if ( entry == NULL ) {
+            psError( __func__, "Couldn't find metadata item from list. Index: %d", where );
+            return false;
+        }
+
+        key = entry->name;
+        if ( key == NULL ) {
+            psError( __func__, "Null key name not allowed. Index: %d", where );
+            return false;
+        }
+
+        // Use recursive remove, now that key is known
+        psMetadataRemove( md, PS_LIST_UNKNOWN, key );
+    }
+
     return true;
 }
@@ -398,22 +399,22 @@
     psHash * mdTable = NULL;
     psMetadataItem *entry = NULL;
-    
+
     mdTable = md->table;
     if ( mdTable == NULL ) {
-            psError( __func__, "Null metadata table not allowed" );
-            return NULL;
-        }
-        
+        psError( __func__, "Null metadata table not allowed" );
+        return NULL;
+    }
+
     if ( key == NULL ) {
-            psError( __func__, "Null key name not allowed" );
-            return NULL;
-        }
-        
+        psError( __func__, "Null key name not allowed" );
+        return NULL;
+    }
+
     entry = ( psMetadataItem* ) psHashLookup( mdTable, key );
     if ( entry == NULL ) {
-            psError( __func__, "Could not find metadata item with given key. Key: %s", key );
-            return NULL;
-        }
-        
+        psError( __func__, "Could not find metadata item with given key. Key: %s", key );
+        return NULL;
+    }
+
     return entry;
 }
@@ -423,17 +424,17 @@
     psList * mdList = NULL;
     psMetadataItem *entry = NULL;
-    
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return NULL;
-        }
-        
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return NULL;
+    }
+
     entry = ( psMetadataItem* ) psListGet( mdList, where );
     if ( entry == NULL ) {
-            psError( __func__, "Couldn't find metadata item with given index. Index: %d", where );
-            return NULL;
-        }
-        
+        psError( __func__, "Couldn't find metadata item with given index. Index: %d", where );
+        return NULL;
+    }
+
     return entry;
 }
@@ -442,13 +443,13 @@
 {
     psList * mdList = NULL;
-    
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return false;
-        }
-        
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return false;
+    }
+
     psListSetIterator( mdList, where );
-    
+
     return true;
 }
@@ -458,33 +459,33 @@
     psList * mdList = NULL;
     psMetadataItem *entry = NULL;
-    
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return NULL;
-        }
-        
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return NULL;
-        }
-        
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return NULL;
+    }
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return NULL;
+    }
+
     psListSetIterator( mdList, which );
     entry = psListGetCurrent( mdList );
     while ( entry != NULL ) {
-            if ( !strncmp( match, entry->name, strlen( match ) ) ) {
-            
-                    // Match found
-                    return entry;
-                }
-            entry = psListGetNext( mdList );
-        }
-        
+        if ( !strncmp( match, entry->name, strlen( match ) ) ) {
+
+            // Match found
+            return entry;
+        }
+        entry = psListGetNext( mdList );
+    }
+
     // Match not found
     if ( entry == NULL ) {
-            psError( __func__, "Couldn't find metadata item with given match. Match: %s", match );
-        }
-        
+        psError( __func__, "Couldn't find metadata item with given match. Match: %s", match );
+    }
+
     return entry;
 }
@@ -494,33 +495,33 @@
     psList * mdList = NULL;
     psMetadataItem *entry = NULL;
-    
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return NULL;
-        }
-        
-    mdList = md->list;
-    if ( mdList == NULL ) {
-            psError( __func__, "Null metadata list not allowed" );
-            return NULL;
-        }
-        
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return NULL;
+    }
+
+    mdList = md->list;
+    if ( mdList == NULL ) {
+        psError( __func__, "Null metadata list not allowed" );
+        return NULL;
+    }
+
     psListSetIterator( mdList, which );
     entry = psListGetCurrent( mdList );
     while ( entry != NULL ) {
-            if ( !strncmp( match, entry->name, strlen( match ) ) ) {
-            
-                    // Match found
-                    return entry;
-                }
-            entry = psListGetPrevious( mdList );
-        }
-        
+        if ( !strncmp( match, entry->name, strlen( match ) ) ) {
+
+            // Match found
+            return entry;
+        }
+        entry = psListGetPrevious( mdList );
+    }
+
     // Match not found
     if ( entry == NULL ) {
-            psError( __func__, "Couldn't find metadata item with given match. Match: %s", match );
-        }
-        
+        psError( __func__, "Couldn't find metadata item with given match. Match: %s", match );
+    }
+
     return entry;
 }
@@ -529,47 +530,47 @@
 {
     psMetadataType type;
-    
+
     if ( fd == NULL ) {
-            psError( __func__, "Null file descriptor not allowed" );
-            return ;
-        }
-        
+        psError( __func__, "Null file descriptor not allowed" );
+        return ;
+    }
+
     if ( format == NULL ) {
-            psError( __func__, "Null format not allowed" );
-            return ;
-        }
-        
+        psError( __func__, "Null format not allowed" );
+        return ;
+    }
+
     if ( metadataItem == NULL ) {
-            psError( __func__, "Null metadata not allowed" );
-            return ;
-        }
-        
+        psError( __func__, "Null metadata not allowed" );
+        return ;
+    }
+
     type = metadataItem->type;
-    
+
     switch ( type ) {
-            case PS_META_BOOL:
-            fprintf( fd, format, metadataItem->data.B );
-            break;
-            case PS_META_S32:
-            fprintf( fd, format, metadataItem->data.S32 );
-            break;
-            case PS_META_F32:
-            fprintf( fd, format, metadataItem->data.F32 );
-            break;
-            case PS_META_F64:
-            fprintf( fd, format, metadataItem->data.F64 );
-            break;
-            case PS_META_STR:
-            fprintf( fd, format, metadataItem->data.V );
-            break;
-            case PS_META_ITEM_SET:
-            case PS_META_IMG:
-            case PS_META_JPEG:
-            case PS_META_PNG:
-            case PS_META_ASTROM:
-            case PS_META_UNKNOWN:
-            default:
-            psError( __func__, " Invalid psMetadataType to print: %d", type );
-        }
+    case PS_META_BOOL:
+        fprintf( fd, format, metadataItem->data.B );
+        break;
+    case PS_META_S32:
+        fprintf( fd, format, metadataItem->data.S32 );
+        break;
+    case PS_META_F32:
+        fprintf( fd, format, metadataItem->data.F32 );
+        break;
+    case PS_META_F64:
+        fprintf( fd, format, metadataItem->data.F64 );
+        break;
+    case PS_META_STR:
+        fprintf( fd, format, metadataItem->data.V );
+        break;
+    case PS_META_ITEM_SET:
+    case PS_META_IMG:
+    case PS_META_JPEG:
+    case PS_META_PNG:
+    case PS_META_ASTROM:
+    case PS_META_UNKNOWN:
+    default:
+        psError( __func__, " Invalid psMetadataType to print: %d", type );
+    }
 }
 
@@ -590,100 +591,102 @@
     int keyNum = 0;
     psMetadataType metadataItemType;
-    
+
     if ( fd == NULL ) {
-            psError( __func__, "Null FITS file descriptor not allowed" );
-            return NULL;
-        }
-        
+        psError( __func__, "Null FITS file descriptor not allowed" );
+        return NULL;
+    }
+
     if ( extname == NULL && extnum == 0 ) {
-            psError( __func__, "Null extname AND extnum = 0 not allowed" );
-            return NULL;
-        } else if ( extname && extnum ) {
+        psError( __func__, "Null extname AND extnum = 0 not allowed" );
+        return NULL;
+    } else
+        if ( extname && extnum ) {
             psError( __func__, "Both extname AND extnum arguments should not have values." );
             return NULL;
         }
-        
+
     // Allocate metadata if user didn't
     if ( output == NULL ) {
-            output = psMetadataAlloc();
-        }
-        
+        output = psMetadataAlloc();
+    }
+
     // Move to user designated HDU number or HDU name in FITS file. HDU numbers starts at one.
     if ( extname == NULL ) {
-            if ( fits_movabs_hdu( fd, extnum, &hduType, &status ) != 0 ) {
-                    fits_get_errstatus( status, fitsErr );
-                    psError( __func__, "FITS error while locating header: %s", fitsErr );
-                    status = 0;
-                    return output;
-                }
-        } else if ( extnum > 0 ) {
+        if ( fits_movabs_hdu( fd, extnum, &hduType, &status ) != 0 ) {
+            fits_get_errstatus( status, fitsErr );
+            psError( __func__, "FITS error while locating header: %s", fitsErr );
+            status = 0;
+            return output;
+        }
+    } else
+        if ( extnum > 0 ) {
             tempExtname = ( char* ) extname;
             if ( fits_movnam_hdu( fd, ANY_HDU, tempExtname, 0, &status ) != 0 ) {
-                    fits_get_errstatus( status, fitsErr );
-                    psError( __func__, "FITS error while locating header: %s", fitsErr );
-                    status = 0;
-                    return output;
-                }
-        }
-        
+                fits_get_errstatus( status, fitsErr );
+                psError( __func__, "FITS error while locating header: %s", fitsErr );
+                status = 0;
+                return output;
+            }
+        }
+
     // Get number of key names
     if ( fits_get_hdrpos( fd, &numKeys, &keyNum, &status ) != 0 ) {
+        fits_get_errstatus( status, fitsErr );
+        psError( __func__, "FITS error while reading keys: %s", fitsErr );
+        status = 0;
+        return output;
+    }
+
+    // Get each key name. Keywords start at one.
+    for ( i = 1; i <= numKeys; i++ ) {
+        if ( fits_read_keyn( fd, i, keyName, keyValue, keyComment, &status ) != 0 ) {
             fits_get_errstatus( status, fitsErr );
-            psError( __func__, "FITS error while reading keys: %s", fitsErr );
+            psError( __func__, "FITS error while reading key number %d: %s", i, fitsErr );
             status = 0;
             return output;
         }
-        
-    // Get each key name. Keywords start at one.
-    for ( i = 1; i <= numKeys; i++ ) {
-            if ( fits_read_keyn( fd, i, keyName, keyValue, keyComment, &status ) != 0 ) {
-                    fits_get_errstatus( status, fitsErr );
-                    psError( __func__, "FITS error while reading key number %d: %s", i, fitsErr );
-                    status = 0;
-                    return output;
-                }
-            if ( fits_get_keytype( keyValue, &keyType, &status ) != 0 ) {
-                    fits_get_errstatus( status, fitsErr );
-                    if ( status != VALUE_UNDEFINED ) {
-                            psError( __func__, "FITS error while determining key type: %s", fitsErr );
-                            status = 0;
-                            return output;
-                        } else {
-                            // Some keywords are still valid even though they don't have a type or value, like COMMENTS
-                            keyType = 'C';
-                            status = 0;
-                        }
-                }
-                
-            switch ( keyType ) {
-                    case 'I':
-                    metadataItemType = PS_META_S32;
-                    success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, atoi( keyValue ) );
-                    break;
-                    case 'F':
-                    metadataItemType = PS_META_F64;
-                    success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, atof( keyValue ) );
-                    break;
-                    case 'C':
-                    metadataItemType = PS_META_STR;
-                    success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, keyValue );
-                    break;
-                    case 'L':
-                    metadataItemType = PS_META_BOOL;
-                    tempBool = ( keyValue[ 0 ] == 'T' ) ? 1 : 0;
-                    success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, tempBool );
-                    break;
-                    case 'U':
-                    case 'X':
-                    default:
-                    psError( __func__, "Invalid psMetadataType: %c", keyType );
-                    return output;
-                }
-                
-            if ( !success ) {
-                    psError( __func__, "Failed to add metadata item. Name: %s", keyName );
-                }
-        }
-        
+        if ( fits_get_keytype( keyValue, &keyType, &status ) != 0 ) {
+            fits_get_errstatus( status, fitsErr );
+            if ( status != VALUE_UNDEFINED ) {
+                psError( __func__, "FITS error while determining key type: %s", fitsErr );
+                status = 0;
+                return output;
+            } else {
+                // Some keywords are still valid even though they don't have a type or value, like COMMENTS
+                keyType = 'C';
+                status = 0;
+            }
+        }
+
+        switch ( keyType ) {
+        case 'I':
+            metadataItemType = PS_META_S32;
+            success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, atoi( keyValue ) );
+            break;
+        case 'F':
+            metadataItemType = PS_META_F64;
+            success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, atof( keyValue ) );
+            break;
+        case 'C':
+            metadataItemType = PS_META_STR;
+            success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, keyValue );
+            break;
+        case 'L':
+            metadataItemType = PS_META_BOOL;
+            tempBool = ( keyValue[ 0 ] == 'T' ) ? 1 : 0;
+            success = psMetadataAdd( output, PS_LIST_HEAD, keyName, metadataItemType, keyComment, tempBool );
+            break;
+        case 'U':
+        case 'X':
+        default:
+            psError( __func__, "Invalid psMetadataType: %c", keyType );
+            return output;
+        }
+
+        if ( !success ) {
+            psError( __func__, "Failed to add metadata item. Name: %s", keyName );
+        }
+    }
+
     return output;
 }
