Skip to content

Commit 98a2009

Browse files
committed
added a method for writing an XDI file
1 parent 2123091 commit 98a2009

File tree

5 files changed

+126
-7
lines changed

5 files changed

+126
-7
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ files containing single-scan XAS data.
1111

1212

1313
**This is a work in progress. Version 1.0 of the XDI specification has
14-
not yet been released.**
14+
not yet been declared done.**
1515

1616

1717
Specification Documents
@@ -27,21 +27,23 @@ Implementations
2727
---------------
2828

2929
Implementations are found in directories named according to the
30-
programming language. At this time, we provide
30+
programming language. The main C library is in `lib/`, all others are
31+
under `languages`.
3132

32-
* C (written by Matt Newville with input from Bruce Ravel)
33+
At this time, we provide
34+
35+
* C (written by Matt Newville and Bruce Ravel)
3336
* Python (written by Matt Newville)
3437
* Perl (written by Bruce Ravel)
3538
* Fortran 77 (written by Matt Newville)
3639

3740
We encourage the contribution of new language implementations,
38-
especially:
41+
such as:
3942

4043
* Matlab
4144
* IDL
4245
* LabView
43-
* C++
44-
* Fortran 95
46+
* Fortran 95 or later
4547
* Java
4648

4749
and any other language that gets used by XAS practitioners.
@@ -52,6 +54,24 @@ community. Unit testing is strongly encouraged. So is complete
5254
documentation of the building, installation, and use of the language
5355
package.
5456

57+
Build
58+
-----
59+
60+
cd into the `lib/` directory and do:
61+
62+
~> ./configure
63+
~> make
64+
~> sudo make install
65+
66+
This will install static and dynamic libraries to `/usr/local/lib` and
67+
header files to `/usr/local/include`.
68+
69+
Obviously, you will need a C compiler.
70+
71+
To build the language specific interfaces, cd to the folder under
72+
`languages/` and follow the build instructions there.
73+
74+
5575
Other files
5676
-----------
5777

@@ -69,4 +89,4 @@ Other files
6989
data/co_metal_rt.xdi: XAS Data Interchange file -- XDI specification 1.0
7090

7191
It also defines an icon and a registry entry for XDI files on
72-
Windows.
92+
Windows. (The Windows reg entry does not currently work)

lib/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ condition of the most recently performed action. The relation between
120120
the returned error/warning codes and the error messages are tabulated
121121
below.
122122

123+
To export data as a valid XDI file, use `XDI_writefile`.
123124

124125

125126
### Read an XDI file
@@ -255,6 +256,14 @@ The return value is 0 if an array with that index is in the data
255256
table. That is, the index argument must be smaller than the `narrays`
256257
attribute. The return value is -1 if the array cannot be retrieved.
257258

259+
### Write an XDI file
260+
261+
Write an XDI file from the content of the XDIFile struct.
262+
263+
```C
264+
XDI_writefile(xdifile, "outfile.xdi");
265+
```
266+
258267
### Destroy and deallocate the XDIFile struct
259268
260269
To deallocate the memory from the XDIFile struct, do this:

lib/writetest.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <errno.h>
5+
#include "strutil.h"
6+
#include "xdifile.h"
7+
8+
9+
void show_syntax(void) {
10+
/* show command line syntax */
11+
printf("\nSyntax: writetest infile outfile\n");
12+
}
13+
14+
int main(int argc, char **argv) {
15+
XDIFile *xdifile;
16+
long ret, i;
17+
18+
/* require 2 arguments! */
19+
if (argc < 3) {
20+
show_syntax();
21+
return 1;
22+
}
23+
24+
25+
/* read xdifile */
26+
xdifile = malloc(sizeof(XDIFile));
27+
ret = XDI_readfile(argv[1], xdifile);
28+
XDI_writefile(xdifile, argv[2]);
29+
}

lib/xdifile.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,66 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
570570

571571
}
572572

573+
_EXPORT(void)
574+
XDI_writefile(XDIFile *xdifile, char *filename) {
575+
int i, j, count;
576+
577+
char quote[1025];
578+
const char s[2] = "\n";
579+
char *token;
580+
581+
int regex_status;
582+
struct slre_cap caps[3];
583+
584+
FILE *fp;
585+
fp = fopen(filename, "w");
586+
587+
/* version line */
588+
strcpy(quote, xdifile->comments);
589+
fprintf(fp, "# XDI/%s %s\n", xdifile->xdi_version, xdifile->extra_version);
590+
591+
/* metadata section */
592+
for (i=0; i < xdifile->nmetadata; i++) {
593+
fprintf(fp, "# %s.%s: %s\n",
594+
xdifile->meta_families[i],
595+
xdifile->meta_keywords[i],
596+
xdifile->meta_values[i]);
597+
}
598+
599+
/* user comments */
600+
fprintf(fp, "#////////////////////////\n");
601+
count = 0;
602+
token = strtok(quote, s); /* get the first token */
603+
while( token != NULL ) { /* walk through other tokens, skipping empty */
604+
if (count == 0) { /* take care with empty first token */
605+
regex_status = slre_match("^\\s*$", token, strlen(token), caps, 2, 0);
606+
if (regex_status < 0) {
607+
fprintf(fp, "#%s\n", token );
608+
}
609+
} else {
610+
fprintf(fp, "#%s\n", token );
611+
}
612+
++count;
613+
token = strtok(NULL, s);
614+
}
615+
fprintf(fp, "#------------------------\n");
616+
617+
/* column labels */
618+
fprintf(fp, "# ");
619+
for (i = 0; i < xdifile->narrays; i++) {
620+
fprintf(fp, " %s ", xdifile->array_labels[i]);
621+
}
622+
fprintf(fp, "\n");
623+
624+
/* data table */
625+
for (i = 0; i < xdifile->npts; i++ ) {
626+
for (j = 0; j < xdifile->narrays; j++ ) {
627+
fprintf(fp, " %-12.8g", xdifile->array[j][i]);
628+
}
629+
fprintf(fp, "\n");
630+
}
631+
632+
}
573633

574634
/* ============================================================================ */
575635
/* array management section */

lib/xdifile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct {
5959
} XDIFile;
6060

6161
_EXPORT(int) XDI_readfile(char *filename, XDIFile *xdifile) ;
62+
_EXPORT(void) XDI_writefile(XDIFile *xdifile, char *filename) ;
6263
_EXPORT(int) XDI_get_array_index(XDIFile *xdifile, long n, double *out);
6364
_EXPORT(int) XDI_get_array_name(XDIFile *xdifile, char *name, double *out);
6465
_EXPORT(int) XDI_required_metadata(XDIFile *xdifile);

0 commit comments

Comments
 (0)