IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2823


Ignore:
Timestamp:
Dec 23, 2004, 5:18:05 PM (22 years ago)
Author:
eugene
Message:

fixed db open/close operations

Location:
trunk/Ohana/src/imregister
Files:
1 deleted
22 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/imregister/Makefile

    r2803 r2823  
    141141$(BIN)/detsearch.$(ARCH)    : $(SRC)/detsearch.$(ARCH).o    $(DS) ; $(CC) $^ -o $@ $(LFLAGS)
    142142
    143 $(BIN)/imsort.$(ARCH)       : $(BIN)/imregister.$(ARCH) ; rm -f $(BIN)/imsort.$(ARCH) ; ln $(BIN)/imregister.$(ARCH) $(BIN)/imsort.$(ARCH)
     143$(BIN)/imsort.$(ARCH)       : $(BIN)/imregister.$(ARCH) ; rm -f $(BIN)/imsort.$(ARCH) ; cp $(BIN)/imregister.$(ARCH) $(BIN)/imsort.$(ARCH)
    144144
    145145$(BIN)/spsearch.$(ARCH)     : $(SRC)/spsearch.$(ARCH).o     $(SS) ; $(CC) $^ -o $@ $(LFLAGS)
  • trunk/Ohana/src/imregister/base/ConfigInit.c

    r2449 r2823  
    7575  sprintf (PtolemyFifo, "%s.source", ElixirBase);
    7676
     77  if (!ScanConfig (config, "CONNECT", "%s",  0, CONNECT)) {
     78    sprintf (CONNECT, "/usr/bin/rsh");
     79  }
     80
    7781  /* load Detrend Alt Databases paths */
    7882  NDB = 10;
  • trunk/Ohana/src/imregister/detrend/db.c

    r2803 r2823  
    33
    44/* variables which describe the db */
    5 char *dBPath;
     5FILE *f;
    66char *dBFile;
    7 char *dBTrash;
    8 FILE *f;
    97Header header;
    108Header theader;
     
    1311DetReg *image;
    1412int Nimage;
    15 int lockstate, dbstate = LCK_UNLOCK;
     13int lockstate;
     14int dbstate;
     15
     16/* special variables unique to detrend.db */
     17char *dBPath;
     18char *dBTrash;
     19
     20void init_db () {
     21  f              = NULL;
     22  dBFile         = NULL;
     23  header.buffer  = NULL;
     24  matrix.buffer  = NULL;
     25  theader.buffer = NULL;
     26  table.buffer   = NULL;
     27  table.header   = &theader;
     28  image          = NULL;
     29  Nimage         = 0;
     30  lockstate      = LCK_EMPTY;
     31  dbstate        = LCK_UNLOCK;
     32  dBPath         = NULL;
     33  dBTrash        = NULL;
     34}
     35
     36char *get_dBPath () {
     37  dBPath = DetrendDB;
     38  return (dBPath);
     39}
    1640
    1741DetReg *get_images (int *N) {
     
    2448  int Nbytes;
    2549
    26   free (image);
    27 
     50  /* assign pointers to new data block */
     51  Nimage = Nnew;
     52  image = new;
     53  if (image == NULL) Nnew = 0;
     54
     55  /* update header, table structures */
    2856  fits_modify (table.header, "NAXIS2", "%d", 1, Nnew);
    2957  table.header[0].Naxis[1] = Nnew;
     58  table.buffer = (char *) image;
     59
     60  /* add padding space to buffer */
    3061  Nbytes = fits_matrix_size (table.header);
    31 
    32   Nimage = Nnew;
    33   image = new;
    34   REALLOCATE (image, DetReg, Nbytes);
    35   table.buffer = (char *) image;
    36   return (TRUE);
    37 }
    38 
    39 char *get_dBPath () {
    40   dBPath = DetrendDB;
    41   return (dBPath);
    42 }
    43 
    44 int delete_image (DetReg *item) {
    45  
    46   int status;
    47   char line[256];
    48 
    49   if (output.verbose) fprintf (stderr, "deleting %s\n", item[0].filename);
    50   sprintf (line, "mv -f %s/%s %s", dBPath, item[0].filename, dBTrash);
    51   status = system (line);
    52   if (status) fprintf (stderr, "trouble moving %s to trash (%s)\n", item[0].filename, dBTrash);
    53 
    54   if (status)
    55     return (FALSE);
    56   else
    57     return (TRUE);
     62  if (Nbytes > 0) {
     63    REALLOCATE (table.buffer, char, Nbytes);
     64    image = (DetReg *) table.buffer;
     65  }
     66  table.size = Nbytes;
     67
     68  return (TRUE);
    5869}
    5970
     
    91102  }
    92103  lockstate = (output.Modify || output.Delete) ? LCK_HARD : LCK_SOFT;
     104  header.buffer = matrix.buffer = theader.buffer = table.buffer = (char *) NULL;
    93105
    94106  /* lock database (soft) */
     
    102114
    103115  /* load in table data */
    104   table.header = &theader;
    105116  if (!fits_fread_header (f, &header))                     escape (UNLOCK, "ERROR: can't read primary header");
    106   if (!fits_fread_matrix (f, &matrix, &header))            escape (UNLOCK, "ERROR: can't read primary header");
    107   if (!fits_fread_ftable  (f, &table, "DETREND_DATABASE")) escape (UNLOCK, "ERROR: can't read primary header");
     117  if (!fits_fread_matrix (f, &matrix, &header))            escape (UNLOCK, "ERROR: can't read primary matrix");
     118  if (!fits_fread_ftable  (f, &table, "DETREND_DATABASE")) escape (UNLOCK, "ERROR: can't read table");
    108119
    109120  /* convert to internal format */
     
    113124  Nimage = Ny;
    114125
    115   if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg")) escape (UNLOCK, "ERROR: can't read primary header");
     126  if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg"))
     127    escape (UNLOCK, "ERROR: can't convert data");
     128
    116129  return (1);
    117130}
    118 
    119131
    120132/* save complete db file */
    121133int save_db () {
    122134
    123   if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg")) escape (UNLOCK, "ERROR: can't convert from native format");
     135  if (!ConvertStruct ((char *) image, sizeof (DetReg), Nimage, "detreg"))
     136    escape (UNLOCK, "ERROR: can't convert from native format");
    124137
    125138  /* write all images to file */
     
    130143  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
    131144  if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
     145
     146  fits_free_table  (&table);
    132147  close_db ();
     148
    133149  return (TRUE);
    134150}
     
    169185  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
    170186  if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't update db");
     187
     188  fits_free_table  (&table);
     189  fits_free_vtable (&vtable);
    171190  close_db ();
    172   return (TRUE);
    173 }
    174 
     191
     192  return (TRUE);
     193}
     194
     195/* add new entries to db, close db */
    175196int append_db (DetReg *new, int Nnew) {
    176197
     
    182203  ConvertStruct ((char *) new, sizeof (DetReg), Nnew, "detreg");
    183204  fits_vadd_rows (&vtable, (char *) new, Nnew, sizeof(DetReg));
     205  Nimage = theader.Naxis[1];
    184206
    185207  /* write subset to file */
     
    189211  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't append to db");
    190212  if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't append to db");
     213
     214  fits_free_table (&table);
     215  fits_free_vtable (&vtable);
    191216  close_db ();
     217
    192218  return (TRUE);
    193219}
     
    208234
    209235int close_db () {
    210   if (dbstate == LCK_UNLOCK) return (TRUE);
    211   if (dbstate == LCK_EMPTY) return (TRUE);
     236
     237  /* these are safe even if db is empty */
     238  fits_free_header (&header);
     239  fits_free_matrix (&matrix);
     240  fits_free_header (&theader);
     241  /* don't free table: db data may still be used */
     242
    212243  fclearlockfile (dBFile, f, lockstate, &dbstate);
     244  if ((Nimage == 0) && (lockstate != LCK_SOFT)) unlink (dBFile);
     245
    213246  return (TRUE);
    214247
     
    220253  exit (1);
    221254}
     255
     256int delete_image (DetReg *item) {
     257 
     258  int status;
     259  char line[256];
     260
     261  if (output.verbose) fprintf (stderr, "deleting %s\n", item[0].filename);
     262  sprintf (line, "mv -f %s/%s %s", dBPath, item[0].filename, dBTrash);
     263  status = system (line);
     264  if (status) fprintf (stderr, "trouble moving %s to trash (%s)\n", item[0].filename, dBTrash);
     265
     266  if (status)
     267    return (FALSE);
     268  else
     269    return (TRUE);
     270}
     271
  • trunk/Ohana/src/imregister/detrend/delete.c

    r2803 r2823  
    22# include "detrend.h"
    33
    4 int DeleteSubset (Match *match, int Nmatch) {
     4void DeleteSubset (Match *match, int Nmatch) {
    55
    66  int i, j;
     
    1010  image = get_images (&Nimage);
    1111
    12   ALLOCATE (keep, int, Nimage);
     12  ALLOCATE (keep, int, MAX (Nimage, 1));
    1313  for (i = 0; i < Nimage; i++) keep[i] = TRUE;
     14  fprintf (stderr, "total of %d detrend images\n", Nimage);
    1415
    1516  Ndel = 0;
    16   /* list matched images */
    1717  for (j = 0; j < Nmatch; j++) {
    1818    i = match[j].image;
    1919    if (i == -1) continue;
    2020    keep[i] = FALSE;
    21 
     21    Ndel ++;
    2222    delete_image (&image[i]);
    23     Ndel ++;
    2423  }
     24  fprintf (stderr, "delete %d images\n", Ndel);
    2525 
    2626  if (Ndel == 0) {
    2727    fprintf (stderr, "SUCCESS\n");
     28    close_db ();
    2829    exit (0);
    2930  }
    3031
    3132  /* create new data list */
    32   ALLOCATE (subset, DetReg, Nimage);
    33   Nsubset = 0;
    34   for (i = 0; i < Nimage; i++) {
    35     if (keep[i]) {
    36       subset[Nsubset] = image[i];
    37       Nsubset ++;
    38     }
     33  Nsubset = Nimage - Ndel;
     34  ALLOCATE (subset, DetReg, MAX (1, Nsubset));
     35  fprintf (stderr, "keeping %d images\n", Nsubset);
     36  for (j = i = 0; i < Nimage; i++) {
     37    if (!keep[i]) continue;
     38    subset[j] = image[i];
     39    j ++;
    3940  }
    4041
     42  free (keep);
     43  free (image);
    4144  set_images (subset, Nsubset);
    4245
    4346  save_db ();
    44   close_db ();
    45 
    4647  fprintf (stderr, "SUCCESS\n");
    4748  exit (0);
  • trunk/Ohana/src/imregister/imreg/SetSignals.c

    r2802 r2823  
    9393
    9494  /* send signal to remote machine */
    95   if (!rconnect (machine, "/usr/bin/rsh", &rsock, &wsock)) {
     95  if (!rconnect (machine, CONNECT, &rsock, &wsock)) {
    9696    fprintf (stderr, "can't make connection to machine %s to kill process %d\n", machine, pid);
    9797    exit (1);
     
    100100  write (wsock, line, strlen (line));
    101101
    102   for (i = 0; i < 100; i++) {
     102  for (i = 0; i < 300; i++) {
    103103    if (stat (pidfile, &filestat) == -1) exit (0);
    104104    usleep (100000);
  • trunk/Ohana/src/imregister/imreg/args.imregister.c

    r2803 r2823  
    7272    sprintf (PIDFILE, "%s/.%s.pid", path, file);
    7373
     74    /* create db.log and db.bfr */
     75    sprintf (TempDB, "%s.bfr", ImageDB);
     76    sprintf (LogFile, "%s.log", ImageDB);
     77   
    7478    /* check for daemon mode */
    7579    if ((N = get_argument (argc, argv, "-daemon"))) {
  • trunk/Ohana/src/imregister/imreg/db.c

    r2803 r2823  
    33
    44/* variables which describe the db */
    5 char *dBFile = (char *) NULL;
    65FILE *f;
     6char *dBFile;
    77Header header;
    88Header theader;
     
    1212int Nimage;
    1313int lockstate;
    14 int dbstate = LCK_UNLOCK;
     14int dbstate;
     15
     16void init_db () {
     17  f              = NULL;
     18  dBFile         = NULL;
     19  header.buffer  = NULL;
     20  matrix.buffer  = NULL;
     21  theader.buffer = NULL;
     22  table.buffer   = NULL;
     23  table.header   = &theader;
     24  image          = NULL;
     25  Nimage         = 0;
     26  lockstate      = LCK_EMPTY;
     27  dbstate        = LCK_UNLOCK;
     28}
    1529
    1630int set_db (char *filename) {
     
    2943
    3044  int Nbytes;
    31   char *data;
    3245
    3346  /* assign pointers to new data block */
    34   free (image);
    3547  Nimage = Nnew;
    3648  image = new;
     49  if (image == NULL) Nnew = 0;
    3750
    3851  /* update header, table structures */
    3952  fits_modify (table.header, "NAXIS2", "%d", 1, Nnew);
    4053  table.header[0].Naxis[1] = Nnew;
     54  table.buffer = (char *) image;
     55
     56  /* add padding space to buffer */
    4157  Nbytes = fits_matrix_size (table.header);
    42 
    43   /* add padding space to buffer */
    44   data = (char *) image;
    45   REALLOCATE (data, char, Nbytes);
    46   image = (RegImage *) data;
    47 
    48   table.buffer = (char *) image;
     58  if (Nbytes > 0) {
     59    REALLOCATE (table.buffer, char, Nbytes);
     60    image = (RegImage *) table.buffer;
     61  }
    4962  table.size = Nbytes;
    5063
     
    6780  f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
    6881  if (dbstate == LCK_EMPTY) return (0);
     82
    6983  if (f == (FILE *) NULL) {
    7084    fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
     
    7387
    7488  /* init & load in table data */
    75   table.header = &theader;
    7689  if (!fits_fread_header (f, &header))                  escape (UNLOCK, "ERROR: can't read primary header");
    7790  if (!fits_fread_matrix (f, &matrix, &header))         escape (UNLOCK, "ERROR: can't read primary matrix");
     
    104117  if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
    105118
    106   fits_free_header (&header);
    107   fits_free_matrix (&matrix);
    108   fits_free_header (&theader);
    109119  fits_free_table  (&table);
    110 
    111   fclearlockfile (dBFile, f, lockstate, &dbstate);
     120  close_db ();
     121
    112122  return (TRUE);
    113123}
     
    149159  if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't update db");
    150160
    151   fits_free_header (&header);
    152   fits_free_matrix (&matrix);
    153   fits_free_header (&theader);
     161  fits_free_table  (&table);
    154162  fits_free_vtable (&vtable);
    155   fits_free_table  (&table);
    156 
    157   fclearlockfile (dBFile, f, lockstate, &dbstate);
    158   return (TRUE);
    159 }
    160 
     163  close_db ();
     164
     165  return (TRUE);
     166}
     167
     168/* add new entries to db, close db */
    161169int append_db (RegImage *new, int Nnew) {
    162170
     
    168176  ConvertStruct ((char *) new, sizeof (RegImage), Nnew, "regimage");
    169177  fits_vadd_rows (&vtable, (char *) new, Nnew, sizeof(RegImage));
     178  Nimage = theader.Naxis[1];
    170179
    171180  /* write subset to file */
     
    176185  if (!fits_fwrite_vtable   (f, &vtable)) escape (LOCK, "ERROR: can't append to db");
    177186
    178   fits_free_header (&header);
    179   fits_free_matrix (&matrix);
    180   fits_free_header (&theader);
     187  fits_free_table (&table);
    181188  fits_free_vtable (&vtable);
    182 
    183   fclearlockfile (dBFile, f, lockstate, &dbstate);
     189  close_db ();
     190
    184191  return (TRUE);
    185192}
     
    201208int close_db () {
    202209
    203   if (dbstate == LCK_EMPTY) return (TRUE);
    204 
    205   fits_free_header (&header);
    206   fits_free_matrix (&matrix);
    207   fits_free_header (&theader);
    208   /* fits_free_table  (&table); */
    209 
    210   if (dbstate == LCK_UNLOCK) return (TRUE);
    211   fclearlockfile (dBFile, f, lockstate, &dbstate);
    212   return (TRUE);
    213 
    214 
    215 int close_lock_db () {
    216 
    217   if (dbstate == LCK_EMPTY) return (TRUE);
    218 
     210  /* these are safe even if db is empty */
    219211  fits_free_header (&header);
    220212  fits_free_matrix (&matrix);
    221213  fits_free_header (&theader);
    222214  /* don't free table: db data may still be used */
    223   /* fits_free_table  (&table); */
    224 
    225   if (dbstate == LCK_UNLOCK) return (TRUE);
    226   fclearlockfile (dBFile, f, LCK_XCLD, &dbstate);
     215
     216  fclearlockfile (dBFile, f, lockstate, &dbstate);
     217  if ((Nimage == 0) && (lockstate != LCK_SOFT)) unlink (dBFile);
     218
    227219  return (TRUE);
    228220
  • trunk/Ohana/src/imregister/imreg/delete.c

    r70 r2823  
    55
    66  int i, j;
    7   int *keep, Nbad, Nimage, Nsubset;
     7  int *keep, Ndel, Nimage, Nsubset;
    88  RegImage *image, *subset;
    99
     
    1414  fprintf (stderr, "total of %d images\n", Nimage);
    1515
    16   Nbad = 0;
     16  Ndel = 0;
    1717  for (i = 0; i < Nmatch; i++) {
    1818    j = match[i];
    1919    if (j == -1) continue;
    2020    keep[j] = FALSE;
    21     Nbad ++;
     21    Ndel ++;
    2222  }
    23   fprintf (stderr, "delete %d images\n", Nbad);
     23  fprintf (stderr, "delete %d images\n", Ndel);
    2424
    25   Nsubset = Nimage - Nbad;
     25  if (Ndel == 0) {
     26    fprintf (stderr, "SUCCESS\n");
     27    close_db ();
     28    exit (0);
     29  }
     30
     31  /* create new data list */
     32  Nsubset = Nimage - Ndel;
    2633  ALLOCATE (subset, RegImage, MAX (1, Nsubset));
    2734  fprintf (stderr, "keeping %d images\n", Nsubset);
     
    2936    if (!keep[i]) continue;
    3037    subset[j] = image[i];
    31     /* this or memcpy ? */
    3238    j++;
    3339  }
    3440
    3541  free (keep);
     42  free (image);
    3643  set_images (subset, Nsubset);
    3744
  • trunk/Ohana/src/imregister/imreg/imregclient.c

    r2803 r2823  
    66  FILE *f;
    77  RegImage *image;
    8   char *TempDB;
    98  int i, Nentry, Nslice, status, tmpint;
    109  float *dtime;
    11 
    12   ALLOCATE (TempDB, char, strlen (ImageDB) + 10);
    13   sprintf (TempDB, "%s.bfr", ImageDB);
    1410
    1511  Nentry = 0;
  • trunk/Ohana/src/imregister/imreg/rconnect.c

    r76 r2823  
    6363  /* try to get evidence connection is alive - wait upto a few seconds */
    6464  status = -1;
    65   for (i = 0; (i < 100) && (status == -1); i++) {
     65  for (i = 0; (i < 300) && (status == -1); i++) {
    6666    fcntl (*rsock, F_SETFL, O_NONBLOCK);
    6767    status = SockScan ("PTOLEMY STARTED", &fifo, *rsock);
     68    if (!(i % 30)) fprintf (stderr, ".");
    6869    if (status == 0) {
    6970      fprintf (stderr, "socket closed unexpectedly\n");
     
    7374    }
    7475  }
    75   if (i == 100) {
     76  if (i == -1) {
    7677    fprintf (stderr, "timeout while connecting\n");
    7778    close (*wsock);
  • trunk/Ohana/src/imregister/include/detrend.h

    r2803 r2823  
    102102
    103103int ModifySubset (Match *match, int Nmatch);
    104 int DeleteSubset (Match *match, int Nmatch);
     104void DeleteSubset (Match *match, int Nmatch);
    105105int usage ();
    106106Criteria *MosaicCriteria (Criteria base, char *filename, int *ncrit);
  • trunk/Ohana/src/imregister/include/imregister.h

    r2803 r2823  
    1919char TransDB[256];
    2020char ImPhotDB[256];
     21char TempDB[256];
     22char LogFile[256];
     23char CONNECT[64];
    2124
    2225int  NDetrendAltDB;
  • trunk/Ohana/src/imregister/photreg/db.c

    r2803 r2823  
    33
    44/* variables which describe the db */
    5 char *dBFile = (char *) NULL;
    65FILE *f;
     6char *dBFile;
    77Header header;
    88Header theader;
     
    1212int Nimage;
    1313int lockstate;
    14 int dbstate = LCK_UNLOCK;
    15 static char *BinaryName = (char *) NULL;
     14int dbstate;
     15char *BinaryName = NULL;
     16
     17void init_db () {
     18  f              = NULL;
     19  dBFile         = NULL;
     20  header.buffer  = NULL;
     21  matrix.buffer  = NULL;
     22  theader.buffer = NULL;
     23  table.buffer   = NULL;
     24  table.header   = &theader;
     25  image          = NULL;
     26  Nimage         = 0;
     27  lockstate      = LCK_EMPTY;
     28  dbstate        = LCK_UNLOCK;
     29}
    1630
    1731/* photreg vs transreg database selection */
     
    6074
    6175  /* assign pointers to new data block */
    62   free (image);
    6376  Nimage = Nnew;
    6477  image = new;
     78  if (image == NULL) Nnew = 0;
    6579
    6680  /* update header, table structures */
    6781  fits_modify (table.header, "NAXIS2", "%d", 1, Nnew);
    6882  table.header[0].Naxis[1] = Nnew;
     83  table.buffer = (char *) image;
     84
     85  /* add padding space to buffer */
    6986  Nbytes = fits_matrix_size (table.header);
    70 
    71   /* add padding space to buffer */
    72   table.buffer = (char *) image;
    73   REALLOCATE (table.buffer, char, Nbytes);
    74   image = (PhotPars *) table.buffer;
     87  if (Nbytes > 0) {
     88    REALLOCATE (table.buffer, char, Nbytes);
     89    image = (PhotPars *) table.buffer;
     90  }
    7591  table.size = Nbytes;
    7692
     
    89105
    90106  lockstate = (output.modify || output.delete) ? LCK_HARD : LCK_SOFT;
     107  header.buffer = matrix.buffer = theader.buffer = table.buffer = (char *) NULL;
    91108
    92109  if (dBFile == (char *) NULL) {
     
    98115  f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
    99116  if (dbstate == LCK_EMPTY) return (0);
     117
    100118  if (f == (FILE *) NULL) {
    101119    fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
     
    104122
    105123  /* load in table data */
    106   table.header = &theader;
    107124  if (!fits_fread_header (f, &header))            escape (UNLOCK, "ERROR: can't read primary header");
    108125  if (!fits_fread_matrix (f, &matrix, &header))   escape (UNLOCK, "ERROR: can't read primary header");
     
    116133
    117134  if (!ConvertStruct ((char *) image, sizeof (PhotPars), Nimage, "photpars"))
    118     escape (UNLOCK, "ERROR: can't read primary header");
     135    escape (UNLOCK, "ERROR: can't convert data");
    119136
    120137  return (1);
     
    139156  /* lock database */
    140157  f = fsetlockfile (dBFile, 300.0, lockstate, &dbstate);
     158  if (dbstate == LCK_EMPTY) return (0);
     159
    141160  if (f == (FILE *) NULL) {
    142161    fprintf (stderr, "ERROR: can't set lock on %s\n", dBFile);
    143162    exit (1);
    144163  }
    145   if (dbstate == LCK_EMPTY) return (0);
    146164
    147165  /* load in table data */
    148   table.header = &theader;
    149166  if (!fits_fread_header (f, &header))            escape (UNLOCK, "ERROR: can't read primary header");
    150167  if (!fits_fread_matrix (f, &matrix, &header))   escape (UNLOCK, "ERROR: can't read primary header");
     
    195212  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
    196213  if (!fits_fwrite_table   (f, &table))   escape (LOCK, "ERROR: can't update db");
    197   fclearlockfile (dBFile, f, lockstate, &dbstate);
     214
     215  fits_free_table  (&table);
     216  close_db ();
     217
    198218  return (TRUE);
    199219}
     
    234254  if (!fits_fwrite_Theader (f, &theader)) escape (LOCK, "ERROR: can't update db");
    235255  if (!fits_fwrite_vtable  (f, &vtable))  escape (LOCK, "ERROR: can't update db");
    236   fclearlockfile (dBFile, f, lockstate, &dbstate);
     256
     257  fits_free_table  (&table);
     258  fits_free_vtable (&vtable);
     259  close_db ();
     260
    237261  return (TRUE);
    238262}
     
    247271  ConvertStruct ((char *) new, sizeof (PhotPars), Nnew, "photpars");
    248272  fits_vadd_rows (&vtable, (char *) new, Nnew, sizeof(PhotPars));
     273  Nimage = theader.Naxis[1];
    249274
    250275  /* write subset to file */
     
    255280  if (!fits_fwrite_vtable  (f, &vtable))  escape (LOCK, "ERROR: can't append to db");
    256281
    257   fclearlockfile (dBFile, f, lockstate, &dbstate);
     282  fits_free_table (&table);
     283  fits_free_vtable (&vtable);
     284  close_db ();
     285
    258286  return (TRUE);
    259287}
     
    274302
    275303int close_db () {
    276   if (dbstate == LCK_UNLOCK) return (TRUE);
     304
     305  /* these are safe even if db is empty */
     306  fits_free_header (&header);
     307  fits_free_matrix (&matrix);
     308  fits_free_header (&theader);
     309  /* don't free table: db data may still be used */
     310
    277311  fclearlockfile (dBFile, f, lockstate, &dbstate);
    278   return (TRUE);
    279 
    280 
    281 int close_lock_db () {
    282   if (dbstate == LCK_UNLOCK) return (TRUE);
    283   fclearlockfile (dBFile, f, LCK_XCLD, &dbstate);
     312  if ((Nimage == 0) && (lockstate != LCK_SOFT)) unlink (dBFile);
     313
    284314  return (TRUE);
    285315
  • trunk/Ohana/src/imregister/photreg/delete.c

    r90 r2823  
    55
    66  int i, j;
    7   int *keep, Nbad, Nphotdata, Nsubset;
     7  int *keep, Ndel, Nphotdata, Nsubset;
    88  PhotPars *photdata, *subset;
    99
     
    1414  fprintf (stderr, "total of %d photdata\n", Nphotdata);
    1515
    16   Nbad = 0;
     16  Ndel = 0;
    1717  for (i = 0; i < Nmatch; i++) {
    1818    j = match[i];
    1919    if (j == -1) continue;
    2020    keep[j] = FALSE;
    21     Nbad ++;
     21    Ndel ++;
    2222  }
    23   fprintf (stderr, "delete %d photdata\n", Nbad);
     23  fprintf (stderr, "delete %d photdata\n", Ndel);
    2424
    25   Nsubset = Nphotdata - Nbad;
     25  if (Ndel == 0) {
     26    fprintf (stderr, "SUCCESS\n");
     27    close_db ();
     28    exit (0);
     29  }
     30
     31  /* create new data list */
     32  Nsubset = Nphotdata - Ndel;
    2633  ALLOCATE (subset, PhotPars, MAX (1, Nsubset));
    2734  fprintf (stderr, "keeping %d photdata\n", Nsubset);
     
    2936    if (!keep[i]) continue;
    3037    subset[j] = photdata[i];
    31     /* this or memcpy ? */
    3238    j++;
    3339  }
    3440
    3541  free (keep);
     42  free (photdata);
    3643  set_images (subset, Nsubset);
    3744
  • trunk/Ohana/src/imregister/src/detregister.c

    r3 r2823  
    11# include "imregister.h"
    22# include "detrend.h"
    3 static char *version = "detregister $Revision: 3.0 $";
     3static char *version = "detregister $Revision: 3.1 $";
    44
    55int main (int argc, char **argv) {
     
    1616  SaveEntry (argv[1], &newdata, descriptor.imageID);
    1717
     18  init_db ();
    1819  status = load_db ();
    1920  if (!status) {
  • trunk/Ohana/src/imregister/src/detsearch.c

    r2803 r2823  
    11# include "imregister.h"
    22# include "detrend.h"
    3 static char *version = "detsearch $Revision: 3.2 $";
     3static char *version = "detsearch $Revision: 3.3 $";
    44
    55int main (int argc, char **argv) {
     
    1515  if (output.Criteria) PrintCriteria ();
    1616       
     17  init_db ();
    1718  status = load_db ();
    1819  if (!status) {
  • trunk/Ohana/src/imregister/src/imregister.c

    r2803 r2823  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imregister $Revision: 3.3 $";
     3static char *version = "imregister $Revision: 3.4 $";
    44
    55int main (int argc, char **argv) {
     
    1212  args (argc, argv);
    1313  image = iminfo (argv[1]);
     14  init_db ();
    1415
    1516  Nnew = 0;
  • trunk/Ohana/src/imregister/src/imregtable.c

    r2455 r2823  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imregtable $Revision: 3.3 $";
     3static char *version = "imregtable $Revision: 3.4 $";
    44
    55int main (int argc, char **argv) {
     
    1616  ConfigCamera ();
    1717  ConfigFilter ();
     18  init_db ();
    1819 
    1920  if (argc != 2) {
  • trunk/Ohana/src/imregister/src/imsearch.c

    r2803 r2823  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imsearch $Revision: 3.2 $";
     3static char *version = "imsearch $Revision: 3.3 $";
    44
    55int main (int argc, char **argv) {
     
    1010  get_version (argc, argv, version);
    1111  args (argc, argv);
     12  init_db ();
    1213
    1314  set_db (ImageDB);
  • trunk/Ohana/src/imregister/src/imstatreg.c

    r2803 r2823  
    11# include "imregister.h"
    22# include "imreg.h"
    3 static char *version = "imstatreg $Revision: 3.7 $";
     3static char *version = "imstatreg $Revision: 3.8 $";
     4# define DEBUG 0
    45
    56int main (int argc, char **argv) {
     
    78  RegImage *image;
    89  int *match, Nmatch, status, child, Nentry;
    9   char *TempDB, *LogFile;
    1010  FILE *f;
    1111
    1212  get_version (argc, argv, version);
    1313  args (argc, argv);
     14  init_db ();
    1415
    1516  if (CLIENT) imregclient (argv[1], argv[2], argv[3]);
    1617
    17   /* create db.log and db.bfr */
    18   ALLOCATE (TempDB, char, strlen (ImageDB) + 10);
    19   sprintf (TempDB, "%s.bfr", ImageDB);
    20   ALLOCATE (LogFile, char, strlen (ImageDB) + 10);
    21   sprintf (LogFile, "%s.log", ImageDB);
    22 
    2318  SetSignals ();
    2419
     20# if (!DEBUG)
    2521  /* fork in background */
    2622  if ((child = fork ())) {
     
    3228    exit (0);
    3329  }
     30# endif
    3431
    3532  /* child process, check for process */
    3633  ConfigPID (PIDFILE);
    3734
     35# if (!DEBUG)
    3836  /* redirect stderr, stdout to logfile */
    3937  f = freopen (LogFile, "a", stdout);
     
    4442    f = freopen (LogFile, "a", stderr);
    4543  }
     44# else
     45  f = stderr;
     46# endif
    4647
    4748  /* start loop */
    4849  while (1) {
    4950
    50     /* check / load temporary database */
     51    /* check / load / delete temporary database */
    5152    set_db (TempDB);
    5253    status = load_db ();
     
    5859    image = get_images (&Nentry);
    5960    print_db_status ("temporary database read");
    60     close_lock_db ();
    61     /* temp DB closed, hardlock on temp DB remains */
    62      
     61    status = set_images (NULL, 0);
     62    close_db ();
     63    print_db_status ("temporary database closed");
     64 
    6365    /* check / load main database */
    6466    set_db (ImageDB);
     
    6769      print_db_status ("main database empty");
    6870      close_db ();
     71      /* add temp data back to temp database */
     72      print_db_status ("resave temporary data");
     73      set_db (TempDB);
     74      status = load_db ();
     75      if (!status) {
     76        print_db_status ("create new temporary database");
     77        create_db ();
     78      }
     79      append_db (image, Nentry);
     80      print_db_status ("temporary database resaved");
    6981      goto next;
    7082    }
     
    7587    print_db_status ("main database read");
    7688    update_db (match, Nmatch);
    77     free (match);
    78     free (image);
     89    if (match != NULL) free (match);
     90    if (image != NULL) free (image);
    7991     
    8092    /* clear temp DB hardlock */
  • trunk/Ohana/src/imregister/src/photreg.c

    r2803 r2823  
    11# include "imregister.h"
    22# include "photreg.h"
    3 static char *version = "photreg $Revision: 1.2 $";
     3static char *version = "photreg $Revision: 1.3 $";
    44
    55int main (int argc, char **argv) {
     
    1212  regargs (argc, argv, &newdata);
    1313
     14  init_db ();
    1415  status = load_db ();
    1516  if (!status) {
  • trunk/Ohana/src/imregister/src/photsearch.c

    r2803 r2823  
    11# include "imregister.h"
    22# include "photreg.h"
    3 static char *version = "photsearch $Revision: 1.2 $";
     3static char *version = "photsearch $Revision: 1.3 $";
    44
    55int main (int argc, char **argv) {
     
    1111  get_version (argc, argv, version);
    1212  args (argc, argv);
    13  
     13
     14  init_db ();
    1415  status = load_db ();
    1516  if (!status) {
Note: See TracChangeset for help on using the changeset viewer.