public software.sextractor

[/] [trunk/] [src/] [assoc.c] - Diff between revs 235 and 241

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 235 Rev 241
Line 5... Line 5...
*
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*
*       This file part of:      SExtractor
*       This file part of:      SExtractor
*
*
*       Copyright:              (C) 1997-2010 Emmanuel Bertin -- IAP/CNRS/UPMC
*       Copyright:              (C) 1997-2011 Emmanuel Bertin -- IAP/CNRS/UPMC
*
*
*       License:                GNU General Public License
*       License:                GNU General Public License
*
*
*       SExtractor is free software: you can redistribute it and/or modify
*       SExtractor is free software: you can redistribute it and/or modify
*       it under the terms of the GNU General Public License as published by
*       it under the terms of the GNU General Public License as published by
Line 20... Line 20...
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*       GNU General Public License for more details.
*       GNU General Public License for more details.
*       You should have received a copy of the GNU General Public License
*       You should have received a copy of the GNU General Public License
*       along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
*       along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
*
*
*       Last modified:          11/10/2010
*       Last modified:          12/01/2011
*
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
 
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
#include        "config.h"
#include        "config.h"
Line 37... Line 37...
 
 
#include        "define.h"
#include        "define.h"
#include        "globals.h"
#include        "globals.h"
#include        "prefs.h"
#include        "prefs.h"
#include        "assoc.h"
#include        "assoc.h"
 
#include        "fitswcs.h"
 
 
/********************************* comp_assoc ********************************/
/********************************* comp_assoc ********************************/
/*
/*
Comparison function for sort_assoc().
Comparison function for sort_assoc().
*/
*/
int     comp_assoc(const void *i1, const void *i2)
int     comp_assoc(const void *i1, const void *i2)
  {
  {
   float        *f1,*f2;
   double       *f1,*f2;
 
 
  f1 = (float *)i1 + 1;
  f1 = (double *)i1 + 1;
  f2 = (float *)i2 + 1;
  f2 = (double *)i2 + 1;
  if (*f1<*f2)
  if (*f1<*f2)
    return -1;
    return -1;
  else return (*f1==*f2)?0:1;
  else return (*f1==*f2)?0:1;
  }
  }
 
 
