misc.c 3.81 KB
Newer Older
1
2
3
4
5
6
7
8
/*
*				misc.c
*
* Miscellaneous functions.
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	This file part of:	SExtractor
Emmanuel Bertin's avatar
Emmanuel Bertin committed
9
*
10
*	Copyright:		(C) 2009-2011 Emmanuel Bertin -- IAP/CNRS/UPMC
Emmanuel Bertin's avatar
Emmanuel Bertin committed
11
*
12
*	License:		GNU General Public License
Emmanuel Bertin's avatar
Emmanuel Bertin committed
13
*
14
15
16
17
18
19
20
21
22
23
*	SExtractor is free software: you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*	SExtractor is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	You should have received a copy of the GNU General Public License
*	along with SExtractor. If not, see <http://www.gnu.org/licenses/>.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
24
*
25
*	Last modified:		11/03/2011
26
27
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
Emmanuel Bertin's avatar
Emmanuel Bertin committed
28
29
30
31
32

#ifdef HAVE_CONFIG_H
#include        "config.h"
#endif

33
#include	<stdlib.h>
34
35
36
#include	<time.h>
#include	<sys/time.h>

Emmanuel Bertin's avatar
Emmanuel Bertin committed
37
38
39
40
#include	"define.h"
#include	"globals.h"


41
42
43
44
45
46
47
48
49
50
51
/*i**** fqcmp **************************************************************
PROTO	int	fqcmp(const void *p1, const void *p2)
PURPOSE	Sorting function for floats in qsort().
INPUT	Pointer to first element,
	pointer to second element.
OUTPUT	1 if *p1>*p2, 0 if *p1=*p2, and -1 otherwise.
NOTES	-.
AUTHOR	E. Bertin (IAP)
VERSION	05/10/2010
 ***/
static int	fqcmp(const void *p1, const void *p2)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
52
  {
53
54
55
56
   double	f1=*((float *)p1),
		f2=*((float *)p2);
  return f1>f2? 1 : (f1<f2? -1 : 0);
  }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
57
58


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/****** fqmedian **************************************************************
PROTO	float   fqmedian(float *ra, int n)
PURPOSE	Compute the median of an array of floats, using qsort().
INPUT	Pointer to the array,
	Number of array elements.
OUTPUT	Median of the array.
NOTES	Warning: the order of input data is modified!.
AUTHOR	E. Bertin (IAP)
VERSION	05/10/2010
 ***/
float	fqmedian(float *ra, int n)

  {
   int dqcmp(const void *p1, const void *p2);

  qsort(ra, n, sizeof(float), fqcmp);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
75
76
  if (n<2)
    return *ra;
77
78
  else
    return n&1? ra[n/2] : (ra[n/2-1]+ra[n/2])/2.0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
79
80
81
  }


Emmanuel Bertin's avatar
Emmanuel Bertin committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/****** propagate_covar ******************************************************
PROTO	void	propagate_covar(double *vi, double *d, double *vo,
				int ni, int no,	double *temp)
PURPOSE	Compute Dt.V.D (propagate covariance matrix errors)
INPUT	Pointer to the original covariance matrix,
	pointer to the matrix of derivatives,
	input number of parameters,
	output number of parameters,
	pointer to a ni*no work array.
OUTPUT	-.
NOTES	-.
AUTHOR	E. Bertin (IAP)
VERSION	20/08/2010
 ***/
void propagate_covar(double *vi, double *d, double *vo,
				int ni, int no,	double *temp)
  {
   double	*vit,*dt,*vot,*tempt,
		dval;
   int		i,j,k;

  tempt = temp;
  vit = vi;
  for (j=0; j<ni; j++)
    {
    dt = d;
    for (i=no; i--;)
      {
      vit = vi + j*ni;
      dval = 0.0;
      for (k=ni; k--;)
        dval += *(vit++)**(dt++);
      *(tempt++) = dval;
      }
    }

  vot = vo;
  for (j=0; j<no; j++)
    {
    for (i=0; i<no; i++)
      {
      dt = d + j*ni;
      tempt = temp + i;
      dval = 0.0;
      for (k=ni; k--; tempt+=no)
        dval += *(dt++)**tempt;
      *(vot++) = dval;
      }
    }

  return;
  }


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/****** counter_seconds *******************************************************
PROTO	double counter_seconds(void)
PURPOSE	Count the number of seconds (with an arbitrary offset).
INPUT	-.
OUTPUT	Returns a number of seconds.
NOTES	Results are meaningful only for tasks that take one microsec or more.
AUTHOR	E. Bertin (IAP)
VERSION	24/09/2009
 ***/
double	counter_seconds(void)
  {
   struct timeval	tp;
   struct timezone	tzp;
   int			dummy;

  dummy = gettimeofday(&tp,&tzp);
  return (double) tp.tv_sec + (double) tp.tv_usec * 1.0e-6;
  }