Index: /branches/dvo-mods-2007-02/Ohana/src/imregister/src/photcode-table.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/imregister/src/photcode-table.c	(revision 12320)
+++ /branches/dvo-mods-2007-02/Ohana/src/imregister/src/photcode-table.c	(revision 12320)
@@ -0,0 +1,102 @@
+# include "imregister.h"
+enum {NONE, IMPORT, EXPORT};
+
+char CatdirPhotcodeFile[256];
+char MasterPhotcodeFile[256];
+
+int args (int argc, char **argv);
+int ConfigInitLocal (int *argc, char **argv);
+void GetConfig (char *config, char *field, char *format, int N, void *ptr);
+void usage ();
+
+int main (int argc, char **argv) {
+
+  int mode;
+
+  ConfigInitLocal (&argc, argv);
+  mode = args (argc, argv);
+    
+  if (mode == IMPORT) {
+    LoadPhotcodesText (MasterPhotcodeFile);
+    SavePhotcodesFITS (CatdirPhotcodeFile);
+    exit (0);
+  }
+
+  if (mode == EXPORT) {
+    LoadPhotcodesFITS (CatdirPhotcodeFile);
+    SavePhotcodesText (MasterPhotcodeFile);
+    exit (0);
+  }
+
+  usage ();
+  exit (1);
+}
+
+int args (int argc, char **argv) {
+
+  int N, mode;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help")) usage ();
+  if (get_argument (argc, argv, "-h")) usage ();
+
+  mode = NONE;
+  if ((N = get_argument (argc, argv, "-import"))) {
+    mode = IMPORT;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-export"))) {
+    mode = EXPORT;
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 2) usage ();
+
+  strcpy (MasterPhotcodeFile, argv[1]);
+  return (mode);
+}
+
+int ConfigInitLocal (int *argc, char **argv) {
+
+  char *config, *file;
+  char CATDIR[256];
+
+  /*** load configuration info ***/
+  file = SelectConfigFile (argc, argv, "ptolemy");
+  config = LoadConfigFile (file);
+  if (config == (char *) NULL) {
+    fprintf (stderr, "ERROR: can't find configuration file %s\n", file);
+    if (file != (char *) NULL) free (file);
+    exit (1);
+  }
+  // if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
+
+  GetConfig (config, "CATDIR",                 	"%s",  0, CATDIR);
+  // GetConfig (config, "PHOTCODE_FILE",          	"%s",  0, MasterPhotcodeFile);
+  
+  // set the CATDIR version based on CATDIR
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+
+  free (config);
+  free (file);
+  return TRUE;
+}
+
+void GetConfig (char *config, char *field, char *format, int N, void *ptr) {
+
+  char *status;
+
+  status = ScanConfig (config, field, format, N, ptr);
+  if (status == NULL) {
+    fprintf (stderr, "error in config, cannot find %s\n", field);
+    exit (1);
+  }
+  return;
+}
+
+void usage () {
+
+  fprintf (stderr, "USAGE: photcode-table -export (textfile) [-D CATDIR catdir]\n");
+  fprintf (stderr, "USAGE: photcode-table -import (textfile) [-D CATDIR catdir]\n");
+  exit (2);
+}
Index: /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/SavePhotcodesFITS.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 12320)
+++ /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 12320)
@@ -0,0 +1,41 @@
+# include <dvo.h>
+
+/* this function saves the FITS photcode file into the internal photcode table */
+/* locking is used to avoid collisions with programs trying to update the photcodes values */
+/* XXX better distinction between NOT FOUND and FAILURE */
+int SavePhotcodesFITS (char *filename) {
+
+  PhotCodeData *table = NULL;
+  PhotCode *photcode;
+  FITS_DB db;
+
+  int i, code, Ncode, Nsec;
+
+  table = GetPhotcodeTable ();
+  if (table == NULL) {
+    fprintf (stderr, "ERROR: no internal photcode table is defined\n");
+    return FALSE;
+  }
+
+  /* XXX choose more sensible lock timeouts! */
+  db.lockstate = LCK_XCLD;
+  db.timeout   = 10.0;
+  gfits_db_init (&db);
+
+  /* does this mean the db is empty, non-existent, or has access errors? */
+  if (!gfits_db_lock (&db, filename)) {
+    fprintf (stderr, "ERROR: failure to lock db, cannot save photcode table to %s\n", filename);
+    gfits_db_close (&db);
+    return FALSE;
+  }
+
+  /* convert FITS format data to internal format (just byteswaps) */
+  gfits_db_create (&db);
+  gfits_table_set_PhotCode (&db.ftable, table[0].code, table[0].Ncode);
+  gfits_db_save (&db);
+  gfits_db_close (&db);
+
+  /* gfits_table_set_PhotCode performs the needed byte swap.  swap back */
+  gfits_convert_PhotCode (table[0].code, sizeof(PhotCode), table[0].Ncode);
+  return TRUE;
+}
Index: /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/SavePhotcodesText.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/SavePhotcodesText.c	(revision 12320)
+++ /branches/dvo-mods-2007-02/Ohana/src/libdvo/src/SavePhotcodesText.c	(revision 12320)
@@ -0,0 +1,96 @@
+# include <dvo.h>
+# define SCALE 0.001
+
+static char *PHOT_PRI_NAME = "pri";
+static char *PHOT_SEC_NAME = "sec";
+static char *PHOT_ALT_NAME = "alt";
+static char *PHOT_REF_NAME = "ref";
+static char *PHOT_DEP_NAME = "dep";
+
+/* this function saves the FITS photcode file into the internal photcode table */
+/* locking is used to avoid collisions with programs trying to update the photcodes values */
+/* XXX better distinction between NOT FOUND and FAILURE */
+int SavePhotcodesText (char *filename) {
+
+  PhotCodeData *table = NULL;
+  PhotCode *photcode;
+  struct stat filestat;
+  char *type;
+  int i, j, status;
+  FILE *f;
+
+  table = GetPhotcodeTable ();
+  if (table == NULL) {
+    fprintf (stderr, "ERROR: no internal photcode table is defined\n");
+    return FALSE;
+  }
+
+  /* check if file exists */
+  status = stat (filename, &filestat);
+  if (status == -1) {
+    if (errno != ENOENT) {
+      fprintf (stderr, "ERROR: problem accessing output path for%s\n", filename);
+      return FALSE;
+    }
+  } else {
+    make_backup (filename);
+  } 
+
+  f = fopen (filename, "w");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: problem creating photcode file %s\n", filename);
+    return FALSE;
+  }
+
+  for (i = 0; i < table[0].Ncode; i++) {
+    switch (table[0].code[i].type) {
+      case PHOT_SEC:
+	type = PHOT_SEC_NAME;
+	break;
+      case PHOT_ALT:
+	type = PHOT_ALT_NAME;
+	break;
+      case PHOT_REF:
+	type = PHOT_REF_NAME;
+	break;
+      case PHOT_DEP:
+	type = PHOT_DEP_NAME;
+	break;
+      default:
+	fprintf (stderr, "ERROR: problem with photcode type for %s\n", GetPhotcodeNamebyCode(table[0].code[i].code));
+	return FALSE;
+    }
+
+    fprintf (f, "  %-5d %-18s  %4s  %6.3f %6.3f %5.3f ",
+	     table[0].code[i].code,
+	     GetPhotcodeNamebyCode (table[0].code[i].code),
+	     type,
+	     table[0].code[i].C*SCALE, 
+	     table[0].code[i].K*SCALE, 
+	     table[0].code[i].dC*SCALE);
+
+    PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].c1);
+    PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].c2);
+
+    for (j = 0; j < table[0].code[i].Nc - 1; j++) {
+      fprintf (f, " %f,", table[0].code[i].X[j]);
+    }
+    fprintf (f, "%f %f ", table[0].code[i].X[j], table[0].code[i].dX);
+    PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].equiv);
+    fprintf (f, "\n");
+  }
+  fclose (f);
+  return TRUE;
+}
+
+void PrintPhotcodeNamebyCode (FILE *f, char *format, int code) {
+
+  char *name;
+  
+  name = GetPhotcodeNamebyCode (code);
+  if (name == NULL) {
+    fprintf (f, "    - ");
+  } else {
+    fprintf (f, format, code);
+  }
+}
