File: fileio_ser.cpp

package info (click to toggle)
odin 2.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,196 kB
  • sloc: cpp: 62,638; sh: 4,541; makefile: 779
file content (234 lines) | stat: -rw-r--r-- 6,988 bytes parent folder | download | duplicates (5)
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
#include "fileio.h"
#include "image.h"

/////////////////////////////////////////////////////////////

void resize4dim(farray& fdata) {
  if(fdata.dim()==4) return; // leave untouched if it is already 4dim
  fdata.autosize();
  ndim nn=fdata.get_extent();
  bool add_first_dim=true;
  if(nn.dim()==1) add_first_dim=false;
  while(nn.dim()<4) nn.add_dim(1,add_first_dim);
  while(nn.dim()>4) --nn;
  fdata.redim(nn);
}


/////////////////////////////////////////////////////////////


struct JdxFormat : public FileFormat {
  STD_string description() const {return "JCAMP-DX data sets";}
  svector suffix() const  {
    svector result; result.resize(2);
    result[0]="smp";
    result[1]="coi";
    return result;
  }
  svector dialects() const {return svector();}
  int read(Data<float,4>& data, const STD_string& filename, const FileReadOpts& opts, Protocol& prot) {
    Log<FileIO> odinlog("JdxFormat","read");

    STD_string arrlabel=opts.ldr;

    if(arrlabel=="") {
      if(LDRfileName(filename).get_suffix()=="smp") arrlabel="spinDensity";
      else {
        ODINLOG(odinlog,errorLog) << "No array label provided, use the option '-" << opts.ldr.get_cmdline_option() <<"' to specify one" << STD_endl;
        return -1;
      }
    }

    LDRblock block;
    farray fdata;

    bool valid_type=false;

    if(!valid_type) {
      LDRdoubleArr dpar;
      dpar.set_label(arrlabel);
      block.clear(); block.append(dpar);
      if(block.load(filename,serializer)>0) {
        ODINLOG(odinlog,normalDebug) << "LDRdoubleArr detected" << STD_endl;
        fdata.redim(dpar.get_extent());
        for(unsigned int i=0; i<dpar.length(); i++) fdata[i]=dpar[i];
        valid_type=true;
      }
    }

    if(!valid_type) {
      LDRfloatArr fpar;
      fpar.set_label(arrlabel);
      block.clear(); block.append(fpar);
      if(block.load(filename,serializer)>0) {
        ODINLOG(odinlog,normalDebug) << "LDRfloatArr detected" << STD_endl;
        fdata.redim(fpar.get_extent());
        for(unsigned int i=0; i<fpar.length(); i++) fdata[i]=fpar[i];
        valid_type=true;
      }
    }

    if(!valid_type) {
      LDRcomplexArr cpar;
      cpar.set_label(arrlabel);
      block.clear(); block.append(cpar);
      if(block.load(filename,serializer)>0) {
        ODINLOG(odinlog,normalDebug) << "LDRcomplexArr detected" << STD_endl;
        ndim nn=cpar.get_extent();
        nn[0]*=2;
        fvector amp=amplitude(cpar);
        fvector pha=phase(cpar);
        ODINLOG(odinlog,normalDebug) << "nn=" << STD_string(nn) << STD_endl;
        fdata.redim(nn);
        unsigned int n=cpar.length();
        for(unsigned int i=0; i<n; i++) {
          fdata[i]=amp[i];
          fdata[n+i]=pha[i];
        }
        valid_type=true;
      }
    }

    if(!valid_type) {
      ODINLOG(odinlog,errorLog) << "Array parameter " << arrlabel << " not found"  << STD_endl;
      return -1;
    }

    resize4dim(fdata);
    data=fdata;
    return data.extent(0)*data.extent(1);
  }

  int write(const Data<float,4>& data, const STD_string& filename, const FileWriteOpts&, const Protocol& prot) {
    Log<FileIO> odinlog("JdxFormat","write");
    ODINLOG(odinlog,errorLog) << "Not implemented"  << STD_endl;
    return -1;
  }

