IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 3, 2007, 11:32:43 AM (19 years ago)
Author:
Paul Price
Message:

Merging branch with FITS compression development. Minor conflicts in pmConfig.c, pmFPAfileDefine.c, pmFPAfileIO.c resolved.

Location:
trunk/psModules/src/camera
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/camera/pmFPACopy.c

    r14884 r15180  
    8383    assert(source);
    8484    assert(xBin > 0 && yBin > 0);
     85
     86    if (!source->data_exists) {
     87        // Copied everything that exists
     88        return true;
     89    }
    8590
    8691    // XXX this is a programming / config error
     
    347352    assert(yBin > 0);
    348353
     354    if (!source->data_exists) {
     355        // Copied everything that exists
     356        return true;
     357    }
     358
    349359    psArray *targetCells = target->cells; // The target cells
    350360    psArray *sourceCells = source->cells; // The source cells
  • trunk/psModules/src/camera/pmFPAfile.c

    r15139 r15180  
    4040        psFitsClose (file->fits);
    4141    }
     42    psFree(file->compression);
    4243
    4344    psFree (file->filerule);
     
    7273    file->fpa = NULL;
    7374    file->fits = NULL;
     75    file->compression = NULL;
     76    file->bitpix = 0;
    7477    file->names = psMetadataAlloc();
    7578
  • trunk/psModules/src/camera/pmFPAfile.h

    r14889 r15180  
    44 * @author EAM, IfA
    55 *
    6  * @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
    7  * @date $Date: 2007-09-19 21:37:58 $
     6 * @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
     7 * @date $Date: 2007-10-03 21:32:43 $
    88 * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
    99 */
     
    5151} pmFPAfileState;
    5252
    53 typedef struct
    54 {
     53typedef struct {
    5554    pmFPAfileMode mode;                 // is this file read, written, or only used internally?
    5655    pmFPAfileType type;                 // what type of data is read from / written to disk?
     
    6362
    6463    pmFPA *fpa;                         // for I/O files, we carry a pointer to the complete fpa
    65     psFits *fits;                       // for I/O files of fits type (IMAGE, CMP, CMF), we carry a file handle
     64    psFits *fits;                       // for I/O files of fits type (IMAGE, CMP, CMF) we carry a file handle
     65    psFitsCompression *compression;     // Compression for FITS images
     66    int bitpix;                         // Bits per pixel for output
    6667
    6768    bool wrote_phu;                     // have we written a PHU for this file?
     
    9394    psMetadata *format;                 // Camera format
    9495    psString formatName;                // name of the camera format
    95 }
    96 pmFPAfile;
     96} pmFPAfile;
    9797
    9898// allocate an empty pmFPAfile structure
  • trunk/psModules/src/camera/pmFPAfileDefine.c

    r15131 r15180  
    88#include <pslib.h>
    99
     10#include "pmErrorCodes.h"
    1011#include "pmConfig.h"
    1112#include "pmDetrendDB.h"
     
    1920#include "pmFPAConstruct.h"
    2021
     22
     23// Get the file rule of interest
     24// Look up the name of the set of file rules to use, get that set from the site configuration, and return the
     25// appropriate rule from the set.
     26static psMetadata *getFileRule(const pmConfig *config, // Configuration
     27                               const psMetadata *camera, // Camera configuration of interest
     28                               const char *name // Name of rule to read
     29    )
     30{
     31    assert(config);
     32    assert(config->site);
     33
     34    psMetadataItem *item = psMetadataLookup(camera, "FILERULES"); // Item with the file rule of interest
     35    if (!item) {
     36        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find FILERULES in the camera configuration.");
     37        return NULL;
     38    }
     39
     40    psMetadata *filerules = NULL;       // File rules from the site configuration
     41    switch (item->type) {
     42      case PS_DATA_METADATA:
     43        // It's what we're after
     44        filerules = item->data.md;
     45        break;
     46      case PS_DATA_STRING: {
     47          // It's the name of a file --- read the file, and store it for future use
     48          if (!pmConfigFileRead(&filerules, item->data.str, "filerules")) {
     49              psError(PM_ERR_CONFIG, false, "Trouble reading reading file rules from %s  --- "
     50                      "ignored.\n", item->data.str);
     51              psFree(filerules);
     52              return NULL;
     53          }
     54
     55          // Muck around under the hood to replace the filename with the metadata; don't try this at home,
     56          // kids
     57          item->type = PS_DATA_METADATA;
     58          psFree(item->data.str);
     59          item->data.md = filerules;
     60          break;
     61      }
     62      default:
     63        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
     64                "Unexpected type for %s (%x) in FILERULES in SITE configuration.",
     65                name, item->type);
     66        return NULL;
     67    }
     68
     69    // select the name from the FILERULES
     70    // check for alias name (type == STR, name is aliased name)
     71    bool mdok;                          // Status of MD lookup
     72    const char *realname = psMetadataLookupStr(&mdok, filerules, name); // Name of file rule to look up
     73    if (!realname || strlen(realname) == 0) {
     74        realname = name;
     75    }
     76
     77    return psMetadataLookupMetadata(NULL, filerules, realname);
     78}
     79
     80// Parse an option from a metadata, returning the appropriate integer value
     81static int parseOptionInt(const psMetadata *md, // Metadata containing the option
     82                          const char *name, // Option name
     83                          const char *source, // Description of source, for warning messages
     84                          int defaultValue // Default value
     85                          )
     86{
     87    psMetadataItem *item = psMetadataLookup(md, name); // Item with the value of interest
     88    if (!item) {
     89        psWarning("Unable to find value for %s in %s --- set to %d.", name, source, defaultValue);
     90        return defaultValue;
     91    }
     92    int value = psMetadataItemParseS32(item); // Value of interst
     93    return value;
     94}
     95
     96
    2197// define an input-type pmFPAfile, bind to the optional fpa if supplied
    2298pmFPAfile *pmFPAfileDefineInput(const pmConfig *config, pmFPA *fpa, const char *name)
     
    30106    char *type;
    31107
    32     // select the FILERULES from config->camera
    33     psMetadata *filerules = psMetadataLookupPtr (&status, config->camera, "FILERULES");
    34     if (filerules == NULL) {
    35         psError(PS_ERR_IO, true, "Can't find FILERULES in the CAMERA configuration!");
    36         return NULL;
    37     }
    38 
    39     // select the name from the FILERULES
    40     // check for alias name (type == STR, name is aliased name)
    41     const char *realname = psMetadataLookupStr (&status, filerules, name);
    42     if (!realname || strlen(realname) == 0) {
    43         realname = name;
    44     }
    45 
    46     psMetadata *data = psMetadataLookupPtr (&status, filerules, realname);
    47     if (data == NULL) {
     108    const psMetadata *camera = (fpa ? fpa->camera : config->camera); // Camera configuration for this file
     109    psMetadata *data = getFileRule(config, camera, name); // File rule
     110    if (!data) {
    48111        psError(PS_ERR_IO, true, "Can't find file rule %s!", name);
    49112        return NULL;
     
    76139    file->freeLevel = file->dataLevel;
    77140
    78     if (fpa != NULL) {
     141    if (fpa) {
    79142        file->fpa = psMemIncrRefCounter(fpa);
    80143        file->camera = psMemIncrRefCounter((psMetadata *)fpa->camera);
     144        file->cameraName = psMemIncrRefCounter(config->cameraName); // XXX Is this the correct thing to do?
     145    } else {
     146        file->camera = psMemIncrRefCounter(config->camera);
    81147        file->cameraName = psMemIncrRefCounter(config->cameraName);
    82148    }
     
    103169{
    104170    bool status;
    105 
    106     // select the FILERULES from the camera config
    107     psMetadata *filerules = psMetadataLookupPtr(&status, config->camera, "FILERULES");
    108     if (filerules == NULL) {
    109         psError(PS_ERR_IO, true, "Can't find FILERULES in the CAMERA configuration!");
    110         return NULL;
    111     }
    112 
    113     // select the name from the FILERULES
    114     // check for alias name (type == STR, name is aliased name)
    115     const char *realname = psMetadataLookupStr(&status, filerules, name);
    116     if (!realname || strlen(realname) == 0) {
    117         realname = name;
    118     }
    119 
    120     psMetadata *data = psMetadataLookupPtr (&status, filerules, realname);
    121     if (data == NULL) {
    122         psError(PS_ERR_IO, true, "Can't find file rule %s!", name);
    123         return NULL;
    124     }
    125 
    126     pmFPAfile *file = pmFPAfileAlloc();
    127 
    128     // save the name of this pmFPAfile
    129     file->name = psStringCopy(name);
    130 
    131     file->filerule = psMemIncrRefCounter(psMetadataLookupStr(&status, data, "FILENAME.RULE"));
    132 
    133     const char *type = psMetadataLookupStr(&status, data, "FILE.TYPE");
    134     file->type = pmFPAfileTypeFromString(type);
    135     if (file->type == PM_FPA_FILE_NONE) {
    136         psError(PS_ERR_IO, true, "FILE.TYPE is not defined for %s\n", name);
    137         psFree(file);
    138         return NULL;
    139     }
    140 
    141     file->mode = PM_FPA_MODE_WRITE;
    142     file->save = false;
    143171
    144172    // Use the camera we were told to, the camera of the provided FPA, or default to the default camera
     
    169197        }
    170198    }
     199
     200    psMetadata *data = getFileRule(config, camera, name); // File rule
     201    if (!data) {
     202        psError(PS_ERR_IO, true, "Can't find file rule %s!", name);
     203        return NULL;
     204    }
     205
     206    pmFPAfile *file = pmFPAfileAlloc();
     207
     208    // save the name of this pmFPAfile
     209    file->name = psStringCopy(name);
     210
     211    file->filerule = psMemIncrRefCounter(psMetadataLookupStr(&status, data, "FILENAME.RULE"));
     212
     213    const char *type = psMetadataLookupStr(&status, data, "FILE.TYPE");
     214    file->type = pmFPAfileTypeFromString(type);
     215    if (file->type == PM_FPA_FILE_NONE) {
     216        psError(PS_ERR_IO, true, "FILE.TYPE is not defined for %s\n", name);
     217        psFree(file);
     218        return NULL;
     219    }
     220
     221    file->mode = PM_FPA_MODE_WRITE;
     222    file->save = false;
     223
     224    // Use the camera we were told to, the camera of the provided FPA, or default to the default camera
     225    psMetadata *camera;                 // Camera configuration
     226    if (!cameraName || strlen(cameraName) == 0) {
     227        if (fpa && fpa->camera) {
     228            camera = (psMetadata*)fpa->camera; // Casting away const, so I can put it in the file
     229        } else {
     230            camera = config->camera;
     231            cameraName = config->cameraName;
     232        }
     233    } else {
     234        bool mdok;                      // Status of MD lookup
     235        psMetadata *cameras = psMetadataLookupMetadata(&mdok, config->site, "CAMERAS"); // Known cameras
     236        if (!mdok || !cameras) {
     237            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find CAMERAS in the site configuration.\n");
     238            return NULL;
     239        }
     240        camera = psMetadataLookupMetadata(&mdok, cameras, cameraName); // Camera configuration of interest
     241        if (!mdok || !camera) {
     242            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find automatically generated "
     243                    "camera configuration %s in site configuration.\n", cameraName);
     244            return NULL;
     245        }
     246
     247        if (fpa && fpa->camera && fpa->camera != camera) {
     248            psAbort("Camera of bound FPA is not the requested camera --- there is an inconsistency!");
     249        }
     250    }
    171251    file->camera = psMemIncrRefCounter(camera);
    172252    file->cameraName = psMemIncrRefCounter(cameraName);
     
    175255    # if (0)
    176256    if (cameraName) {
    177         if (!strcmp(cameraName + strlen(cameraName) - 5, "-CHIP")) {
    178             file->mosaicLevel = PM_FPA_LEVEL_CHIP;
    179         }
    180         if (!strcmp(cameraName + strlen(cameraName) - 5, "-FPA")) {
    181             file->mosaicLevel = PM_FPA_LEVEL_FPA;
    182         }
     257        if (!strcmp(cameraName + strlen(cameraName) - 5, "-CHIP")) {
     258            file->mosaicLevel = PM_FPA_LEVEL_CHIP;
     259        }
     260        if (!strcmp(cameraName + strlen(cameraName) - 5, "-FPA")) {
     261            file->mosaicLevel = PM_FPA_LEVEL_FPA;
     262        }
    183263    }
    184264    # endif
     
    209289        file->fpa = pmFPAConstruct(file->camera);
    210290    }
     291
     292    // Get FITS output scheme
     293    const char *fitsType = psMetadataLookupStr(&status, data, "FITS.TYPE"); // Name of FITS scheme to use
     294    if (fitsType && strcasecmp(fitsType, "NONE") != 0) {
     295        psMetadata *fitsTypes = psMetadataLookupMetadata(&status, camera, "FITS"); // The FITS schemes
     296        if (!fitsTypes) {
     297            psWarning("Unable to find FITS in camera configuration --- compression disabled.");
     298            goto COMPRESSION_DONE;
     299        }
     300        psMetadata *scheme = psMetadataLookupMetadata(NULL, fitsTypes, fitsType); // FITS scheme
     301        if (!scheme) {
     302            psWarning("Unable to find %s in FITS in camera configuration --- compression disabled.",
     303                      fitsType);
     304            goto COMPRESSION_DONE;
     305        }
     306        const char *typeString = psMetadataLookupStr(NULL, scheme, "COMPRESSION"); // Compression type
     307        if (!typeString || strlen(typeString) == 0) {
     308            psWarning("Can't find COMPRESSION in FITS scheme %s --- compression disabled.", fitsType);
     309            goto COMPRESSION_DONE;
     310        }
     311        psFitsCompressionType type = psFitsCompressionTypeFromString(typeString); // Compression type enum
     312
     313        psString source = NULL;         // Source of options
     314        psStringAppend(&source, "%s in FITS in camera %s", fitsType, cameraName);
     315        psVector *tile = psVectorAlloc(3, PS_TYPE_S32); // Tile sizes
     316        file->bitpix = parseOptionInt(scheme, "BITPIX", source, 0); // Bits per pixel
     317        tile->data.S32[0] = parseOptionInt(scheme, "TILE.X", source, 0); // Tiling in x
     318        tile->data.S32[1] = parseOptionInt(scheme, "TILE.Y", source, 1); // Tiling in y
     319        tile->data.S32[2] = parseOptionInt(scheme, "TILE.Z", source, 1); // Tiling in z
     320        int noise = parseOptionInt(scheme, "NOISE", source, 16); // Noise bits
     321        int hscale = parseOptionInt(scheme, "HSCALE", source, 0); // Scaling for HCOMPRESS
     322        int hsmooth = parseOptionInt(scheme, "HSMOOTH", source, 0); // Smoothing for HCOMPRESS
     323        psFree(source);
     324
     325        file->compression = psFitsCompressionAlloc(type, tile, noise, hscale, hsmooth);
     326        psFree(tile);
     327    }
     328COMPRESSION_DONE:
    211329
    212330    file->fileLevel = pmFPAPHULevel(format);
  • trunk/psModules/src/camera/pmFPAfileFitsIO.c

    r14204 r15180  
    471471    if (file->wrote_phu) return true;
    472472
    473     // select or generate the desired fpa in the correct output format 
     473    // select or generate the desired fpa in the correct output format
    474474    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config);
     475    pmHDU *phu = pmFPAviewThisHDU(view, fpa);
     476    if (!phu || !phu->blankPHU) {
     477        // No PHU to write!
     478        psFree(fpa);
     479        return true;
     480    }
    475481
    476482    switch (file->fileLevel) {
    477483      case PM_FPA_LEVEL_FPA:
    478         status = pmFPAWrite(fpa, file->fits, NULL, true, false);
    479         break;
     484        status = pmFPAWrite(fpa, file->fits, NULL, true, false);
     485        break;
    480486      case PM_FPA_LEVEL_CHIP: {
    481           pmChip *chip = pmFPAviewThisChip(view, fpa);
    482           status = pmChipWrite(chip, file->fits, NULL, true, false);
    483           break;
     487          pmChip *chip = pmFPAviewThisChip(view, fpa);
     488          status = pmChipWrite(chip, file->fits, NULL, true, false);
     489          break;
    484490      }
    485491      case PM_FPA_LEVEL_CELL: {
    486           pmCell *cell = pmFPAviewThisCell(view, fpa);
    487           status = pmCellWrite(cell, file->fits, NULL, true);
    488           break;
     492          pmCell *cell = pmFPAviewThisCell(view, fpa);
     493          status = pmCellWrite(cell, file->fits, NULL, true);
     494          break;
    489495      }
    490496      default:
    491         psAbort("fileLevel not correctly set");
    492         break;
     497        psAbort("fileLevel not correctly set");
     498        break;
    493499    }
    494500
  • trunk/psModules/src/camera/pmFPAfileIO.c

    r15140 r15180  
    359359            return false;
    360360        }
     361
     362        // do we need to write out a PHU?
     363        if (!pmFPAfileWritePHU(file, fileView, config)) {
     364            psError(PS_ERR_IO, false, "failed to write phu for %s (%s)", file->filename, file->name);
     365            return false;
     366        }
     367
    361368        psFree (fileView);
    362369    }
    363370
    364     // do we need to write out a PHU?
    365     if (level >= file->fileLevel) {
    366         if (!pmFPAfileWritePHU (file, view, config)) {
    367             psError(PS_ERR_IO, false, "failed to write phu for %s (%s)", file->filename, file->name);
     371    if (file->compression) {
     372        psTrace("psModules.camera", 7, "Setting compression for %s (%s)\n", file->filename, file->name);
     373        if (!psFitsCompressionApply(file->fits, file->compression)) {
     374            psError(PS_ERR_UNKNOWN, false, "Unable to set compression options for %s (%s) (%d:%d:%d)\n",
     375                    file->filename, file->name, view->chip, view->cell, view->readout);
    368376            return false;
    369377        }
     
    667675      case PM_FPA_FILE_CMF:
    668676      case PM_FPA_FILE_PSF:
    669         psTrace ("psModules.camera", 5, "opening %s (%s) (%d:%d:%d)\n", file->filename, file->name, view->chip, view->cell, view->readout);
     677        psTrace ("psModules.camera", 5, "opening %s (%s) (%d:%d:%d)\n",
     678                 file->filename, file->name, view->chip, view->cell, view->readout);
    670679        file->fits = psFitsOpen (file->filename, mode);
    671680        if (file->fits == NULL) {
     
    679688            psMetadata *fileMenu = psMetadataLookupMetadata (NULL, file->format, "FILE");
    680689            if (!fileMenu) {
    681                 psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n", file->formatName);
     690                psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n",
     691                         config->formatName);
    682692                return false;
    683693            }
     
    688698        }
    689699
    690         // In some cases, we need to read the PHU after we've opened the file.  This happens for
    691         // the images supplied by the detrend database, which are only identified here (pmConfigConvertFilename).
     700        if (file->compression) {
     701            psTrace("psModules.camera", 7, "Setting compression for %s (%s)\n", file->filename, file->name);
     702            if (!psFitsCompressionApply(file->fits, file->compression)) {
     703                psError(PS_ERR_UNKNOWN, false, "Unable to set compression options for %s (%s) (%d:%d:%d)\n",
     704                        file->filename, file->name, view->chip, view->cell, view->readout);
     705                return false;
     706            }
     707        }
     708
     709        // In some cases, we need to read the PHU after we've opened the file.  This happens for the images
     710        // supplied by the detrend database, which are only identified here (pmConfigConvertFilename).
    692711        if (!pmFPAfileReadPHU (file, view, config)) {
    693             psError (PS_ERR_IO, true, "error reading PHU for %s (%s) (%d:%d:%d)\n", file->filename, file->name, view->chip, view->cell, view->readout);
     712            psError (PS_ERR_IO, true, "error reading PHU for %s (%s) (%d:%d:%d)\n",
     713                     file->filename, file->name, view->chip, view->cell, view->readout);
    694714            return false;
    695715        }
     
    758778            return false;
    759779        }
    760         file->formatName = psStringCopy(config->formatName);
     780        file->formatName = psStringCopy(config->formatName);
    761781
    762782    } else {
     
    796816      case PM_FPA_FILE_WEIGHT:
    797817      case PM_FPA_FILE_FRINGE:
    798         status = pmFPAviewFitsWritePHU (view, file, config);
    799         break;
     818        status = pmFPAviewFitsWritePHU (view, file, config);
     819        break;
    800820      case PM_FPA_FILE_CMF:
    801         status = pmSource_CMF_WritePHU (view, file, config);
    802         break;
     821        status = pmSource_CMF_WritePHU (view, file, config);
     822        break;
    803823      case PM_FPA_FILE_PSF:
    804         status = pmPSFmodelWritePHU (view, file, config);
    805         break;
     824        status = pmPSFmodelWritePHU (view, file, config);
     825        break;
    806826      case PM_FPA_FILE_SX:
    807827      case PM_FPA_FILE_RAW:
Note: See TracChangeset for help on using the changeset viewer.