Index: /branches/eam_branches/ohana.20160226/src/relastro/src/load_images.c
===================================================================
--- /branches/eam_branches/ohana.20160226/src/relastro/src/load_images.c	(revision 39445)
+++ /branches/eam_branches/ohana.20160226/src/relastro/src/load_images.c	(revision 39446)
@@ -27,15 +27,4 @@
   MARKTIME("build chip match: %f sec\n", dtime);
 
-  char mapfile[DVO_MAX_PATH];
-  snprintf (mapfile, DVO_MAX_PATH, "%s/AstroMap.fits", CATDIR);
-  table = AstromOffsetMapLoad (mapfile, 100000, VERBOSE);
-
-  // assign images.coords.offsetMap -> table->map[i]
-  if (table) {
-    AstromOffsetTableMatchChips (image, Nimage, table);
-  } else {
-    table = AstromOffsetTableInit ();
-  }
-
   // select the images which overlap the selected sky regions
   if (UseAllImages) {
@@ -54,4 +43,27 @@
   }
     
+  /* unlock, if we can (else, unlocked below) and free, if we can */
+  int unlockImages = !UPDATE || (RELASTRO_OP == OP_UPDATE_OFFSETS);
+  if (unlockImages) { 
+    if (subset != image) {
+      // if we have generated an image subset and we are running UPDATE_OFFSETS, the we can free images here
+      free (image);
+      db[0].ftable.buffer = NULL;
+      BuildChipMatch (subset, Nsubset);
+    }
+    dvo_image_unlock (db); 
+  }
+
+  char mapfile[DVO_MAX_PATH];
+  snprintf (mapfile, DVO_MAX_PATH, "%s/AstroMap.fits", CATDIR);
+  table = AstromOffsetMapLoad (mapfile, 100000, VERBOSE);
+
+  // assign images.coords.offsetMap -> table->map[i]
+  if (table) {
+    AstromOffsetTableMatchChips (subset, Nsubset, table);
+  } else {
+    table = AstromOffsetTableInit ();
+  }
+
   initImages (subset, LineNumber, Nsubset);
   MARKTIME("  init images: %f sec\n", dtime);
@@ -60,8 +72,4 @@
   MARKTIME("  init mosaics: %f sec\n", dtime);
   
-  /* unlock, if we can (else, unlocked below) */
-  int unlockImages = !UPDATE || (RELASTRO_OP == OP_UPDATE_OFFSETS);
-  if (unlockImages) dvo_image_unlock (db); 
-
   return TRUE;
 }
