IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 8328


Ignore:
Timestamp:
Aug 14, 2006, 1:14:22 PM (20 years ago)
Author:
eugene
Message:

modifying dvo_catalog APIs

Location:
trunk/Ohana/src
Files:
2 added
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/addstar/src/addstar.c

    r7974 r8328  
    9595
    9696    catalog.filename = skylist[0].filename[i];
    97     load_pt_catalog (&catalog, skylist[0].regions[i]);
     97    catalog.catformat = dvo_catalog_format (CATFORMAT);  // set the default catformat from config data
     98    catalog.catmode   = dvo_catalog_mode (CATMODE);      // set the default catmode from config data
     99    catalog.lockmode  = LCK_XCLD;
     100    dvo_catalog_open (&catalog, skylist[0].regions[i], Nsecfilt, "w");
    98101
    99102    /* for only_match, skip empty catalogs XXX EAM : this leaves behind empty files */
    100103    if ((catalog.Nave_disk == 0) && options.only_match) {
    101       unlock_catalog (&catalog);
    102       free (catalog.filename);
    103       free_catalog (&catalog);
     104      dvo_catalog_unlock (&catalog);
     105      free (catalog.filename);  // can this be done within the dvo_catalog_free function??
     106      dvo_catalog_free (&catalog);
    104107      continue;
    105108    }
     
    136139
    137140    if (Nsubset == 0) {
     141      // XXX remove empty catalogs
    138142      unlock_catalog (&catalog);
    139143    } else {
    140144      SetProtect (TRUE);
    141       if (!options.only_images) wcatalog (&catalog);
     145      if (!options.only_images) dvo_catalog_save (&catalog);
    142146      SetProtect (FALSE);
    143       unlock_catalog (&catalog);
     147      dvo_catalog_unlock (&catalog);
    144148    }
    145     free_catalog (&catalog);
    146     free (catalog.filename);
     149    dvo_catalog_free (&catalog);
     150    free (catalog.filename); // XXX ????
    147151
    148152    if (options.mode == M_REFCAT) free (stars);
  • trunk/Ohana/src/addstar/src/find_matches.c

    r8243 r8328  
    11# include "addstar.h"
    22
    3 void find_matches (SkyRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
    4 
    5   int i, j, n, N, J;
     3void find_matches (SkyRegion *region, Stars *stars, int NstarsIn, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
     4
     5  int i, j, n, N, J, status, Nstars;
    66  double X, Y, RADIUS, RADIUS2, secz;
    77  float *X1, *Y1, *X2, *Y2;
     
    2323
    2424  /** allocate local arrays (stars) **/
    25   ALLOCATE (X1, float, Nstars);
    26   ALLOCATE (Y1, float, Nstars);
    27   ALLOCATE (N1, int,   Nstars);
     25  ALLOCATE (X1, float, NstarsIn);
     26  ALLOCATE (Y1, float, NstarsIn);
     27  ALLOCATE (N1, int,   NstarsIn);
    2828
    2929  /** allocate local arrays (catalog) **/
     
    5555    tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
    5656    tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
     57    tcoords.Npolyterms = 1;
    5758    strcpy (tcoords.ctype, "RA---TAN");
    5859  }
    5960
    6061  /* build spatial index (RA sort) */
    61   for (i = 0; i < Nstars; i++) {
    62     fRD_to_XY (&X1[i], &Y1[i], stars[i].R, stars[i].D, &tcoords);
    63     N1[i] = i;
     62  Nstars = 0;
     63  for (i = 0; i < NstarsIn; i++) {
     64    status = fRD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
     65    if (!status) continue;
     66    N1[Nstars] = i;
     67    Nstars ++;
     68  }
     69  if (Nstars < 1) {
     70    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", catalog[0].filename);
     71    free (catalog[0].found);
     72    free (X1);
     73    free (Y1);
     74    free (N1);
     75    free (X2);
     76    free (Y2);
     77    free (N2);
     78    return;
    6479  }
    6580  if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
     
    333348
    334349  /* note stars which have been found in this catalog */
    335   for (i = 0; i < Nstars; i++) {
     350  for (i = 0; i < NstarsIn; i++) {
    336351    if (stars[i].found > -1) {
    337352      stars[i].found = -2;
     
    346361  catalog[0].Nmissing = Nmiss;
    347362  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", Nstars, Nave, Nmeas, Nmiss, Nmatch);
     363
     364  free (catalog[0].found);
     365  free (X1);
     366  free (Y1);
     367  free (N1);
     368  free (X2);
     369  free (Y2);
     370  free (N2);
    348371  return;
    349372}
  • trunk/Ohana/src/addstar/src/find_matches_closest.c

    r8304 r8328  
    11# include "addstar.h"
    2 # define DEBUG 1
    3 
    4 void find_matches_closest (SkyRegion *region, Stars *stars, int Nstars, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
    5 
    6   int i, j, n, N, J, Jmin;
     2
     3void find_matches_closest (SkyRegion *region, Stars *stars, int NstarsIn, Catalog *catalog, Image *image, Image *overlap, int Noverlap, Coords *mosaic, AddstarClientOptions options) {
     4
     5  int i, j, n, N, J, Jmin, status, Nstars;
    76  double X, Y, RADIUS, RADIUS2, Rmin, secz;
    87  float *X1, *Y1, *X2, *Y2;
     
    2423
    2524  /** allocate local arrays (stars) **/
    26   ALLOCATE (X1, float, Nstars);
    27   ALLOCATE (Y1, float, Nstars);
    28   ALLOCATE (N1, int,   Nstars);
     25  ALLOCATE (X1, float, NstarsIn);
     26  ALLOCATE (Y1, float, NstarsIn);
     27  ALLOCATE (N1, int,   NstarsIn);
    2928
    3029  /** allocate local arrays (catalog) **/
     
    4039  NMEAS = Nmeas = catalog[0].Nmeasure;
    4140  NMISS = Nmiss = catalog[0].Nmissing;
    42  
     41
    4342  /* project onto rectilinear grid with 1 arcsec pixels */
    4443  /* we keep the original crpix1,2 and crref1,2 */
     
    5655    tcoords.pc1_1 = tcoords.pc2_2 = 1.0;
    5756    tcoords.pc1_2 = tcoords.pc2_1 = 0.0;
     57    tcoords.Npolyterms = 1;
    5858    strcpy (tcoords.ctype, "RA---TAN");
    5959  }
    6060
    61   /* build spatial index (RA sort) */
    62   for (i = 0; i < Nstars; i++) {
    63     fRD_to_XY (&X1[i], &Y1[i], stars[i].R, stars[i].D, &tcoords);
    64     N1[i] = i;
     61  /* build spatial index (RA sort) referencing input array sequence */
     62  Nstars = 0;
     63  for (i = 0; i < NstarsIn; i++) {
     64    status = fRD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
     65    if (!status) continue;
     66    N1[Nstars] = i;
     67    Nstars ++;
     68  }
     69  if (Nstars < 1) {
     70    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", catalog[0].filename);
     71    free (catalog[0].found);
     72    free (X1);
     73    free (Y1);
     74    free (N1);
     75    free (X2);
     76    free (Y2);
     77    free (N2);
     78    return;
    6579  }
    6680  if (Nstars > 1) sort_lists (X1, Y1, N1, Nstars);
    67  
     81
    6882  /* build spatial index (RA sort) */
    6983  for (i = 0; i < Nave; i++) {
     
    149163    n = N2[Jmin];
    150164    N = N1[i];
    151 
    152     if (DEBUG) fprintf (stderr, "matched %f,%f and %f,%f\n",
    153                         catalog[0].average[n].R, catalog[0].average[n].D,
    154                         stars[N].R, stars[N].D);
    155165
    156166    /* add to end of measurement list */
     
    329339  }
    330340     
    331   free (catalog[0].found);
    332341  REALLOCATE (catalog[0].average, Average, Nave);
    333342  REALLOCATE (catalog[0].measure, Measure, Nmeas);
     
    344353
    345354  /* note stars which have been found in this catalog */
    346   for (i = 0; i < Nstars; i++) {
     355  for (i = 0; i < NstarsIn; i++) {
    347356    if (stars[i].found > -1) {
    348357      stars[i].found = -2;
     
    356365  catalog[0].Nmeasure = Nmeas;
    357366  catalog[0].Nmissing = Nmiss;
    358   if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", Nstars, Nave, Nmeas, Nmiss, Nmatch);
     367  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nmiss: %d %d %d %d, (%d matches)\n", NstarsIn, Nave, Nmeas, Nmiss, Nmatch);
     368
     369  free (catalog[0].found);
     370  free (X1);
     371  free (Y1);
     372  free (N1);
     373  free (X2);
     374  free (Y2);
     375  free (N2);
    359376  return;
    360377}
  • trunk/Ohana/src/addstar/src/gcatalog.c

    r7080 r8328  
    88
    99  /* read catalog header */
    10   if (!load_catalog (catalog, VERBOSE)) {
     10  if (!dvo_catalog_load (catalog, VERBOSE)) {
    1111    fprintf (stderr, "ERROR: failure loading catalog\n");
    1212    exit (1);
    1313  }
    1414
    15   /* should this be moved into save_catalog?? */
    16   status = gfits_scan (&catalog[0].header, "SORTED", "%t", 1, &catalog[0].sorted);
    17   if (!status) catalog[0].sorted = TRUE;
    18   /* XXX EAM - is this a good choice?  should the default be 'FALSE'? */
    19 
    2015  /* check Nsecfile value, update if needed */
    2116  Nsecfilt = GetPhotcodeNsecfilt ();
    22   if (catalog[0].Nsecfilt < Nsecfilt) {
    23 
    24     int i, j, Nextra, in, out;
    25     SecFilt *insec, *outsec;
    26 
    27     Nextra = Nsecfilt - catalog[0].Nsecfilt;
    28     insec = catalog[0].secfilt;
    29     ALLOCATE (outsec, SecFilt, catalog[0].Naverage * Nsecfilt);
    30     for (in = out = i = 0; i < catalog[0].Naverage; i++) {
    31       for (j = 0; j < catalog[0].Nsecfilt; j++, in++, out++) {
    32         outsec[out].M_PS  = insec[in].M_PS;
    33         outsec[out].dM_PS = insec[in].dM_PS;
    34         outsec[out].Xm = insec[in].Xm;
    35       }
    36       for (j = 0; j < Nextra; j++, out++) {
    37         outsec[out].M_PS  = NO_MAG;
    38         outsec[out].dM_PS = NO_MAG;
    39         outsec[out].Xm    = NO_MAG;
    40       }
    41     }
    42     free (catalog[0].secfilt);
    43     catalog[0].secfilt = outsec;
    44     catalog[0].Nsecfilt = Nsecfilt;
    45   }
    46 
    47   if (catalog[0].Nsecfilt > Nsecfilt) {
     17  if (!dvo_catalog_check (catalog, Nsecfilt, TRUE)) {
    4818    fprintf (stderr, "ERROR: can't reduce number of secondary filters\n");
    49     exit (1);
     19    exit (2);
    5020  }
    5121  return (TRUE);
  • trunk/Ohana/src/addstar/src/load_pt_catalog.c

    r7691 r8328  
    11# include "addstar.h"
    22
     3// XXX can I replace this with dvo_catalog_open ()
     4// XXX set the catalog properties before the function call (not as args)
    35int load_pt_catalog (Catalog *catalog, SkyRegion *region) {
    46 
    5   if (!check_file_access (catalog[0].filename, TRUE, TRUE)) {
    6     exit (1);
    7   }
     7  if (!check_file_access (catalog[0].filename, TRUE, TRUE)) exit (1);
    88
    99  if (VERBOSE) fprintf (stderr, "adding to %s\n", catalog[0].filename);
    1010   
     11  /* check Nsecfile value, update if needed */
     12  Nsecfilt = GetPhotcodeNsecfilt ();
     13
    1114  switch (lock_catalog (catalog, LCK_XCLD)) {
    1215  case 0:
     
    1417    exit (1);
    1518  case 1:
    16     gcatalog (catalog); /* load from disk */
    17     if (VERBOSE) fprintf (stderr, "loading existing file %s\n", catalog[0].filename);
     19    if (!dvo_catalog_load (catalog, VERBOSE)) {
     20      fprintf (stderr, "ERROR: failure loading catalog\n");
     21      exit (1);
     22    }
     23    if (!dvo_catalog_check (catalog, Nsecfilt, TRUE)) {
     24      fprintf (stderr, "ERROR: can't reduce number of secondary filters\n");
     25      exit (2);
     26    }
     27    if (VERBOSE) fprintf (stderr, "loaded existing file %s\n", catalog[0].filename);
    1828    break;
    1929  case 2:
    20     mkcatalog (region, catalog); /* fills in new header info */
     30    dvo_catalog_create (region, catalog, Nsecfilt, CATFORMAT, CATMODE); /* fills in new header info */
    2131    if (VERBOSE) fprintf (stderr, "creating new file %s\n", catalog[0].filename);
    2232    break;
  • trunk/Ohana/src/libdvo/include/dvo.h

    r7080 r8328  
    197197void coords_precess (double *ra, double *dec, double in_epoch, double out_epoch);
    198198
    199 int lock_catalog (Catalog *catalog, int lockmode);
    200 int unlock_catalog (Catalog *catalog);
    201 int load_catalog (Catalog *catalog, int VERBOSE);
    202 int save_catalog (Catalog *catalog, char VERBOSE);
    203 int update_catalog (Catalog *catalog, char VERBOSE);
    204 
    205199int FindMosaicForImage (Image *images, int Nimages, int entry);
    206200int FindMosaicForImage_TableSearch (Image *images, int Nimages, int entry);
     
    221215PhotCode *GetPhotcodebyNsec (int Nsec);
    222216PhotCode *GetPhotcodeEquivbyCode (int code);
    223 
    224217char     *GetPhotcodeNamebyCode (int code);
    225218
     
    274267int   ConvertStruct (char *buffer, int size, int Nbytes, char *type);
    275268
     269/** dvo_catalog APIs */
     270void dvo_catalog_init (Catalog *catalog);
     271void dvo_catalog_create (SkyRegion *region, Catalog *catalog, int Nsecfilt, char *catformat, char *catmode);
     272int dvo_catalog_lock (Catalog *catalog, int lockmode);
     273int dvo_catalog_unlock (Catalog *catalog);
     274int dvo_catalog_load (Catalog *catalog, int VERBOSE);
     275int dvo_catalog_save (Catalog *catalog, char VERBOSE);
     276int dvo_catalog_update (Catalog *catalog, char VERBOSE);
     277
     278/* catmode-specific APIs */
     279int dvo_catalog_load_raw (Catalog *catalog, int VERBOSE);
     280int dvo_catalog_save_raw (Catalog *catalog, char VERBOSE);
     281int dvo_catalog_load_mef (Catalog *catalog, int VERBOSE);
     282int dvo_catalog_save_mef (Catalog *catalog, char VERBOSE);
     283int dvo_catalog_load_split (Catalog *catalog, int VERBOSE);
     284int dvo_catalog_save_split (Catalog *catalog, char VERBOSE);
     285int dvo_catalog_update_split (Catalog *catalog, char VERBOSE);
     286
    276287/*** conversion functions / I/O conversions ***/
    277 
    278288Average *ReadRawAverage (FILE *f, int Naverage, int format);
    279289Measure *ReadRawMeasure (FILE *f, int Nmeasure, int format);
     
    312322int MeasureToFtable (FTable *ftable, Measure *measure, int Nmeasure, int format);
    313323int SecFiltToFtable (FTable *ftable, SecFilt *secfilt, int Nsecfilt, int format);
    314 
    315 int load_catalog_raw (Catalog *catalog, int VERBOSE);
    316 int save_catalog_raw (Catalog *catalog, char VERBOSE);
    317 int load_catalog_mef (Catalog *catalog, int VERBOSE);
    318 int save_catalog_mef (Catalog *catalog, char VERBOSE);
    319 int load_catalog_split (Catalog *catalog, int VERBOSE);
    320 int save_catalog_split (Catalog *catalog, char VERBOSE);
    321 int update_catalog_split (Catalog *catalog, char VERBOSE);
    322324
    323325/*** DVO image db I/O Functions ***/
  • trunk/Ohana/src/libdvo/src/dvo_catalog.c

    r8001 r8328  
    11# include <ohana.h>
    22# include <dvo.h>
     3# define DEBUG 1
     4
     5void dvo_catalog_init (Catalog *catalog) {
     6
     7  catalog[0].f = NULL;
     8  catalog[0].filename = NULL;
     9
     10  gfits_init_header (&catalog[0].header);
     11
     12  catalog[0].average = NULL;
     13  catalog[0].measure = NULL;
     14  catalog[0].missing = NULL;
     15  catalog[0].secfilt = NULL;
     16 
     17  catalog[0].Naverage = 0;
     18  catalog[0].Nmeasure = 0;
     19  catalog[0].Nmissing = 0;
     20  catalog[0].Nsecfilt = 0;
     21
     22  catalog[0].Nave_disk  = 0;
     23  catalog[0].Nmeas_disk = 0;
     24  catalog[0].Nmiss_disk = 0;
     25  catalog[0].Nmeas_off  = 0;
     26
     27  /* pointers to SPLIT data files */
     28  catalog[0].measure_catalog = NULL;
     29  catalog[0].missing_catalog = NULL;
     30  catalog[0].secfilt_catalog = NULL;
     31
     32  /* extra catalog information */
     33  catalog[0].lockmode = 0;
     34  catalog[0].catmode  = 0;
     35  catalog[0].catformat = 0;
     36  catalog[0].catflags = 0;
     37  catalog[0].sorted = 0;
     38 
     39  /* pointers for data manipulation */
     40  catalog[0].found = NULL;
     41  catalog[0].image = NULL;
     42  catalog[0].mosaic = NULL;
     43  catalog[0].X = NULL;
     44  catalog[0].Y = NULL;
     45}
    346
    447/* possible exit status for lock_catalog:
     
    750   2 - empty file (file may be open or closed!)
    851*/
    9 
    10 int lock_catalog (Catalog *catalog, int lockmode) {
     52int dvo_catalog_lock (Catalog *catalog, int lockmode) {
    1153
    1254  int dbstate;
     
    1658
    1759  /* set lock on database, create stream f */
     60  // fprintf (stderr, "locking: %s\n", catalog[0].filename);
    1861  catalog[0].f = fsetlockfile (catalog[0].filename, 3600.0, catalog[0].lockmode, &dbstate);
    1962
     
    2669}
    2770
    28 int unlock_catalog (Catalog *catalog) {
     71int dvo_catalog_unlock (Catalog *catalog) {
    2972
    3073  int dbstate;
    3174
    3275  if (catalog[0].f == (FILE *) NULL) return (2);
     76  // fprintf (stderr, "unlocking: %s\n", catalog[0].filename);
    3377  fclearlockfile (catalog[0].filename, catalog[0].f, catalog[0].lockmode, &dbstate);
     78
     79  if (catalog[0].catmode == DVO_MODE_SPLIT) {
     80    if (catalog[0].measure_catalog != NULL) dvo_catalog_unlock (catalog[0].measure_catalog);
     81    if (catalog[0].missing_catalog != NULL) dvo_catalog_unlock (catalog[0].missing_catalog);
     82    if (catalog[0].secfilt_catalog != NULL) dvo_catalog_unlock (catalog[0].secfilt_catalog);
     83  }
    3484  return (1);
    3585}
    3686
    37 int load_catalog (Catalog *catalog, int VERBOSE) {
     87int dvo_catalog_open (Catalog *catalog, int NSECFILT, int VERBOSE, int MODE) {
     88
     89  if (!check_file_access (catalog[0].filename, TRUE, VERBOSE)) {
     90    if (VERBOSE) fprintf (stderr, "no permission to access %f\n", catalog[0].filename);
     91    return (FALSE);
     92  }
     93
     94  if (VERBOSE) fprintf (stderr, "adding to %s\n", catalog[0].filename);
     95   
     96  switch (dvo_catalog_lock (catalog, catalog[0].lockmode)) {
     97  case 0:
     98    if (VERBOSE) fprintf (stderr, "can't lock file %s\n", catalog[0].filename);
     99    return (FALSE);
     100  case 1:
     101    if (!dvo_catalog_load (catalog, VERBOSE)) {
     102      if (VERBOSE) fprintf (stderr, "failure loading catalog\n");
     103      return (FALSE);
     104    }
     105    if (!dvo_catalog_check (catalog, Nsecfilt, TRUE)) {
     106      if (VERBOSE) fprintf (stderr, "can't reduce number of secondary filters\n");
     107      return (FALSE);
     108    }
     109    if (VERBOSE) fprintf (stderr, "loaded existing file %s\n", catalog[0].filename);
     110    break;
     111  case 2:
     112    if (MODE == DVO_READ_ONLY) return (FALSE);
     113    dvo_catalog_create (region, catalog, NSECFILT); /* fills in new header info */
     114    if (VERBOSE) fprintf (stderr, "creating new file %s\n", catalog[0].filename);
     115    break;
     116  }
     117  return (TRUE);
     118}
     119
     120int dvo_catalog_load (Catalog *catalog, int VERBOSE) {
    38121 
    39122  int Naxis, split;
     
    53136     an old table will keep its mode
    54137  */
     138
     139  /* check if the table has been sorted or not */
     140  status = gfits_scan (&catalog[0].header, "SORTED", "%t", 1, &catalog[0].sorted);
     141  if (!status) catalog[0].sorted = TRUE;
    55142
    56143  catalog[0].catmode = DVO_MODE_MEF;
     
    68155    case DVO_MODE_RAW:
    69156      if (VERBOSE) fprintf (stderr, "reading catalog (mode DVO_MODE_RAW)\n");
    70       load_catalog_raw (catalog, VERBOSE);
     157      dvo_catalog_load_raw (catalog, VERBOSE);
    71158      break;
    72159    case DVO_MODE_MEF:
    73160      if (VERBOSE) fprintf (stderr, "reading catalog (mode DVO_MODE_MEF)\n");
    74       load_catalog_mef (catalog, VERBOSE);
     161      dvo_catalog_load_mef (catalog, VERBOSE);
    75162      break;
    76163    case DVO_MODE_SPLIT:
    77164      if (VERBOSE) fprintf (stderr, "reading catalog (mode DVO_MODE_SPLIT)\n");
    78       load_catalog_split (catalog, VERBOSE);
     165      dvo_catalog_load_split (catalog, VERBOSE);
    79166      break;
    80167    default:
     
    85172}
    86173
    87 int save_catalog (Catalog *catalog, char VERBOSE) {
     174int dvo_catalog_save (Catalog *catalog, char VERBOSE) {
    88175
    89176  switch (catalog[0].catmode) {
    90177    case DVO_MODE_RAW:
    91       save_catalog_raw (catalog, VERBOSE);
     178      dvo_catalog_save_raw (catalog, VERBOSE);
    92179      break;
    93180    case DVO_MODE_MEF:
    94       save_catalog_mef (catalog, VERBOSE);
     181      dvo_catalog_save_mef (catalog, VERBOSE);
    95182      break;
    96183    case DVO_MODE_SPLIT:
    97       save_catalog_split (catalog, VERBOSE);
     184      dvo_catalog_save_split (catalog, VERBOSE);
    98185      break;
    99186    default:
     
    104191}
    105192
    106 int update_catalog (Catalog *catalog, char VERBOSE) {
     193int dvo_catalog_update (Catalog *catalog, char VERBOSE) {
    107194
    108195  /* update is only valid for catmode SPLIT */
    109196  switch (catalog[0].catmode) {
    110197    case DVO_MODE_RAW:
    111       save_catalog_raw (catalog, VERBOSE);
     198      dvo_catalog_save_raw (catalog, VERBOSE);
    112199      break;
    113200    case DVO_MODE_MEF:
    114       save_catalog_mef (catalog, VERBOSE);
     201      dvo_catalog_save_mef (catalog, VERBOSE);
    115202      break;
    116203    case DVO_MODE_SPLIT:
    117204      /* new file needs to use save_catalog_split */
    118205      if (catalog[0].Nave_disk == 0) {
    119         save_catalog_split (catalog, VERBOSE);
     206        dvo_catalog_save_split (catalog, VERBOSE);
    120207      } else {
    121         update_catalog_split (catalog, VERBOSE);
     208        dvo_catalog_update_split (catalog, VERBOSE);
    122209      }
    123210      break;
     
    125212      fprintf (stderr, "invalid catalog mode\n");
    126213      exit (2);
     214  }
     215  return (TRUE);
     216}
     217
     218int dvo_catalog_check (Catalog *catalog, int Nsecfilt, int extend) {
     219
     220  int i, j, Nextra, in, out;
     221  SecFilt *insec, *outsec;
     222
     223  if (catalog[0].Nsecfilt == Nsecfilt) return (TRUE);
     224
     225  if (catalog[0].Nsecfilt > Nsecfilt) return (FALSE);
     226
     227  if ((catalog[0].Nsecfilt < Nsecfilt) && !extend) return (FALSE);
     228
     229  if ((catalog[0].Nsecfilt < Nsecfilt) && extend) {
     230
     231    Nextra = Nsecfilt - catalog[0].Nsecfilt;
     232    insec = catalog[0].secfilt;
     233    ALLOCATE (outsec, SecFilt, catalog[0].Naverage * Nsecfilt);
     234    for (in = out = i = 0; i < catalog[0].Naverage; i++) {
     235      for (j = 0; j < catalog[0].Nsecfilt; j++, in++, out++) {
     236        outsec[out].M_PS  = insec[in].M_PS;
     237        outsec[out].dM_PS = insec[in].dM_PS;
     238        outsec[out].Xm = insec[in].Xm;
     239      }
     240      for (j = 0; j < Nextra; j++, out++) {
     241        outsec[out].M_PS  = NO_MAG;
     242        outsec[out].dM_PS = NO_MAG;
     243        outsec[out].Xm    = NO_MAG;
     244      }
     245    }
     246    free (catalog[0].secfilt);
     247    catalog[0].secfilt = outsec;
     248    catalog[0].Nsecfilt = Nsecfilt;
    127249  }
    128250  return (TRUE);
  • trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c

    r7963 r8328  
    11# include <dvo.h>
    22
    3 int load_catalog_mef (Catalog *catalog, int VERBOSE) {
     3int dvo_catalog_load_mef (Catalog *catalog, int VERBOSE) {
    44 
    55  int Nitems, Nbytes, Nexpect, Naverage, Nmeasure, Nmissing, Nsecfilt;
     
    131131/* save_catalog_mef writes a complete new file from scratch */
    132132
    133 int save_catalog_mef (Catalog *catalog, char VERBOSE) {
     133int dvo_catalog_save_mef (Catalog *catalog, char VERBOSE) {
    134134
    135135  int Nitems;
  • trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c

    r7963 r8328  
    33/* read data from raw-style catalog file; set data format values based on file */
    44
    5 int load_catalog_raw (Catalog *catalog, int VERBOSE) {
     5int dvo_catalog_load_raw (Catalog *catalog, int VERBOSE) {
    66 
    77  int Nitems, nitems;
     
    189189}
    190190
    191 int save_catalog_raw (Catalog *catalog, char VERBOSE) {
     191int dvo_catalog_save_raw (Catalog *catalog, char VERBOSE) {
    192192
    193193  int Nitems, nitems;
  • trunk/Ohana/src/libdvo/src/dvo_catalog_split.c

    r7080 r8328  
    11# include <dvo.h>
    22
    3 int load_catalog_split (Catalog *catalog, int VERBOSE) {
     3int dvo_catalog_load_split (Catalog *catalog, int VERBOSE) {
    44
    55  int Nitems, Nexpect, Naverage, Nmeasure, Nmissing, Nsecfilt;
     
    6868  if (catalog[0].catflags & LOAD_MEAS) {
    6969    ALLOCATE (measure, Catalog, 1);
     70    measure[0].measure_catalog = NULL;
     71    measure[0].missing_catalog = NULL;
     72    measure[0].secfilt_catalog = NULL;
    7073
    7174    /* get split filename from main header (paths relative to cpt file) */
     
    145148  if (catalog[0].catflags & LOAD_MISS) {
    146149    ALLOCATE (missing, Catalog, 1);
     150    missing[0].measure_catalog = NULL;
     151    missing[0].missing_catalog = NULL;
     152    missing[0].secfilt_catalog = NULL;
    147153
    148154    /* get split filename from main header (paths relative to cpt file) */
     
    192198  if (catalog[0].catflags & LOAD_SECF) {
    193199    ALLOCATE (secfilt, Catalog, 1);
    194  
     200    secfilt[0].measure_catalog = NULL;
     201    secfilt[0].missing_catalog = NULL;
     202    secfilt[0].secfilt_catalog = NULL;
     203
    195204    /* get split filename from main header (paths relative to cpt file) */
    196205    if (!gfits_scan (&catalog[0].header, "SECFILT",  "%s", 1, string)) return (FALSE);
     
    244253/* save_catalog_split writes complete new files from scratch */
    245254
    246 int save_catalog_split (Catalog *catalog, char VERBOSE) {
     255int dvo_catalog_save_split (Catalog *catalog, char VERBOSE) {
    247256
    248257  int Nitems;
     
    434443 */
    435444
    436 int update_catalog_split (Catalog *catalog, char VERBOSE) {
     445int dvo_catalog_update_split (Catalog *catalog, char VERBOSE) {
    437446
    438447  int i, Nx, Ny, Nlines;
  • trunk/Ohana/src/libdvo/src/skyregion_gsc.c

    r8295 r8328  
    11# include "dvo.h"
    2 # define NBANDS 24
     2# define NDECBANDS 24
    33# define NDIV 4
    44
    5 static int DecLines[] = {593, 584, 551, 530, 522, 465, 406, 362, 280, 198, 123, 25, 0,
    6                          597, 578, 574, 577, 534, 499, 442, 376, 294, 212, 144, 48, 0};
    7 
    8 static double DecBands[] = {0.0, +7.5, +15.0, +22.5, +30.0, +37.5, +45.0, +52.5, +60.0, +67.5, +75.0, +82.5, +90.0,
    9                             0.0, -7.5, -15.0, -22.5, -30.0, -37.5, -45.0, -52.5, -60.0, -67.5, -75.0, -82.5, -90.0};
    10 
    11 static char *DecNames[] = {"n0000", "n0730", "n1500", "n2230", "n3000", "n3730", "n4500", "n5230", "n6000", "n6730", "n7500", "n8230", "none",
    12                            "s0000", "s0730", "s1500", "s2230", "s3000", "s3730", "s4500", "s5230", "s6000", "s6730", "s7500", "s8230", "none"};
     5static int DecLines[] = {593, 584, 551, 530, 522, 465, 406, 362, 280, 198, 123, 25, 597, 578, 574, 577, 534, 499, 442, 376, 294, 212, 144, 48};
     6static double DecMin[] = {0.0, +7.5, +15.0, +22.5, +30.0, +37.5, +45.0, +52.5, +60.0, +67.5, +75.0, +82.5, -7.5, -15.0, -22.5, -30.0, -37.5, -45.0, -52.5, -60.0, -67.5, -75.0, -82.5, -90.0};
     7static double DecMax[] = {+7.5, +15.0, +22.5, +30.0, +37.5, +45.0, +52.5, +60.0, +67.5, +75.0, +82.5, +90.0, 0.0, -7.5, -15.0, -22.5, -30.0, -37.5, -45.0, -52.5, -60.0, -67.5, -75.0, -82.5};
     8static char *DecNames[] = {"n0000", "n0730", "n1500", "n2230", "n3000", "n3730", "n4500", "n5230", "n6000", "n6730", "n7500", "n8230", "s0000", "s0730", "s1500", "s2230", "s3000", "s3730", "s4500", "s5230", "s6000", "s6730", "s7500", "s8230"};
     9
     10// L0 : full sky
     11// L1 : Dec bands
     12// L2 : RA segments
     13// L3 : GSC regions
     14// L4 : GSC subdivisions
     15
     16// a zone contains a set of L3 regions of the same size
     17typedef struct {
     18  int L1band;   // parent band
     19  int L3start;  // start of zone in L1 band
     20  int L3end;    // end of zone in L1 band (last + 1)
     21  float dDec;   // height of L3 region in zone
     22  float Rmin;
     23  float Rmax;
     24  float Dmin;
     25  float Dmax;
     26  int Nset;
     27} SkyRegionZone;
     28
     29SkyTable *SkyRegionForDecBand (char *buffer, int Nregions, char *DecName, float Dmin, float Dmax);
     30SkyRegionZone *SkyRegionFindZones (SkyTable *band, int *Nzones, int parent);
     31void SkyTableL2fromZone (SkyTable *L2, SkyTable *L3, SkyTable *L4, SkyTable *band, SkyRegionZone *zone, int parent);
     32void SkyTableL3fromL2 (SkyRegion *L2, SkyTable *L3, SkyTable *L4, SkyTable *band, int Ns, int Ne);
     33void SkyTableL4fromL3 (SkyRegion *L3, SkyTable *L4);
    1334
    1435void SkyTableSort (SkyTable *table);
     36void SkyTableAppend (SkyTable *old, SkyTable *new, int Nprev);
     37int NsetForDecRange (float dDec);
     38void SkyRegionPrint (SkyRegion *region);
    1539
    1640SkyTable *SkyTableFromGSC (char *filename, int depth, int VERBOSE) {
    1741
    18   int i, j, Nx, Ny, No, Nb, Nr, NR, Nlines;
    19   int nx, ny, Ne, Ns, Nbox;
    20   double RA0, RA1, DEC0, DEC1, dR, dD;
     42  int i, j, skipLines, Nzones;
    2143  char temp[80], name[80];
    2244
     
    2547  SkyTable *skytable;
    2648  SkyRegion *regions;
    27   SkyTable bandregions;
     49  SkyTable *band;
     50  SkyTable L0, L1, L2, L3, L4;
     51  SkyRegionZone *zones;
     52
    2853  FILE *f;
    2954 
     
    3459  }
    3560
    36   /* load in table data */
     61/* load in table data */
    3762  ftable.header = &theader;
    3863  if (!gfits_fread_ftable (f, &ftable, "REGIONS")) {
     
    4166    return (NULL);
    4267  }
    43 
    44   gfits_scan (ftable.header, "NAXIS1", "%d", 1, &Nx);
    45   gfits_scan (ftable.header, "NAXIS2", "%d", 1, &Ny);
    46  
    4768  gfits_free_header (&theader);
    4869
    49   /* build supporting level 0 and 1 regions */
    50   Nr = 0;
    51   NR = 100;
    52   ALLOCATE (regions, SkyRegion, 100);
    53  
    54   /* level 0 : full sky */
    55   regions[Nr].Rmin      =   0;
    56   regions[Nr].Rmax      = 360;
    57   regions[Nr].Dmin      = -90;
    58   regions[Nr].Dmax      = +90;
    59   regions[Nr].index     =  0;
    60   regions[Nr].depth     =  0;
    61   regions[Nr].parent    = -1;
    62   regions[Nr].child     =  TRUE;
    63   regions[Nr].table     =  (depth == 0);
    64   strcpy (regions[Nr].name, "fullsky");
    65  
    66  
    67   No = Nr;
    68   Nr ++;
    69 
    70   /* level 1 : add the dec bands */
    71   regions[No].childS = Nr;
    72   /* first north */
    73   for (i = 0; i < 12; i++, Nr++) {
    74     regions[Nr].Rmin      =   0;
    75     regions[Nr].Rmax      = 360;
    76     regions[Nr].Dmin      = DecBands[i];
    77     regions[Nr].Dmax      = DecBands[i+1];
    78     regions[Nr].index     =  i+1;
    79     regions[Nr].depth     =  1;
    80     regions[Nr].parent    =  0;
    81     regions[Nr].child     =  TRUE;
    82     regions[Nr].table     =  (depth == 1);
    83     strcpy (regions[Nr].name, DecNames[i]);
    84   }
    85   /* now south */
    86   for (i = 0; i < 12; i++, Nr++) {
    87     regions[Nr].Rmin      =   0;
    88     regions[Nr].Rmax      = 360;
    89     regions[Nr].Dmin      = DecBands[i+14];
    90     regions[Nr].Dmax      = DecBands[i+13];
    91     regions[Nr].index     =  i+1;
    92     regions[Nr].depth     =  1;
    93     regions[Nr].parent    =  0;
    94     regions[Nr].child     =  TRUE;
    95     regions[Nr].table     =  (depth == 1);
    96     strcpy (regions[Nr].name, DecNames[i+13]);
    97   }
    98   regions[No].childE = Nr;
    99 
    100   /* level 2 : identify the blocks in the DEC bands */
    101   No = 1;
    102   for (i = 0; i < 25; i++) {
    103     if (i == 12) continue;
    104 
    105     Nlines = 0;
    106     for (j = 0; j < i; j++) Nlines += DecLines[j];
    107 
    108     /* find and save all GSC Regions in this band */
    109     Nb = 0;
    110     ALLOCATE (bandregions.regions, SkyRegion, DecLines[i]);
    111     ALLOCATE (bandregions.filename, char *, DecLines[i]);
    112     for (j = 0; j < DecLines[i]; j++) {
    113       strncpy (temp, &ftable.buffer[(j + Nlines)*48], 48);
    114       temp[49] = 0;
    115       hstgsc_hms_to_deg (&RA0, &RA1, &DEC0, &DEC1, &temp[7]);
    116       if (DEC1 < DEC0) SWAP (DEC0, DEC1);
    117 
    118       /* check that we are in correct Dec band */
    119       if (DEC1 < regions[No].Dmin) {
    120         fprintf (stderr, "error: table mis-match (1)\n");
    121         fprintf (stderr, "line: %s\n", temp);
    122         fprintf (stderr, "region %d: %f - %f\n", No, regions[No].Dmin, regions[No].Dmax);
    123         return (NULL);
    124       }
    125       if (DEC0 > regions[No].Dmax) {
    126         fprintf (stderr, "error: table mis-match (2)\n");
    127         fprintf (stderr, "line: %s\n", temp);
    128         fprintf (stderr, "region %d: %f - %f\n", No, regions[No].Dmin, regions[No].Dmax);
    129         return (NULL);
    130       }
    131 
    132       /* regions with RA0 < 360 have RA1 == 0.0 */
    133       if (RA1 < RA0) RA1 += 360.0;
    134 
    135       bandregions.regions[Nb].Dmin = DEC0;
    136       bandregions.regions[Nb].Dmax = DEC1;
    137       bandregions.regions[Nb].Rmin = RA0;
    138       bandregions.regions[Nb].Rmax = RA1;
    139       bandregions.filename[Nb] = NULL;
    140       Nb ++;
    141     }
    142     bandregions.Nregions = Nb;
     70/* generate the regions for each level independently. indices (parent,childE,childS)
     71   refer to the value within the independent group.  at the end, we combine and adjust
     72   the indices */
     73
     74/* L0 : full sky */
     75  L0.Nregions = 1;
     76  ALLOCATE (L0.regions, SkyRegion, L0.Nregions);
     77  L0.regions[0].Rmin    =   0;
     78  L0.regions[0].Rmax    = 360;
     79  L0.regions[0].Dmin    = -90;
     80  L0.regions[0].Dmax    = +90;
     81  L0.regions[0].index   =  0;
     82  L0.regions[0].depth   =  0;
     83  L0.regions[0].parent  = -1;
     84  L0.regions[0].child   =  TRUE;
     85  L0.regions[0].table   = -1;
     86  L0.regions[0].childS  =  0;
     87  L0.regions[0].childE  =  NDECBANDS;
     88  strcpy (L0.regions[0].name, "fullsky");
     89  // SkyRegionPrint (&L0.regions[0]);
     90
     91  /* allocate space for all levels */
     92  L1.Nregions = L2.Nregions = L3.Nregions = L4.Nregions = 0;
     93  ALLOCATE (L1.regions, SkyRegion, 1);
     94  ALLOCATE (L2.regions, SkyRegion, 1);
     95  ALLOCATE (L3.regions, SkyRegion, 1);
     96  ALLOCATE (L4.regions, SkyRegion, 1);
     97
     98  /* L1 : dec bands */
     99  skipLines = 0;
     100  for (i = 0; i < NDECBANDS; i++) {
     101    L1.regions[i].Rmin     = 0;
     102    L1.regions[i].Rmax     = 360;
     103    L1.regions[i].Dmin     = DecMin[i];
     104    L1.regions[i].Dmax     = DecMax[i];
     105    L1.regions[i].index    = i;
     106    L1.regions[i].depth    = 1;
     107    L1.regions[i].parent   = 0;
     108    L1.regions[i].child    = TRUE;
     109    L1.regions[i].table    = -1;
     110    strcpy (L1.regions[i].name, DecNames[i]);
     111    // SkyRegionPrint (&L1.regions[i]);
     112
     113    /* build the L2 regions for this L1 region (based on zones) */
     114    L1.regions[i].childS   = L2.Nregions;
     115
     116    /* load all GSC Regions in this band */
     117    band = SkyRegionForDecBand (&ftable.buffer[skipLines*48], DecLines[i], DecNames[i], L1.regions[i].Dmin, L1.regions[i].Dmax);
     118    skipLines += DecLines[i];
    143119
    144120    /* sort the band regions by Rmin */
    145     SkyTableSort (&bandregions);
     121    SkyTableSort (band);
    146122   
    147     {
    148       int k, Nt, Nrange, Ndiv, Nset;
    149       int *transitions;
    150       float dDec, dDcur;
    151 
    152       /* search for transitions in the region dDec */
    153       ALLOCATE (transitions, int, 50);
    154       Nt = 0;
    155       dDec = bandregions.regions[0].Dmax - bandregions.regions[0].Dmin;
    156       dDcur = dDec;
    157       transitions[0] = 0;
    158       Nt++;
    159       DEC0 = bandregions.regions[0].Dmin;
    160       DEC1 = bandregions.regions[0].Dmax;
    161       for (j = 0; j < Nb; j++) {
    162         DEC0 = MIN (bandregions.regions[j].Dmin, DEC0);
    163         DEC1 = MAX (bandregions.regions[j].Dmax, DEC1);
    164         dDec = bandregions.regions[j].Dmax - bandregions.regions[j].Dmin;
    165         if (dDec != dDcur) {
    166           dDcur = dDec;
    167           transitions[Nt] = j;
    168           Nt++;
    169         }
    170       }
    171       transitions[Nt] = Nb;
    172       Nt++;
    173 
    174       /* subdivide each transition range */
    175       for (j = 1; j < Nt; j++) {
    176         Ne = transitions[j];
    177         Ns = transitions[j-1];
    178         dDec = bandregions.regions[Ns].Dmax - bandregions.regions[Ns].Dmin;
    179         fprintf (stderr, "trans: %d - %d : ", Ns, Ne);
    180         fprintf (stderr, "%f,%f - %f,%f\n",
    181                  bandregions.regions[Ns].Rmin, bandregions.regions[Ne-1].Rmax,
    182                  bandregions.regions[Ns].Dmin, bandregions.regions[Ns].Dmax);
    183 
    184         Nrange = transitions[j] - transitions[j-1];
    185         Ndiv = (int) (0.5 + 7.5 / dDec);
    186         switch (Ndiv) {
    187           case 2:
    188             Nset = 4*2;
    189             break;
    190           case 3:
    191             Nset = 6*3;
    192             break;
    193           case 4:
    194             Nset = 8*4;
    195             break;
    196           default:
    197             fprintf (stderr, "programming error\n");
    198             exit (2);
    199         }
    200         for (k = Ns; k < Ne; k += Nset) {
    201           regions[Nr].Rmin = bandregions.regions[k].Rmin;
    202           if (k + Nset < Ne) {
    203             regions[Nr].Rmax = bandregions.regions[k+Nset-1].Rmax;
    204           } else {
    205             regions[Nr].Rmax = bandregions.regions[Ne-1].Rmax;
    206           }     
    207           regions[Nr].Dmin = DEC0;
    208           regions[Nr].Dmax = DEC1;
    209           fprintf (stderr, "new: %f - %f :%f - %f\n",
    210                    regions[Nr].Rmin, regions[Nr].Rmax,
    211                    regions[Nr].Dmin, regions[Nr].Dmax);
    212         }
    213 
    214         regions[Nr].index    =  Nr;
    215         regions[Nr].depth    =  2;
    216         regions[Nr].parent   =  No;
    217         regions[Nr].child    =  TRUE;
    218         regions[Nr].table    =  (depth == 2);
    219         regions[Nr].childS   =  0;
    220         regions[Nr].childE   =  0;
    221       }
    222       regions[No].childS = Nr;
    223       regions[No].childE = Nr;
    224       No ++;
    225     }
    226   }
    227 
    228   /* level 3 : copy the data from the GSC Region files */
    229   /* XXX fix this: level here is now 3 */
    230   No = 1;
    231   for (i = 0; i < 25; i++) {
    232     if (i == 12) continue;
    233 
    234     Nlines = 0;
    235     for (j = 0; j < i; j++) Nlines += DecLines[j];
    236 
    237     /* find all GSC Regions in this band */
    238     regions[No].childS = Nr;
    239     for (j = 0; j < DecLines[i]; j++) {
    240       strncpy (temp, &ftable.buffer[(j + Nlines)*48], 48);
    241       temp[49] = 0;
    242       hstgsc_hms_to_deg (&RA0, &RA1, &DEC0, &DEC1, &temp[7]);
    243       if (DEC1 < DEC0) SWAP (DEC0, DEC1);
    244 
    245       /* check that we are in correct Dec band */
    246       if (DEC1 < regions[No].Dmin) {
    247         fprintf (stderr, "error: table mis-match (1)\n");
    248         fprintf (stderr, "line: %s\n", temp);
    249         fprintf (stderr, "region %d: %f - %f\n", No, regions[No].Dmin, regions[No].Dmax);
    250         return (NULL);
    251       }
    252       if (DEC0 > regions[No].Dmax) {
    253         fprintf (stderr, "error: table mis-match (2)\n");
    254         fprintf (stderr, "line: %s\n", temp);
    255         fprintf (stderr, "region %d: %f - %f\n", No, regions[No].Dmin, regions[No].Dmax);
    256         return (NULL);
    257       }
    258 
    259       /* regions with RA0 < 360 have RA1 == 0.0 */
    260       if (RA1 < RA0) RA1 += 360.0;
    261 
    262       regions[Nr].Dmin = DEC0;
    263       regions[Nr].Dmax = DEC1;
    264       regions[Nr].Rmin = RA0;
    265       regions[Nr].Rmax = RA1;
    266 
    267       regions[Nr].index    =  Nr;
    268       regions[Nr].depth    =  2;
    269       regions[Nr].parent   =  No;
    270       regions[Nr].child    =  TRUE;
    271       regions[Nr].table    =  (depth == 2);
    272       regions[Nr].childS   =  0;
    273       regions[Nr].childE   =  0;
    274 
    275       temp[5] = 0;
    276       sprintf (name, "%s/%s", DecNames[i], &temp[1]);
    277       strcpy (regions[Nr].name, name);
    278       Nr ++;
    279       CHECK_REALLOCATE (regions, SkyRegion, NR, Nr, 100);
    280     }
    281     regions[No].childE = Nr;
    282     No ++;
    283   }
    284 
    285   /* subdivide level 2 into NDIV boxes */
    286   /* XXX level here is now 4 */
    287   Ns = regions[regions[0].childS].childS;
    288   Ne = regions[regions[0].childE - 1].childE;
    289   for (i = Ns; i < Ne; i++) {
    290 
    291     Nbox = 0;
    292     RA0 = regions[i].Rmin;
    293     DEC0 = regions[i].Dmin;
    294     dR = (regions[i].Rmax - regions[i].Rmin) / NDIV;
    295     dD = (regions[i].Dmax - regions[i].Dmin) / NDIV;
    296 
    297     regions[i].childS = Nr;
    298     for (ny = 0; ny < NDIV; ny ++) {
    299       for (nx = 0; nx < NDIV; nx ++) {
    300         regions[Nr].Rmin     = RA0  + (nx + 0)*dR;
    301         regions[Nr].Rmax     = RA0  + (nx + 1)*dR;
    302         regions[Nr].Dmin     = DEC0 + (ny + 0)*dD;
    303         regions[Nr].Dmax     = DEC0 + (ny + 1)*dD;
    304 
    305         regions[Nr].index    =  Nr;
    306         regions[Nr].depth    =  3;
    307         regions[Nr].parent   =  i;
    308         regions[Nr].child    =  FALSE;
    309         regions[Nr].table    =  (depth == 3);
    310         regions[Nr].childS   =  0;
    311         regions[Nr].childE   =  0;
    312 
    313         temp[5] = 0;
    314         sprintf (name, "%s.%02d", regions[i].name, Nbox);
    315         strcpy (regions[Nr].name, name);
    316 
    317         Nr ++;
    318         Nbox ++;
    319         CHECK_REALLOCATE (regions, SkyRegion, NR, Nr, 100);
    320       }
    321     }
    322     regions[i].childE = Nr;
    323   }
    324 
     123    zones = SkyRegionFindZones (band, &Nzones, i);
     124
     125    /* subdivide each zone */
     126    for (j = 0; j < Nzones; j++) {
     127      SkyTableL2fromZone (&L2, &L3, &L4, band, &zones[j], i);
     128    }
     129    free (zones);
     130    SkyTableFree (band);
     131
     132    /* at end of loop, L2.Nregions has been updated to end of L2.regions for L1.region */
     133    L1.regions[i].childE   = L2.Nregions;
     134
     135    L1.Nregions ++;
     136    REALLOCATE (L1.regions, SkyRegion, L1.Nregions + 1);
     137  }
     138
     139  /* merge L1 into L0 */
    325140  ALLOCATE (skytable, SkyTable, 1);
    326   skytable[0].regions = regions;
    327   skytable[0].Nregions = Nr;
    328 
     141  ALLOCATE (skytable[0].regions, SkyRegion, 1);
     142  skytable[0].Nregions = 0;
     143
     144  SkyTableAppend (skytable, &L0, 0);
     145  SkyTableAppend (skytable, &L1, skytable[0].Nregions - L0.Nregions);
     146  SkyTableAppend (skytable, &L2, skytable[0].Nregions - L1.Nregions);
     147  SkyTableAppend (skytable, &L3, skytable[0].Nregions - L2.Nregions);
     148  SkyTableAppend (skytable, &L4, skytable[0].Nregions - L3.Nregions);
     149
     150  /* fix the depth elements and create the filename array */
    329151  ALLOCATE (skytable[0].filename, char *, skytable[0].Nregions);
    330152  for (i = 0; i < skytable[0].Nregions; i++) {
    331153    skytable[0].filename[i] = NULL;
    332   }
    333 
     154    skytable[0].regions[i].table = (skytable[0].regions[i].depth == depth);
     155  }
    334156  return (skytable);
     157}
     158
     159SkyTable *SkyRegionForDecBand (char *buffer, int Nregions, char *DecName, float DminBand, float DmaxBand) {
     160
     161  int i;
     162  double Rmin, Rmax, Dmin, Dmax;
     163  char temp[80], name[80];
     164  SkyTable *band;
     165  SkyRegion *regions;
     166
     167  ALLOCATE (band, SkyTable, 1);
     168  ALLOCATE (regions, SkyRegion, Nregions);
     169  for (i = 0; i < Nregions; i++) {
     170    strncpy (temp, &buffer[i*48], 48);
     171    temp[49] = 0;
     172    hstgsc_hms_to_deg (&Rmin, &Rmax, &Dmin, &Dmax, &temp[7]);
     173    if (Dmax < Dmin) SWAP (Dmin, Dmax);
     174
     175    /* check that we are in correct Dec band */
     176    if ((Dmax < DminBand) || (Dmin > DmaxBand)) {
     177      fprintf (stderr, "error with raw table: table mis-match\n");
     178      fprintf (stderr, "line: %s\n", temp);
     179      fprintf (stderr, "region %d: %f - %f vs %f - %f\n", i, Dmin, Dmax, DminBand, DmaxBand);
     180      exit (2);
     181    }
     182
     183    /* regions near 0,360 boundary have Rmax == 0.0 */
     184    if (Rmax < Rmin) Rmax += 360.0;
     185
     186    regions[i].Dmin = Dmin;
     187    regions[i].Dmax = Dmax;
     188    regions[i].Rmin = Rmin;
     189    regions[i].Rmax = Rmax;
     190
     191    temp[5] = 0;
     192    sprintf (name, "%s/%s", DecName, &temp[1]);
     193    strcpy (regions[i].name, name);
     194  }
     195  band[0].filename = NULL;
     196  band[0].regions = regions;
     197  band[0].Nregions = Nregions;
     198  return (band);
    335199}
    336200
     
    355219      l--;
    356220      tempregion = regions[l];
    357       tempfile   = filename[l];
     221      if (filename) tempfile   = filename[l];
    358222    } else {
    359223      tempregion   = regions[ir];
    360       tempfile     = filename[ir];
     224      if (filename) tempfile     = filename[ir];
    361225      regions[ir]   = regions[0];
    362       filename[ir] = filename[0];
     226      if (filename) filename[ir] = filename[0];
    363227      if (--ir == 0) {
    364228        regions[0]   = tempregion;
    365         filename[0] = tempfile;
     229        if (filename) filename[0] = tempfile;
    366230        return;
    367231      }
     
    373237      if (tempregion.Rmin < regions[j].Rmin) {
    374238        regions[i]   = regions[j];
    375         filename[i] = filename[j];
     239        if (filename) filename[i] = filename[j];
    376240        j += (i=j) + 1;
    377241      }
     
    379243    }
    380244    regions[i]   = tempregion;
    381     filename[i] = tempfile;
    382   }
    383 }
     245    if (filename) filename[i] = tempfile;
     246  }
     247}
     248
     249/* give a complete set of regions in a Dec band, subdivide into zones of equal-sized regions */
     250SkyRegionZone *SkyRegionFindZones (SkyTable *band, int *Nzones, int parent) {
     251 
     252  float Dmin, Dmax, dDec;
     253  int i, Nz, NZ, Ndiv, Nregions;
     254  SkyRegion *regions;
     255  SkyRegionZone *zones;
     256 
     257  regions = band[0].regions;
     258  Nregions = band[0].Nregions;
     259
     260  Nz = 0;
     261  NZ = 10;
     262  ALLOCATE (zones, SkyRegionZone, NZ);
     263 
     264  zones[0].dDec = regions[0].Dmax - regions[0].Dmin;
     265  zones[0].Rmin = regions[0].Rmin;
     266  zones[0].L3start = 0;
     267  zones[0].L1band = parent;
     268  zones[0].Nset = NsetForDecRange (zones[0].dDec);
     269
     270  /* search for transitions in the region dDec, find the max Dec range */
     271  Dmin = regions[0].Dmin;
     272  Dmax = regions[0].Dmax;
     273  for (i = 0; i < Nregions; i++) {
     274    Dmin = MIN (regions[i].Dmin, Dmin);
     275    Dmax = MAX (regions[i].Dmax, Dmax);
     276    dDec = regions[i].Dmax - regions[i].Dmin;
     277    if (dDec != zones[Nz].dDec) {
     278      /* we've found the end of the current zone */
     279      zones[Nz].L3end = i;
     280      zones[Nz].Rmax = regions[i-1].Rmax;
     281      Nz++;
     282      CHECK_REALLOCATE (zones, SkyRegionZone, NZ, Nz, 10);
     283
     284      /* start info for the new zone */
     285      zones[Nz].Rmin = regions[i].Rmin;
     286      zones[Nz].dDec = dDec;
     287      zones[Nz].L3start = i;
     288      zones[Nz].L1band = parent;
     289      zones[Nz].Nset = NsetForDecRange (zones[Nz].dDec);
     290    }
     291  }
     292  zones[Nz].L3end = i;
     293  zones[Nz].Rmax = regions[i-1].Rmax;
     294  Nz ++;
     295
     296  for (i = 0; i < Nz; i++) {
     297    zones[i].Dmin = Dmin;
     298    zones[i].Dmax = Dmax;
     299  }
     300
     301  *Nzones = Nz;
     302  return (zones);
     303}
     304
     305int NsetForDecRange (float dDec) {
     306
     307  int Ndiv, Nset;
     308 
     309  /* max segment size is ~7.5 x 15 degrees */
     310  Ndiv = (int) (0.5 + 7.5 / dDec);
     311  Nset = 2*Ndiv*Ndiv;
     312  return (Nset);
     313}
     314 
     315void SkyTableL2fromZone (SkyTable *L2, SkyTable *L3, SkyTable *L4, SkyTable *band, SkyRegionZone *zone, int parent) {
     316
     317  int i, Nr, Ns, Ne;
     318  char *p, name[80];
     319
     320  Nr = L2[0].Nregions;
     321  REALLOCATE (L2[0].regions, SkyRegion, Nr + 1);
     322 
     323  /* divide this zone into L2 regions with Nset L3 regions each (fewer on ends) */
     324  for (i = zone[0].L3start; i < zone[0].L3end; i += zone[0].Nset) {
     325    Ns = i;
     326    Ne = MIN (i + zone[0].Nset, zone[0].L3end);
     327
     328    L2[0].regions[Nr].Rmin = band[0].regions[Ns].Rmin;
     329    L2[0].regions[Nr].Rmax = band[0].regions[Ne - 1].Rmax;
     330    L2[0].regions[Nr].Dmin = zone[0].Dmin;
     331    L2[0].regions[Nr].Dmax = zone[0].Dmax;
     332   
     333    L2[0].regions[Nr].index    =  Nr;
     334    L2[0].regions[Nr].depth    =  2;
     335    L2[0].regions[Nr].table    =  -1;
     336    L2[0].regions[Nr].parent   =  parent;
     337    L2[0].regions[Nr].child    =  FALSE;
     338    L2[0].regions[Nr].childS   =  0;
     339    L2[0].regions[Nr].childE   =  0;
     340
     341    /* define names for L2 regions */
     342    p = strchr (band[0].regions[Ns].name, '/');
     343    if (p == NULL) {
     344      fprintf (stderr, "programming error in SkyTableL2fromZone\n");
     345      exit (2);
     346    }
     347    *p = 0;
     348    sprintf (name, "%s/z%03d", band[0].regions[Ns].name, Nr);
     349    *p = '/';
     350    strcpy (L2[0].regions[Nr].name, name);
     351    // SkyRegionPrint (&L2[0].regions[Nr]);
     352
     353    /* childS and childE are set in SkyTableL3fromL2 */
     354    SkyTableL3fromL2 (&L2[0].regions[Nr], L3, L4, band, Ns, Ne);
     355
     356    Nr++;
     357    REALLOCATE (L2[0].regions, SkyRegion, Nr + 1);
     358  }
     359  L2[0].Nregions = Nr;
     360}
     361
     362void SkyTableL3fromL2 (SkyRegion *L2, SkyTable *L3, SkyTable *L4, SkyTable *band, int Ns, int Ne) {
     363
     364  int i, Nr;
     365
     366  Nr = L3[0].Nregions;
     367  L3[0].Nregions += Ne - Ns;
     368  REALLOCATE (L3[0].regions, SkyRegion, L3[0].Nregions);
     369
     370  L2[0].child  = TRUE;
     371  L2[0].childS = Nr;
     372  L2[0].childE = L3[0].Nregions;
     373
     374  /* copy the band entries corresponding to this region in the L3 table */
     375  for (i = Ns; i < Ne; i++) {
     376
     377    L3[0].regions[Nr] = band[0].regions[i];
     378   
     379    L3[0].regions[Nr].index    =  Nr;
     380    L3[0].regions[Nr].depth    =  3;
     381    L3[0].regions[Nr].table    =  -1;
     382    L3[0].regions[Nr].parent   =  L2[0].index;
     383    L3[0].regions[Nr].child    =  FALSE;
     384    L3[0].regions[Nr].childS   =  0;
     385    L3[0].regions[Nr].childE   =  0;
     386
     387    // SkyRegionPrint (&L3[0].regions[Nr]);
     388    /* name is set for the band in SkyRegionForDecBand */
     389
     390    /* childS and childE are set in SkyTableL3fromL3 */
     391    SkyTableL4fromL3 (&L3[0].regions[Nr], L4);
     392    Nr++;
     393  }
     394
     395  return;
     396}
     397
     398void SkyTableL4fromL3 (SkyRegion *L3, SkyTable *L4) {
     399
     400  int nx, ny, Nr, Nbox;
     401  double Rmin, Dmin, dR, dD;
     402  char name[80];
     403
     404  Nr = L4[0].Nregions;
     405  L4[0].Nregions += NDIV*NDIV;
     406  REALLOCATE (L4[0].regions, SkyRegion, L4[0].Nregions);
     407
     408  L3[0].child  = TRUE;
     409  L3[0].childS = Nr;
     410  L3[0].childE = L4[0].Nregions;
     411
     412  /* subdivide L3 into NDIV boxes */
     413  Rmin = L3[0].Rmin;
     414  Dmin = L3[0].Dmin;
     415  dR = (L3[0].Rmax - L3[0].Rmin) / NDIV;
     416  dD = (L3[0].Dmax - L3[0].Dmin) / NDIV;
     417
     418  Nbox = 0;
     419  for (ny = 0; ny < NDIV; ny ++) {
     420    for (nx = 0; nx < NDIV; nx ++) {
     421      L4[0].regions[Nr].Rmin     = Rmin  + (nx + 0)*dR;
     422      L4[0].regions[Nr].Rmax     = Rmin  + (nx + 1)*dR;
     423      L4[0].regions[Nr].Dmin     = Dmin + (ny + 0)*dD;
     424      L4[0].regions[Nr].Dmax     = Dmin + (ny + 1)*dD;
     425
     426      L4[0].regions[Nr].index    =  Nr;
     427      L4[0].regions[Nr].depth    =  4;
     428      L4[0].regions[Nr].table    =  -1;
     429      L4[0].regions[Nr].parent   =  L3[0].index;
     430      L4[0].regions[Nr].child    =  FALSE;
     431      L4[0].regions[Nr].childS   =  0;
     432      L4[0].regions[Nr].childE   =  0;
     433
     434      sprintf (name, "%s.%02d", L3[0].name, Nbox);
     435      strcpy (L4[0].regions[Nr].name, name);
     436      // SkyRegionPrint (&L4[0].regions[Nr]);
     437
     438      Nr ++;
     439      Nbox ++;
     440    }
     441  }
     442  return;
     443}
     444
     445void SkyTableAppend (SkyTable *old, SkyTable *new, int Nprev) {
     446
     447  int i, Nold;
     448
     449  Nold = old[0].Nregions;
     450
     451  old[0].Nregions += new[0].Nregions;
     452  REALLOCATE (old[0].regions, SkyRegion, old[0].Nregions);
     453
     454  for (i = Nprev; i < Nold; i++) {
     455    old[0].regions[i].childS += Nold;
     456    old[0].regions[i].childE += Nold;
     457  }
     458
     459  for (i = 0; i < new[0].Nregions; i++) {
     460    old[0].regions[i + Nold] = new[0].regions[i];
     461    old[0].regions[i + Nold].parent += Nprev;
     462  }
     463  return;
     464}
     465
     466void SkyRegionPrint (SkyRegion *region) {
     467
     468  int i;
     469
     470  fprintf (stderr, "L%d:", region[0].depth);
     471  for (i = 0; i < region[0].depth; i++) {
     472    fprintf (stderr, " ");
     473  }
     474  fprintf (stderr, "%s : %f - %f : %f - %f\n", region[0].name, region[0].Rmin, region[0].Rmax, region[0].Dmin, region[0].Dmax);
     475  return;
     476}
     477
     478/***
     479    notes and questions:
     480    1) is the regions.index value used?
     481
     482
     483***/
  • trunk/Ohana/src/libdvo/src/skyregion_ops.c

    r7080 r8328  
    327327  int i;
    328328
     329  /* XXX do we need to free the filename array as well? */
    329330  if (list == NULL) return (TRUE);
    330331  if (list[0].regions != NULL) {
     
    349350
    350351  if (table == NULL) return (TRUE);
    351   if (table[0].regions != NULL) {
     352  if (table[0].filename != NULL) {
    352353    for (i = 0; i < table[0].Nregions; i++) {
    353354      if (table[0].filename[i] != NULL) {
     
    355356      }
    356357    }
     358    free (table[0].filename);
     359  }
     360  if (table[0].regions != NULL) {
    357361    free (table[0].regions);
    358362  }
  • trunk/Ohana/src/opihi/dvo/skycat.c

    r7917 r8328  
    66 
    77  double Radius;
    8   int i, j, N, Nregions, ShowAll, NPTS, Npts, leftside, Depth, TableDepth, VERBOSE;
     8  int i, j, N, Nregions, ShowAll, NPTS, Npts, leftside, Depth, VERBOSE;
    99  struct stat filestat;
    1010  Vector Xvec, Yvec;
     
    3939  }
    4040  SetGraph (graphmode);
    41 
    42   TableDepth = (Depth == 3) ? 3 : 2;
    4341
    4442  Radius = MAX (fabs(graphmode.xmax), fabs(graphmode.ymax));
  • trunk/Ohana/src/relphot/src/load_catalogs.c

    r7080 r8328  
    1515  for (i = 0; i < skylist[0].Nregions; i++) {
    1616    tcatalog.filename = skylist[0].filename[i];
     17
     18    tcatalog.catformat = dvo_catalog_format (CATFORMAT);  // set the default catformat from config data
     19    tcatalog.catmode   = dvo_catalog_mode (CATMODE);      // set the default catmode from config data
     20    tcatalog.lockmode  = LCK_SOFT;
     21    dvo_catalog_open (&tcatalog, skylist[0].regions[i], Nsecfilt, "r");
     22
    1723    switch (lock_catalog (&tcatalog, LCK_SOFT)) {
    1824    case 0:
Note: See TracChangeset for help on using the changeset viewer.