File: common.c

package info (click to toggle)
fitsh 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,768 kB
  • ctags: 4,050
  • sloc: ansic: 53,352; makefile: 1,120; sh: 25
file content (163 lines) | stat: -rw-r--r-- 3,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*****************************************************************************/
/* common.c								     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Some common stuff for the 'fi' package.				     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* (c) 2004-2008; Pal, A. (apal@szofi.elte.hu)				     */
/*****************************************************************************/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <fits/fits.h>

#include "fitsmask.h"
#include "math/fit/lmfit.h"
#include "math/spline/spline.h"
#include "statistics.h"
#include "math/poly.h"
#include "math/polyfit.h"
#include "io/tokenize.h"
#include "io/iof.h"
#include "tensor.h"

#include "fitsh.h"
#include "common.h"

/*****************************************************************************/

int mark_saturated_pixels(fitsimage *img,char **mask,fitsimage *satimg,double param,int method)
{
 int	i,j,sx,sy,c;

 if ( mask==NULL )	return(-1);

 sx=img->sx,
 sy=img->sy;

 if ( satimg != NULL )
  {	if ( sx != satimg->sx || sy != satimg->sy )	return(1);
	for ( i=0 ; i<sy ; i++ )
	 {  for ( j=0 ; j<sx ; j++ )
	     {	if ( img->data[i][j]>=satimg->data[i][j]*param )
			mask[i][j] |= MASK_OVERSATURATED;
	     }
	 }
  }
 else
  {	for ( i=0 ; i<sy ; i++ )
	 {  for ( j=0 ; j<sx ; j++ )
	     {	if ( img->data[i][j]>=param )
			mask[i][j] |= MASK_OVERSATURATED;
	     }
	 }
  }

 for ( i=0 ; i<sy ; i++ )
  {	for ( j=0 ; j<sx ; j++ )
	 {	c=mask[i][j];
		if ( ! ( c & MASK_OVERSATURATED ) )	continue;
		if ( method & 1 )
		 {	if ( i>0    )	mask[i-1][j] |= MASK_LEAKED;
			if ( i<sy-1 )	mask[i+1][j] |= MASK_LEAKED;
		 }
		if ( method & 2 )
		 {	if ( j>0    )	mask[i][j-1] |= MASK_LEAKED;
			if ( j<sx-1 )	mask[i][j+1] |= MASK_LEAKED;
		 }
	 }
  }
 for ( i=0 ; i<sy ; i++ )
  {	for ( j=0 ; j<sx ; j++ )
	 {	c=mask[i][j];
		if ( (c & MASK_OVERSATURATED) && (c & MASK_LEAKED) )
			mask[i][j] &= ~MASK_LEAKED;
	 }
  }

 return(0);
}

/*****************************************************************************/

int join_masks_from_files(char **mask,int sx,int sy,char **inmasklist)
{
 char		*inmaskname,**inmask;
 fitsheaderset	header;
 fitsimage	fi;
 int		i,j;
 FILE		*ft;

 if ( inmasklist != NULL )
  {
	for ( ; *inmasklist != NULL ; inmasklist++ )
	 {	inmaskname=*inmasklist;
		ft=fopenread(inmaskname);
		if ( ft==NULL )		return(1);
		fits_headerset_reset(&header);
		fits_headerset_read(ft,&header);
		fclose(ft);
		fits_image_get_params(&header,&fi);
		if ( sx != fi.sx || sy != fi.sy )	return(2);
		inmask=fits_mask_read_from_header(&header,sx,sy,NULL);
		fits_headerset_free(&header);
		for ( i=0 ; i<sy ; i++ )
		 {	for ( j=0 ; j<sx ; j++ )
			 {	mask[i][j] |= inmask[i][j];	}
		 }
		fits_mask_free(inmask);
	 }
  }

 return(0);
}

/*****************************************************************************/

int mark_integerlimited_pixels(fitsimage *i,char **mask,int bitpix,int is_corr,int mvlo,int mvhi)
{
 double	llo,lhi;
 int	k,l;

 if ( i==NULL || i->sx <=0 || i->sy <=0 || i->data==NULL ) /* invalid */
	return(-1);
 else if ( bitpix<0 )	/* real data, do nothing */
	return(0);
 else if ( bitpix==8 )
  {	llo=-128.0;
	lhi=+127.0;
  }
 else if ( bitpix==16 )
  {	llo=-32768.0;
	lhi=+32767.0;
  }
 else if ( bitpix==32 )
  {	llo=-2147483648.0;
	lhi=+2147483647.0;
  }
 else			/* invalid bitpix value	*/
	return(-1);

 
 for ( k=0 ; k<i->sy ; k++ )
  {	for ( l=0 ; l<i->sx ; l++ )
	 {	if ( i->data[k][l]<llo )
		 {	if ( mask != NULL )	mask[k][l] |= mvlo;
			if ( is_corr )		i->data[k][l]=llo;
		 }
		else if ( i->data[k][l]>lhi )
		 {	if ( mask != NULL )	mask[k][l] |= mvhi;
			if ( is_corr )		i->data[k][l]=lhi;
		 }
		else if ( is_corr )
			i->data[k][l]=floor(i->data[k][l]);
	 }
  }

 return(0);
}

/*****************************************************************************/