IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 18, 2009, 11:11:15 AM (17 years ago)
Author:
eugene
Message:

import updates from camera group (fits table outputs, fixes to persistent trails); modify import to avoid libpsf (fortran)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.c

    r23552 r23924  
    2020#define EXTERN  /* Define EXTERN to declare variables in params.h */
    2121#include "burnparams.h"
     22#include "persist_fits.h"
    2223
    2324int
     
    2829   HeaderUnit ehu;
    2930   const char* ifilename = "-";
    30    const char* ofilename = NULL;
    3131   char extname[FH_MAX_STRLEN+1], otaposn[FH_MAX_STRLEN+1];
    3232   int cellmask[MAXCELL];
     
    3434   int nextend, cellxy, cell, cellcode;
    3535   int ext, update, restore, psfsize, psfavg;
    36    int outfd;
    3736   IMTYPE *buf;
    38    const char *burnfile=NULL,  *persistfile=NULL;
     37   const char *burnfile=NULL,  *persistfile=NULL, *persistfitsfile=NULL;
     38   const char *deltablefitsfile=NULL;
    3939   const char *psffile=NULL, *psfstatfile=NULL;
    4040   CELL OTA[MAXCELL];   /* Cell structure for entire OTA */
    4141
    42    if (argc == 1)  {
    43      syntax(argv[0]);
    44      exit(EXIT_SUCCESS);
    45    }
    46 
    4742   if(argc > 1 && strncmp(argv[1], "help", 4) == 0) {
    48       syntax(argv[0]);
    49       exit(EXIT_SUCCESS);
    50    }
    51 
    52    if(argc > 1 && strncmp(argv[1], "--help", 6) == 0) {
    53       syntax(argv[0]);
    54       exit(EXIT_SUCCESS);
    55    }
    56 
    57    if(argc > 1 && strncmp(argv[1], "-h", 6) == 0) {
    5843      syntax(argv[0]);
    5944      exit(EXIT_SUCCESS);
     
    7156      fprintf(stderr, "\rerror: `%s' is not a multi-extension FITS\n",
    7257              ifilename);
     58#ifndef JT2DHACK
    7359      exit(EXIT_FAILURE);
     60#endif
    7461   }
    7562
     
    9178   MAX_READ_NOISE = 20;         /* Maximum believable read noise (ADU) */
    9279   MIN_EADU = 0.3;              /* Minimum believable e/ADU */
     80   SAT4SURE  = 60000;           /* Ignore pixels above for noise estimate */
     81   MIN_BLAST_PASS = 0.1;        /* Allow blasted cells if they have BIG satfrac */
     82   MAX_BLAST_PASS = 0.9;        /* But not if it's all wiped out! */
    9383
    9484   BURN_THRESH  = 30000;        /* Threshold for onset of burning */
     
    10898
    10999   NEGLIGIBLE_TRAIL = 0.5;      /* Don't sweat less than this * sigma */
    110 
     100   EXPIRE_TRAIL_TIME = 2000;    /* Expire a persist after this [sec] */
    111101
    112102/* Parse the args */
     
    155145         burnfile = argv[i] + 9;
    156146
    157 /* Output file for burn streaks */
     147/* Output text file for burn streaks */
    158148      } else if(strncmp(argv[i], "out=", 4) == 0) {     /* out=fname */
    159149         burnfile = argv[i] + 4;
    160150
    161 # if (0)
    162 /* XXX disable for now: is not yet working */
    163 /* Alternate Output file for deburned image */
    164       } else if(strncmp(argv[i], "outimage=", 9) == 0) {        /* outimage=fname */
    165         ofilename = argv[i] + 9;
    166 # endif
    167 
    168 /* Input file for previous burn persistence streaks */
    169       } else if(strncmp(argv[i], "trailin=", 8) == 0) { /* trailin=fname */
     151/* Input text file for previous burn persistence streaks */
     152      } else if(strncmp(argv[i], "infits=", 8) == 0) {  /* in=fname */
    170153         persistfile = argv[i] + 8;
     154
     155/* Same thing, but information is stored in tables in a FITS file. */
     156      } else if(strncmp(argv[i], "trailinfits=", 8) == 0) { /* infits=fname */
     157         persistfitsfile = argv[i] + 12;
    171158
    172159/* Input file for previous burn persistence streaks */
    173160      } else if(strncmp(argv[i], "in=", 3) == 0) {      /* in=fname */
    174161         persistfile = argv[i] + 3;
     162
     163/* Same thing, but information is stored in tables in a FITS file. */
     164      } else if(strncmp(argv[i], "trailinfits=", 8) == 0) { /* trailin=fname */
     165         persistfitsfile = argv[i] + 12;
    175166
    176167/* Output file for PSF gallery */
     
    244235      } else if(strncmp(argv[i], "thrpsf=", 7) == 0) {  /* thrpsf=thresh */
    245236         if(sscanf(argv[i]+7, "%d", &PSF_THRESH) != 1) {
     237            fprintf(stderr, "\rerror: cannot get value from `%s'\n", argv[i]);
     238            exit(EXIT_FAILURE);
     239         }
     240
     241      } else if(strncmp(argv[i], "expire=", 7) == 0) {  /* expire=nsec */
     242         if(sscanf(argv[i]+7, "%d", &EXPIRE_TRAIL_TIME) != 1) {
    246243            fprintf(stderr, "\rerror: cannot get value from `%s'\n", argv[i]);
    247244            exit(EXIT_FAILURE);
     
    295292         }
    296293
     294      } else if(strncmp(argv[i], "deltables=", 8) == 0) { /* trailin=fname */
     295         deltablefitsfile = argv[i] + 10;       
     296
    297297      } else if(strncmp(argv[i], "help", 4) == 0) {     /* help output */
    298298         syntax(argv[0]);
     
    306306   }
    307307
     308   /* If we're told to remove the table from a FITS file, then
     309    * do nothing else. */
     310   if(deltablefitsfile) {
     311     fprintf(stderr,
     312             "\rRemoving burn tables from %s, all other options ignored.\n",
     313             ifilename);
     314     if(persist_fits_remove_tables(ihu, deltablefitsfile) != FH_SUCCESS) {
     315       exit(EXIT_FAILURE);
     316     }
     317     else {
     318       exit(EXIT_SUCCESS);
     319     }
     320   }
     321   
     322   /* If there is no other persistence info supplied, try getting
     323    * it from the input FITS file. */
    308324   if(restore && persistfile == NULL) {
    309       fprintf(stderr, "\rerror: must specify an input file for restore\n");
    310       exit(EXIT_FAILURE);
     325     if(persistfitsfile == NULL) persistfitsfile = ifilename;
    311326   }
    312327
     
    322337      if(persist_read(OTA, persistfile)) exit(EXIT_FAILURE);
    323338   }
     339   else if(persistfitsfile != NULL) {
     340     if(persist_fits_read(OTA, persistfitsfile) != FH_SUCCESS) exit(EXIT_FAILURE);
     341   }
    324342
    325343/* Which OTA is this??? */
     
    333351   }
    334352
    335    if (ofilename) {
    336      outfd = creat (ofilename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    337      if (outfd == -1) {
    338          fprintf(stderr, "\rerror: Cannot open %s for output\n", ofilename);
    339          exit(EXIT_FAILURE);
    340       }
    341 
    342      if (fh_write(ihu, outfd)) {
    343          fprintf(stderr, "\rerror: Trouble writing PHU to %s\n", ofilename);
    344          exit(EXIT_FAILURE);
    345       }
     353   /* Warn that what you're doing might not be a good idea. */
     354   {
     355     fh_bool burn_applied;
     356     
     357     if(fh_get_bool(ihu, PHU_NAME_BURN_APPLIED,
     358                    &burn_applied) != FH_SUCCESS) {
     359        if(VERBOSE > 0) {
     360           fprintf(stderr,
     361                   "warning: Unable to determine whether burn correction "
     362                   "already applied - unable to find %s in primary header\n",
     363                   PHU_NAME_BURN_APPLIED);
     364        }
     365        burn_applied = FH_FALSE;
     366     }
     367     if(restore && (burn_applied == FH_FALSE)) {
     368       fprintf(stderr,
     369               "warning: Restoring old burns, but header indicates no burns previously corrected.\n");             
     370     }
     371     else if (update && (burn_applied == FH_TRUE)) {
     372       fprintf(stderr,
     373               "warning: Applying burn correction, but header indicates burns previously corrected.\n");             
     374       }
    346375   }
    347376
    348377/* Look at all the MEF's extensions */
     378#ifndef JT2DHACK
    349379   for (ext = 1; ext <= nextend; ext++) {
     380#else
     381   for (ext = 1; ext <= MAX(1,nextend); ext++) {
     382#endif
    350383      int naxis, naxis1, naxis2, naxis3;
    351384      int prescan1, ovrscan1, ovrscan2, pontime;
     
    353386      char xtension[FH_MAX_STRLEN + 1];
    354387
     388#ifdef JT2DHACK
     389      if(nextend == 0) {
     390         ehu = ihu;
     391         sprintf(extname, "%s", "xy00");
     392      } else {
     393#endif
    355394      if (!(ehu = fh_ehu(ihu, ext)) ||
    356395          fh_get_str(ehu, "EXTNAME", extname, sizeof(extname)) != FH_SUCCESS) {
     
    377416         fprintf(stderr,
    378417                 "warning: Skipping non-image extension %s\n", extname);
     418#ifdef JT2DHACK
     419      }
     420#endif
    379421/*
    380422 * %%% When reading from a pipe, does the data still need
     
    448490      if (fh_get_BZERO(ehu, &bzero_d) == FH_SUCCESS) BZERO = bzero_d;
    449491
     492      if(VERBOSE & VERB_NORM) {
     493         printf("nx=%d ny=%d prex=%d postx=%d posty=%d BZERO=%.1f\n",
     494                naxis1, naxis2, prescan1, ovrscan1, ovrscan2, BZERO);
     495      }
     496
    450497      buf = (IMTYPE*)malloc(naxis1*naxis2*naxis3*sizeof(short));
    451498      if (fh_read_padded_image(ehu, fh_file_desc(ehu), buf,
     
    471518/* Get bias and sky levels */
    472519         err = cell_stats(nx, ny, naxis1, naxis2, imbuf, OTA+cell);
     520         if(err) {
     521            if(VERBOSE > 0) {
     522               fprintf(stderr, "logonly: error getting bias/sky/rms for cell %d, skipping...\n", cell);
     523            }
     524            continue;
     525         }
     526
     527/* Does this cell get a pass because it's heavily blasted? */
     528/* Turn it in to a relaxation factor for the "read noise" */
     529         i = OTA[cell].satfrac > MIN_BLAST_PASS &&
     530             OTA[cell].satfrac < MAX_BLAST_PASS &&
     531             OTA[cell].sky < TRAIL_THRESH ? 30 : 1;
    473532
    474533/* Does this cell look kosher? */
    475534         if(OTA[cell].rms*OTA[cell].rms >
    476             OTA[cell].sky/MIN_EADU + MAX_READ_NOISE*MAX_READ_NOISE) {
     535            OTA[cell].sky/MIN_EADU + i*i*MAX_READ_NOISE*MAX_READ_NOISE) {
    477536            if(VERBOSE > 0) {
    478537               fprintf(stderr, "logonly: cell %d is unreasonably noisy, skipping...\n", cell);
     538               fprintf(stderr, "logonly: bias = %d sky = %d rms = %d satfrac = %.2f\n",
     539                       OTA[cell].bias, OTA[cell].sky, OTA[cell].rms, OTA[cell].satfrac);
    479540            }
    480541            continue;
     
    506567
    507568         } else {
    508 
    509569/* Restore the old burns */
    510570            burn_restore(naxis1-ovrscan1, naxis2-ovrscan2, naxis1,
     
    514574/* Write the corrected data back to the FITS. */
    515575         if(update) {
    516            if (ofilename) {
    517              fh_ehu(ehu, 0);    /* Seek back to the start of data */
    518 
    519              if (fh_write(ehu, outfd)) {
    520                fprintf(stderr, "\rerror: Trouble writing EXT %d to %s\n", ext, ofilename);
    521                exit(EXIT_FAILURE);
    522              }
    523 
    524              if (fh_write_padded_image(ehu, outfd, buf,
    525                                        naxis1*naxis2*naxis3*sizeof(short),
    526                                        FH_TYPESIZE_16) != FH_SUCCESS) {
     576            fh_ehu(ehu, 0);     /* Seek back to the start of data */
     577            if (fh_write_padded_image(ehu, fh_file_desc(ehu), buf,
     578                                      naxis1*naxis2*naxis3*sizeof(short),
     579                                      FH_TYPESIZE_16) != FH_SUCCESS) {
    527580               fprintf(stderr, "\rerror: failed to re-write image data for extension `%s'.\n",
    528581                       extname);
    529582               free(buf);
    530583               exit(EXIT_FAILURE);
    531              }
    532            } else {
    533              fh_ehu(ehu, 0);    /* Seek back to the start of data */
    534              if (fh_write_padded_image(ehu, fh_file_desc(ehu), buf,
    535                                        naxis1*naxis2*naxis3*sizeof(short),
    536                                        FH_TYPESIZE_16) != FH_SUCCESS) {
    537                fprintf(stderr, "\rerror: failed to re-write image data for extension `%s'.\n",
    538                        extname);
    539                free(buf);
    540                exit(EXIT_FAILURE);
    541              }
    542            }
    543          }
    544       }
    545      
     584            }
     585         }
     586      }
     587
    546588      free(buf);
    547589   }
    548590
    549    if (ofilename) {
    550      if (close(outfd)) {
    551        fprintf(stderr, "\rerror: trouble writing data to output file %s\n", ofilename);
    552        exit(EXIT_FAILURE);
    553      }
    554    }
    555      
    556591/* Dump out the postage stamp file */
    557592   if(psffile != NULL) {
     
    564599   }
    565600
     601/* Write burn info to FITS file. */
     602   if(update) persist_fits_write(OTA, ihu);
     603
     604   
     605   if(restore) {   
     606     /* Indicate in the header that the burns are not applied. */
     607     fh_set_bool(ihu, FH_AUTO, PHU_NAME_BURN_APPLIED,
     608                 FH_FALSE, PHU_COMMENT_BURN_APPLIED);
     609     fh_rewrite(ihu);
     610   }
     611
    566612   fh_destroy(ihu);
    567613
    568614/* Write the persistence data for the next image */
    569615   if(burnfile != NULL) persist_write(OTA, burnfile);
     616
    570617
    571618   exit(EXIT_SUCCESS);
     
    613660                      CELL *cell)
    614661{
    615    int i, j, k, n;
     662   int i, j, k, n, nsat;
    616663
    617664/* Get bias stats */
     
    624671
    625672/* Get sky stats */
    626    for(k=n=0; k<nx*ny; k+=((617*nx)/1000)) {
     673   for(k=n=nsat=0; k<nx*ny; k+=((617*nx)/1000)) {
    627674      i = k % nx;
    628675      j = k / nx;
    629       median_buf[n++] = data[i+NX*j];
    630    }
     676      if(data[i+NX*j] > SAT4SURE) {
     677         nsat++;
     678      } else if(data[i+NX*j] != NODATA) {
     679         median_buf[n++] = data[i+NX*j];
     680      }
     681   }
     682
     683   if(n < 20) {         /* Better have hit at least 20! */
     684      cell->sky = cell->rms = 0.0;
     685      return(-1);
     686   }
     687   cell->satfrac = ((double)nsat) / n;
     688
    631689/* First pass at sky and quartile */
    632690   cell->sky = int_median(n, median_buf);
     
    656714   printf(" restore={t|f}  Restore the input MEF by adding input fits?\n");
    657715   printf(" in=fname       Input file for previous burn persistence streaks\n");
     716   printf(" infits=fname   Input FITS file for previous burn persistence streaks (stored\n");
     717   printf("                   in table extensions).  If both this and the 'in' input\n");
     718   printf("                   file option are specified, then 'in' takes precedence.\n");
    658719   printf(" out=fname      Output file for burn streaks\n");
    659    printf(" outimage=imname Output file for MEF image\n");
     720
    660721//   printf(" trailin=fname  Input file for previous burn persistence streaks\n");
     722//   printf(" trailinfits=fname  Input FITS file for previous burn persistence streaks\n");
    661723//   printf(" trailout=fname Output file for burn streaks\n");
     724   printf(" deltables=fname Copy mef_file to new FITS file 'fname' with burn streak\n");
     725   printf("                   tables removed. NOTE: if specified, all other options\n");
     726   printf("                   will be ignored!\n");
    662727   printf(" psf=fname      Output file for PSF FITS stamp gallery\n");
    663728   printf(" psfstat=fname  Output file for PSF statistics listing\n");
     
    676741   printf(" rmask=X        Diameter growth factor of burned spots\n");
    677742   printf(" bmask=X        Box size growth of burn/star boxes\n");
     743   printf(" expire=N       Retire a blasted burn after N seconds\n");
    678744   printf(" quiet={t|f}    Quiet?\n");
    679745   printf(" verbose=N      Set verbosity bits:\n");
Note: See TracChangeset for help on using the changeset viewer.