Index: trunk/psLib/test/fits/tst_psFits.c
===================================================================
--- trunk/psLib/test/fits/tst_psFits.c	(revision 5168)
+++ trunk/psLib/test/fits/tst_psFits.c	(revision 5511)
@@ -6,6 +6,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-09-28 20:02:49 $
+*  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-11-14 22:18:38 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -41,5 +41,5 @@
 static psS32 testImageWrite(void);
 
-static psS32 tst_psFitsAlloc( void );
+static psS32 tst_psFitsOpen( void );
 static psS32 tst_psFitsMoveExtName( void ); // also tests psFitsGetExtName
 static psS32 tst_psFitsMoveExtNum( void );  // also tests psFitsGetExtNum, psFitsGetSize
@@ -53,5 +53,5 @@
 
 testDescription tests[] = {
-                              {tst_psFitsAlloc, 801, "psFitsAlloc", 0, false},
+                              {tst_psFitsOpen, 801, "psFitsOpen", 0, false},
                               {tst_psFitsMoveExtName, 802, "psFitsMoveExtName", 0, false},
                               {tst_psFitsMoveExtName, 802, "psFitsGetExtName", 0, true},
@@ -80,9 +80,5 @@
 bool makeMulti(void)
 {
-
-    remove
-        (multiFilename);
-
-    psFits* fitsFile = psFitsAlloc(multiFilename);
+    psFits* fitsFile = psFitsOpen(multiFilename,"w");
 
     if (fitsFile == NULL) {
@@ -145,9 +141,5 @@
 bool makeTable(void)
 {
-
-    remove
-        (tableFilename);
-
-    psFits* fitsFile = psFitsAlloc(tableFilename);
+    psFits* fitsFile = psFitsOpen(tableFilename,"w");
 
     if (fitsFile == NULL) {
@@ -223,5 +215,5 @@
 }
 
-psS32 tst_psFitsAlloc( void )
+psS32 tst_psFitsOpen( void )
 {
 
@@ -230,9 +222,9 @@
     }
 
-    psFits* fitsFile = psFitsAlloc(multiFilename);
+    psFits* fitsFile = psFitsOpen(multiFilename,"r");
 
     if (fitsFile == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -241,10 +233,10 @@
     if (extNum != 0) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc was not queued to the PHU, but to extension #%d.",
+                "psFitsOpen was not queued to the PHU, but to extension #%d.",
                 extNum);
         return 2;
     }
 
-    psFree(fitsFile);
+    psFitsClose(fitsFile);
 
     // make sure the file doesn't already exist.
@@ -258,29 +250,95 @@
     }
 
-    fitsFile = psFitsAlloc("new.fits");
+    fitsFile = psFitsOpen("new.fits","w");
 
     if (fitsFile == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on w mode.");
         return 4;
     }
 
-    psFree(fitsFile);
-
-    // now, if psFitsAlloc actually created the file, I shouldn't error in removing it.
+    // write something to the file, otherwise CFITSIO will complain on close
+    psImage* img = psImageAlloc(16,16,PS_TYPE_F32);
+    psFitsWriteImage(fitsFile,NULL,img,1);
+
+    psFree(fitsFile); // psFree should be equivalent to psFitsClose
+
+    // now, if psFitsOpen actually created the file, I shouldn't error in removing it.
     if (remove
             ("new.fits") != 0) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc seemed to not have created a new file.");
+                "psFitsOpen seemed to not have created a new file.");
         return 5;
+    }
+
+    fitsFile = psFitsOpen("new.fits","w+");
+
+    if (fitsFile == NULL) {
+        psError(PS_ERR_UNKNOWN, false,
+                "psFitsOpen returned NULL on w+ mode.");
+        return 6;
+    }
+
+    // write something to the file, otherwise CFITSIO will complain on close
+    psFitsWriteImage(fitsFile,NULL,img,1);
+
+    psFitsClose(fitsFile);
+
+    // now, if psFitsOpen actually created the file, I shouldn't error in removing it.
+    if (remove
+            ("new.fits") != 0) {
+        psError(PS_ERR_UNKNOWN, false,
+                "psFitsOpen seemed to not have created a new file.");
+        return 7;
+    }
+
+    fitsFile = psFitsOpen("new.fits","a");
+
+    if (fitsFile == NULL) {
+        psError(PS_ERR_UNKNOWN, false,
+                "psFitsOpen returned NULL on a mode.");
+        return 8;
+    }
+
+    // write something to the file, otherwise CFITSIO will complain on close
+    psFitsWriteImage(fitsFile,NULL,img,1);
+
+    psFitsClose(fitsFile);
+
+    fitsFile = psFitsOpen("new.fits","a+");
+
+    if (fitsFile == NULL) {
+        psError(PS_ERR_UNKNOWN, false,
+                "psFitsOpen returned NULL on a+ mode.");
+        return 9;
+    }
+
+    psFitsClose(fitsFile);
+
+    // now, if psFitsOpen actually created the file, I shouldn't error in removing it.
+    if (remove
+            ("new.fits") != 0) {
+        psError(PS_ERR_UNKNOWN, false,
+                "psFitsOpen seemed to not have created a new file.");
+        return 10;
     }
 
     // Attempt to allocate with NULL filename
     psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message");
