00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #ifdef HAVE_CONFIG_H
00057 # include <config.h>
00058 #endif
00059
00060
00094
00098
00099
00100
00101
00102 #include <uves_extract_iterate.h>
00103
00104 #include <uves_utils.h>
00105
00106 #include <cpl.h>
00107
00108
00109
00110
00111 static
00112 bool illegal_position(const uves_iterate_position *p);
00113
00114
00115
00116
00117
00118
00119
00155
00156 uves_iterate_position *
00157 uves_iterate_new(int nx, int ny,
00158 const polynomial *order_locations,
00159 int minorder, int maxorder,
00160 slit_geometry sg)
00161 {
00162 uves_iterate_position *p = cpl_calloc(1, sizeof(uves_iterate_position));
00163
00164 p->nx = nx;
00165 p->ny = ny;
00166 p->order_locations = order_locations;
00167 p->minorder = minorder;
00168 p->maxorder = maxorder;
00169 p->sg = sg;
00170
00171 return p;
00172 }
00173
00174
00179
00180 void
00181 uves_iterate_delete(uves_iterate_position **p)
00182 {
00183 if (p != NULL)
00184 {
00185 cpl_free(*p);
00186 *p = NULL;
00187 }
00188 }
00189
00190
00204
00205 void
00206 uves_iterate_set_first(uves_iterate_position *p,
00207 int xmin, int xmax,
00208 int ordermin, int ordermax,
00209 const cpl_binary *bpm,
00210 bool loop_y)
00211 {
00212
00213 p->xmin = xmin;
00214 p->xmax = xmax;
00215 p->ordermax = ordermax;
00216 p->bpm = bpm;
00217 p->loop_y = loop_y;
00218 p->end = false;
00219
00220
00221 p->x = xmin;
00222 p->order = ordermin;
00223
00224 p->ycenter = uves_polynomial_evaluate_2d(p->order_locations, p->x, p->order)
00225 + p->sg.offset;
00226 p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00227 p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
00228 if (loop_y)
00229 {
00230 p->y = p->ylow;
00231 }
00232
00233
00234 while (illegal_position(p) && !uves_iterate_finished(p))
00235 {
00236 uves_iterate_increment(p);
00237 }
00238 }
00239
00240
00247
00248 void
00249 uves_iterate_increment(uves_iterate_position *p)
00250 {
00251 do {
00252 if (p->loop_y && p->y < p->yhigh)
00253 {
00254 (p->y)++;
00255 }
00256 else if (p->x < p->xmax)
00257 {
00258 (p->x)++;
00259
00260 p->ycenter =
00261 uves_polynomial_evaluate_2d(p->order_locations,
00262 p->x, p->order)
00263 + p->sg.offset;
00264
00265 p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00266 p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
00267 if (p->loop_y) p->y = p->ylow;
00268 }
00269 else if (p->order < p->ordermax)
00270 {
00271 (p->order)++;
00272 p->x = p->xmin;
00273
00274 p->ycenter =
00275 uves_polynomial_evaluate_2d(p->order_locations,
00276 p->x, p->order)
00277 + p->sg.offset;
00278
00279 p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00280 p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
00281 if (p->loop_y) p->y = p->ylow;
00282 }
00283 else
00284 {
00285 p->end = true;
00286 }
00287 } while (illegal_position(p) && !uves_iterate_finished(p));
00288
00289 return;
00290 }
00291
00292
00302
00303 bool
00304 uves_iterate_finished(const uves_iterate_position *p)
00305 {
00306 return p->end;
00307 }
00308
00309
00315
00316 void
00317 uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
00318 {
00319 fprintf(stream, "Position:\n");
00320 fprintf(stream, "order = %d\n", p->order);
00321 fprintf(stream, "x = %d\n", p->x);
00322 fprintf(stream, "y = %d\n", p->y);
00323 fprintf(stream, "ycenter = %f\n", p->ycenter);
00324 fprintf(stream, "ylow, yhigh = %d, %d\n", p->ylow, p->yhigh);
00325 fprintf(stream, "Limits:\n");
00326 fprintf(stream, "xmin, xmax = %d, %d\n", p->xmin, p->xmax);
00327 fprintf(stream, "ordermax = %d\n", p->ordermax);
00328 fprintf(stream, "bpm = %d\n", p->bpm != NULL ? 1 : 0);
00329 fprintf(stream, "loop_y = %s\n", p->loop_y ? "true" : "false");
00330 fprintf(stream, "end = %s\n", p->end ? "true" : "false");
00331 fprintf(stream, "Geometry:\n");
00332 fprintf(stream, "nx, ny = %d, %d\n", p->nx, p->ny);
00333 fprintf(stream, "minorder, maxorder = %d, %d\n", p->minorder, p->maxorder);
00334 fprintf(stream, "order_locations = %d\n", p->order_locations != NULL ? 1 : 0);
00335 fprintf(stream, "slit length = %f\n", p->sg.length);
00336 fprintf(stream, "slit offset = %f\n", p->sg.offset);
00337
00338 return;
00339 }
00340
00341
00348
00349 static
00350 bool illegal_position(const uves_iterate_position *p)
00351 {
00352 return p->ylow < 1 || p->yhigh > p->ny ||
00353 (p->loop_y && p->bpm != NULL &&
00354 p->bpm[(p->x-1) + (p->y-1)*p->nx] != CPL_BINARY_0);
00355 }
00356