IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 23, 2005, 8:55:22 PM (21 years ago)
Author:
eugene
Message:

converted to the libohana/dvo_image I/O routines; improvements in fits_db.c

Location:
trunk/Ohana/src/addstar
Files:
1 added
7 edited

Legend:

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

    r4805 r4864  
    6161$(SRC)/update_coords.$(ARCH).o \
    6262$(SRC)/wcatalog.$(ARCH).o \
    63 $(SRC)/wimage.$(ARCH).o \
     63$(SRC)/Shutdown.$(ARCH).o \
    6464$(SRC)/rfits.$(ARCH).o \
    6565$(SRC)/rtext.$(ARCH).o
  • trunk/Ohana/src/addstar/include/addstar.h

    r4826 r4864  
    138138Stars     *getgsc                 PROTO((GSCRegion *patch, int *NSTARS));
    139139Stars     *getusno                PROTO((GSCRegion *catstats, int *Nstars));
    140 Image     *gimages                PROTO((Image *image, int *Npimage));
     140Image     *gimages                PROTO((FITS_DB *db, Image *image, int *Npimage));
    141141Stars     *grefcat                PROTO((char *Refcat, GSCRegion *catstats, int *nstars));
    142142Stars     *grefstars              PROTO((char *file, int *Nstars));
    … …  
    152152int        load_pt_catalog        PROTO((Catalog *catalog, GSCRegion *region));  /*** choose new name ***/
    153153void       load_subpix            PROTO(());
    154 void       lock_image_db          PROTO(());
     154void       lock_image_db          PROTO((FITS_DB *db, char *filename));
    155155int        main                   PROTO((int argc, char **argv));
    156156void       make_backup            PROTO((char *filename));
    … …  
    170170void       sort_stars             PROTO((Stars *stars, int N));
    171171int        str_to_radec           PROTO((double *ra, double *dec, char *str1, char *str2));
    172 void       unlock_image_db        PROTO((Image *image));
     172void       unlock_image_db        PROTO((FITS_DB *db));
    173173void       update_coords          PROTO((Average *average, Measure *measure, int *next));
    174174void       wcatalog               PROTO((Catalog *catalog));
    175 void       wimage                 PROTO((Image *image));
     175void       wimage                 PROTO((FITS_DB *db, Image *image));
     176void       create_image_db        PROTO((FITS_DB *db));
     177void       set_db                 PROTO((FITS_DB *in));
    176178
    177179void       uppercase              PROTO((char *string));
  • trunk/Ohana/src/addstar/src/addstar.c

    r4854 r4864  
    88  GSCRegion *regions;
    99  Catalog catalog;
     10  FITS_DB db;
    1011
    1112  double dtime;
    … …  
    2122  overlap = NULL;
    2223  regions = NULL;
     24  set_db (&db);
    2325
    2426  if (SKYPROBE) load_subpix ();
    2527
    26   /* we use the image table to lock db access */
    27   lock_image_db ();
     28  /* we use the image table to lock db access -- perhaps this is not necessary?*/
     29  lock_image_db (&db, ImageCat);
    2830
    2931  switch (MODE) {
    … …  
    3234    if ((DUMP != NULL) && !strcmp (DUMP, "rawstars")) dump_rawstars (stars, Nstars);
    3335    regions = gregion_image (&image, &Nregions);
    34     overlap = gimages (&image, &Noverlap);
     36    /* lock_image_db (&db, ImageCat); */
     37    overlap = gimages (&db, &image, &Noverlap);
     38    /* unlock_image_db (&db); */
    3539    break;
    3640  case M_REFLIST:
    … …  
    7579
    7680  if (CALIBRATE) { FindCalibration (&image); }
    77   unlock_image_db (&image);
     81
     82  if (db.dbstate == LCK_EMPTY) {
     83    create_image_db (&db);
     84  }
     85 
     86  ohana_memcheck (FALSE);
     87
     88  /* write out new image */
     89  if (MODE == M_IMAGE) {
     90    dvo_image_addrows (&db, &image, 1);
     91    SetProtect (TRUE);
     92    dvo_image_update (&db, VERBOSE);
     93    SetProtect (FALSE);
     94  }
     95  ohana_memcheck (FALSE);
     96  /* unlock_image_db (&db); */
    7897
    7998  gettimeofday (&stop, NULL);
  • trunk/Ohana/src/addstar/src/gimages.c

    r4854 r4864  
    22
    33/* given image, find catalog images which overlap it */
    4 Image *gimages (Image *image, int *Npimage) {
     4Image *gimages (FITS_DB *db, Image *image, int *Npimage) {
    55 
    6   int i, j, k, Nimage, addtolist, size, dbstate;
    7   int NTIMAGE, Ntimage, ntimage;
    8   int NPIMAGE, npimage;
     6  int i, j, k, addtolist;
     7  int NPIMAGE, npimage, Ntimage;
    98  Image *timage, *pimage;
    109  Coords tcoords;
    … …  
    1211  double Xi[5], Yi[5], Xo[5], Yo[5];  /* image and original corners */
    1312  double zeropt;
    14   struct stat filestatus;
    15   FILE *f;
    16 
    17   FITS_DB *db;
    1813
    1914  /* if image.db is opened successfully, f will not be NULL.
    … …  
    2116     if image.db is not locked & opened successfully, we will not reach here!
    2217  */
    23   db = GetDB ();
    2418  if (db[0].dbstate == LCK_EMPTY) {
    2519    *Npimage = 0;
    2620    return (NULL);
    2721  }
    28   fseek (f, 0, SEEK_SET);
    2922
    30   if (!dvo_image_load (db, TRUE)) {
    31     Shutdown ("can't read image catalog %s", ImageCat);
     23  /* read entire db table (use block read eventually?) */
     24  if (!dvo_image_load (db, VERBOSE, FORCE_READ)) {
     25    Shutdown ("can't read image catalog %s", db[0].filename);
    3226  }
    33   timage = fits_table_get_Image (&db.ftable, &Ntimage);
    34   /* timage represents the complete image table */
     27  timage = fits_table_get_Image (&db[0].ftable, &Ntimage, &db[0].swapped);
    3528
    3629  fits_scan (&db[0].header, "ZERO_PT", "%lf", 1, &zeropt);
    … …  
    8073  BuildChipMatch (timage, Ntimage);
    8174
    82   /* run through image table and search for overlaps */
     75  /* run through image table and search for overlaps
     76     also define the vtable entries for the images we keep  */
     77
    8378  for (i = 0; i < Ntimage; i++) {
    8479
    … …  
    116111
    117112  BuildChipMatch (pimage, npimage);
    118 
     113 
    119114  if (VERBOSE) fprintf (stderr, "found %d overlapping images\n", npimage);
    120115
    … …  
    134129   we are now loading in the entire image db table into memory. 
    135130
    136    we will need to write out the new image with a vtable.
    137131*/
  • trunk/Ohana/src/addstar/src/image-db.c

    r4854 r4864  
    11# include "addstar.h"
    22
    3 # define LOCK LCK_XCLD
     3void lock_image_db (FITS_DB *db, char *filename) {
    44
    5 /* file-pointer to Image.db table. if image db does not yet exist */
    6 static FITS_DB db;
     5  /* lock the image catalog */
     6  check_permissions (filename);
    77
    8 FITS_DB *GetDB () {
    9   return (&db);
    10 }
     8  db[0].lockstate = LCK_XCLD;
     9  db[0].timeout   = 3600.0;
     10  fits_db_init (db);
    1111
    12 void lock_image_db () {
    13   /* lock the image catalog */
    14   check_permissions (ImageCat);
    15 
    16   db.lockstate = LCK_XCLD;
    17   db.timeout   = 3600.0;
    18   fits_db_init (&db);
    19 
    20   if (!fits_db_lock (&db, ImageCat)) {
     12  if (!fits_db_lock (db, filename)) {
    2113    fprintf (stderr, "ERROR: can't lock image catalog\n");
    2214    exit (1);
    … …  
    2416}
    2517
    26 void unlock_image_db (Image *image) {
     18void unlock_image_db (FITS_DB *db) {
    2719
    2820  mode_t mode;
    2921
    30   /* protect wimage from interrupt signals */
    31   if (MODE == M_IMAGE) {
    32     SetProtect (TRUE);
    33     wimage (image);
    34     SetProtect (FALSE);
     22  fprintf (stderr, "closing file\n");
     23  if (db[0].f == NULL) {
     24    fprintf (stderr, "re-closing file??\n");
    3525  }
    36 
    37   fits_db_close (&db);
     26  fits_db_close (db);
     27  db[0].f = NULL;
    3828
    3929  /* force permissions to 666 */
    4030  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    41   chmod (ImageCat, mode);
     31  chmod (db[0].filename, mode);
    4232}
    4333
    44 /* clean up open / locked ImageCat before shutting down */
    45 int Shutdown (char *format, ...) { 
    46   va_list argp;
    47   char *formatplus;
    48  
    49   ALLOCATE (formatplus, char, strlen(format));
    50   strcpy (formatplus, format);
    51   strcat (formatplus, "\n");
     34void create_image_db (FITS_DB *db) {
    5235
    53   va_start (argp, format);
    54   vfprintf (stderr, formatplus, argp);
    55   free (formatplus);
    56   va_end (argp);
     36  if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
    5737
    58   SetProtect (TRUE);
    59   fits_db_close (&db);
    60   fprintf (stderr, "ERROR: addstar halted\n");
    61   exit (1);
     38  /* create new header */
     39  fits_init_header (&db[0].header);
     40
     41  if (!strcmp (CATMODE, "RAW")) {
     42    /* make header a fake image */
     43    db[0].header.bitpix   = 16;
     44    db[0].header.Naxes    = 2;
     45    db[0].header.Naxis[0] = 1;
     46    db[0].header.Naxis[1] = 1;
     47    fits_create_header (&db[0].header);
     48  }
     49
     50  if (!strcmp (CATMODE, "MEF") || !strcmp (CATMODE, "SPLIT")) {
     51    fits_create_header (&db[0].header);
     52  }
     53
     54  fits_create_matrix (&db[0].header, &db[0].matrix);
     55  fits_table_set_Image (&db[0].ftable, NULL, 0);
     56
     57  /* assign Zero Point provided by first image in dB */
     58  fits_modify (&db[0].header, "ZERO_PT", "%lf", 1, ZeroPt);
     59  fits_modify (&db[0].header, "NIMAGES", "%d", 1, 0);
    6260}
     61
  • trunk/Ohana/src/addstar/src/rfits.c

    r4299 r4864  
    1616  if (!fits_fread_ftable (f, &table, "SMPFILE")) goto escape;
    1717
    18   smpdata = fits_table_get_SMPData (&table, &Nstars);
     18  smpdata = fits_table_get_SMPData (&table, &Nstars, NULL);
    1919
    2020  ALLOCATE (stars, Stars, Nstars);
  • trunk/Ohana/src/addstar/src/wimage.c

    r4854 r4864  
    22
    33/* add 1 image to image db */
    4 void wimage (Image *image) {
     4void wimages (FITS_DB *db, Image *image, int Nimage) {
    55 
    6   FILE *f;
    7   int Nimages, status, offset, dbstate;
    8   Header header;
     6  int status, Nimages;
    97
    10   db = GetDB ();
    11   fseek (f, 0, SEEK_SET);
     8  /* set permissions somewhere ...
     9  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
     10  fchmod (ImageCat, mode); */
    1211
    13   if (dbstate == LCK_EMPTY) {
    14     if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
    15     if (!fits_read_header (ImageTemplate, &header)) {
    16       fprintf (stderr, "ERROR: can't find template header %s\n", ImageTemplate);
    17       exit (1);
    18     }
    19     /* assign Zero Point provided by first image in dB */
    20     fits_modify (&header, "ZERO_PT", "%lf", 1, ZeroPt);
    21     fits_modify (&header, "NIMAGES", "%d", 1, 0);
    22   } else {
    23     if (!fits_load_header (f, &header)) {
    24       fprintf (stderr, "ERROR: can't read image header %s\n", ImageCat);
    25       exit (1);
    26     }
    27   }
    28  
     12  /* adjust header */
    2913  Nimages = 0;
    30   fits_scan (&header, "NIMAGES", "%d", 1, &Nimages);
     14  fits_scan (&db[0].header, "NIMAGES", "%d", 1, &Nimages);
    3115  Nimages ++;
    32   fits_modify (&header, "NIMAGES", "%d", 1, Nimages);
     16  fits_modify (&db[0].header, "NIMAGES", "%d", 1, Nimages);
    3317
    34   /* position to begining of file to write header */
    35   fseek (f, 0, SEEK_SET);
    36   status = Fwrite (header.buffer, 1, header.size, f, "char");
    37   if (status != header.size) {
    38     fprintf (stderr, "ERROR: failed writing data to image header\n");
    39     exit (1);
     18  fits_table_to_vtable (&db[0].ftable, &db[0].vtable, 0, 0);
     19  for (i = 0; i < Nimage; i++) {
     20    fits_vadd_rows (&db[0].vtable, (char *) image, 1, sizeof(Image));
    4021  }
    4122
    42   /* position to end of data array */
    43   offset = (Nimages - 1)*sizeof(Image) + header.size;
    44   fseek (f, offset, SEEK_SET);
     23  /* check that primary header and table header agree */
     24  if (Nimages != db[0].header.Naxis[1]) {
     25    fprintf (stderr, "header / table length mismatch!\n");
     26  }
    4527
    46   status = Fwrite (image, sizeof(Image), 1, f, "image");
    47   if (status != 1) {
     28  SetProtect (TRUE);
     29  status = dvo_image_update (db, VERBOSE);
     30  SetProtect (FALSE);
     31
     32  if (!status) {
    4833    fprintf (stderr, "ERROR: failed writing data to image catalog\n");
    4934    exit (1);
    … …  
    5136}
    5237
    53 /* the image we add in this routine is always a new image. 
    54    We only need to do two things:
    55      1) append the data for the image to the end of the file
    56      2) update the NIMAGES field in the header (which means, read in header, re-write)
    57  */
     38/* add the new image to the image table:
     39   - use the table header (from ftable)
     40   - create an empty vtable
     41   - add the new image to the end of the vtable
     42   
     43   - write out the vtable, either as TEXT or FITS
     44   * dvo_image_update performs the needed byte-swap on the image data
     45*/
Note: See TracChangeset for help on using the changeset viewer.