aboutsummaryrefslogtreecommitdiff
path: root/src/ioconvert.cc
blob: 0b1f82332189a9cc9afec1e810bfae9ea6e6d4d4 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "IO.hh"
#include "IEEEIO.hh"
#ifdef WITH_HDF4
#include "HDFIO.hh"
#endif
#ifdef WITH_HDF5
#include "H5IO.hh"
#endif

char *programname;
void usage(){
  printf("Correct usage is %s <infile> <outfile> [-swapbytes]\n",programname);
  puts("-swapbytes requests opposite of native byte-order for IEEEIO output");
  puts("The default is to write IEEE files in native byte-order");
  exit(1); // failure code
}

int main(int argc,char *argv[]){
  programname=argv[0]; // for usage/error_exit routine
  if(argc<3) usage();
  int i,swap;
  int sindex=0,dindex=0,findex=0;
  IObase *infile,*outfile;
#ifdef WITH_HDF4
  HDFIO *hdffile = NULL;
  int hdfin=0;
#endif
  for(i=1;i<argc;i++){
    if(*(argv[i])=='-') findex=i;
    else if(!sindex) sindex=i;
    else dindex=i;
  }
  if(!sindex || !dindex || // doesn't have both infile and outfile specified
     (findex && !strcmp("-help",argv[findex])) || // used help flag
     (findex && strcmp("-swapbytes",argv[findex])) )  // invalid flag
    usage();
  if(findex && !strcmp("-swapbytes",argv[findex])) swap=1;
  else swap=0;

  // for the input file
#if defined(WITH_HDF4) || defined(WITH_HDF5)
  char *s_ext=strrchr(argv[sindex],'.');
  char *d_ext=strrchr(argv[dindex],'.');
#endif
#ifdef WITH_HDF4
  if(s_ext && !strcmp(s_ext,".hdf"))
  {
    infile=hdffile=new HDFIO(argv[sindex],IObase::Read);
    hdfin=1;
  }
  else
#endif
#ifdef WITH_HDF5
  if(s_ext && !strcmp(s_ext,".h5"))
  {
    infile=new H5IO(argv[sindex],IObase::Read);
  }
  else
#endif
  {
    // assume its raw
    infile=new IEEEIO(argv[sindex],IObase::Read);
  }
  if(!infile->isValid()){
    printf("cant read %s\n",argv[sindex]); 
    usage();
  }

  // for the outfile, 
#ifdef WITH_HDF4
  if(d_ext && !strcmp(d_ext,".hdf"))
  {
    outfile=new HDFIO(argv[dindex],IObase::Create);
  }
  else
#endif
#ifdef WITH_HDF5
  if(d_ext && !strcmp(d_ext,".h5"))
  {
    outfile=new H5IO(argv[dindex],IObase::Create);
  }
  else
#endif
  {
    // assume its raw
    outfile=new IEEEIO(argv[dindex],IObase::Create,swap); // swap only affects IEEEIO output
  }
  if(!outfile->isValid()){
    printf("cant write %s\n",argv[dindex]); 
    delete infile; 
    usage();
  }

  // Now do the conversion
  for(i=0;i<infile->nDatasets();i++){
    int rank,dims[5];
    IObase::DataType type;
    char *data;
    infile->seek(i);
#ifdef WITH_HDF4
    if(hdfin && hdffile->isCoord()) continue; // skip coord vars
#endif
    // fprintf(stderr,".%u",i);
    infile->readInfo(type,rank,dims);
    {
      int j,sz;
      for(j=0,sz=1;j<rank;j++) sz*=dims[j];
      sz*=IObase::sizeOf(type);
      data=new char[sz];
    }
    infile->read(data);
    outfile->write(type,rank,dims,data);
    delete data; // OK, so I'm not being smart about mem allocation here
    for(int j=0;j<infile->nAttributes();j++){
      char name[256]; // should be big enough
      int length;
      // fprintf(stderr,"a");
      infile->readAttributeInfo(j,name,type,length);
      data = new char[length * IObase::sizeOf(type)];
      infile->readAttribute(j,data);
      outfile->writeAttribute(name,type,length,data);
      delete data;
    }
  }
  puts("");
  delete outfile;
  // delete infile;
  return 0; // success code
}