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 #ifdef HAVE_CONFIG_H
00051 # include <config.h>
00052 #endif
00053
00054
00088
00092
00093
00094
00095
00096 #include <uves_extract_iterate.h>
00097
00098 #include <uves_utils.h>
00099
00100 #include <cpl.h>
00101
00102
00103
00104
00105 static
00106 bool illegal_position(const uves_iterate_position *p);
00107
00108
00109
00110
00111
00112
00113
00149
00150 uves_iterate_position *
00151 uves_iterate_new(int nx, int ny,
00152 const polynomial *order_locations,
00153 int minorder, int maxorder,
00154 slit_geometry sg)
00155 {
00156 uves_iterate_position *p = cpl_calloc(1, sizeof(uves_iterate_position));
00157
00158 p->nx = nx;
00159 p->ny = ny;
00160 p->order_locations = order_locations;
00161 p->minorder = minorder;
00162 p->maxorder = maxorder;
00163 p->sg = sg;
00164
00165 return p;
00166 }
00167
00168
00173
00174 void
00175 uves_iterate_delete(uves_iterate_position **p)
00176 {
00177 if (p != NULL)
00178 {
00179 cpl_free(*p);
00180 *p = NULL;
00181 }
00182 }
00183
00184
00198
00199 void
00200 uves_iterate_set_first(uves_iterate_position *p,
00201 int xmin, int xmax,
00202 int ordermin, int ordermax,
00203 const cpl_binary *bpm,
00204 bool loop_y)
00205 {
00206
00207 p->xmin = xmin;
00208 p->xmax = xmax;
00209 p->ordermax = ordermax;
00210 p->bpm = bpm;
00211 p->loop_y = loop_y;
00212 p->end = false;
00213
00214
00215 p->x = xmin;
00216 p->order = ordermin;
00217
00218 p->ycenter = uves_polynomial_evaluate_2d(p->order_locations, p->x, p->order)
00219 + p->sg.offset;
00220 p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00221 p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
00222 if (loop_y)
00223 {
00224 p->y = p->ylow;
00225 }
00226
00227
00228 while (illegal_position(p) && !uves_iterate_finished(p))
00229 {
00230 uves_iterate_increment(p);
00231 }
00232 }
00233
00234
00241
00242 inline void
00243 uves_iterate_increment(uves_iterate_position *p)
00244 {
00245 do {
00246 if (p->loop_y && p->y < p->yhigh)
00247 {
00248 (p->y)++;
00249 }
00250 else if (p->x < p->xmax)
00251 {
00252 (p->x)++;
00253
00254 p->ycenter =
00255 uves_polynomial_evaluate_2d(p->order_locations,
00256 p->x, p->order)
00257 + p->sg.offset;
00258
00259 p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00260 p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
00261 if (p->loop_y) p->y = p->ylow;
00262 }
00263 else if (p->order < p->ordermax)
00264 {
00265 (p->order)++;
00266 p->x = p->xmin;
00267
00268 p->ycenter =
00269 uves_polynomial_evaluate_2d(p->order_locations,
00270 p->x, p->order)
00271 + p->sg.offset;
00272
00273 p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00274 p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
00275 if (p->loop_y) p->y = p->ylow;
00276 }
00277 else
00278 {
00279 p->end = true;
00280 }
00281 } while (illegal_position(p) && !uves_iterate_finished(p));
00282
00283 return;
00284 }
00285
00286
00296
00297 inline bool
00298 uves_iterate_finished(const uves_iterate_position *p)
00299 {
00300 return p->end;
00301 }
00302
00303
00309
00310 void
00311 uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
00312 {
00313 fprintf(stream, "Position:\n");
00314 fprintf(stream, "order = %d\n", p->order);
00315 fprintf(stream, "x = %d\n", p->x);
00316 fprintf(stream, "y = %d\n", p->y);
00317 fprintf(stream, "ycenter = %f\n", p->ycenter);
00318 fprintf(stream, "ylow, yhigh = %d, %d\n", p->ylow, p->yhigh);
00319 fprintf(stream, "Limits:\n");
00320 fprintf(stream, "xmin, xmax = %d, %d\n", p->xmin, p->xmax);
00321 fprintf(stream, "ordermax = %d\n", p->ordermax);
00322 fprintf(stream, "bpm = %d\n", p->bpm != NULL ? 1 : 0);
00323 fprintf(stream, "loop_y = %s\n", p->loop_y ? "true" : "false");
00324 fprintf(stream, "end = %s\n", p->end ? "true" : "false");
00325 fprintf(stream, "Geometry:\n");
00326 fprintf(stream, "nx, ny = %d, %d\n", p->nx, p->ny);
00327 fprintf(stream, "minorder, maxorder = %d, %d\n", p->minorder, p->maxorder);
00328 fprintf(stream, "order_locations = %d\n", p->order_locations != NULL ? 1 : 0);
00329 fprintf(stream, "slit length = %f\n", p->sg.length);
00330 fprintf(stream, "slit offset = %f\n", p->sg.offset);
00331
00332 return;
00333 }
00334
00335
00342
00343 static
00344 bool illegal_position(const uves_iterate_position *p)
00345 {
00346 return p->ylow < 1 || p->yhigh > p->ny ||
00347 (p->loop_y && p->bpm != NULL &&
00348 p->bpm[(p->x-1) + (p->y-1)*p->nx] != CPL_BINARY_0);
00349 }
00350