IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 25, 2008, 12:14:04 PM (18 years ago)
Author:
Paul Price
Message:

Adding I/O of subtraction kernels to pmFPAfile stuff.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/imcombine/pmSubtractionIO.c

    r18146 r19189  
    11#include <stdio.h>
    22#include <pslib.h>
     3#include <strings.h>
    34
    45#include "pmHDU.h"
    56#include "pmFPA.h"
     7#include "pmFPAview.h"
     8#include "pmFPAfile.h"
     9#include "pmFPAfileFitsIO.h"
     10#include "pmConcepts.h"
    611
    712#include "pmSubtraction.h"
     
    6267}
    6368
     69//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     70
    6471bool pmReadoutWriteSubtractionKernels(pmReadout *ro, psFits *fits)
    6572{
    6673    PM_ASSERT_READOUT_NON_NULL(ro, false);
    6774    PS_ASSERT_FITS_NON_NULL(fits, false);
    68 
    6975
    7076    // Extract the regions and solutions used in the image matching
     
    159165    // CVS tags, used to identify the version of this file (in case incompatibilities are introduced)
    160166    psString cvsFile = psStringCopy("$RCSfile: pmSubtractionIO.c,v $");
    161     psString cvsRev  = psStringCopy("$Revision: 1.1 $");
    162     psString cvsDate = psStringCopy("$Date: 2008-06-16 21:59:43 $");
     167    psString cvsRev  = psStringCopy("$Revision: 1.2 $");
     168    psString cvsDate = psStringCopy("$Date: 2008-08-25 22:14:04 $");
    163169    psStringSubstitute(&cvsFile, NULL, "RCSfile: ");
    164170    psStringSubstitute(&cvsRev,  NULL, "Revision: ");
     
    190196    return true;
    191197}
     198
     199static bool pmCellWriteSubtractionKernels(pmCell *cell, const pmFPAview *view,
     200                                          pmFPAfile *file, pmConfig *config)
     201{
     202    PS_ASSERT_PTR_NON_NULL(cell, false);
     203    PS_ASSERT_PTR_NON_NULL(cell->readouts, false);
     204    PS_ASSERT_PTR_NON_NULL(view, false);
     205
     206    pmFPAview *thisView = pmFPAviewAlloc(view->nRows); // Copy of input view
     207    *thisView = *view;
     208
     209    for (int i = 0; i < cell->readouts->n; i++) {
     210        pmReadout *readout = cell->readouts->data[i];
     211        thisView->readout = i;
     212        if (!pmReadoutWriteSubtractionKernels(readout, file->fits)) {
     213            psError(PS_ERR_IO, false, "Failed to write %dth readout", i);
     214            psFree(thisView);
     215            return false;
     216        }
     217    }
     218    psFree(thisView);
     219    return true;
     220}
     221
     222static bool pmChipWriteSubtractionKernels(pmChip *chip, const pmFPAview *view,
     223                                          pmFPAfile *file, pmConfig *config)
     224{
     225    PS_ASSERT_PTR_NON_NULL(chip, false);
     226    PS_ASSERT_PTR_NON_NULL(chip->cells, false);
     227    PS_ASSERT_PTR_NON_NULL(view, false);
     228
     229    pmFPAview *thisView = pmFPAviewAlloc(view->nRows); // Copy of input view
     230    *thisView = *view;
     231
     232    for (int i = 0; i < chip->cells->n; i++) {
     233        pmCell *cell = chip->cells->data[i];
     234        thisView->cell = i;
     235        if (!pmCellWriteSubtractionKernels(cell, thisView, file, config)) {
     236            psError(PS_ERR_IO, false, "Failed to write %dth cell", i);
     237            psFree(thisView);
     238            return false;
     239        }
     240    }
     241    psFree(thisView);
     242    return true;
     243}
     244
     245static bool pmFPAWriteSubtractionKernels(pmFPA *fpa, const pmFPAview *view,
     246                                         pmFPAfile *file, pmConfig *config)
     247{
     248    PS_ASSERT_PTR_NON_NULL(view, false);
     249    PS_ASSERT_PTR_NON_NULL(fpa, false);
     250    PS_ASSERT_PTR_NON_NULL(fpa->chips, false);
     251
     252    pmFPAview *thisView = pmFPAviewAlloc(view->nRows); // Copy of input view
     253    *thisView = *view;
     254
     255    for (int i = 0; i < fpa->chips->n; i++) {
     256        pmChip *chip = fpa->chips->data[i];
     257        thisView->chip = i;
     258        if (!pmChipWriteSubtractionKernels(chip, thisView, file, config)) {
     259            psError(PS_ERR_IO, false, "Failed to write %dth chip", i);
     260            psFree(thisView);
     261            return false;
     262        }
     263    }
     264    psFree(thisView);
     265    return true;
     266}
     267
     268//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    192269
    193270
     
    283360    return true;
    284361}
     362
     363static bool pmCellReadSubtractionKernels(pmCell *cell, const pmFPAview *view,
     364                                         pmFPAfile *file, pmConfig *config)
     365{
     366    PS_ASSERT_PTR_NON_NULL(view, false);
     367    PS_ASSERT_PTR_NON_NULL(cell, false);
     368    PS_ASSERT_PTR_NON_NULL(cell->readouts, false);
     369
     370    pmFPAview *thisView = pmFPAviewAlloc(view->nRows); // Copy of input view
     371    *thisView = *view;
     372
     373    // Create a readout if none exists
     374    if (!cell->readouts || cell->readouts->n == 0) {
     375        pmReadout *readout = pmReadoutAlloc(cell); // New readout
     376        psFree(readout);                // Drop reference
     377    }
     378
     379    cell->data_exists = false;
     380    for (int i = 0; i < cell->readouts->n; i++) {
     381        pmReadout *readout = cell->readouts->data[i];
     382        thisView->readout = i;
     383        pmReadoutReadSubtractionKernels(readout, file->fits);
     384        if (!readout->data_exists) {
     385            continue;
     386        }
     387
     388        // load in the concept information for this cell
     389        if (!pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
     390            psErrorClear();
     391            psWarning("Difficulty reading concepts for cell; attempting to proceed.");
     392        }
     393        cell->data_exists = true;
     394    }
     395    psFree(thisView);
     396
     397    return true;
     398}
     399
     400static bool pmChipReadSubtractionKernels(pmChip *chip, const pmFPAview *view,
     401                                         pmFPAfile *file, pmConfig *config)
     402{
     403    PS_ASSERT_PTR_NON_NULL(view, false);
     404    PS_ASSERT_PTR_NON_NULL(chip, false);
     405    PS_ASSERT_PTR_NON_NULL(chip->cells, false);
     406
     407    pmFPAview *thisView = pmFPAviewAlloc(view->nRows); // Copy of input view
     408    *thisView = *view;
     409
     410    chip->data_exists = false;
     411    for (int i = 0; i < chip->cells->n; i++) {
     412        pmCell *cell = chip->cells->data[i];
     413        thisView->cell = i;
     414        pmCellReadSubtractionKernels(cell, thisView, file, config);
     415        if (!cell->data_exists) {
     416            continue;
     417        }
     418        chip->data_exists = true;
     419    }
     420    psFree(thisView);
     421
     422    if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
     423        psError(PS_ERR_IO, false, "Failed to read concepts for chip.\n");
     424        return false;
     425    }
     426
     427    return true;
     428}
     429
     430static bool pmFPAReadSubtractionKernels(pmFPA *fpa, const pmFPAview *view,
     431                                        pmFPAfile *file, pmConfig *config)
     432{
     433    PS_ASSERT_PTR_NON_NULL(view, false);
     434    PS_ASSERT_PTR_NON_NULL(file, false);
     435    PS_ASSERT_PTR_NON_NULL(file->fpa, false);
     436    PS_ASSERT_PTR_NON_NULL(file->fpa->chips, false);
     437
     438    pmFPAview *thisView = pmFPAviewAlloc(view->nRows); // Copy of input view
     439    *thisView = *view;
     440
     441    for (int i = 0; i < fpa->chips->n; i++) {
     442        pmChip *chip = fpa->chips->data[i];
     443        thisView->chip = i;
     444        pmChipReadSubtractionKernels(chip, thisView, file, config);
     445    }
     446    psFree(thisView);
     447
     448    if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
     449        psError(PS_ERR_IO, false, "Failed to read concepts for fpa.\n");
     450        return false;
     451    }
     452
     453    return true;
     454}
     455
     456//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     457
     458bool pmSubtractionWriteKernels(const pmFPAview *view, pmFPAfile *file, pmConfig *config)
     459{
     460    PS_ASSERT_PTR_NON_NULL(view, false);
     461    PS_ASSERT_PTR_NON_NULL(file, false);
     462    PS_ASSERT_PTR_NON_NULL(file->fpa, false);
     463
     464    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
     465
     466    if (view->chip == -1) {
     467        if (!pmFPAWriteSubtractionKernels(fpa, view, file, config)) {
     468            psError(PS_ERR_IO, false, "Failed to write subtraction kernels from fpa");
     469            psFree(fpa);
     470            return false;
     471        }
     472        psFree(fpa);
     473        return true;
     474    }
     475
     476    if (view->chip >= fpa->chips->n) {
     477        psError(PS_ERR_UNKNOWN, false, "Writing chip == %d (>= chips->n == %ld)", view->chip, fpa->chips->n);
     478        psFree(fpa);
     479        return false;
     480    }
     481    pmChip *chip = fpa->chips->data[view->chip];
     482
     483    if (view->cell == -1) {
     484        if (!pmChipWriteSubtractionKernels(chip, view, file, config)) {
     485            psError(PS_ERR_IO, false, "Failed to write objects from chip");
     486            psFree(fpa);
     487            return false;
     488        }
     489        psFree(fpa);
     490        return true;
     491    }
     492
     493    if (view->cell >= chip->cells->n) {
     494        psError(PS_ERR_UNKNOWN, false, "Writing cell == %d (>= cells->n == %ld)",
     495                view->cell, chip->cells->n);
     496        psFree(fpa);
     497        return false;
     498    }
     499    pmCell *cell = chip->cells->data[view->cell];
     500
     501    if (view->readout == -1) {
     502        if (!pmCellWriteSubtractionKernels(cell, view, file, config)) {
     503            psError(PS_ERR_IO, false, "Failed to write objects from cell");
     504            psFree(fpa);
     505            return false;
     506        }
     507        psFree(fpa);
     508        return true;
     509    }
     510
     511    if (view->readout >= cell->readouts->n) {
     512        psError(PS_ERR_UNKNOWN, false, "Writing readout == %d (>= readouts->n == %ld)",
     513                view->readout, cell->readouts->n);
     514        psFree(fpa);
     515        return false;
     516    }
     517    pmReadout *readout = cell->readouts->data[view->readout];
     518
     519    if (!pmReadoutWriteSubtractionKernels(readout, file->fits)) {
     520        psError(PS_ERR_IO, false, "Failed to write objects from readout %d", view->readout);
     521        psFree(fpa);
     522        return false;
     523    }
     524
     525    psFree(fpa);
     526    return true;
     527}
     528
     529bool pmSubtractionWritePHU(const pmFPAview *view, pmFPAfile *file, pmConfig *config)
     530{
     531    PS_ASSERT_PTR_NON_NULL(view, false);
     532    PS_ASSERT_PTR_NON_NULL(file, false);
     533
     534    if (file->wrote_phu) {
     535        return true;
     536    }
     537    if (file->fileLevel != PM_FPA_LEVEL_FPA) {
     538        return true;
     539    }
     540    if (file->fpa->chips->n == 1) {
     541        return true;
     542    }
     543
     544    // find the FPA phu
     545    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
     546    pmHDU *phu = psMemIncrRefCounter(pmFPAviewThisPHU(view, fpa));
     547    psFree(fpa);
     548
     549    // if there is no PHU, this is a single header+image (extension-less) file. This could be the case for an
     550    // input SPLIT set of files being written out as a MEF.  if there is a PHU, write it out as a 'blank'
     551    psMetadata *outhead = psMetadataAlloc();
     552    if (phu) {
     553        psMetadataCopy (outhead, phu->header);
     554    }
     555    psFree(phu);
     556
     557    pmConfigConformHeader(outhead, file->format);
     558
     559    psFitsWriteBlank(file->fits, outhead, "");
     560    file->wrote_phu = true;
     561
     562    psTrace("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
     563    psFree(outhead);
     564
     565    return true;
     566}
     567
     568bool pmSubtractionReadKernels(const pmFPAview *view, pmFPAfile *file, pmConfig *config)
     569{
     570    PS_ASSERT_PTR_NON_NULL(view, false);
     571    PS_ASSERT_PTR_NON_NULL(file, false);
     572    PS_ASSERT_PTR_NON_NULL(file->fpa, false);
     573
     574    pmFPA *fpa = file->fpa;
     575
     576    if (view->chip == -1) {
     577        pmFPAReadSubtractionKernels(fpa, view, file, config);
     578        return true;
     579    }
     580
     581    if (view->chip >= fpa->chips->n) {
     582        return false;
     583    }
     584    pmChip *chip = fpa->chips->data[view->chip];
     585
     586    if (view->cell == -1) {
     587        pmChipReadSubtractionKernels(chip, view, file, config);
     588        return true;
     589    }
     590
     591    if (view->cell >= chip->cells->n) {
     592        return false;
     593    }
     594    pmCell *cell = chip->cells->data[view->cell];
     595
     596    if (view->readout == -1) {
     597        pmCellReadSubtractionKernels(cell, view, file, config);
     598        return true;
     599    }
     600
     601    if (view->readout >= cell->readouts->n) {
     602        return false;
     603    }
     604    pmReadout *readout = cell->readouts->data[view->readout];
     605
     606    return pmReadoutReadSubtractionKernels(readout, file->fits);
     607}
Note: See TracChangeset for help on using the changeset viewer.