Index: /trunk/Ohana/src/imregister/imreg/db.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/db.c	(revision 3)
+++ /trunk/Ohana/src/imregister/imreg/db.c	(revision 4)
@@ -11,5 +11,6 @@
 RegImage *image;
 int Nimage;
-int lockstate, dbstate;
+int lockstate;
+int dbstate = LCK_UNLOCK;
 
 RegImage *get_images (int *N) {
@@ -44,16 +45,15 @@
   /* lock database (soft) */
   f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
-  if (dbstate == LCK_EMPTY) return (0);
-
   if (f == (FILE *) NULL) {
     fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
     exit (1);
   }
+  if (dbstate == LCK_EMPTY) return (0);
 
   /* load in table data */
   table.header = &theader;
-  fits_fread_header (f, &header);
-  fits_fread_matrix (f, &matrix, &header);
-  fits_fread_ftable  (f, &table, "IMAGE_DATABASE");
+  if (!fits_fread_header (f, &header))                     escape (UNLOCK, "ERROR: can't read primary header"); 
+  if (!fits_fread_matrix (f, &matrix, &header))            escape (UNLOCK, "ERROR: can't read primary header");
+  if (!fits_fread_ftable  (f, &table, "IMAGE_DATABASE"))   escape (UNLOCK, "ERROR: can't read primary header");
 
   /* convert to internal format */
@@ -63,5 +63,7 @@
   Nimage = Ny;
 
-  ConvertStruct ((char *) image, sizeof (RegImage), Nimage, "regimage");
+  if (!ConvertStruct ((char *) image, sizeof (RegImage), Nimage, "regimage"))
+    escape (UNLOCK, "ERROR: can't read primary header"); 
+
   return (1);
 }
@@ -70,13 +72,14 @@
 int save_db () {
 
-  ConvertStruct ((char *) image, sizeof (RegImage), Nimage, "regimage");
+  if (!ConvertStruct ((char *) image, sizeof (RegImage), Nimage, "regimage")) 
+    escape (UNLOCK, "ERROR: can't convert from native format");
 
   /* write all images to file */
   make_backup (dBFile);
   Fseek (f, 0, SEEK_SET);
-  fits_fwrite_header (f, &header);
-  fits_fwrite_matrix (f, &matrix);
-  fits_fwrite_Theader (f, &theader);
-  fits_fwrite_table   (f, &table);
+  if (!fits_fwrite_header (f, &header))   escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_matrix (f, &matrix))   escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
   fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
@@ -114,8 +117,8 @@
   make_backup (dBFile);
   Fseek (f, 0, SEEK_SET);
-  fits_fwrite_header (f, &header);
-  fits_fwrite_matrix (f, &matrix);
-  fits_fwrite_Theader (f, &theader);
-  fits_fwrite_vtable   (f, &vtable);
+  if (!fits_fwrite_header (f, &header))   escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_matrix (f, &matrix))   escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
+  if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't update db");
   fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
@@ -126,5 +129,6 @@
   VTable vtable;
 
-  fits_table_to_vtable (&table, &vtable);
+  /* add error checks! */
+  fits_table_to_vtable (&table, &vtable, 0, 0);
 
   ConvertStruct ((char *) new, sizeof (RegImage), Nnew, "regimage");
@@ -133,8 +137,9 @@
   /* write subset to file */
   Fseek (f, 0, SEEK_SET);
-  fits_fwrite_header (f, &header);
-  fits_fwrite_matrix (f, &matrix);
-  fits_fwrite_Theader (f, &theader);
-  fits_fwrite_vtable   (f, &vtable);
+  if (!fits_fwrite_header (f, &header))   escape (LOCK, "ERROR: can't append to db");
+  if (!fits_fwrite_matrix (f, &matrix))   escape (LOCK, "ERROR: can't append to db");
+  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't append to db");
+  if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't append to db");
+
   fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
@@ -156,6 +161,13 @@
 
 int close_db () {
-  fclearlockfile (dBFile, f, LCK_HARD, &dbstate);
+  if (dbstate == LCK_UNLOCK) return (TRUE);
+  fclearlockfile (dBFile, f, lockstate, &dbstate);
   return (TRUE);
 }  
 
+int escape (int mode, char *message) {
+  
+  if (mode == UNLOCK) close_db ();
+  fprintf (stderr, "%s\n", message);
+  exit (1);
+}
Index: /trunk/Ohana/src/imregister/src/imregtable.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imregtable.c	(revision 3)
+++ /trunk/Ohana/src/imregister/src/imregtable.c	(revision 4)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imregtable $Revision: 3.0 $";
+static char *version = "imregtable $Revision: 3.1 $";
 
 int main (int argc, char **argv) {
@@ -22,10 +22,11 @@
   }
   infile = argv[1];
+  output.modify = TRUE;
 
   /* load in table data */
   table.header = &theader;
-  fits_read_header (infile, &header);
-  fits_read_matrix (infile, &matrix, &header);
-  fits_read_ftable  (infile, &table, "IMAGE_DATABASE");
+  if (!fits_read_header (infile, &header))                   escape (UNLOCK, "can't open file header");
+  if (!fits_read_matrix (infile, &matrix, &header))          escape (UNLOCK, "can't open file matrix");
+  if (!fits_read_ftable  (infile, &table, "IMAGE_DATABASE")) escape (UNLOCK, "can't open file table");
 
   /* convert to internal format */
@@ -46,3 +47,2 @@
   exit (0);
 }
-
