/
smf_initial_sky.c
243 lines (204 loc) · 8.81 KB
/
smf_initial_sky.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
*+
* Name:
* smf_initial_sky
* Purpose:
* Subtract the initial sky estimate from the cleaned data.
* Language:
* Starlink ANSI C
* Type of Module:
* C function
* Invocation:
* int smf_initial_sky( ThrWorkForce *wf, AstKeyMap *keymap,
* smfDIMMData *dat, int *iters, int *status )
* Arguments:
* wf = ThrWorkForce * (Given)
* Pointer to a pool of worker threads (can be NULL)
* keymap = AstKeyMap * (Given)
* Configuration parameters that control the map-maker.
* dat = smfDIMMData * (Given)
* Struct of pointers to information required by model calculation.
* iters = int * (Returned)
* If the initial sky NDF was created by a previous run of makemap
* that was interupted using control-C, "*iters" will be returned
* holding the number of iterations that were completed before the
* map was created. Otherwise, -1 is returned in "*iters".
* status = int* (Given and Returned)
* Pointer to global status.
* Returned Value:
* Non-zero if an initial sky estimate was subtracted from the data.
* Zero otherwise.
* Description:
* If the IMPORTSKY configuration parameter is set, this function
* reads a sky map in using the ADAM parameter specified by the
* IMPORTSKY. It then stores this map as the current map in the
* iterative map-making process. It then samples the map at each
* bolometer position and subtracts the bolometer value from the
* current residuals.
* Authors:
* David S Berry (JAC, Hawaii)
* {enter_new_authors_here}
* History:
* 22-OCT-2012 (DSB):
* Original version.
* 3-JUL-2013 (DSB):
* - Added argument iters.
* - Ensure the bad bits mask is set so that the mapped NDF data
* array is masked by the quality array.
* - Import variance from supplied NDF if available.
* 10-DEC-2013 (DSB):
* - Re-structured to avoid setting map pixels bad if they are
* flagged only by FLT or COM masking. That is, map pixels should
* be bad if and only if they are flagged by AST masking.
* - Report an error if the NDF contains unexpected quality names.
* - Clear and RING or COM flags in the quality array of the time series data.
* 23-JAN-2014 (DSB):
* Mask the AST map in smf_calcmodel_ast as normal, rather than masking it here.
* 4-MAR-2014 (DSB):
* Do not clear and RING or COM flags in the quality array of the time
* series data (it will never be present anyway).
* 5-MAR-2014 (DSB):
* Ignore the quality in the supplied map.
* 10-MAR-2014 (DSB):
* Update the Quality component of the supplied NDF to match the
* mask created by smf_calcmodel_ast.
* {enter_further_changes_here}
* Copyright:
* Copyright (C) 2012-2014 Science & Technology Facilities Council.
* All Rights Reserved.
* Licence:
* This program 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.
*
* This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA
* Bugs:
* {note_any_bugs_here}
*-
*/
/* Starlink includes */
#include "ast.h"
#include "mers.h"
#include "sae_par.h"
#include "dat_par.h"
/* SMURF includes */
#include "libsmf/smf.h"
#include "libsmf/smf_typ.h"
int smf_initial_sky( ThrWorkForce *wf, AstKeyMap *keymap, smfDIMMData *dat,
int *iters, int *status ) {
/* Local Variables: */
char refparam[ DAT__SZNAM ];/* Name for reference NDF parameter */
const char *cval; /* The IMPORTSKY string value */
double *ptr; /* Pointer to NDF Data array */
double *vptr; /* Pointer to NDF Variance array */
int indf1; /* Id. for supplied reference NDF */
int indf2; /* Id. for used section of reference NDF */
int nel; /* Number of mapped NDF pixels */
int result; /* Returned flag */
int there; /* Is there a smurf extension in the NDF? */
int update; /* Was NDF opened for UPDATE access? */
/* Initialise the returned value to indicate no sky has been subtractred. */
result = 0;
/* Assume the sky map was not created by an interupted previous run of
makemap. */
*iters = -1;
/* Check inherited status. */
if( *status != SAI__OK ) return result;
/* Begin an AST context. */
astBegin;
/* The IMPORTSKY config parameter should have the name of the ADAM
parameter to use for acquiring the NDF that contains the initial sky
estimate. If IMPORTSKY is "1", use REF. */
cval = NULL;
astMapGet0C( keymap, "IMPORTSKY", &cval );
if( cval ) {
if( !astChrMatch( cval, "REF" ) &&
!astChrMatch( cval, "MASK2" ) &&
!astChrMatch( cval, "MASK3" ) ) {
astMapGet0I( keymap, "IMPORTSKY", &result );
cval = ( result > 0 ) ? "REF" : NULL;
}
if( cval ) {
result = 1;
strcpy( refparam, cval );
astChrCase( NULL, refparam, 1, 0 );
}
}
/* Do nothing more if we are not subtracting an initial sky from the data. */
if( result && *status == SAI__OK ) {
/* Begin an NDF context. */
ndfBegin();
/* Get an identifier for the NDF using the associated ADAM parameter.
First try UPDATE access. If this fails try READ access. */
ndfAssoc( refparam, "UPDATE", &indf1, status );
if( *status != SAI__OK ) {
errAnnul( status );
ndfAssoc( refparam, "READ", &indf1, status );
update = 0;
} else {
update = 1;
}
/* Tell the user what we are doing. */
ndfMsg( "N", indf1 );
msgOut( "", "Using ^N as the initial guess at the sky", status );
/* Get a section from this NDF that matches the bounds of the map. */
ndfSect( indf1, 2, dat->lbnd_out, dat->ubnd_out, &indf2, status );
/* Ensure masked values are not set bad in the mapped data array. */
ndfSbb( 0, indf2, status );
/* Map the data array section, and copy it into the map buffer. */
ndfMap( indf2, "DATA", "_DOUBLE", "READ", (void **) &ptr, &nel, status );
if( *status == SAI__OK ) {
memcpy( dat->map, ptr, dat->msize*sizeof(*ptr));
}
/* Map the variance array section, and copy it into the map buffer. */
ndfState( indf2, "VARIANCE", &there, status );
if( there ) {
ndfMap( indf2, "VARIANCE", "_DOUBLE", "READ", (void **) &vptr, &nel, status );
if( *status == SAI__OK ) {
memcpy( dat->mapvar, vptr, dat->msize*sizeof(*vptr));
}
}
/* If the NDF was created by a previous run of makemap that was interupted
using control-C, it will contain a NUMITER item in the smurf extension,
which gives the number of iterations that were completed before the
map was created. Obtain and return this value, if it exists. */
ndfXstat( indf1, SMURF__EXTNAME, &there, status );
if( there ) ndfXgt0i( indf1, SMURF__EXTNAME, "NUMITER", iters,
status );
/* Indicate the map arrays within the supplied smfDIMMData structure now
contain usable values. We need to do this before calling
smf_calcmodel_ast below so that the right mask gets used in
smf_calcmodel_ast. */
dat->mapok = 1;
/* Apply any existinction correction to the cleaned bolometer data. */
if( dat->ext ) smf_calcmodel_ext( wf, dat, 0, keymap, dat->ext, 0,
status);
/* Sample the above map at the position of each bolometer sample and
subtract the sampled value from the cleaned bolometer value. */
smf_calcmodel_ast( wf, dat, 0, keymap, NULL, SMF__DIMM_PREITER, status);
/* Remove any existinction correction to the modifed bolometer data. */
if( dat->ext ) smf_calcmodel_ext( wf, dat, 0, keymap, dat->ext,
SMF__DIMM_INVERT, status);
/* If the NDF was opened with UPDATE access, update the quality array in
the NDF to reflect the AST mask created by smf_calcmodel_ast above. */
if( update ) {
smf_qual_t *qarray = astStore( NULL, dat->mapqual, dat->msize*sizeof(*qarray) );
qarray = smf_qual_unmap( wf, indf2, SMF__QFAM_MAP, qarray, status );
}
/* End the NDF context. */
ndfEnd( status );
}
/* End the AST context. */
astEnd;
/* Return the pointer to the boolean mask. */
return result;
}