Changeset 1541 for trunk/bindings


Ignore:
Timestamp:
06/10/10 18:28:25 (20 months ago)
Author:
Tobias Richter
Message:

refs #105

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/java/test/TestJapi.java

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