Index: trunk/psLib/src/astronomy/psMetadata.c
===================================================================
--- trunk/psLib/src/astronomy/psMetadata.c	(revision 3381)
+++ trunk/psLib/src/astronomy/psMetadata.c	(revision 3407)
@@ -12,6 +12,6 @@
 *  @author Ross Harman, MHPCC
 *
-*  @version $Revision: 1.54 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-03-07 20:58:50 $
+*  @version $Revision: 1.55 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-03-11 20:38:56 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -67,4 +67,32 @@
 /*  FUNCTION IMPLEMENTATION - LOCAL                                          */
 /*****************************************************************************/
+
+static psMetadataItem* makeMetaMulti(psHash* table, const char* key, psMetadataItem* existing)
+{
+
+    if (existing != NULL && existing->type == PS_META_MULTI) {
+        return existing;
+    }
+
+    // move any existing entry into a psList
+    psList* newList = psListAlloc(existing);
+
+    psMetadataItem* item = psMetadataItemAlloc(key,
+                           PS_META_MULTI,
+                           "",
+                           newList);
+
+    if (existing != NULL) {
+        psHashRemove(table,key); // take out the old entry
+    }
+
+    psHashAdd(table, key, item); // put in the new entry
+
+    // free local references of newly allocated items.
+    psFree(newList);
+    psFree(item);
+
+    return item;
+}
 
 static void metadataItemFree(psMetadataItem* metadataItem)
@@ -253,27 +281,31 @@
     existingEntry = (psMetadataItem*)psHashLookup(mdTable, key);
 
-    // how the item is added to the hash depends on flags & prior existence
-    if(existingEntry != NULL) {
-        if ((flags & PS_META_DUPLICATE_OK) != 0) {
+    if (metadataItem->type == PS_META_MULTI) {
+        // the incoming entry is PS_META_MULTI
+
+        // force the hash entry to be PS_META_MULTI
+        existingEntry = makeMetaMulti(mdTable,key,existingEntry);
+
+        // add all the items in the incoming entry to metadata
+        psList* list = metadataItem->data.list;
+        if (list != NULL) {
+            psListIterator* iter = psListIteratorAlloc(list,PS_LIST_HEAD,true);
+            psMetadataItem* listItem;
+            while ((listItem=(psMetadataItem*)psListGetAndIncrement(iter)) != NULL) {
+                psMetadataAddItem(md,listItem,location,flags);
+            }
+            psFree(iter);
+        }
+
+        return true; // all done.
+    }
+
+    // how the item is added to the hash depends on prior existence, flags, etc.
+    if(existingEntry != NULL) { // prior existence
+        if (existingEntry->type == PS_META_MULTI || (flags & PS_META_DUPLICATE_OK) != 0) {
             // duplicate entries allowed - add another entry.
-            if (existingEntry->type != PS_META_MULTI) {
-                // first duplicate, transfer the hash's old entry into a
-                // list of entries with the type PS_META_MULTI.
-
-                // add entry to a list and dereference the local pointer
-                psList* newList = psListAlloc(existingEntry);
-
-                existingEntry = psMetadataItemAlloc(key,
-                                                    PS_META_MULTI,
-                                                    "",
-                                                    newList);
-                psHashRemove(mdTable,key); // take out the old entry
-                psHashAdd(mdTable, key, existingEntry); // put in the new entry
-
-                // free local references of newly allocated items.
-                psFree(newList);
-                psFree(existingEntry);
-
-            }
+
+            // make sure the existing entry is PS_META_MULTI
+            existingEntry = makeMetaMulti(mdTable,key,existingEntry);
 
             // add to the hash's list of duplicate entries
@@ -282,12 +314,9 @@
                 return false;
             }
-            // (added to list below)
-
         } else if ((flags & PS_META_REPLACE) != 0) {
             // replace entry instead of creating a duplicate entry.
 
             // remove the existing entry from metadata
-            psListRemoveData(mdList, existingEntry);
-            psHashRemove(mdTable, key);
+            psMetadataRemove(md,0,key);
 
             // treat as if new (added to list below)
@@ -423,11 +452,4 @@
             // multiple entries with same key, remove just the specified one
             psListRemoveData(tableItem->data.list, entry);
-            if (psListGet(tableItem->data.list,PS_LIST_HEAD) == NULL) {
-                // list is empty, so let's clear the whole entry now.
-                if (!psHashRemove(mdTable, key)) {
-                    psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psMetadata_REMOVE_TABLE_FAILED, key);
-                    return false;
-                }
-            }
         } else {
             if (!psHashRemove(mdTable, key)) {
