1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/**
* TestJapi does some testing of the NeXus for Java API.
* It can also serve as an example for the usage of the NeXus API for Java.
* Mark Koennecke, October 2000 updated for NAPI-2 with HDF-5 support Mark Koennecke, August 2001
*/
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.lang.reflect.Array;
import org.nexusformat.*;
public class TestJapi {
void fileTest(int fileType, String fileName) throws Exception {
NexusFile nf = null;
NXlink gid, did;
String group = "entry1";
String nxclass = "NXentry";
Set<String> excludeattr = new HashSet<String>(Arrays.asList("HDF5_Version", "file_time", "NeXus_version", "HDF_version"));
// create a NexusFile
nf = new NexusFile(fileName, fileType);
try {
// error handling check
try {
nf.opengroup(group, nxclass);
throw new RuntimeException("Exception handling broken");
} catch (NexusException nex) {
System.out.println("Exception handling mechanism works");
}
int iData1[][] = new int[3][10];
int iData2[][] = new int[3][10];
float fData1[][] = new float[3][10];
int islab[] = new int[10];
int iDim[] = new int[2], i, j;
int iStart[] = new int[2];
int signal[] = new int[1];
int iEnd[] = new int[2];
String attname, attvalue, vname, vclass;
AttributeEntry atten;
// create some data
for (i = 0; i < 3; i++) {
for (j = 0; j < 10; j++) {
iData1[i][j] = i * 10 + j;
fData1[i][j] = (float) (i * 10.1 + j * .2);
}
}
for (i = 0; i < 10; i++) {
islab[i] = 10000 + i;
}
// add attributes, the first one is also an example how to write
attvalue = "@012345abcdef";
nf.putattr("standardstringattribute", attvalue.getBytes(), NexusFile.NX_CHAR);
signal[0] = -177;
nf.putattr("singlenumber", signal, NexusFile.NX_INT32);
nf.putattr("intarray", new int[] { 1, 2, 3, 4}, new int[] {2, 2}, NexusFile.NX_INT32);
nf.putattr("floatarray", fData1, new int[] {3, 10}, NexusFile.NX_FLOAT32);
// closedata
// create and open a group
nf.makegroup(group, nxclass);
nf.opengroup(group, nxclass);
// get a link ID for this group
gid = nf.getgroupID();
// create and open a dataset
iDim[0] = 3;
iDim[1] = 10;
nf.makedata("iData1", NexusFile.NX_INT32, 2, iDim);
nf.opendata("iData1");
// get a link ID to this data set
did = nf.getdataID();
// write data to it
nf.putdata(iData1);
// add attributes, the first one is also an example how to write
// strings (by converting to byte arrays)
String units = "MegaFarts";
nf.putattr("Units", units.getBytes(), NexusFile.NX_CHAR);
iStart[0] = 1;
signal[0] = 1;
nf.putattr("signal", signal, NexusFile.NX_INT32);
// closedata
nf.closedata();
// try unlimimited dim
int unDim[] = new int[1];
unDim[0] = -1;
nf.makedata("Stuart", NexusFile.NX_FLOAT64, 1, unDim);
// write a compressed data set
nf.compmakedata("iData1_compressed", NexusFile.NX_INT32, 2, iDim, NexusFile.NX_COMP_LZW, iDim);
nf.opendata("iData1_compressed");
nf.putdata(iData1);
nf.closedata();
// write a float data set
nf.makedata("fData1", NexusFile.NX_FLOAT32, 2, iDim);
nf.opendata("fData1");
nf.putdata(fData1);
nf.closedata();
// write a dataset in slabs */
nf.makedata("slabbed", NexusFile.NX_INT32, 2, iDim);
nf.opendata("slabbed");
iStart[1] = 0;
iEnd[1] = 10;
iEnd[0] = 1;
for (i = 0; i < 3; i++) {
iStart[0] = i;
nf.putslab(islab, iStart, iEnd);
}
nf.closedata();
// closegroup
nf.closegroup();
// test linking code
nf.makegroup("entry2", "NXentry");
nf.opengroup("entry2", "NXentry");
nf.makegroup("data", "NXdata");
nf.opengroup("data", "NXdata");
nf.makelink(did);
// nf.debugstop();
nf.closegroup();
// close file explicitly (important!)
nf.close();
System.out.println(" *** Writing Tests passed with flying banners");
// **************** reading tests *******************************
iData2[2][5] = 66666;
fData1[2][5] = 66666.66f;
nf = new NexusFile(fileName, NexusFile.NXACC_READ);
// test attribute enquiry routine at global attributes
Hashtable h = nf.attrdir();
Enumeration e = h.keys();
byte bData[];
while (e.hasMoreElements()) {
attname = (String) e.nextElement();
atten = (AttributeEntry) h.get(attname);
if (!excludeattr.contains(attname)) {
StringBuilder sb = new StringBuilder();
for(i = 0; i < atten.dim.length; i++) {
sb.append(" "); sb.append(atten.dim[i]);
}
System.out.println("Found global attribute: " + attname + " type: " + atten.type
+ ", dimensions:" + sb.toString() + ", length: " + atten.length);
try {
Object attr = nf.getattr(attname);
sb = new StringBuilder(Array.get(attr, 0).toString());
for(i = 1 ; i < Array.getLength(attr) ; i++) {
sb.append(", ");
sb.append(Array.get(attr, i).toString());
}
System.out.println(String.format("%s = %s", attname, sb.toString()));
} catch (NexusException ne) {
System.out.println(String.format("ERROR reading attribute %s (%s)", attname, ne.getMessage()));
}
} else {
System.out.println("Found global attribute: " + attname + " type: " + atten.type);
System.out.println(attname + "= XXXX (volatile information withheld to aid automatic testing)");
}
}
// test reading vGroup directory
// nf.debugstop();
nf.opengroup(group, nxclass);
h = nf.groupdir();
e = h.keys();
System.out.println("Found in vGroup entry:");
while (e.hasMoreElements()) {
vname = (String) e.nextElement();
vclass = (String) h.get(vname);
System.out.println(" Item: " + vname + " class: " + vclass);
}
// test reading SDS info and attributes
nf.opendata("iData1");
nf.getinfo(iDim, iStart);
System.out.println("Found iData1 with: rank = " + iStart[0] + " type = " + iStart[1] + " dims = " + iDim[0]
+ ", " + iDim[1]);
h = nf.attrdir();
e = h.keys();
while (e.hasMoreElements()) {
attname = (String) e.nextElement();
atten = (AttributeEntry) h.get(attname);
System.out.println("Found SDS attribute: " + attname + " type: " + atten.type + ", length: "
+ atten.length);
}
// success for inquiry routines
nf.closedata();
nf.closegroup();
System.out.println(" **** Inquiry routines passed test");
// test the data reading routines
nf.opengroup(group, nxclass);
nf.opendata("iData1");
nf.getdata(iData2);
for (i = 0; i < 3; i++) {
for (j = 0; j < 10; j++) {
if (iData1[i][j] != iData2[i][j])
System.out.println(" Data Reading Error at : " + i + ", " + j);
}
}
// test attribute reading. This is also an example for reading
// Strings from a NeXus file.
byte bString[] = new byte[60];
iDim[0] = 60;
iDim[1] = NexusFile.NX_CHAR;
nf.getattr("Units", bString, iDim);
System.out.println("Read attribute Units to: " + new String(bString, 0, iDim[0]));
// check reading a slab
iStart[0] = 0;
iStart[1] = 0;
iEnd[0] = 1;
iEnd[1] = 10;
nf.getslab(iStart, iEnd, islab);
for (i = 0; i < 10; i++) {
if (islab[i] != iData1[0][i])
System.out.println(" Slab Reading Error at : " + i + " expected: " + iData1[0][i] + ", got: "
+ islab[i]);
}
nf.closedata();
// check compressed data
nf.opendata("iData1_compressed");
nf.getdata(iData2);
for (i = 0; i < 3; i++) {
for (j = 0; j < 10; j++) {
if (iData1[i][j] != iData2[i][j])
System.out.println(" Data Reading Error at : " + i + ", " + j);
}
}
nf.closedata();
// now, for completeness: check float data as well
nf.opendata("fData1");
nf.getdata(fData1);
nf.closedata();
for (i = 0; i < 3; i++) {
for (j = 0; j < 10; j++) {
if (Math.abs(fData1[i][j] - (float) (i * 10.1 + j * .2)) > .05) {
System.out.println(" Float Reading Error at : " + i + ", " + j);
}
}
}
// reading success
System.out.println(" *** Data Reading routines appear to work");
// test openpath
nf.openpath("/entry2/data/iData1");
nf.openpath("/entry2/data/iData1");
nf.openpath("../");
System.out.println("*** openpath seems to work");
} finally {
nf.close();
}
}
public static final Map<String, Integer> backendmap = new LinkedHashMap<String, Integer>(){{
put("HDF5", NexusFile.NXACC_CREATE5);
put("XML", NexusFile.NXACC_CREATEXML);
put("HDF4", NexusFile.NXACC_CREATE4);
}};
static public void main(String args[]) {
TestJapi tj = new TestJapi();
System.out.println("=================");
System.out.println("Testing Java API.");
System.out.println("=================");
if (args.length == 0) {
args = backendmap.keySet().toArray(new String[] {});
}
for(String arg: args) {
String lower = arg.toLowerCase();
String upper = arg.toUpperCase();
System.out.println("");
if (backendmap.containsKey(upper)) {
System.out.println("Testing "+upper);
System.out.println("------------");
try {
tj.fileTest(backendmap.get(upper), "japitest."+lower);
System.out.println("Success.");
} catch (Exception e) {
System.err.println("Failed with exception: "+e.getMessage());
e.printStackTrace();
}
} else {
System.out.println("Failed to parse argument. "+arg+" is not a known NeXus backend");
}
}
System.out.println("");
System.out.println("Exiting.");
}
}
|