00001
00002 #define HIMGCFGFILE "/home/jsoc/cvs/Development/JSOC/proj/tables/img_cnfg_ids"
00003
00004 #define XXX_CANT_OPEN_HIMGCFGFILE -1
00005 #define XXX_CORRUPT_HIMGCFGFILE -2
00006 #define XXX_CANT_FIND_HIMGCFID -3
00007 #define XXX_INVALID_HIMGCFID -4
00008 #define XXX_INVALID_EXPTIME -5
00009
00010 #define MAXHIMGCFGS 4096
00011 static int overscan_nrows[MAXHIMGCFGS];
00012 static int overscan_ncols[MAXHIMGCFGS];
00013 static int overscan_rowstr[MAXHIMGCFGS];
00014 static int overscan_colstr[MAXHIMGCFGS];
00015 static int overscan_valid[MAXHIMGCFGS];
00016 static int dvals[MAXHIMGCFGS];
00017
00018 #define NBINS 1048576
00019 #define MINOUT -256
00020 #define MAXOUT 1048320
00021 static int hist[NBINS];
00022
00023 #include "aia_despike.c"
00024
00026 int get_overscan_info(int himgcfid)
00027 {
00028 static int firstcall = 1;
00029 static FILE *fp;
00030 static char line[256];
00031 int found = 0;
00032 int id;
00033 char j[64];
00034
00035 if (firstcall) {
00036 fp = fopen(HIMGCFGFILE,"r");
00037 if (!fp)
00038 return XXX_CANT_OPEN_HIMGCFGFILE;
00039 firstcall = 0;
00040 }
00041
00042 rewind(fp);
00043 while (fgets(line, 256, fp)) {
00044 if (1 != sscanf(line, "%d", &id))
00045 continue;
00046 if (id != himgcfid)
00047 continue;
00048 if (15 != sscanf(line, "%s%s%s%s%s%s%s%s%s%d%d%d%d%s%d",
00049 j,j,j,j,j,j,j,j,j, &overscan_nrows[id], &overscan_ncols[id],
00050 &overscan_rowstr[id], &overscan_colstr[id], j, &dvals[id]))
00051 return XXX_CORRUPT_HIMGCFGFILE;
00052 found = 1;
00053 overscan_valid[himgcfid] = 1;
00054 }
00055
00056 if (!found)
00057 return XXX_CANT_FIND_HIMGCFID;
00058
00059 return 0;
00060 }
00061
00063 int do_flat(LEV0LEV1 *info)
00064 {
00065 int i,j,idx,IDX;
00066 int nr,nc,r1,r2,c1,c2;
00067 int status;
00068 int is_dark = info->darkflag;
00069 float *flat = info->adataff;
00070 float *dark = info->adatadark;
00071 short *in = info->adata0;
00072 float *out = info->dat1.adata1;
00073 short tmp;
00074 int itmp;
00075 float ftmp;
00076 double dtmp;
00077 double exptime;
00078 double s, s2, s3, s4, ss;
00079 int npix, min, max, medn;
00080
00081 exptime = drms_getkey_double(rs0, "EXPTIME", &status);
00082 if (!is_dark && (status || !isfinite(exptime) || exptime <= 0.0))
00083 return XXX_INVALID_EXPTIME;
00084
00085 if (info->himgcfid < 0 || info->himgcfid >= MAXHIMGCFGS)
00086 return XXX_INVALID_HIMGCFID;
00087 if (!overscan_valid[info->himgcfid]) {
00088 status = get_overscan_info(info->himgcfid);
00089 if (status) return status;
00090 }
00091
00092 nr = overscan_nrows[info->himgcfid];
00093 if (nr) {
00094 r1 = overscan_rowstr[info->himgcfid];
00095 r2 = r1 + nr;
00096 } else
00097 r1 = r2 = 4096;
00098
00099 nc = overscan_ncols[info->himgcfid];
00100 if (nc) {
00101 c1 = overscan_colstr[info->himgcfid];
00102 c2 = c1 + nc;
00103 } else
00104 c1 = c2 = 4096;
00105
00106
00107
00108
00109 info->oscnmean = info->oscnrms = DRMS_MISSING_DOUBLE;
00110 npix = 0;
00111 s = s2 = 0.0;
00112
00113 if (nr) {
00114 for (i=r1; i<r2; ++i) {
00115 for (j=0; j<c1; ++j) {
00116 tmp = in[4096*i+j];
00117 if (tmp == DRMS_MISSING_SHORT)
00118 continue;
00119 dtmp = tmp;
00120 s += dtmp;
00121 s2 += dtmp*dtmp;
00122 ++npix;
00123 }
00124 for (j=c2; j<4096; ++j) {
00125 tmp = in[4096*i+j];
00126 if (tmp == DRMS_MISSING_SHORT)
00127 continue;
00128 dtmp = tmp;
00129 s += dtmp;
00130 s2 += dtmp*dtmp;
00131 ++npix;
00132 }
00133 }
00134 }
00135
00136 if (nc) {
00137 for (i=0; i<r1; ++i)
00138 for (j=c1; j<c2; ++j) {
00139 tmp = in[4096*i+j];
00140 if (tmp == DRMS_MISSING_SHORT)
00141 continue;
00142 dtmp = tmp;
00143 s += dtmp;
00144 s2 += dtmp*dtmp;
00145 ++npix;
00146 }
00147 for (i=r2; i<4096; ++i)
00148 for (j=c1; j<c2; ++j) {
00149 tmp = in[4096*i+j];
00150 if (tmp == DRMS_MISSING_SHORT)
00151 continue;
00152 dtmp = tmp;
00153 s += dtmp;
00154 s2 += dtmp*dtmp;
00155 ++npix;
00156 }
00157 }
00158
00159 if (npix > 1) {
00160 info->oscnmean = s/npix;
00161 info->oscnrms = sqrt((s2-s*s/npix) / (npix-1));
00162 }
00163
00164
00165
00166
00167
00168 memset(hist, 0, 4*NBINS);
00169 npix = 0;
00170 nr /= 2;
00171 nc /= 2;
00172 for (i=0; i<r1; ++i) {
00173 idx = 4096*i;
00174 IDX = idx + 4096*nr + nc;
00175 for (j=0; j<c1; ++j) {
00176 tmp = in[idx++];
00177 if (DRMS_MISSING_SHORT == tmp)
00178 out[IDX++] = DRMS_MISSING_FLOAT;
00179 else {
00180
00181 ftmp = is_dark ? tmp : (tmp - dark[IDX]) / (exptime * flat[IDX]);
00182 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00183 itmp = roundf(ftmp);
00184 out[IDX] = ftmp;
00185 ++hist[itmp-MINOUT];
00186 ++npix;
00187 } else if (ftmp < 0) {
00188 out[IDX] = MINOUT;
00189 ++hist[0];
00190 ++npix;
00191 } else if (ftmp > 0) {
00192 out[IDX] = MAXOUT;
00193 ++hist[NBINS-1];
00194 ++npix;
00195 }
00196
00197 ++IDX;
00198 }
00199 }
00200 }
00201 for (i=0; i<r1; ++i) {
00202 idx = 4096*i + c2;
00203 IDX = idx + 4096*nr - nc;
00204 for (j=c2; j<4096; ++j) {
00205 tmp = in[idx++];
00206 if (DRMS_MISSING_SHORT == tmp)
00207 out[IDX++] = DRMS_MISSING_FLOAT;
00208 else {
00209
00210 ftmp = is_dark ? tmp : (tmp - dark[IDX]) / (exptime * flat[IDX]);
00211 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00212 itmp = roundf(ftmp);
00213 out[IDX] = ftmp;
00214 ++hist[itmp-MINOUT];
00215 ++npix;
00216 } else if (ftmp < 0) {
00217 out[IDX] = MINOUT;
00218 ++hist[0];
00219 ++npix;
00220 } else if (ftmp > 0) {
00221 out[IDX] = MAXOUT;
00222 ++hist[NBINS-1];
00223 ++npix;
00224 }
00225
00226 ++IDX;
00227 }
00228 }
00229 }
00230 for (i=r2; i<4096; ++i) {
00231 idx = 4096*i;
00232 IDX = idx - 4096*nr + nc;
00233 for (j=0; j<c1; ++j) {
00234 tmp = in[idx++];
00235 if (DRMS_MISSING_SHORT == tmp)
00236 out[IDX++] = DRMS_MISSING_FLOAT;
00237 else {
00238
00239 ftmp = is_dark ? tmp : (tmp - dark[IDX]) / (exptime * flat[IDX]);
00240 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00241 itmp = roundf(ftmp);
00242 out[IDX] = ftmp;
00243 ++hist[itmp-MINOUT];
00244 ++npix;
00245 } else if (ftmp < 0) {
00246 out[IDX] = MINOUT;
00247 ++hist[0];
00248 ++npix;
00249 } else if (ftmp > 0) {
00250 out[IDX] = MAXOUT;
00251 ++hist[NBINS-1];
00252 ++npix;
00253 }
00254
00255 ++IDX;
00256 }
00257 }
00258 }
00259 for (i=r2; i<4096; ++i) {
00260 idx = 4096*i + c2;
00261 IDX = idx - 4096*nr - nc;
00262 for (j=c2; j<4096; ++j) {
00263 tmp = in[idx++];
00264 if (DRMS_MISSING_SHORT == tmp)
00265 out[IDX++] = DRMS_MISSING_FLOAT;
00266 else {
00267
00268 ftmp = is_dark ? tmp : (tmp - dark[IDX]) / (exptime * flat[IDX]);
00269 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00270 itmp = roundf(ftmp);
00271 out[IDX] = ftmp;
00272 ++hist[itmp-MINOUT];
00273 ++npix;
00274 } else if (ftmp < 0) {
00275 out[IDX] = MINOUT;
00276 ++hist[0];
00277 ++npix;
00278 } else if (ftmp > 0) {
00279 out[IDX] = MAXOUT;
00280 ++hist[NBINS-1];
00281 ++npix;
00282 }
00283
00284 ++IDX;
00285 }
00286 }
00287 }
00288
00289 info->datavals = npix;
00290 info->missvals = dvals[info->himgcfid] - npix;
00291
00292
00293
00294
00295 if (nr) {
00296 for (i=0; i<4096*nr; ++i) out[i] = DRMS_MISSING_FLOAT;
00297 for (i=4096*(4096-nr); i<MAXPIXELS; ++i) out[i] = DRMS_MISSING_FLOAT;
00298 }
00299 if (nc) {
00300 for (i=0; i<4096; ++i) {
00301 for (j=0; j<nc; ++j)
00302 out[i*4096+j] = DRMS_MISSING_FLOAT;
00303 for (j=4096-nc; j<4096; ++j)
00304 out[i*4096+j] = DRMS_MISSING_FLOAT;
00305 }
00306 }
00307
00308
00309
00310
00311 info->datamin = info->datamax = info->datamedn = DRMS_MISSING_INT;
00312 info->datamean = info->data_rms = info->dataskew
00313 = info->datakurt = DRMS_MISSING_DOUBLE;
00314
00315 min = 0;
00316 max = NBINS-1;
00317 if (npix) {
00318 while (hist[min] == 0)
00319 ++min;
00320 info->datamin = min + MINOUT;
00321
00322 while (hist[max] == 0)
00323 --max;
00324 info->datamax = max + MINOUT;
00325
00326 medn = min;
00327 i = hist[medn];
00328 while (2*i < npix)
00329 i += hist[++medn];
00330 info->datamedn = medn + MINOUT;
00331
00332 s = s2 = s3 = s4 = 0.0;
00333 for (i=min; i<=max; ++i) {
00334 double ii = i + MINOUT;
00335 s += (dtmp = ii*hist[i]);
00336 s2 += (dtmp *= ii);
00337 s3 += (dtmp *= ii);
00338 s4 += (dtmp *= ii);
00339 }
00340 s /= npix;
00341 info->datamean = s;
00342 ss = s*s;
00343 s2 /= npix;
00344 s3 /= npix;
00345 s4 /= npix;
00346 if (npix > 1) {
00347 dtmp = npix * (s2-ss) / (npix-1);
00348 info->data_rms = sqrt(dtmp);
00349 if (dtmp > 0.0) {
00350 info->dataskew = (s3 - s * (3*s2 - 2*ss)) /
00351 (dtmp*info->data_rms);
00352 info->datakurt = (s4 - 4*s*s3 + 3*ss*(2*s2-ss)) /
00353 (dtmp*dtmp) - 3;
00354 }
00355 }
00356 }
00357
00358 return 0;
00359 }
00360
00362 int do_flat_aia(LEV0LEV1 *info)
00363 {
00364 int i,j,idx,IDX;
00365 int nr,nc,r1,r2,c1,c2;
00366 int status;
00367 float *flat = info->adataff;
00368 float *dark = info->adatadark;
00369 short *in = info->adata0;
00370 int *out = info->dat1.adata1A;
00371 short tmp;
00372 float ftmp;
00373 double dtmp;
00374 double s=0.0, s2=0.0, s3=0.0, s4=0.0, ss;
00375 int npix, min, max, medn;
00376
00377 if (info->himgcfid < 0 || info->himgcfid >= MAXHIMGCFGS)
00378 return XXX_INVALID_HIMGCFID;
00379 if (!overscan_valid[info->himgcfid]) {
00380 status = get_overscan_info(info->himgcfid);
00381 if (status) return status;
00382 }
00383
00384 nr = overscan_nrows[info->himgcfid];
00385 if (nr) {
00386 r1 = overscan_rowstr[info->himgcfid];
00387 r2 = r1 + nr;
00388 } else
00389 r1 = r2 = 4096;
00390 nr /= 2;
00391
00392 nc = overscan_ncols[info->himgcfid];
00393 if (nc) {
00394 c1 = overscan_colstr[info->himgcfid];
00395 c2 = c1 + nc;
00396 } else
00397 c1 = c2 = 4096;
00398 nc /= 2;
00399
00400 info->oscnmean = info->oscnrms = DRMS_MISSING_DOUBLE;
00401
00402 memset(hist, 0, 4*NBINS);
00403
00404
00405
00406
00407
00408 npix = 0;
00409 for (i=0; i<r1; ++i) {
00410 idx = 4096*i;
00411 IDX = idx + 4096*nr + nc;
00412 for (j=0; j<c1; ++j) {
00413 tmp = in[idx++];
00414 if (DRMS_MISSING_SHORT == tmp)
00415 out[IDX++] = DRMS_MISSING_INT;
00416 else {
00417 ftmp = roundf((tmp - dark[IDX]) / flat[IDX]);
00418 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00419 out[IDX] = ftmp;
00420 ++hist[out[IDX]-MINOUT];
00421 ++npix;
00422 } else
00423 out[IDX] = DRMS_MISSING_INT;
00424 ++IDX;
00425 }
00426 }
00427 }
00428 for (i=0; i<r1; ++i) {
00429 idx = 4096*i + c2;
00430 IDX = idx + 4096*nr - nc;
00431 for (j=c2; j<4096; ++j) {
00432 tmp = in[idx++];
00433 if (DRMS_MISSING_SHORT == tmp)
00434 out[IDX++] = DRMS_MISSING_INT;
00435 else {
00436 ftmp = roundf((tmp - dark[IDX]) / flat[IDX]);
00437 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00438 out[IDX] = ftmp;
00439 ++hist[out[IDX]-MINOUT];
00440 ++npix;
00441 } else
00442 out[IDX] = DRMS_MISSING_INT;
00443 ++IDX;
00444 }
00445 }
00446 }
00447 for (i=r2; i<4096; ++i) {
00448 idx = 4096*i;
00449 IDX = idx - 4096*nr + nc;
00450 for (j=0; j<c1; ++j) {
00451 tmp = in[idx++];
00452 if (DRMS_MISSING_SHORT == tmp)
00453 out[IDX++] = DRMS_MISSING_INT;
00454 else {
00455 ftmp = roundf((tmp - dark[IDX]) / flat[IDX]);
00456 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00457 out[IDX] = ftmp;
00458 ++hist[out[IDX]-MINOUT];
00459 ++npix;
00460 } else
00461 out[IDX] = DRMS_MISSING_INT;
00462 ++IDX;
00463 }
00464 }
00465 }
00466 for (i=r2; i<4096; ++i) {
00467 idx = 4096*i + c2;
00468 IDX = idx - 4096*nr - nc;
00469 for (j=c2; j<4096; ++j) {
00470 tmp = in[idx++];
00471 if (DRMS_MISSING_SHORT == tmp)
00472 out[IDX++] = DRMS_MISSING_INT;
00473 else {
00474 ftmp = roundf((tmp - dark[IDX]) / flat[IDX]);
00475 if (ftmp >= MINOUT && ftmp < MAXOUT) {
00476 out[IDX] = ftmp;
00477 ++hist[out[IDX]-MINOUT];
00478 ++npix;
00479 } else
00480 out[IDX] = DRMS_MISSING_INT;
00481 ++IDX;
00482 }
00483 }
00484 }
00485
00486 info->datavals = npix;
00487 info->missvals = dvals[info->himgcfid] - npix;
00488
00489
00490
00491
00492 if (nr) {
00493 for (i=0; i<4096*nr; ++i) out[i] = DRMS_MISSING_INT;
00494 for (i=4096*(4096-nr); i<MAXPIXELS; ++i) out[i] = DRMS_MISSING_INT;
00495 }
00496 if (nc) {
00497 for (i=0; i<4096; ++i) {
00498 for (j=0; j<nc; ++j)
00499 out[i*4096+j] = DRMS_MISSING_INT;
00500 for (j=4096-nc; j<4096; ++j)
00501 out[i*4096+j] = DRMS_MISSING_INT;
00502 }
00503 }
00504
00505 {
00506 int *oldvalues, *spikelocs, *newvalues;
00507 int aia_status, i, level = 7, niter = 3, nbads, nspikes=0, respike, rstatus;
00508 float frac = 0.8;
00509 int wl = drms_getkey_int(rs, "WAVELNTH", &rstatus);
00510
00511 switch (wl) {
00512 case 1600:
00513 case 1700:
00514 case 4500:
00515 respike = 1;
00516 break;
00517 default:
00518 respike = 0;
00519 break;
00520 }
00521 for (i=0; i<4096*4096; i++) {
00522
00523 if (out[i] > 32767) out[i] = 32767;
00524 }
00525 if (respike) niter = 0; else niter = 3;
00526 nbads = ArrayBad->axis[0];
00527 aia_status = aia_despike(info->dat1.adata1A, NULL, 4096, 4096, frac, level,
00528 niter, info->adatabad, nbads, &nspikes, &oldvalues,
00529 &spikelocs, &newvalues);
00530 set_nspikes(nspikes);
00531 set_spikelocs(spikelocs);
00532 set_oldvalues(oldvalues);
00533 set_newvalues(newvalues);
00534 drms_setkey_int(rs, "NSPIKES", nspikes);
00535 }
00536
00537
00538
00539 npix = 0; memset(hist, 0, 4*NBINS);
00540
00541
00542
00543
00544
00545
00546
00547
00548 npix = 0; memset(hist, 0, 4*NBINS);
00549 for (i=0; i<4096*4096; i++) {
00550 if(out[i] != DRMS_MISSING_INT) {
00551 if (out[i] < MINOUT) out[i] = MINOUT;
00552 if (out[i] > 16383) out[i] = 16383;
00553 ++hist[out[i]-MINOUT];
00554 ++npix;
00555 }
00556 }
00557
00558 info->datavals = npix;
00559 info->missvals = dvals[info->himgcfid] - npix;
00560 info->datamin = info->datamax = info->datamedn = DRMS_MISSING_INT;
00561
00562 info->datamean = info->data_rms = info->dataskew
00563 = info->datakurt = DRMS_MISSING_DOUBLE;
00564
00565 min = 0;
00566 max = NBINS-1;
00567 if (npix) {
00568 while (hist[min] == 0)
00569 ++min;
00570 info->datamin = min + MINOUT;
00571
00572 while (hist[max] == 0)
00573 --max;
00574 info->datamax = max + MINOUT;
00575
00576 medn = min;
00577 i = hist[medn];
00578 while (2*i < npix)
00579 i += hist[++medn];
00580 info->datamedn = medn + MINOUT;
00581
00582 for (i=min; i<=max; ++i) {
00583 double ii = i + MINOUT;
00584 s += (dtmp = ii*hist[i]);
00585 s2 += (dtmp *= ii);
00586 s3 += (dtmp *= ii);
00587 s4 += (dtmp *= ii);
00588 }
00589 s /= npix;
00590 info->datamean = s;
00591 ss = s*s;
00592 s2 /= npix;
00593 s3 /= npix;
00594 s4 /= npix;
00595 if (npix > 1) {
00596 dtmp = npix * (s2-ss) / (npix-1);
00597 info->data_rms = sqrt(dtmp);
00598 if (dtmp > 0.0) {
00599 info->dataskew = (s3 - s * (3*s2 - 2*ss)) /
00600 (dtmp*info->data_rms);
00601 info->datakurt = (s4 - 4*s*s3 + 3*ss*(2*s2-ss)) /
00602 (dtmp*dtmp) - 3;
00603 }
00604 }
00605
00606 {
00607 int n=0, nsatpix=0, sum=0, dp[8];
00608 dp[0] = 1; dp[1] = 10; dp[2] = 25; dp[3] = 75;
00609 dp[4] = 90; dp[5] = 95; dp[6] = 98; dp[7] = 99;
00610 for (i=min; i<=max; ++i) {
00611 sum += hist[i];
00612 if (sum > dp[n]*npix/100) switch (n) {
00613 case 0:
00614 drms_setkey_float(rs, "DATAP01", 1.0 + i + MINOUT);
00615 if (sum > dp[n+1]*npix/100) n++;
00616 case 1:
00617 drms_setkey_float(rs, "DATAP10", 1.0 + i + MINOUT);
00618 if (sum > dp[n+1]*npix/100) n++;
00619 case 2:
00620 drms_setkey_float(rs, "DATAP25", 1.0 + i + MINOUT);
00621 if (sum > dp[n+1]*npix/100) n++;
00622 case 3:
00623 drms_setkey_float(rs, "DATAP75", 1.0 + i + MINOUT);
00624 if (sum > dp[n+1]*npix/100) n++;
00625 case 4:
00626 drms_setkey_float(rs, "DATAP90", 1.0 + i + MINOUT);
00627 if (sum > dp[n+1]*npix/100) n++;
00628 case 5:
00629 drms_setkey_float(rs, "DATAP95", 1.0 + i + MINOUT);
00630 if (sum > dp[n+1]*npix/100) n++;
00631 case 6:
00632 drms_setkey_float(rs, "DATAP98", 1.0 + i + MINOUT);
00633 if (sum > dp[n+1]*npix/100) n++;
00634 case 7:
00635 drms_setkey_float(rs, "DATAP99", 1.0 + i + MINOUT);
00636 n++;
00637 break;
00638 }
00639 if (i>max) n++;
00640 if (i > (15000 - MINOUT)) nsatpix += hist[i];
00641 }
00642 drms_setkey_int(rs, "NSATPIX", nsatpix);
00643 }
00644 }
00645
00646 return 0;
00647 }