-    fitsFile = psFitsAlloc(NULL);
+    fitsFile = psFitsOpen(NULL,"r");
     if(fitsFile != NULL) {
-        psError(PS_ERR_UNKNOWN,true,"psFitsAlloc did not return NULL for NULL input");
-        return 5;
-    }
+        psError(PS_ERR_UNKNOWN,true,"psFitsOpen did not return NULL for NULL input");
+        return 11;
+    }
+
+    // Attempt to use an invalid mode
+    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message");
+    fitsFile = psFitsOpen("new.fits","b+");
+    if(fitsFile != NULL) {
+        psError(PS_ERR_UNKNOWN,true,"psFitsOpen did not return NULL for NULL input");
+        return 12;
+    }
+
+    psFree(img);
 
     return 0;
@@ -294,9 +352,9 @@
     }
 
-    psFits* fits = psFitsAlloc(multiFilename);
+    psFits* fits = psFitsOpen(multiFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -422,9 +480,9 @@
     }
 
-    psFits* fits = psFitsAlloc(multiFilename);
+    psFits* fits = psFitsOpen(multiFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -613,5 +671,5 @@
     }
 
-    psFree(fits);
+    psFitsClose(fits);
 
     // Attempt to get ext name from null fits file
@@ -631,9 +689,9 @@
     }
 
-    psFits* fits = psFitsAlloc(multiFilename);
+    psFits* fits = psFitsOpen(multiFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -741,9 +799,9 @@
     }
 
-    psFits* fits = psFitsAlloc(multiFilename);
+    psFits* fits = psFitsOpen(multiFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -852,5 +910,5 @@
     // set2 should have been freed by psFitsReadHeaderSet upon an error.
 
-    psFree(fits);
+    psFitsClose(fits);
 
     return 0;
@@ -865,9 +923,9 @@
     }
 
-    psFits* fits = psFitsAlloc(tableFilename);
+    psFits* fits = psFitsOpen(tableFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -977,9 +1035,9 @@
     }
 
-    psFits* fits = psFitsAlloc(tableFilename);
+    psFits* fits = psFitsOpen(tableFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -1079,9 +1137,9 @@
     }
 
-    psFits* fits = psFitsAlloc(tableFilename);
+    psFits* fits = psFitsOpen(tableFilename,"r");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 1;
     }
@@ -1206,9 +1264,9 @@
     }
 
-    psFits* fits = psFitsAlloc(tableFilename);
+    psFits* fits = psFitsOpen(tableFilename,"rw");
 
     if (fits == NULL) {
         psError(PS_ERR_UNKNOWN, false,
-                "psFitsAlloc returned NULL on existing file.");
+                "psFitsOpen returned NULL on existing file.");
         return 2;
     }
@@ -1332,5 +1390,5 @@
     psFree(err);
     psFree(md);
-    psFree(fits);
+    psFitsClose(fits);
 
     return 0;
@@ -1374,6 +1432,5 @@
         psImageClip(img3,32.0,32.0,120.0,120.0); \
         img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \
-        remove(filename); \
-        psFits* fits = psFitsAlloc(filename); \
+        psFits* fits = psFitsOpen(filename, "w"); \
         psRegion region = {0,0,0,0}; \
         if (! psFitsWriteImage(fits, NULL, img, 2)) { \
@@ -1398,5 +1455,5 @@
         psFree(img3); \
         img3 = NULL; \
-        fits = psFitsAlloc(filename); \
+        fits = psFitsOpen(filename,"r"); \
         psRegion reg = {readM0, readM, readN0, readN}; \
         img = psFitsReadImage(img, fits, reg, 0); \
@@ -1536,10 +1593,6 @@
     GENIMAGE(img2,m,n,F32,row+2*col);
     mkdir("tmpImages",0777);
-    remove
-        ("tmpImages/writeTest.fits");
-    psRegion region = {
-                          0,0,0,0
-                      };
-    psFits* fits = psFitsAlloc("tmpImages/writeTest.fits");
+    psRegion region = {0,0,0,0};
+    psFits* fits = psFitsOpen("tmpImages/writeTest.fits","w");
 
     if (! psFitsWriteImage(fits, NULL, img,1)) {
@@ -1555,6 +1608,6 @@
 
     // Did it really overwrite the pixel values?  Let's read it in and see.
-    psFree(fits);
-    fits = psFitsAlloc("tmpImages/writeTest.fits");
+    psFitsClose(fits);
+    fits = psFitsOpen("tmpImages/writeTest.fits","r");
     img = psFitsReadImage(NULL, fits, region, 0);
     if (img == NULL) {
@@ -1597,5 +1650,5 @@
 
     psMetadata* header   = psMetadataAlloc();
-    psFits*     fitsFile = psFitsAlloc(multiFilename);
+    psFits*     fitsFile = psFitsOpen(multiFilename,"a+");
 
     // Test psFitsReadWrite generates files from psFitsWriteImage which calls psFitsWriteHeader
