swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] Subject: alternaties to inFile and outFile for ascii


From: Marcus G. Daniels
Subject: Re: [Swarm-Support] Subject: alternaties to inFile and outFile for ascii text?
Date: Mon, 09 May 2005 08:54:34 -0600
User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)

Paul Box wrote:

What are the alternatives these days for someone trying to reading and writing ascii files in swarm?
These instructions are for Windows, but it works similarly on other systems.

1. Install R for Windows
   http://cran.cnr.berkeley.edu/bin/windows/base/rw2010.exe

2. Run R and go to the Packages menu and then Install Packages(s). Choose a mirror near you and then select hdf5 from the menu that follows.

3. Save the attached input.R and input.txt in the same folder somewhere

4. Change R's directory to that folder with File/Change dir.

5. Use File/Source R code to load input.R.

6. Compile the attached input.m (this assumes a Swarm installation on Windows) as per the comments at the end. 7. Running ./input from the command line will print out the contents of the file by loading the HDF5 file that R created.

The idea here is to use R to load the data. R has good capabilities for parsing text files and manipulating and summarizing their contents. The input.R code doesn't do any preprocessing in this case, it simply loads the data, declares its type for Swarm (the calls to "attr") and then saves it in a form Swarm can load.

In turn, the Swarm input.m file then doesn't even need to declare classes for the imported data. The classes will be generated on the fly with instance variable names that track the column names in the original text file. (This is why to print out the values I used probes to get the values.)

library(hdf5)

data <- read.table("input.txt",header=TRUE)

attr(data,"type") <- "List"
attr(data,"component-type") <- "MyClass"

hdf5save("test.hdf", "data")

Attachment: input.txt
Description: Zip archive

#import <simtools.h> // initSwarm
#import <objectbase.h> // VarProbe
#import <defobj.h> // Archiver

int
main (int argc, const char **argv)
{
  id <Archiver> archiver;
  id <VarProbe> vp1, vp2;
  id list;

  initSwarmBatch (argc, argv);

  archiver = [[[HDF5Archiver createBegin: globalZone]
                 setPath: "test.hdf"]
               createEnd];

  list = [archiver getObject: "data"];

  {
    Class class = [[list getFirst] getClass];

    vp1 = [[[[VarProbe createBegin: globalZone]
              setProbedClass: class]
             setProbedVariable: "val1"]
            createEnd];

    vp2 = [[[[VarProbe createBegin: globalZone]
              setProbedClass: class]
             setProbedVariable: "val2"]
            createEnd];
  }
  {
    id <Index> index = [list begin: scratchZone];
    id obj;
    
    for (obj = [index next]; [index getLoc] == Member; obj = [index next])
      printf ("%d %d\n", [vp1 probeAsInt: obj], [vp2 probeAsInt: obj]);
    
    [index drop];
  }
  [vp1 drop];
  [vp2 drop];
  [list drop];
  [archiver drop];
}

/*
Local Variables:
compile-command: "/usr/bin/libtool-swarm --mode=link gcc -I/usr/include/swarm 
-o input -g -Wno-import input.m -L/usr/lib/swarm -lswarm"
End:
*/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]