SourceForge has been redesigned. Learn more.
Close

#29 Improve import of data files in xdr format

closed
nobody
None
5
2009-07-03
2009-05-29
Craig Finch
No

I'm using xdr files to transfer data from Python to Asymptote for plotting. It seems that Asymptote is not capable of reading integers from XDR files. It would be really good to save the array length in the XDR file along with the data. For example, the xdrlib module in Python has a method called pack_array which record the length of the array as an unsigned integer, and then stores the data elements. Asymptote doesn't recognize the leading integer and just tries to read in the whole array, leading to garbled data. As a workaround, I store the array length as a double, read it into Asymptote as a double, and then typecast to int. I also tried storing the length and reading the length as an int, but Asymptote can't read that either. Integer support would be good, and automatically handling fixed-length arrays would be even better.

Also, the documentation for Asymptote's xdr interface could really be improved. The docs don't tell you that Asymptote only implements a subset of XDR--you have to figure out which features of XDR are actually implemented!

Discussion

  • John Bowman

    John Bowman - 2009-07-02

    Asymptote doesn't know about unsigned integers (there's normally no need to distract Asymptote users with this distinction: the int type is really a long long integer type, so we can afford wasting an extra sign bit).

    Asymptote already supports reading and writing (signed) int and (signed) long long int to xdr. For example:
    file f=single(xoutput("test.dat"),0,false);
    write(f,intMax);
    close(f);

    file g=single(xinput("test.dat"),0,false);
    int i=g;
    write(i);

    So all you need to do is use a signed integer rather than an unsigned integer. Definitely don't convert it to a double!
    You probably should use the predefined functions read1, read2, and read3 for reading your arrays.

    If you don't wish to change the code that is writing your data, we could add an unsigned=true option to single(file,int,bool b) to help you.
    What other xdr features are missing?

     
  • John Bowman

    John Bowman - 2009-07-03
    • status: open --> closed
     
  • John Bowman

    John Bowman - 2009-07-03

    I just committed the following change to svn (along with improving the documentation of xdr and binary files).

    Improve interace to routines for setting single precision mode: remove the
    unused x argument, distinguishing the integer case with a new bool signedint
    argument before the file.

     
  • John Bowman

    John Bowman - 2009-07-13

    For the record, here's the example in my original reply updated to Asymptote 1.80:

    file f=single(xoutput("test.dat"),int=false);
    write(f,intMax);
    close(f);

    file g=single(xinput("test.dat"),int=false);
    int i=g;
    write(i);

    And here's another example, showing how read1 can be used to read a
    (signed) int[] array:

    int n=10;
    file f=signed(xoutput("test.dat"),false);
    write(f,n);
    f=signed(f);
    write(f,-sequence(n));
    close(f);

    file g=signed(xinput("test.dat"));
    int[] i=read1(g);
    write(i);

     
  • Nobody/Anonymous

    I just want to say "thank you" for working on this. I haven't had time to go back and rewrite my code to use the new features, but I'm looking forward to it!

     

Log in to post a comment.