Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/pure-cfg/src/lib/common/output.c
ViewVC logotype

View of /branches/pure-cfg/src/lib/common/output.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1479 - (download) (as text) (annotate)
Fri Sep 2 14:17:54 2011 UTC (9 years, 3 months ago) by nseltzer
File size: 5150 byte(s)
Moved output proceedure to seperate file and updated c-target to use new output.
#include <stdio.h>
#include "Diderot/diderot.h"
#include <teem/nrrd.h>
#include <teem/air.h>

#include "Diderot/output.h"


void Diderot_Output(Output_Args* args)
{
    airArray *mop = airMopNew();
    if (args->NrrdOutputFlg) {
        int status;
        char *err;

      // get correct output file name
        int outFileNameLen = strlen(args->name) + 6;
        char *outFileName = (char *)CheckedAlloc(outFileNameLen);
        airMopAdd(mop, outFileName, airFree, airMopAlways);
        snprintf (outFileName, outFileNameLen, "%s.nrrd", args->name);

      // allocate the correct amount of space for output
        int numStabilized = 0;
        for (int i = 0;  i < args->numStrands;  i++) {
            if (args->status[i] == DIDEROT_STABLE)
                numStabilized++;
        }
        void *outData = CheckedAlloc(numStabilized * Diderot_Strands[0]->outputSzb);
        airMopAdd(mop, outData, airFree, airMopAlways);

      // copy data into outData
        void *outPos = outData;
        for (int i = 0;  i < args->numStrands;  i++) {
            if (args->status[i] == DIDEROT_STABLE) {
                Diderot_Strands[0]->output (outPos, args->inState[i]);
                outPos += Diderot_Strands[0]->outputSzb;
            }
        }

      // make a new nrrd
        Nrrd *outNrrd;
        outNrrd = nrrdNew();
        if (outNrrd == NULL) {
            fprintf(stderr, "Trouble allocating nrrd struct:\n%s", err);
            airMopError(mop);
            exit(1);
        }
        airMopAdd(mop, outNrrd, (airMopper)nrrdNix, airMopAlways);

      // wrap the data (outData) with the nrrd (outNrrd)
        if (args->isArray) {
            size_t *sizes = NEWVEC(size_t, args->nDims + 1);
            airMopAdd(mop, sizes, airFree, airMopAlways);
            switch(Diderot_Strands[0]->nrrdType) {
                case 5:         //nrrdTypeInt
                case 9:         //nrrdTypeFloat
                    *(sizes) = Diderot_Strands[0]->outputSzb / 4;
                    break;
                case 7:         //nrrdTypeLLong
                case 10:        //nrrdTypeDouble
                    *(sizes) = Diderot_Strands[0]->outputSzb / 8;
                    break;
                default:
                    fprintf(stderr, "Bad nrrdType\n");
                    airMopError(mop);
                    exit(1);
                    break;
            }
            for (int i = 1; i <= args->nDims; i++) {
                *(sizes + i) = *(args->size + args->nDims - i);
            }
            status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, args->nDims + 1, sizes);
            if (status) {
                err = biffGetDone(NRRD);
                fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
                airMopError(mop);
                free(err);
                exit(1);
            }
        }
        else {
            size_t *sizes = NEWVEC(size_t, 2);
            airMopAdd(mop, sizes, airFree, airMopAlways);
            switch(Diderot_Strands[0]->nrrdType) {
                case 5:         //nrrdTypeInt
                case 9:         //nrrdTypeFloat
                    *(sizes) = Diderot_Strands[0]->outputSzb / 4;
                    break;
                case 7:         //nrrdTypeLLong
                case 10:        //nrrdTypeDouble
                    *(sizes) = Diderot_Strands[0]->outputSzb / 8;
                    break;
                default:
                    fprintf(stderr, "Bad nrrdType\n");
                    airMopError(mop);
                    exit(1);
                    break;
            }
            *(sizes + 1) = numStabilized;
            status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, args->nDims, sizes);
            if (status != 0) {
                err = biffGetDone(NRRD);
                fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
                airMopError(mop);
                free(err);
                exit(1);
            }
        }

        // save the data
        status = nrrdSave(outFileName, outNrrd, NULL);
        if(status) {
            err = biffGetDone(NRRD);
            fprintf(stderr, "Trouble saving nrrd struct:\n%s", err);
            airMopError(mop);
            free(err);
            exit(1);
        }
    }
    else {
      // here we have the final state of all of the strands in the "in" buffer
        int outFileNameLen = strlen(args->name) + 5;
        char *outFileName = (char *)CheckedAlloc(outFileNameLen);
        airMopAdd(mop, outFileName, airFree, airMopAlways);
        snprintf (outFileName, outFileNameLen, "%s.txt", args->name);
        FILE *outS = fopen(outFileName, "w");
        if (outS == NULL) {
            fprintf (stderr, "Cannot open output file %s\n", outFileName);
            airMopError(mop);
            exit (8);
        }
        airMopAdd(mop, outS, (airMopper)airFclose, airMopAlways);

        for (int i = 0;  i < args->numStrands;  i++) {
            if (args->status[i] == DIDEROT_STABLE)
                Diderot_Strands[0]->print (outS, args->inState[i]);
        }
    }
    airMopOkay(mop);
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0