aboutsummaryrefslogtreecommitdiff
path: root/src/ioinfo.cc
blob: d8136626854519440e9157cf9d55516f9b8c927a (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include "Arch.h"
#include "IO.hh"
#include "IEEEIO.hh"

#ifdef WITH_HDF4
#include "HDFIO.hh"
#endif
#ifdef WITH_HDF5
#include "H5IO.hh"
#endif

struct CmdlnParams {
  char *programname;
  char *filename,*filetype;
  int showdatasetinfo,showannotationinfo,showattribinfo;
  int showdatamax,showannotationmax,showattribmax;
  int showdatastats,showattribstats;

  void setDefaults(){
    programname="";
    filename=0;
    filetype=0;
    showdatasetinfo=1;
    showannotationinfo=0;
    showattribinfo=0;
    showdatamax=0;
    showannotationmax=0;
    showattribmax=0;
    showdatastats=0;
    showattribstats=0;
  }

  CmdlnParams(){
    setDefaults();
  }

  CmdlnParams(int argc,char *argv[]){
    setDefaults();
    parse(argc,argv);
  }

  void parse(int argc,char *argv[]){
    programname=argv[0];
    for(int i=1;i<argc;i++){
      char *str=argv[i];
      if(*str=='-'){
	char *val=argv[i+1];
	str++;
	//printf("read option[%u]=[%s]\n",i,str);
	if(!strcmp(str,"showdata"))      { 
	  puts("option -showdata:\tShow first 10 data entries");
	  showdatamax=10;}
	else if(!strcmp(str,"hidedata")) {
	  puts("option -hidedata:\tDon't show data");
	  showdatamax=0; }
	else if(!strncmp(str,"showann",7)&&
		strstr(str,"data")) {
	  puts("option -showanndata:\tShow annotation text (max of 10 chars)");
	  showannotationmax=10; showannotationinfo=1; }
	else if(!strncmp(str,"hideann",7)&&
		strstr(str,"data")) {
	  puts("option -hideanndata:\tSupress printing of annotation text");
	  showannotationmax=0; }
	else if(!strncmp(str,"showatt",7) &&
		strstr(str,"data"))  {
	  puts("option -showattdata:\tShow first 10 elements of attribute data");
	  showattribmax=10; 
	  showattribinfo=1;}	
	else if(!strncmp(str,"hideatt",7)&&
		strstr(str,"data")) {
	  puts("option -hideatt:\tHide attribute info");
	  showattribmax=0; }
	else if(!strncmp(str,"showann",7)&&
		strstr(str,"info")) {showannotationinfo=1;}
	else if(!strncmp(str,"hideann",7)&&
		strstr(str,"info")) {showannotationinfo=0; }
	else if(!strncmp(str,"showatt",7) &&
		strstr(str,"info"))  {
	  puts("option -showattinfo:\tShow attribute info (name,type,length)");
	  showattribinfo=1; }	
	else if(!strncmp(str,"hideatt",7) &&
		strstr(str,"info")) {showattribinfo=0; }
	else if(!strcmp(str,"file"))     {
	  filename=val;   
	  i++;
	  filetype=strrchr(str,'.') + 1;
	  printf("option -file %s: Read file named [%s]\n",filename,filetype);
	}
	else if(!strcmp(str,"type"))     {filetype=val;   i++;}
      }
      else { // its probably a filename
	filename=str;
	filetype=strrchr(str,'.');
	if(filetype) filetype++;       // skip the '.'
	else filetype = "";
      }
    }
  }
};

char *Typename(IObase::DataType datatype){
  char *typestring;
  switch(datatype){
  case IObase::Int8:
    typestring="Int8";
    break;
  case IObase::Int16:
    typestring="Int16";
    break;
  case IObase::Int32:
    typestring="Int32";
    break;
  case IObase::Int64:
    typestring="Int64";
    break;
  case IObase::Float32:
    typestring="Float32";
    break;
  case IObase::Float64:
    typestring="Float64";
    break;
  case IObase::Char8:
    typestring="String";
    break;
  default:
    typestring="<Unknown>";
    break;
  }
  return typestring;
}

int main(int argc,char *argv[]){
  CmdlnParams cmdln(argc,argv); // parse commandline parameters & flags
  if(!cmdln.filename) {
    puts("HEY!  Need a filename there buddy!");
    exit(1); // failure code
  }

  int i;
  IObase *infile = NULL;

  // Figure out what kind of file to open as
#ifdef WITH_HDF5
  if(!strcmp(cmdln.filetype,"h5")){
    infile = new H5IO(cmdln.filename,IObase::Read);
  }
#endif
#ifdef WITH_HDF4
  if(!strcmp(cmdln.filetype,"hdf")){
    infile = new HDFIO(cmdln.filename,IObase::Read);
  }
#endif
  if(!strcmp(cmdln.filetype,"raw") || !strcmp(cmdln.filetype,"ieee")){
    infile = new IEEEIO(cmdln.filename,IObase::Read);
  }
  if(!infile){
    puts("I don't recognize that type of file, but I'll try to open as ieee");
    cmdln.filetype="ieee";
    infile = new IEEEIO(cmdln.filename,IObase::Read);
  }
  if(!infile->isValid()){
    printf("%s: The file %s is not a valid %s file... sorry\n",
	   cmdln.programname,cmdln.filename,cmdln.filetype);
    exit(1);
  }
  int ndatasets=infile->nDatasets();
  printf("Number of datasets=%d\n",ndatasets);
  puts  ("===========================");

  // OK... now its time for da main event loop....
  for(i=0;i<ndatasets;i++){
    int j;
    int rank,dims[5];
    IObase::DataType datatype;

    printf("Dataset[%u]\n",i);
    puts("--------------");
    infile->readInfo(datatype,rank,dims);
    
    printf("Datatype=%s Rank=%u Dims=",Typename(datatype),rank);
    for(j=0;j<rank-1;j++)
      printf("%u,",dims[j]);
    printf("%u\n",dims[rank-1]); 
#if 0 // blow this away for now
    if(cmdln.showdatamax){// || cmdln.showdatastats (not implemented yet)
      // must allocate memory for it
      void *buffer=0;
      int *idims=new int[rank];
      for(int dim=0;dim<rank;dim++) idims[dim]=0;
      idim[0]=showdatamax;
      if(IObase::sizeOf(datatype)>0 && dims[0]>0)
	buffer=new char[idims[0] * IObase::sizeOf(datatype)];
      //infile->readChunk(buffer);
      delete buffer;
      delete idims;
    }
#endif
    int nattribs=infile->nAttributes();
    int nannotations=infile->nAnnotations();
    printf("Annotations: %u\n",nannotations);
    printf("Attributes:  %u\n",nattribs);

    if(cmdln.showattribinfo){
      for(j=0;j<nattribs;j++){
	char attribname[128];
	Long length;
	infile->readAttributeInfo(j,attribname,datatype,length);
	printf("\tAttribute[%u] Name=%s Type=%s Length=%u\n",
	       j,attribname,Typename(datatype),length);
	if(cmdln.showattribmax || cmdln.showattribstats){
	//printf("Length of Attribute=%u for total bytes=%u\n",length,IObase::sizeOf(datatype));
	  void *dataptr;
	  dataptr=0;
	  if(IObase::sizeOf(datatype)>0 && length>0)
	    dataptr=new char[length * IObase::sizeOf(datatype)];
	  else {
	    //puts("no data to display");
	    continue; // no data to display
	  }
	  
	  infile->readAttribute(j,dataptr);
	  switch(datatype){
	  case IObase::Char8:{
	    char *d=(char*)dataptr;
	    d[length]='\0';
	    //puts("show char data");
	    printf("\t%s string[%u]:= %s\n",attribname,length,d);
	  }break;
	  case IObase::Int8:
	  case IObase::uInt8:{
	    char *d=(char*)dataptr;
	    for(int k=0;k<cmdln.showattribmax && k<length;k++)
	      if(d[k]>=0x20 && d[k]<=0x7e)
		printf("\t%s[%u]:=%c\n",attribname,k,d[k]);
	      else {
		int intchar=(unsigned int)(d[k]);
		printf("\t%s[%u]:=%u\n",attribname,k,intchar);
	      }
	  }break;
	  case IObase::Int16:{
	    short *d=(short*)dataptr;
	    for(int k=0;k<cmdln.showattribmax && k<length;k++)
	      printf("\t%s[%u]:=%u\n",attribname,k,d[k]);
	  }break;
	  case IObase::Int32:{
	    int *d=(int*)dataptr;
	    for(int k=0;k<cmdln.showattribmax && k<length;k++)
	      printf("\t%s[%u]:=%u\n",attribname,k,d[k]);
	  }break;
	  case IObase::Int64:{
	    Long8 *d=(Long8*)dataptr;
	    for(int k=0;k<cmdln.showattribmax && k<length;k++)
	      printf("\t%s[%u]:=%lu\n",attribname,k,d[k]);
	  }break;
	  case IObase::Float32:{
	    float *d=(float*)dataptr;
	    for(int k=0;k<cmdln.showattribmax && k<length;k++)
	      printf("\t%s[%u]:=%f\n",attribname,k,d[k]);
	  }break;
	  case IObase::Float64:{
	    double *d=(double*)dataptr;
	    for(int k=0;k<cmdln.showattribmax && k<length;k++)
	      printf("\t%s[%u]:=%f\n",attribname,k,d[k]);
	  }break;
	  default:
	    puts("-----<nil>-----");
	    break;
	  }
	  if(dataptr)
	    delete (char*)dataptr;
	}
      }
    }
  }
  puts("Closing file");
  delete infile;
  puts("***done");
  return 0;
}