 private:
  LDRserJDX serializer;

};

/////////////////////////////////////////////////////////////

template<class Serializer>
struct ImageFormat : public FileFormat {
  STD_string description() const {return "ODIN Image based on "+serializer.get_description();}
  svector suffix() const  {
    svector result; result.resize(1);
    result[0]=serializer.get_default_file_prefix();
    return result;
  }
  svector dialects() const {return svector();}

  int read(FileIO::ProtocolDataMap& pdmap, const STD_string& filename, const FileReadOpts& opts, const Protocol& protocol_template) {
    Log<FileIO> odinlog("ImageFormat","read");
    int result=0;

    ImageSet imgset;
    if(imgset.load(filename,serializer)<0) return -1;

    int nsets=imgset.get_numof_images();
    if(nsets<=0) return -1;
    ODINLOG(odinlog,normalDebug) << "nsets=" << nsets << STD_endl;

    Protocol prot(protocol_template);

    for(int i=0; i<nsets; i++) {
      prot.geometry=imgset.get_image(i).get_geometry();
      prot.study.set_Series(imgset.get_image(i).get_label(), i); // Make protocols distinguishable

      Data<float,4>& data=pdmap[prot];
      farray fdata=imgset.get_image(i).get_magnitude();
      resize4dim(fdata);
      data=fdata;
      result+=data.extent(0)*data.extent(1);
    }
    return result;
  }


  int write(const FileIO::ProtocolDataMap& pdmap, const STD_string& filename, const FileWriteOpts& opts) {
    Log<FileIO> odinlog("ImageFormat","write");
    int result=0;
    ImageSet imgset(LDRfileName(filename).get_basename());
    for(FileIO::ProtocolDataMap::const_iterator pdit=pdmap.begin(); pdit!=pdmap.end(); ++pdit) {
      STD_string series;
      int number;
      pdit->first.study.get_Series(series, number);

      Image img(series); // use series description as image label
      img.set_geometry(pdit->first.geometry);
      img.set_magnitude(pdit->second);
      imgset.append_image(img);
      result+=pdit->second.extent(0)*pdit->second.extent(1);
    }
    if(imgset.write(filename,serializer)<0) return -1;
    return result;
  }

 private:
  Serializer serializer;

};

/////////////////////////////////////////////////////////////


template<class Serializer>
struct ProtFormat : public FileFormat {

  STD_string description() const {return "ODIN protocols based on "+serializer.get_description();}

  svector suffix() const  {
    svector result; result.resize(1);
    if(serializer.get_default_file_prefix()=="xml") result[0]="x";
    result[0]+="pro";
    return result;
  }

  svector dialects() const {return svector();}

  int read(Data<float,4>& data, const STD_string& filename, const FileReadOpts& opts, Protocol& prot) {
    Log<FileIO> odinlog("ProtFormat","read");

    if(prot.load(filename,serializer)<0) return false;

    int nslices=prot.geometry.get_nSlices();
    if(prot.geometry.get_Mode()==voxel_3d) nslices=prot.seqpars.get_MatrixSize(sliceDirection);

    data.resize(1, nslices, prot.seqpars.get_MatrixSize(phaseDirection), prot.seqpars.get_MatrixSize(readDirection)); // only one repetition
    data=0.0;

    return data.extent(0)*data.extent(1);
  }

  int write(const Data<float,4>& data, const STD_string& filename, const FileWriteOpts&, const Protocol& prot) {
    Log<FileIO> odinlog("ProtFormat","write");
    return prot.write(filename,serializer);
  }

 private:
  Serializer serializer;

};



//////////////////////////////////////////////////////////////

void register_ser_format() {
 static JdxFormat jf;
 static ImageFormat<LDRserJDX> jdximf;
 static ImageFormat<LDRserXML> xmlimf;
 static ProtFormat<LDRserJDX> jdxpf;
 static ProtFormat<LDRserXML> xmlpf;
 jf.register_format();
 jdximf.register_format();
 xmlimf.register_format();
 jdxpf.register_format();
 xmlpf.register_format();
}