Next: About this document Up: Programmers Previous: Hello World

snaptrans

From $NEMO/src/tutor/nbody/snaptrans.c we quote a NEMO versino of a program which reads in a (time selected) series of snapshots, and allows you to operate on the basic snapshot variables. Most of the programs in $NEMO/src/nbody/trans can be derived from this template.


/*
 *  SNAPTRANS:  template to operate on snapshot files
 *
 *  With the $NEMOBIN/cc script it should compile as follows:
 *             cc -o snaptrans snaptrans.c -lnemo -lm
 *
 *      26-jan-94       V1.0    Created                 Peter Teuben
 */

#include <stdinc.h>
#include <getparam.h>
#include <vectmath.h>
#include <filestruct.h>

#include <snapshot/snapshot.h>
#include <snapshot/body.h>
#include <snapshot/get_snap.c>
#include <snapshot/put_snap.c>

string defv[] = {   /* Nemo_Keys: "key=val\n    help",      */
    "in=???\n                   input (snapshot) file",
    "out=???\n                  output (snapshot) file",
    "times=all\n                times to process",
    "VERSION=1.0\n              26-jan-94 PJT",
    NULL,
};

string usage = "template for snapshot operations";

nemo_main()         /* nemo_main:   this replaces main(argc,argv) */
{
    stream instr, outstr;
    string times = getparam("times");
    real   tsnap;
    Body  *btab = NULL, *bp;
    int    i, nbody, bits;
    bool   first = TRUE;

    instr = stropen(getparam("in"), "r");
    outstr = stropen(getparam("out"), "w");

    for (;;) {    /* infinite loop, broken only when ran out of snapshots */
        get_history(instr);                    /* read history */
        if (!get_tag_ok(instr, SnapShotTag)) break; /* check if done */
        get_snap(instr, &btab, &nbody, &tsnap, &bits);  /* get next */
        
        /* Operate on the snapshot here : */
        for (bp=btab, i=0; i<nbody; bp++, i++) {
            /* all the work goes here */
        }
        if (first) {
            put_history(outstr);
            first = FALSE;
        }
        put_snap(outstr, &btab, &nbody, &tsnap, &bits);     /* output */
    }
    strclose(instr);
    if (first) {
        warning("No snapshots processed");
        strdelete(outstr);
    } else
        strclose(outstr);
}


teuben@
Sat Oct 8 19:06:02 EDT 1994