Index: /branches/eam_branch_20071130/Ohana/src/dvosplit/src/dvosplit.c
===================================================================
--- /branches/eam_branch_20071130/Ohana/src/dvosplit/src/dvosplit.c	(revision 15728)
+++ /branches/eam_branch_20071130/Ohana/src/dvosplit/src/dvosplit.c	(revision 15729)
@@ -1,4 +1,3 @@
 # include "dvosplit.h"
-# define NROWS 100000 /* ~10MB per row for measures */
 
 int main (int argc, char **argv) {
@@ -31,7 +30,4 @@
     // XXX : we need to copy these files to their new names
 
-    // change outsky.regions[i].depth for these regions
-    outlist = SkyListByPatch (outsky, OUT_DEPTH, skylist[0].regions[i]);
-
     // set the parameters which guide catalog open/load/create
     incatalog.filename = skylist[0].filename[i];
@@ -52,91 +48,22 @@
     }
 
-    // split out the average entries:
-    incatalog.catflags = LOAD_AVES;
-    Nblocks = incatalog.Nave_disk / NROWS;
-    for (j = 0; j < Nblocks; j++) {
+    // change outsky.regions[i].depth for these regions
+    outlist = SkyListByPatch (outsky, OUT_DEPTH, skylist[0].regions[i]);
 
-      // read up to NROWS at a time
-      dvo_catalog_load_segment (&incatalog, VERBOSE, j*NROWS, NROWS);
+    outcatalogs = open_output_catalogs (outlist);
 
-      for (k = 0; k < incatalog.Naverage; k++) {
-	averef = j*NROWS + k;
-	
-	inR  = incatalog.average[k].R;
-	inD = incatalog.average[k].D;
+    avelinks = split_averages (&incatalog, outlist, outcatalogs); 
 
-	// which of the outcatalogs contains this coordinate?
+    split_measures (&incatalog, outlist, outcatalogs, avelinks); 
 
-	N = -1;
-	for (n = 0; n < outlist[0].Nregions; n++) {
-	  if (inR < outregions[n].Rmin) continue;
-	  if (inR > outregions[n].Rmax) continue;
-	  if (inD < outregions[n].Dmin) continue;
-	  if (inD > outregions[n].Dmax) continue;
-	  Ncat = n;
-	  break;
-	}
-	if (Ncat == -1) continue;
+    // XXX missing entries have to be reconstructed if they are desired
+    // split_missings (&incatalog, outlist, outcatalogs, avelinks); 
 
-	// XXX this probably needs to be Nave_disk so we can have partial saves
-	Nout = outcatalog[Ncat].Naverage;
-	outref[averef] = Nout;
-	outcat[averef] = Ncat;
+    dvo_catalog_close (&incatalog);
 
-	outcatalog[Ncat].average[Nout] = incatalog.average[k];
-	outcatalog[Ncat].Naverage++;
-      }
-
-      for (n = 0; n < Noutcatalog; n++) {
-	dvo_catalog_save_segment (&outcatalog[Ncat], VERBOSE, outcatalog[Ncat].Nave_disk, outcatalog[Ncat].Naverage);
-      }
+    for (j = 0; j < outlist[0].Nregions; j++) {
+      dvo_catalog_close (&outcatalogs[j]);
     }
-
-    // split out the measure entries:
-    incatalog.catflags = LOAD_MEAS;
-    Nblocks = incatalog.Nmeas_disk / NROWS;
-    for (j = 0; j < Nblocks; j++) {
-
-      // read up to NROWS at a time
-      dvo_catalog_load_segment (&incatalog, VERBOSE, j*NROWS, NROWS);
-
-      for (k = 0; k < incatalog.Nmeasure; k++) {
-
-	averef = incatalog.measure[k].averef;
-	Ncat = outcat[averef];
-
-	Nout = outcatalog[Ncat].Nmeasure;
-	outcatalog[Ncat].measure[Nout] = incatalog.measure[k];
-	outcatalog[Ncat].measure[Nout].averef = outref[averef];
-	outcatalog[Ncat].Nmeasure++;
-      }
-
-      for (n = 0; n < Noutcatalog; n++) {
-	dvo_catalog_save_segment (&outcatalog[N], VERBOSE, outcatalog[N].Nmeas_disk, outcatalog[N].Nmeasure);
-      }
-    }
-
-    // split out the secfilt entries:
-    incatalog.catflags = LOAD_SECF;
-    Nblocks = incatalog.Nsecfilt_disk / NROWS;
-    for (j = 0; j < Nblocks; j++) {
-
-      // read up to NROWS at a time
-      dvo_catalog_load_segment (&incatalog, VERBOSE, j*NROWS, NROWS);
-
-      for (k = 0; k < incatalog.Naverage; k++) {
-
-	averef = j*NROWS + k;
-	Ncat = outcat[averef];
-	Nout = outref[averef] * Nsecfilt;
-
-	for (n = 0; n < Nsecfilt; n++) {
-	  outcatalog[Ncat].secfilt[Nout + n] = incatalog.measure[k*Nsecfilt + n];
-	}
-      }
-
-      for (n = 0; n < Noutcatalog; n++) {
-	dvo_catalog_save_segment (&outcatalog[N], VERBOSE, outcatalog[N].Nmeas_disk, outcatalog[N].Nmeasure);
-      }
-    }
-
+  }
+  exit (0);
+}
Index: /branches/eam_branch_20071130/Ohana/src/dvosplit/src/open_output_catalogs.c
===================================================================
--- /branches/eam_branch_20071130/Ohana/src/dvosplit/src/open_output_catalogs.c	(revision 15729)
+++ /branches/eam_branch_20071130/Ohana/src/dvosplit/src/open_output_catalogs.c	(revision 15729)
@@ -0,0 +1,24 @@
+# include "dvosplit.h"
+
+Catalog *open_output_catalogs (SkyList *outlist) {
+
+  Catalog *outcatalogs;
+
+  ALLOCATE (outcatalogs, Catalog, outlist[0].Nregions);
+
+  // an error exit status here is a significant error
+  for (i = 0; i < outlist[0].Nregions; i++) {
+    
+    // set the parameters which guide catalog open/load/create
+    outcatalogs[i].filename = skylist[0].filename[i];
+    outcatalogs[i].Nsecfilt = GetPhotcodeNsecfilt ();
+    outcatalogs[i].catflags = LOAD_NONE;
+
+    if (!dvo_catalog_open (&outcatalogs[i], outlist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure to open catalog file %s\n", outcatalogs[i].filename);
+      exit (2);
+    }
+  }
+  
+  return (outcatalogs);
+}
Index: /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_averages.c
===================================================================
--- /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_averages.c	(revision 15729)
+++ /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_averages.c	(revision 15729)
@@ -0,0 +1,94 @@
+# include "dvosplit.h"
+# define NROWS 100000 /* ~10MB per block for measures */
+
+AveLinks *split_averages (Catalog *incatalog, SkyList *outlist, Catalog *outcatalogs) {
+
+  double inR, inD;
+  int block, ave, cat, Nblocks, Ncat, Nout;
+  int *outref, *outcat;
+  AveLinks *avelinks;
+
+  ALLOCATE (outref, int, incatalog[0].Nave_disk);
+  ALLOCATE (outcat, int, incatalog[0].Nave_disk);
+
+  Nsecfilt = GetPhotcodeNsecfilt ();
+
+  // allocate enough space for these output buffers: use Nsecfilt + 1 incase the file
+  // contains primary photcodes, which will increase Nsecfilt by one.
+  for (cat = 0; cat < outlist[0].Nregions; cat++) {
+    REALLOCATE (outcatalog[cat].average, Average, NROWS);
+    REALLOCATE (outcatalog[cat].secfilt, SecFilt, NROWS*(Nsecfilt + 1));
+  }
+
+  // split out the average & secfilt entries:
+  incatalog[0].catflags = LOAD_AVES | LOAD_SECF;
+  Nblocks = incatalog[0].Nave_disk / NROWS;
+  for (block = 0; block < Nblocks; block++) {
+
+    // read up to NROWS at a time
+    dvo_catalog_load_segment (&incatalog, VERBOSE, block*NROWS, NROWS);
+    assert (block*NROWS == incatalogs[0].Naves_off);
+
+    for (ave = 0; ave < incatalog[0].Naverage; ave++) {
+      averef = ave + incatalog[0].Naves_off;
+	
+      inR = incatalog[0].average[ave].R;
+      inD = incatalog[0].average[ave].D;
+
+      // which of the outcatalogs contains this coordinate?
+
+      Ncat = -1;
+      for (cat = 0; cat < outlist[0].Nregions; cat++) {
+	if (inR < outlist[0].regions[cat][0].Rmin) continue;
+	if (inR > outlist[0].regions[cat][0].Rmax) continue;
+	if (inD < outlist[0].regions[cat][0].Dmin) continue;
+	if (inD > outlist[0].regions[cat][0].Dmax) continue;
+	Ncat = cat;
+	break;
+      }
+
+      if (Ncat == -1) {
+	fprintf (stderr, "WARNING: missed %d (%f, %f)\n", averef, inR, inD);
+	continue;
+      }
+
+      Nout = outcatalog[Ncat].Naverage;
+      outref[averef] = Nout + outcatalog[Ncat].Naves_off;
+      outcat[averef] = Ncat;
+
+      // assign the value to the next element of the output catalog
+      outcatalog[Ncat].average[Nout] = incatalog[0].average[k];
+      outcatalog[Ncat].Naverage++;
+
+      // update secfilt at the same time
+      for (n = 0; n < Nsecfilt; n++) {
+	outcatalog[Ncat].secfilt[Nout*Nsecfilt + n] = incatalog[0].secfilt[ave*Nsecfilt + n];
+	outcatalog[Ncat].Nsecf_mem++;
+      }
+    }
+
+    // double check the values of Naverage, Nsecf_mem?
+
+    // write out the new values
+    for (cat = 0; cat < outlist[0].Nregions; cat++) {
+      outcatalog[cat].catflags = LOAD_AVES | LOAD_SECF;
+      dvo_catalog_save_segment (&outcatalog[cat], VERBOSE);
+
+      // XXX I need to advance the pointers and free the current data
+      // XXX these should be done within save segment:
+      outcatalog[cat].Naves_disk += outcatalog[cat].Naverage;
+      outcatalog[cat].Naves_off  += outcatalog[cat].Naverage;
+      outcatalog[cat].Naverage    = 0;
+
+      outcatalog[cat].Nsecf_disk += Nsecfilt * outcatalog[cat].Naverage;
+      outcatalog[cat].Nsecf_off  += Nsecfilt * outcatalog[cat].Naverage;
+      outcatalog[cat].Nsecf_mem   = 0;
+    }
+  }
+
+  ALLOCATE (avelinks, AveLinks, 1);
+  avelinks[0].outref = outref;
+  avelinks[0].outcat = outcat;
+
+  return (avelinks);
+}
Index: /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_measures.c
===================================================================
--- /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_measures.c	(revision 15729)
+++ /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_measures.c	(revision 15729)
@@ -0,0 +1,46 @@
+# include "dvosplit.h"
+# define NROWS 100000 /* ~10MB per row for measures */
+
+int split_measures (Catalog *incatalog, SkyList *outlist, Catalog *outcatalogs, AveLinks *avelinks) {
+
+  int *outref, *outcat;
+
+  outref = avelinks->outref;
+  outcat = avelinks->outcat;
+
+  // allocate enough space for the output buffer
+  for (cat = 0; cat < outlist[0].Nregions; cat++) {
+    REALLOCATE (outcatalog[cat].measure, Measure, NROWS);
+  }
+
+  // split out the measure entries:
+  incatalog[0].catflags = LOAD_MEAS;
+  Nblocks = incatalog[0].Nmeas_disk / NROWS;
+  for (block = 0; block < Nblocks; block++) {
+
+    // read up to NROWS at a time
+    dvo_catalog_load_segment (&incatalog, VERBOSE, block*NROWS, NROWS);
+
+    for (meas = 0; meas < incatalog[0].Nmeasure; meas++) {
+
+      averef = incatalog[0].measure[meas].averef;
+      Ncat = outcat[averef];
+
+      Nout = outcatalog[Ncat].Nmeasure;
+      outcatalog[Ncat].measure[Nout] = incatalog[0].measure[meas];
+      outcatalog[Ncat].measure[Nout].averef = outref[averef];
+      outcatalog[Ncat].Nmeasure++;
+    }
+
+    for (cat = 0; cat < outlist[0].Nregions; cat++) {
+      outcatalogs[cat].catflags = LOAD_AVES | LOAD_SECF;
+      dvo_catalog_save_segment (&outcatalog[cat], VERBOSE);
+
+      outcatalog[cat].Nmeas_disk += outcatalog[cat].Nmeasure;
+      outcatalog[cat].Nmeas_off  += outcatalog[cat].Nmeasure;
+      outcatalog[cat].Nmeasure    = 0;
+
+    }
+  }
+  return (TRUE);
+}
Index: /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_missings.c
===================================================================
--- /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_missings.c	(revision 15729)
+++ /branches/eam_branch_20071130/Ohana/src/dvosplit/src/split_missings.c	(revision 15729)
@@ -0,0 +1,46 @@
+# include "dvosplit.h"
+# define NROWS 100000 /* ~10MB per row for missings */
+
+int split_missings (Catalog *incatalog, SkyList *outlist, Catalog *outcatalogs, AveLinks *avelinks) {
+
+  int *outref, *outcat;
+
+  outref = avelinks->outref;
+  outcat = avelinks->outcat;
+
+  // allocate enough space for the output buffer
+  for (cat = 0; cat < outlist[0].Nregions; cat++) {
+    REALLOCATE (outcatalog[cat].missing, Missing, NROWS);
+  }
+
+  // split out the missing entries:
+  incatalog[0].catflags = LOAD_MISS;
+  Nblocks = incatalog[0].Nmiss_disk / NROWS;
+  for (block = 0; block < Nblocks; block++) {
+
+    // read up to NROWS at a time
+    dvo_catalog_load_segment (&incatalog, VERBOSE, block*NROWS, NROWS);
+
+    for (miss = 0; miss < incatalog[0].Nmissing; miss++) {
+
+      averef = incatalog[0].missing[miss].averef;
+      Ncat = outcat[averef];
+
+      Nout = outcatalog[Ncat].Nmissing;
+      outcatalog[Ncat].missing[Nout] = incatalog[0].missing[miss];
+      outcatalog[Ncat].missing[Nout].averef = outref[averef];
+      outcatalog[Ncat].Nmissing++;
+    }
+
+    for (cat = 0; cat < outlist[0].Nregions; cat++) {
+      outcatalogs[cat].catflags = LOAD_AVES | LOAD_SECF;
+      dvo_catalog_save_segment (&outcatalog[cat], VERBOSE);
+
+      outcatalog[cat].Nmiss_disk += outcatalog[cat].Nmissing;
+      outcatalog[cat].Nmiss_off  += outcatalog[cat].Nmissing;
+      outcatalog[cat].Nmissing    = 0;
+
+    }
+  }
+  return (TRUE);
+}