Line 62... Line 63...
*/
*/
void  sort_assoc(picstruct *field, assocstruct *assoc)
void  sort_assoc(picstruct *field, assocstruct *assoc)
 
 
  {
  {
   int          comp_assoc(const void *i1, const void *i2);
   int          comp_assoc(const void *i1, const void *i2);
   float        *list, rad;
   double       *list, rad;
   int          i,j, step,nobj, *hash;
   int          i,j, step,nobj, *hash;
 
 
  step = assoc->ncol;
  step = assoc->ncol;
  nobj = assoc->nobj;
  nobj = assoc->nobj;
  list = assoc->list;
  list = assoc->list;
  qsort(assoc->list, assoc->nobj, step*sizeof(float), comp_assoc);
  qsort(assoc->list, assoc->nobj, step*sizeof(double), comp_assoc);
/* Build the hash table that contains the first object in the sorted list */
/* Build the hash table that contains the first object in the sorted list */
/* which may be in reach from the current scanline */
/* which may be in reach from the current scanline */
  QMALLOC(assoc->hash, int, field->height);
  QMALLOC(assoc->hash, int, field->height);
  list = assoc->list+1; /* This is where the 1st y coordinate is stored */
  list = assoc->list+1; /* This is where the 1st y coordinate is stored */
  hash = assoc->hash;
  hash = assoc->hash;
Line 92... Line 93...
/********************************* load_assoc ********************************/
/********************************* load_assoc ********************************/
/*
/*
Read an assoc-list, and returns a pointer to the new assoc struct (or NULL if
Read an assoc-list, and returns a pointer to the new assoc struct (or NULL if
no list was found).
no list was found).
*/
*/
assocstruct  *load_assoc(char *filename)
assocstruct  *load_assoc(char *filename, wcsstruct *wcs)
 
 
  {
  {
   assocstruct  *assoc;
   assocstruct  *assoc;
   FILE         *file;
   FILE         *file;
   float        *list, val;
   double       *list, val;
   char         str[MAXCHAR], str2[MAXCHAR], *sstr;
   char         str[MAXCHARL], str2[MAXCHARL], *sstr;
   int          *data,
   int          *data,
                i,ispoon,j,k,l, ncol, ndata, nlist, size,spoonsize,
                i,ispoon,j,k,l, ncol, ndata, nlist, size,spoonsize,
                xindex,yindex,mindex;
                xindex,yindex,mindex;
 
 
  if (!(file = fopen(filename, "r")))
  if (!(file = fopen(filename, "r")))
Line 112... Line 113...
  list  = NULL;                         /* To avoid gcc -Wall warnings */
  list  = NULL;                         /* To avoid gcc -Wall warnings */
  data  = NULL;                         /* To avoid gcc -Wall warnings */
  data  = NULL;                         /* To avoid gcc -Wall warnings */
  ispoon = ncol = ndata = nlist = size = spoonsize = xindex = yindex
  ispoon = ncol = ndata = nlist = size = spoonsize = xindex = yindex
        = mindex = 0;
        = mindex = 0;
  NFPRINTF(OUTPUT, "Reading ASSOC input-list...");
  NFPRINTF(OUTPUT, "Reading ASSOC input-list...");
  for (i=0; fgets(str, MAXCHAR, file);)
  for (i=0; fgets(str, MAXCHARL, file);)
    {
    {
/*-- Examine current input line (discard empty and comment lines) */
/*-- Examine current input line (discard empty and comment lines) */
    if (!*str || strchr("#\t\n",*str))
    if (!*str || strchr("#\t\n",*str))
      continue;
      continue;
 
 
Line 169... Line 170...
 
 
      nlist = ndata+3;
      nlist = ndata+3;
 
 
/*---- Allocate memory for the ASSOC struct and the filtered list */
/*---- Allocate memory for the ASSOC struct and the filtered list */
      QMALLOC(assoc, assocstruct, 1);
      QMALLOC(assoc, assocstruct, 1);
      ispoon = ASSOC_BUFINC/(nlist*sizeof(float));
      ispoon = ASSOC_BUFINC/(nlist*sizeof(double));
      spoonsize = ispoon*nlist;
      spoonsize = ispoon*nlist;
      QMALLOC(assoc->list, float, size = spoonsize);
      QMALLOC(assoc->list, double, size = spoonsize);
      list = assoc->list;
      list = assoc->list;
      }
      }
    else  if (!(i%ispoon))
    else  if (!(i%ispoon))
      {
      {
      QREALLOC(assoc->list, float, size += spoonsize);
      QREALLOC(assoc->list, double, size += spoonsize);
      list = assoc->list + i*nlist;
      list = assoc->list + i*nlist;
      }
      }
 
 
    if (!(++i%1000))
    if (!(++i%1000))
      {
      {
Line 190... Line 191...
 
 
/*-- Read the data normally */
/*-- Read the data normally */
    *(list+2) = 0.0;
    *(list+2) = 0.0;
    for (sstr = str, j=0; j<ncol; j++)
    for (sstr = str, j=0; j<ncol; j++)
      {
      {
      val = (float)strtod(sstr, &sstr);
      val = (double)strtod(sstr, &sstr);
      if (j==xindex)
      if (j==xindex)
        *list = val;
        *list = val;
      else if (j==yindex)
      else if (j==yindex)
        *(list+1) = val;
        *(list+1) = val;
      else if (j==mindex)
      else if (j==mindex)
        *(list+2) = val;
        *(list+2) = val;
      if ((k=data[j]))
      if ((k=data[j]))
        *(list+2+k) = val;
        *(list+2+k) = val;
      }
      }
 
    if (wcs)
 
      wcs_to_raw(wcs, list, list);
    list += nlist;
    list += nlist;
    }
    }
 
 
  fclose(file);
  fclose(file);
  free(data);
  free(data);
  QREALLOC(assoc->list, float, i*nlist);
  QREALLOC(assoc->list, double, i*nlist);
  assoc->nobj = i;
  assoc->nobj = i;
  assoc->radius = prefs.assoc_radius;
  assoc->radius = prefs.assoc_radius;
  assoc->ndata = ndata;
  assoc->ndata = ndata;
  assoc->ncol = nlist;
  assoc->ncol = nlist;
 
 
Line 225... Line 228...
 
 
  {
  {
   assocstruct  *assoc;
   assocstruct  *assoc;
 
 
/* Load the assoc-list */
/* Load the assoc-list */
  if (!(assoc = field->assoc = load_assoc(prefs.assoc_name)))
  if (!(assoc = field->assoc = load_assoc(prefs.assoc_name,
 
                                prefs.assoccoord_type==ASSOCCOORD_WORLD?
 
                                        field->wcs : NULL)))
    error(EXIT_FAILURE, "*Error*: Assoc-list file not found: ",
    error(EXIT_FAILURE, "*Error*: Assoc-list file not found: ",
        prefs.assoc_name);
        prefs.assoc_name);
 
 
/* Sort the assoc-list by y coordinates, and build the hash table */
/* Sort the assoc-list by y coordinates, and build the hash table */
  sort_assoc(field, assoc);
  sort_assoc(field, assoc);
Line 262... Line 267...
 
 
/********************************** do_assoc *********************************/
/********************************** do_assoc *********************************/
/*
/*
Perform the association task for a source and return the number of IDs.
Perform the association task for a source and return the number of IDs.
*/
*/
int     do_assoc(picstruct *field, float x, float y)
int     do_assoc(picstruct *field, double x, double y)
  {
  {
   assocstruct  *assoc;
   assocstruct  *assoc;
   double       aver;
   double       aver, dx,dy, dist, rad, rad2, comp, wparam,
   float        dx,dy, dist, rad, rad2, comp, wparam,
 
                *list, *input, *data;
                *list, *input, *data;
   int          h, step, i, flag, iy, nobj;
   int          h, step, i, flag, iy, nobj;
 
 
  assoc = field->assoc;
  assoc = field->assoc;
/* Need to initialize the array */
/* Need to initialize the array */
  memset(assoc->data, 0, prefs.assoc_size*sizeof(float));
  memset(assoc->data, 0, prefs.assoc_size*sizeof(double));
  aver = 0.0;
  aver = 0.0;
 
 
  if (prefs.assoc_type == ASSOC_MIN || prefs.assoc_type == ASSOC_NEAREST)
  if (prefs.assoc_type == ASSOC_MIN || prefs.assoc_type == ASSOC_NEAREST)
    comp = BIG;
    comp = BIG;
  else
  else
Line 302... Line 306...
      {
      {
      flag++;
      flag++;
      input = list+3;
      input = list+3;
      if (prefs.assoc_type == ASSOC_FIRST)
      if (prefs.assoc_type == ASSOC_FIRST)
        {
        {
        memcpy(assoc->data, input, assoc->ndata*sizeof(float));
        memcpy(assoc->data, input, assoc->ndata*sizeof(double));
        return 1;
        return 1;
        }
        }
      wparam = *(list+2);
      wparam = *(list+2);
      data = assoc->data;
      data = assoc->data;
      switch(prefs.assoc_type)
      switch(prefs.assoc_type)
        {
        {
        case ASSOC_NEAREST:
        case ASSOC_NEAREST:
          if (dist<comp)
          if (dist<comp)
            {
            {
            memcpy(data, input, assoc->ndata*sizeof(float));
            memcpy(data, input, assoc->ndata*sizeof(double));
            comp = dist;
            comp = dist;
            }
            }
          break;
          break;
        case ASSOC_MEAN:
        case ASSOC_MEAN:
          aver += wparam;
          aver += wparam;
Line 338... Line 342...
            *(data++) += fabs(wparam=*(input++))<99.0? DEXP(-0.4*wparam):0.0;
            *(data++) += fabs(wparam=*(input++))<99.0? DEXP(-0.4*wparam):0.0;
          break;
          break;
        case ASSOC_MIN:
        case ASSOC_MIN:
          if (wparam<comp)
          if (wparam<comp)
            {
            {
            memcpy(data, input, assoc->ndata*sizeof(float));
            memcpy(data, input, assoc->ndata*sizeof(double));
            comp = wparam;
            comp = wparam;
            }
            }
          break;
          break;
        case ASSOC_MAX:
        case ASSOC_MAX:
          if (wparam>comp)
          if (wparam>comp)
            {
            {
            memcpy(data, input, assoc->ndata*sizeof(float));
            memcpy(data, input, assoc->ndata*sizeof(double));
            comp = wparam;
            comp = wparam;
            }
            }
          break;
          break;
        default:
        default:
          error(EXIT_FAILURE, "*Internal Error*: unknown ASSOC type in ",
          error(EXIT_FAILURE, "*Internal Error*: unknown ASSOC type in ",