IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 32232


Ignore:
Timestamp:
Aug 30, 2011, 5:09:24 PM (15 years ago)
Author:
bills
Message:

use psFitsReadTableNew and psFitsWriteTableNew to reduce memory usage when censoring
sources by about 100 fold

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/magic/remove/src/streaksremove.c

    r31716 r32232  
    99
    1010#include "streaksremove.h"
     11
     12#include "psFitsTableNew.h"
    1113
    1214static pmConfig *parseArguments(int argc, char **argv);
     
    302304    psLibFinalize();
    303305
    304     fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "streaksremove");
     306    int leaks = psMemCheckLeaks (0, NULL, stdout, false);
     307
     308    fprintf(stderr, "Found %d leaks at %s\n", leaks, "streaksremove");
    305309
    306310    return 0;
     
    917921    sFile *out = sfiles->outSources;
    918922
    919 
    920923    // Primary header, should be "something.hdr"
    921924    {
     
    960963        }
    961964
    962         psArray *inTable = psFitsReadTable(in->fits);
    963         if (!inTable) {
     965#ifdef CHECK_MEM_USE
     966        bool printStats = false;
     967        size_t allocated = 0;
     968        size_t persistent = 0;
     969        size_t allocatedBefore = psMemStats(printStats, &allocated, &persistent);
     970        printf("Before: %12lld %12lld %12lld\n", (long long) allocated, (long long) persistent, (long long) allocatedBefore);
     971#endif
     972
     973        psFitsTable *table = psFitsReadTableNew(in->fits);
     974
     975#ifdef CHECK_MEM_USE
     976        size_t allocatedAfter = psMemStats(printStats, &allocated, &persistent);
     977        printf("After:  %12lld %12lld %12lld\n", (long long) allocated, (long long) persistent, (long long) allocatedAfter);
     978        printf("Change: %12lld\n", (long long) allocatedAfter - allocatedBefore);
     979#endif
     980
     981        if (!table) {
    964982            psErrorStackPrint(stderr, "failed to read table in %s", in->resolved_name);
    965983            streaksExit("", PS_EXIT_DATA_ERROR);
    966984        }
    967985
    968         psArray *outTable = psArrayAllocEmpty(inTable->n);
    969         int j = 0;
     986        // XXX: Handle case where numCols == 0 Can that happen?
     987        // Previously we were writing a blank extension
     988        int xCol = psFitsTableGetColumnIndex(table, "X_PSF");
     989        assert(xCol >= 0 && xCol < table->numCols);
     990
     991        int yCol = psFitsTableGetColumnIndex(table, "Y_PSF");
     992        assert(yCol >= 0 && yCol < table->numCols);
     993
     994        bool *rowCensored = psAlloc(table->numRows * sizeof(bool));
    970995        int numCensored = 0;
    971         for (int i = 0 ; i < inTable->n; i++) {
    972             psMetadata *row = inTable->data[i];
    973 
    974             psF32 x = psMetadataLookupF32(NULL, row, "X_PSF");
    975             psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF");
     996
     997        for (int i = 0 ; i < table->numRows; i++) {
     998            psF32 x = psFitsTableGetCellF32(table, i, xCol);
     999            psF32 y = psFitsTableGetCellF32(table, i, yCol);
    9761000
    9771001            psImageMaskType mask;
     
    9841008            // Key the source if the center pixel is not masked with maskStreak
    9851009            if (!(mask & maskStreak) ) {
    986                 psArraySet(outTable, j++, row);
     1010                // psArraySet(outTable, j++, row);
     1011                rowCensored[i] = false;
    9871012            } else {
     1013                rowCensored[i] = true;
    9881014                numCensored++;
    9891015            }
    9901016        }
    9911017
    992         // get rid of unused elements (don't know if this is necessary)
    993         psArrayRealloc(outTable, j);
     1018        if (!psFitsTableCensor(table, rowCensored)) {
     1019            streaksExit("", PS_EXIT_PROG_ERROR);
     1020        }
    9941021
    9951022        addDestreakKeyword(header, true);
    996         if (psArrayLength(outTable) > 0) {
     1023
     1024        if (table->numCols > 0) {
    9971025            printf("Censored %d sources\n", numCensored);
    998             if (! psFitsWriteTable(out->fits, header, outTable, extname)) {
     1026            if (! psFitsWriteTableNew(out->fits, header, table, extname)) {
    9991027                psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
    10001028                streaksExit("", PS_EXIT_DATA_ERROR);
    10011029            }
    1002         } else if (psArrayLength(inTable) == 0) {
     1030        } else {
    10031031            printf("No input sources\n");
    10041032            // We'd like to write a blank table, but this is as good as it gets without more work to parse the
     
    10081036                streaksExit("", PS_EXIT_DATA_ERROR);
    10091037            }
    1010         } else {
    1011             printf("Censored ALL %d sources\n", numCensored);
    1012             if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) {
    1013                 psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name);
    1014                 streaksExit("", PS_EXIT_DATA_ERROR);
    1015             }
    10161038        }
    10171039        psFree(header);
    1018         psFree(outTable);
    1019         psFree(inTable);
     1040        psFree(table);
     1041        psFree(rowCensored);
    10201042    }
    10211043
     
    10491071    }
    10501072
    1051 
    10521073    if (!psFitsClose(out->fits)) {
    10531074        psErrorStackPrint(stderr, "failed to close table %s", out->resolved_name);
    10541075        streaksExit("", PS_EXIT_DATA_ERROR);
    10551076    }
    1056 }
     1077    out->fits = NULL;
     1078}
Note: See TracChangeset for help on using the changeset viewer.