Changeset 40838
- Timestamp:
- Jul 19, 2019, 5:33:23 PM (7 years ago)
- Location:
- branches/eam_branches/ohana.20190329/src/opihi/mana
- Files:
-
- 6 edited
-
Makefile (modified) (1 diff)
-
deimos_arclines.c (modified) (2 diffs)
-
deimos_fitalt.c (modified) (20 diffs)
-
deimos_mkalt.c (modified) (1 diff)
-
deimos_mkmodel.c (modified) (8 diffs)
-
deimos_mkobj.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20190329/src/opihi/mana/Makefile
r40753 r40838 28 28 $(SRC)/deimos_fitobj.$(ARCH).o \ 29 29 $(SRC)/deimos_fitalt.$(ARCH).o \ 30 $(SRC)/deimos_mkalt.$(ARCH).o \31 30 $(SRC)/deimos_mkmodel.$(ARCH).o \ 32 31 $(SRC)/deimos_arclines.$(ARCH).o \ 33 32 $(SRC)/deimos_fitprofile.$(ARCH).o \ 34 33 $(SRC)/findrowpeaks.$(ARCH).o 34 35 # $(SRC)/deimos_mkalt.$(ARCH).o 35 36 36 37 cmds = \ -
branches/eam_branches/ohana.20190329/src/opihi/mana/deimos_arclines.c
r40817 r40838 53 53 deimos_make_LSF_kernel (LSF, stilt, Nx); 54 54 55 // FILE *Fout = NULL; 56 55 57 // loop over the rows 56 58 for (int iy = 0; iy < Ny; iy++) { 57 59 float Fsum = 0; 58 60 float Ksum = 0; 61 62 // if (iy == 5802) Fout = fopen ("test.5802.dat", "w"); 63 // if (iy == 5815) Fout = fopen ("test.5815.dat", "w"); 64 59 65 // cross-correlation of buffer values and kernel values 60 66 for (int ix = 0; ix < Nx; ix++) { … … 67 73 Fsum += Fin[ix + (iy + ky)*Nx] * kernel[ix + ko*Nx]; 68 74 Ksum += kernel[ix + ko*Nx]; 75 // if ((iy == 5815) || (iy == 5802)) { 76 // fprintf (Fout, "%d %d %d : %f %f\n", iy, ix, ky, Fin[ix + (iy + ky)*Nx], kernel[ix + ko*Nx]); 77 // } 69 78 } 70 79 } 80 81 // if ((iy == 5815) || (iy == 5802)) fclose (Fout); 82 71 83 coord->elements.Flt[iy] = iy; 72 84 flux->elements.Flt[iy] = Fsum; -
branches/eam_branches/ohana.20190329/src/opihi/mana/deimos_fitalt.c
r40663 r40838 1 1 # include "data.h" 2 2 # include "deimos.h" 3 4 # if (0)5 int deimos_fitalt (int argc, char **argv) {6 OHANA_UNUSED_PARAM(argc);7 OHANA_UNUSED_PARAM(argv);8 return FALSE;9 }10 11 # else12 3 13 4 /* … … 18 9 19 10 // internal functions to fitobj 20 static float deimos_get_chisq (float *buffer, float * model, int Nx, int Ny, int row, int *Npts);21 double deimos_LMM_update (float *data, float * model_ref, float *model_obj, float *model_sky, float *model_bck, float *dObj, float *dSky, float *dBck, int Nx, int Ny, int row, float lambda);11 static float deimos_get_chisq (float *buffer, float *vardata, float *model, int Nx, int Ny, int row, int *Npts); 12 double deimos_LMM_update (float *data, float *vardata, float *model_ref, float *model_obj, float *model_sky, float *model_bck, float *dObj, float *dSky, float *dBck, int Nx, int Ny, int row, float lambda); 22 13 static opihi_flt *deimos_make_test (opihi_flt *guess, float sigma, int row, int Nrow, float *dValue); 23 14 opihi_flt *deimos_subvector (opihi_flt *fullVec, int row, int Nrow); … … 26 17 27 18 // input parameters: 28 // * buffer : 2D image of slit (full frame or cutout?) 29 // * slit_trace : spline fit of slit central x pos vs y-coord 30 // * psf_trace : spline fit of slit central x pos vs y-coord 31 // * profile : slit window profile (vector) 32 // * PSF : point-spread function vector (flux normalized, x-dir) 33 // * stilt : slit tilt response : 2D kernel? 19 // * buffer : 2D image of slit (full frame or cutout?) 20 // * slit_trace_red : spline fit of slit central x pos vs y-coord 21 // * slit_trace_blu : spline fit of slit central x pos vs y-coord 22 // * psf_trace : spline fit of slit central x pos vs y-coord 23 // * profile : slit window profile (vector) 24 // * PSF : point-spread function vector (flux normalized, x-dir) 25 // * stilt : slit tilt response : 2D kernel? 34 26 35 27 // in-out parameters: … … 40 32 int N; 41 33 42 Spline *slit_trace = NULL; 34 Spline *slit_trace_red = NULL; 35 Spline *slit_trace_blu = NULL; 43 36 Spline *psf_trace = NULL; 44 Vector *profile = NULL; 45 46 Vector *obj = NULL; 47 Vector *sky = NULL; 48 Vector *bck = NULL; 49 50 Vector *psf = NULL; 51 52 Buffer *buffer = NULL; 53 54 float stilt = 0.0; // angle of the slit 37 38 Vector *profile = NULL; 39 Vector *psf = NULL; 40 41 Vector *obj = NULL; 42 Vector *sky = NULL; 43 Vector *bck = NULL; 44 45 Buffer *buffer = NULL; 46 47 float stilt = 0.0; // angle of the slit 48 55 49 ohana_gaussdev_init (); 56 50 … … 65 59 remove_argument (N, &argc, argv); 66 60 VERBOSE = TRUE; 61 } 62 63 int SKIPFIT = FALSE; 64 if ((N = get_argument (argc, argv, "-skipfit"))) { 65 remove_argument (N, &argc, argv); 66 SKIPFIT = TRUE; 67 } 68 69 // for a red vs blu spline, we need to specify the split point 70 // XXX this is REALLY ad-hoc for Deimos. not sure how to make 71 // this more generic (need to define the ranges somewhere) 72 int redlimit = 4096; 73 if ((N = get_argument (argc, argv, "-redlimit"))) { 74 remove_argument (N, &argc, argv); 75 redlimit = atoi (argv[N]); 76 remove_argument (N, &argc, argv); 67 77 } 68 78 … … 108 118 109 119 // Input parameters: 110 if ((N = get_argument (argc, argv, "-slit-trace"))) { 111 remove_argument (N, &argc, argv); 112 if ((slit_trace = FindSpline (argv[N])) == NULL) return (FALSE); 113 remove_argument (N, &argc, argv); 114 } else { goto usage; } 120 if ((N = get_argument (argc, argv, "-slit-trace-red"))) { 121 remove_argument (N, &argc, argv); 122 if ((slit_trace_red = FindSpline (argv[N])) == NULL) return (FALSE); 123 remove_argument (N, &argc, argv); 124 } else { goto usage; } 125 if ((N = get_argument (argc, argv, "-slit-trace-blu"))) { 126 remove_argument (N, &argc, argv); 127 if ((slit_trace_blu = FindSpline (argv[N])) == NULL) return (FALSE); 128 remove_argument (N, &argc, argv); 129 } else { goto usage; } 130 115 131 if ((N = get_argument (argc, argv, "-psf-trace"))) { 116 132 remove_argument (N, &argc, argv); … … 150 166 remove_argument (N, &argc, argv); 151 167 } else { goto usage; } 168 169 // save the model 170 Buffer *model = NULL; 171 if ((N = get_argument (argc, argv, "-model"))) { 172 remove_argument (N, &argc, argv); 173 if ((model = SelectBuffer (argv[N], ANYBUFFER, TRUE)) == NULL) return (FALSE); 174 remove_argument (N, &argc, argv); 175 } 176 177 // save the model 178 Buffer *varim = NULL; 179 if ((N = get_argument (argc, argv, "-variance"))) { 180 remove_argument (N, &argc, argv); 181 if ((varim = SelectBuffer (argv[N], OLDBUFFER, TRUE)) == NULL) return (FALSE); 182 remove_argument (N, &argc, argv); 183 } 152 184 153 185 if (argc != 3) goto usage; … … 171 203 float *bufVal = (float *) buffer->matrix.buffer; 172 204 205 if (varim) { 206 if (Nx != varim[0].matrix.Naxis[0]) { gprint (GP_ERR, "inconsistent sizes for image and varim\n"); return FALSE; } 207 if (Ny != varim[0].matrix.Naxis[1]) { gprint (GP_ERR, "inconsistent sizes for image and varim\n"); return FALSE; } 208 } 209 float *varVal = varim ? (float *) varim->matrix.buffer : NULL; 210 173 211 // obj, sky, bck must be consistent with data 174 212 if (Ny != obj->Nelements) { … … 183 221 gprint (GP_ERR, "inconsistent wavelength scales (backgnd)\n"); 184 222 return FALSE; 223 } 224 225 if (model) { 226 if (!CreateBuffer (model, Nx, Ny, -32, 1.0, 0.0)) { gprint (GP_ERR, "error generating output model buffer\n"); return FALSE; } 185 227 } 186 228 … … 230 272 // use scaled-noise to define test ranges 231 273 // we are going to try each row using a window Nrow wide to measure chisq: 232 for (int row = dRow; row < Ny - dRow - 1; row++) {274 for (int row = dRow; !SKIPFIT && (row < Ny - dRow - 1); row++) { 233 275 234 276 // I am using a numerical equivalent of Levenberg-Marquardt, but only for a single … … 251 293 // current vector value: 252 294 // for deimos_make_model, row is the starting point of the subimage 253 float *model_ref = deimos_make_model (objCurr, skyCurr, bckCurr, psf, profile, slit_trace , psf_trace, Nx, Nrow, row_0);295 float *model_ref = deimos_make_model (objCurr, skyCurr, bckCurr, psf, profile, slit_trace_red, slit_trace_blu, redlimit, psf_trace, Nx, Nrow, row_0); 254 296 255 297 // in the test functions below, dRow is the central pixel of the subset, e.g., obj[row] => objCurr[dRow] … … 259 301 // delta obj (save dObj) 260 302 opihi_flt *objtest = deimos_make_test (objCurr, objNoise[row], dRow, Nrow, &dObj); 261 float *model_obj = deimos_make_model (objtest, skyCurr, bckCurr, psf, profile, slit_trace , psf_trace, Nx, Nrow, row_0);303 float *model_obj = deimos_make_model (objtest, skyCurr, bckCurr, psf, profile, slit_trace_red, slit_trace_blu, redlimit, psf_trace, Nx, Nrow, row_0); 262 304 263 305 // delta sky (save dSky) 264 306 opihi_flt *skytest = deimos_make_test (skyCurr, skyNoise[row], dRow, Nrow, &dSky); 265 float *model_sky = deimos_make_model (objCurr, skytest, bckCurr, psf, profile, slit_trace , psf_trace, Nx, Nrow, row_0);307 float *model_sky = deimos_make_model (objCurr, skytest, bckCurr, psf, profile, slit_trace_red, slit_trace_blu, redlimit, psf_trace, Nx, Nrow, row_0); 266 308 267 309 // delta bck (save dBck) 268 310 opihi_flt *bcktest = deimos_make_test (bckCurr, bckNoise[row], dRow, Nrow, &dBck); 269 float *model_bck = deimos_make_model (objCurr, skyCurr, bcktest, psf, profile, slit_trace , psf_trace, Nx, Nrow, row_0);311 float *model_bck = deimos_make_model (objCurr, skyCurr, bcktest, psf, profile, slit_trace_red, slit_trace_blu, redlimit, psf_trace, Nx, Nrow, row_0); 270 312 271 313 // dObj, dSky, dBck carry the delta used derivatives in and the delta applied to the values out 272 float chisq = deimos_LMM_update (bufVal, model_ref, model_obj, model_sky, model_bck, &dObj, &dSky, &dBck, Nx, Nrow, row_0, lambda); 314 float chisq = deimos_LMM_update (bufVal, varVal, model_ref, model_obj, model_sky, model_bck, &dObj, &dSky, &dBck, Nx, Nrow, row_0, lambda); 315 316 if (FALSE && (row > 10) && (row < 20)) { 317 fprintf (stderr, "%d %d : %f %f %f\n", iter, row, dObj, dSky, dBck); 318 } 273 319 274 320 if (1) { … … 301 347 } 302 348 303 float *model_full = deimos_make_model (objVal, skyVal, bckVal, psf, profile, slit_trace , psf_trace, Nx, Ny, 0);349 float *model_full = deimos_make_model (objVal, skyVal, bckVal, psf, profile, slit_trace_red, slit_trace_blu, redlimit, psf_trace, Nx, Ny, 0); 304 350 305 351 int Npts = 0; 306 float chisq_full = deimos_get_chisq (bufVal, model_full, Nx, Ny, 0, &Npts); 352 float chisq_full = deimos_get_chisq (bufVal, varVal, model_full, Nx, Ny, 0, &Npts); 353 354 if (model) { 355 free (model[0].matrix.buffer); 356 model[0].matrix.buffer = (char *) model_full; 357 } else { 358 free (model_full); 359 } 307 360 308 361 fprintf (stderr, "** Full chisq: %f (%d) : %f)\n", chisq_full, Npts, chisq_full / (1.0 * Npts)); … … 312 365 313 366 usage: 314 gprint (GP_ERR, "USAGE: deimos fitobj (buffer) (Xref) -object vector -sky vector -backgnd vector -slit-trace spline -psf-trace spline-profile vector -psf vector -stilt (angle)\n");367 gprint (GP_ERR, "USAGE: deimos fitobj (buffer) (Xref) -object vector -sky vector -backgnd vector -slit-trace-red (spline) -slit-trace-blu (spline) -psf-trace (spline) -profile vector -psf vector -stilt (angle)\n"); 315 368 return FALSE; 316 369 } … … 351 404 352 405 353 double deimos_LMM_update (float *data, float * model_ref,406 double deimos_LMM_update (float *data, float *vardata, float *model_ref, 354 407 float *model_obj, float *model_sky, float *model_bck, 355 408 float *dObj, float *dSky, float *dBck, int Nx, int Ny, int row, float lambda) { … … 365 418 double dF_obj = 0.0, dF_sky = 0.0, dF_bck = 0.0; 366 419 double d2F_obj_obj = 0.0, d2F_obj_sky = 0.0, d2F_obj_bck = 0.0, d2F_sky_sky = 0.0, d2F_sky_bck = 0.0, d2F_bck_bck = 0.0; 367 368 float W = 1.0;369 420 370 421 for (int iy = 0; iy < Ny; iy++) { … … 380 431 if (!isfinite(model_bck[pix])) continue; 381 432 433 if (vardata && !isfinite(vardata[pix_data])) continue; 434 float Var = vardata ? vardata[pix_data] : 1.0; 435 float W = (fabs(Var) < 0.01) ? 100.0 : 1.0 / Var; 436 382 437 float df = W * (data[pix_data] - model_ref[pix]); 383 438 … … 431 486 } 432 487 433 static float deimos_get_chisq (float *buffer, float * model, int Nx, int Ny, int row, int *Npts) {488 static float deimos_get_chisq (float *buffer, float *vardata, float *model, int Nx, int Ny, int row, int *Npts) { 434 489 435 490 int npts = 0; … … 445 500 if (!isfinite(buffer[pix_buffer])) continue; 446 501 if (!isfinite(model[pix_model])) continue; 447 chisq += SQ(buffer[pix_buffer] - model[pix_model]); 502 503 if (vardata && !isfinite(vardata[pix_buffer])) continue; 504 float Var = vardata ? vardata[pix_buffer] : 1.0; 505 float W = (fabs(Var) < 0.01) ? 100.0 : 1.0 / Var; 506 507 float dchisq = SQ(buffer[pix_buffer] - model[pix_model]) * W; 508 if (!isfinite(dchisq)) continue; 509 chisq += dchisq; 448 510 npts ++; 449 511 } … … 452 514 return chisq; 453 515 } 454 # endif -
branches/eam_branches/ohana.20190329/src/opihi/mana/deimos_mkalt.c
r40741 r40838 1 1 # include "data.h" 2 2 # include "deimos.h" 3 4 XXX: deprecated 3 5 4 6 // Use this to test the deimos_make_object function used by deimos_fitobj -
branches/eam_branches/ohana.20190329/src/opihi/mana/deimos_mkmodel.c
r40663 r40838 86 86 // generate a subimage of the full model for the range row - row + Ny 87 87 // obj,sky,bck are subset vectors corresponding to the range row to row+Ny 88 float *deimos_make_model (opihi_flt *obj, opihi_flt *sky, opihi_flt *bck, Vector *psf, Vector *profile, Spline *slit_trace, Spline *psf_trace, int Nx, int Ny, int row) { 88 // the slit_trace_* and psf_trace refer to the full image starting at row 89 float *deimos_make_model (opihi_flt *obj, opihi_flt *sky, opihi_flt *bck, Vector *psf, Vector *profile, Spline *slit_trace_red, Spline *slit_trace_blu, float redlimit, Spline *psf_trace, int Nx, int Ny, int row) { 89 90 90 91 float *raw = deimos_make_straight_image (obj, sky, psf, psf_trace, Nx, Ny, row); … … 92 93 deimos_apply_profile (profile, new, Nx, Ny); 93 94 deimos_add_background (bck, new, Nx, Ny, row); 94 float *out = deimos_apply_trace (slit_trace , new, Nx, Ny, row);95 float *out = deimos_apply_trace (slit_trace_red, slit_trace_blu, redlimit, new, Nx, Ny, row); 95 96 96 97 free (raw); … … 113 114 114 115 // Xref is a global supplied by the user (defaults to Nx/2 if < 0) 115 int Xoff = (int)(Xref - Npsf);116 int XoffRef = (int)(Xref - Npsf); 116 117 117 118 // Nx : width of output window … … 123 124 124 125 // we are generating the image for just the row range row to row+Ny 126 // note that obj & sky are subset vectors for just this range of pixels 125 127 opihi_flt objVy = obj[iy]; // if we shift to using the row offset: obj[iy + row] 126 128 opihi_flt skyVy = sky[iy]; // if we shift to using the row offset: sky[iy + row] … … 129 131 int dxi = 0; 130 132 float dxf = 0.0, dxr = 1.0; 133 int Xoff = XoffRef; 131 134 132 135 if (psf_trace) { 133 float dx = spline_apply_dbl (psf_trace->xk, psf_trace->yk, psf_trace->y2, psf_trace->Nknots, iy); 136 // the psf_trace is referenced against the full image, so we need to add row to iy: 137 float dx = spline_apply_dbl (psf_trace->xk, psf_trace->yk, psf_trace->y2, psf_trace->Nknots, iy + row); 134 138 dxi = floor(dx); 135 139 dxf = dx - dxi; // -1.7 -> +0.3, -0.5 -> +0.5, +0.5 ->+0.5, +1.7 -> +0.7 136 140 dxr = 1 - dxf; 137 Xref += dxi; 141 Xoff += dxi; 142 // fprintf (stderr, "%d: %f : %d\n", iy + row, dx, Xoff); 138 143 } 139 144 … … 153 158 if (doInterp) { 154 159 if ((n > 0) && (n < NpsfFull)) { 155 value += objVy*psfV[n]*dxr + objVy*psfV[n-1]*dxf; 160 if (isfinite(psfV[n]) && isfinite(psfV[n-1])) { 161 value += objVy*psfV[n]*dxr + objVy*psfV[n-1]*dxf; 162 } 156 163 } 157 164 if (n == 0) { 158 value += objVy*psfV[n]*dxr; 165 if (isfinite(psfV[n])) { 166 value += objVy*psfV[n]*dxr; 167 } 159 168 } 160 169 } else { 161 value += objVy * psfV[n]; 170 if (isfinite(psfV[n])) { 171 value += objVy * psfV[n]; 172 } 162 173 } 163 174 } … … 243 254 244 255 // shift pixels in x based on the trace 245 float *deimos_apply_trace (Spline *slit_trace , float *input, int Nx, int Ny, int row) {256 float *deimos_apply_trace (Spline *slit_trace_red, Spline *slit_trace_blu, float redlimit, float *input, int Nx, int Ny, int row) { 246 257 247 258 ALLOCATE_PTR (output, float, Nx*Ny); … … 251 262 // evaluate the trace spline at this y-coord to find the x-coord offset of the profile center 252 263 // NOTE: spline is evaluated at full dispersion coordinate 253 float dx = -spline_apply_dbl (slit_trace->xk, slit_trace->yk, slit_trace->y2, slit_trace->Nknots, iy + row); 264 int iy_full = iy + row; 265 Spline *slit_trace = (iy_full < redlimit) ? slit_trace_red : slit_trace_blu; 266 float dx = -spline_apply_dbl (slit_trace->xk, slit_trace->yk, slit_trace->y2, slit_trace->Nknots, iy_full); 254 267 255 268 // extract the integer pixel offset and the fractional offset -
branches/eam_branches/ohana.20190329/src/opihi/mana/deimos_mkobj.c
r40817 r40838 223 223 dxr = 1 - dxf; 224 224 Npof += dxi; 225 // fprintf (stderr, "%d: %f : %d\n", iy, dx, Npof); 225 226 if (VERBOSE && (iy % 200 == 0)) { gprint (GP_ERR, "%d : %f : %d : %f %f\n", iy, dx, dxi, dxf, dxr); } 226 227 } … … 241 242 if (doInterp) { 242 243 if ((n > 0) && (n < NpsfFull)) { 243 value += objVy*psfV[n]*dxr + objVy*psfV[n-1]*dxf; 244 if (isfinite(psfV[n]) && isfinite(psfV[n-1])) { 245 value += objVy*psfV[n]*dxr + objVy*psfV[n-1]*dxf; 246 } 244 247 } 245 248 if (n == 0) { 246 value += objVy*psfV[n]*dxr; 249 if (isfinite(psfV[n])) { 250 value += objVy*psfV[n]*dxr; 251 } 247 252 } 248 253 } else { 249 value += objVy*psfV[n]; 254 if (isfinite(psfV[n])) { 255 value += objVy*psfV[n]; 256 } 250 257 } 251 258 }
Note:
See TracChangeset
for help on using the changeset viewer.
