Bsoft  2.1.0
Bernard's software package
rwGRD.cpp
Go to the documentation of this file.
1 
9 #include "rwGRD.h"
10 #include "file_util.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
33 int readGRD(Bimage *p, int readdata, int img_select)
34 {
35  ifstream* fimg = new ifstream(p->file_name());
36  if ( fimg->fail() ) return -1;
37 
38  GRDhead* header = new GRDhead;
39 
40  fimg->read((char *)header, GRDSIZE);
41  if ( fimg->fail() ) return -2;
42 
43  // Determine byte order and swap bytes if from little-endian machine
44  unsigned char* b = (unsigned char *) header;
45  int i, sb = 0;
46  if ( abs( header->mode ) > SWAPTRIG ) {
47  if ( verbose & VERB_PROCESS )
48  cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
49  sb = 1;
50  int extent = GRDSIZE; // Swap whole header
51  for ( i=0; i<extent; i+=4 ) swapbytes(b+i, 4);
52  if ( b[16] > 0 ) header->origp = 1; // Little-endian (DEC,PC)
53  if ( b[19] > 0 ) header->origp = 2; // Big-endian (SGI,MAC)
54  }
55 
56  // Map the parameters
57  p->size(header->nx, header->ny, header->nz);
58  p->images(header->nn);
59  p->channels(header->channels);
60  p->sampling(header->ux, header->uy, header->uz);
61  switch ( header->mode ) {
62  case 1: p->data_type(UCharacter); break;
63  case 2: p->data_type(Float); break;
64  case 3: p->data_type(Float);
65  p->compound_type(TComplex); p->channels(2);
66  p->fourier_type(Standard); break;
67  case 4: p->data_type(Float);
68  p->compound_type(TVector3); p->channels(3); break;
69  case 5: p->data_type(Float);
70  p->compound_type(TView); p->channels(4); break;
71  case 10: p->data_type(Float);
72  p->compound_type(TMulti); break;
73  default: p->data_type(UCharacter); break;
74  }
75  if ( verbose & VERB_DEBUG )
76  cout << "DEBUG readGRD: mode=" << header->mode << " datatype=" << p->data_type() << endl;
77  p->data_offset(GRDSIZE + header->doffset);
78  p->minimum(header->amin);
79  p->maximum(header->amax);
80  p->average(header->amean);
81  p->standard_deviation(header->arms);
82  p->unit_cell(UnitCell(header->a, header->b, header->c, header->alpha, header->beta, header->gamma));
83 
84  // Allocating the single sub-image and setting its parameters
85  p->background(0, header->bkg);
86  p->origin(header->ox, header->oy, header->oz);
87  p->view(header->vx, header->vy, header->vz, header->va);
88 
89  if ( header->doffset > 0 ) {
90  if ( verbose & VERB_DEBUG )
91  cout << "DEBUG readGRD: reading JSON header:" << header->doffset << endl;
92  char* ext_header = new char[header->doffset];
93  fimg->read((char *)ext_header, header->doffset);
94  if ( fimg->fail() ) return -1;
95  if ( verbose & VERB_DEBUG )
96  cout << ext_header << endl;
97  JSparser parser(ext_header, header->doffset);
98  p->meta_data() = parser.parse();
99  if ( img_select > -1 ) p->meta_data_retain_one_image(img_select);
101  delete[] ext_header;
102  }
103 
104  tm* t = p->get_localtime();
105  sscanf(header->datetime, "%4d%2d%2d%2d%2d%2d",
106  &t->tm_year, &t->tm_mon, &t->tm_mday,
107  &t->tm_hour, &t->tm_min, &t->tm_sec);
108  t->tm_mon -= 1;
109  t->tm_year -= 1900;
110  p->set_time(t);
111 
112  p->symmetry(header->symmetry);
113  p->label(header->label);
114 
115  delete header;
116 
117  if ( img_select < 0 ) img_select = 0;
118 
119  long readsize = p->alloc_size();
120 
121  if ( readdata ) {
122  p->data_alloc();
123  if ( fread_large(p->data_pointer(), readsize, p->data_offset() + img_select*readsize, fimg) < 0 ) return -1;
124  if ( sb ) swapbytes(readsize, p->data_pointer(), p->data_type_size());
125  }
126 
127  fimg->close();
128  delete fimg;
129 
130  return 0;
131 }
132 
140 {
141  p->color_to_simple();
142 
143  if ( p->compound_type() == TComplex ) p->change_type(Float);
144 
145  switch ( p->data_type() ) {
146  case Bit: case UCharacter: case SCharacter:
147  p->change_type(UCharacter); break;
148  default:
149  p->change_type(Float); break;
150  }
151 
152  if ( verbose & VERB_DEBUG )
153  cout << "DEBUG writeGRD: header size=" << sizeof(GRDhead) << endl;
154 
155  GRDhead* header = new GRDhead;
156  memset(header, 0, sizeof(GRDhead));
157 
158  header->magic = 42;
159 
160  // Map the parameters
161  UnitCell uc = p->unit_cell();
162  header->nx = p->size()[0];
163  header->ny = p->size()[1];
164  header->nz = p->size()[2];
165  header->nn = p->images();
166  header->channels = p->channels();
167  header->ux = p->sampling(0)[0];
168  header->uy = p->sampling(0)[1];
169  header->uz = p->sampling(0)[2];
170 
171  switch ( systype(0) ) {
172  case BigIEEE: header->origp = 2; break; // MIPS, Motorola
173  case LittleIEEE: header->origp = 1; break; // Alpha, Intel
174  case LittleVAX: header->origp = 1; break; // VAX
175  default: break;
176  }
177  switch ( p->data_type() ) {
178  case UCharacter: header->mode = 1; break;
179  case Float:
180  switch ( p->compound_type() ) {
181  case TComplex: header->mode = 3; break;
182  case TVector3: header->mode = 4; break;
183  case TView: header->mode = 5; break;
184  case TMulti: header->mode = 10; break;
185  default: header->mode = 2;
186  }
187  break;
188  default: header->mode = 1; break;
189  }
190 
191  if ( verbose & VERB_DEBUG )
192  cout << "DEBUG writeGRD: mode=" << header->mode << endl;
193 
194  stringstream ss;
195  ss << p->meta_data() << endl;
196 // cout << p->meta_data() << endl;
197 // ss.seekp(0, ios::end);
198  header->doffset = ss.tellp();
199 // string jstr = ss.str();
200 // cout << jstr << endl;
201 // header->doffset = jstr.size();
202 
203  if ( verbose & VERB_DEBUG )
204  cout << "DEBUG writeGRD: offset=" << header->doffset << endl;
205 
206  p->data_offset(GRDSIZE+header->doffset);
207  header->amin = p->minimum();
208  header->amax = p->maximum();
209  header->amean = p->average();
210  header->arms = p->standard_deviation();
211 
212  header->a = uc.a();
213  header->b = uc.b();
214  header->c = uc.c();
215  header->alpha = uc.alpha()*180/M_PI;
216  header->beta = uc.beta()*180/M_PI;
217  header->gamma = uc.gamma()*180/M_PI;
218 
219  header->bkg = p->background(long(0));
220  header->ox = p->image->origin()[0];
221  header->oy = p->image->origin()[1];
222  header->oz = p->image->origin()[2];
223  header->vx = p->image->view()[0];
224  header->vy = p->image->view()[1];
225  header->vz = p->image->view()[2];
226  header->va = p->image->view().angle();
227 
228  if ( verbose & VERB_DEBUG )
229  cout << "DEBUG writeGRD: bkg=" << header->bkg << endl;
230 
231  tm* t = p->get_localtime();
232  snprintf(header->datetime, 16, "%4d%02d%02d%02d%02d%02d", t->tm_year+1900,
233  t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
234 
235  if ( verbose & VERB_DEBUG )
236  cout << "DEBUG writeGRD: datetime=" << header->datetime << endl;
237 
238  strncpy(header->symmetry, p->symmetry().c_str(), 30);
239  strncpy(header->label, p->label().c_str(), 200);
240 
241  if ( verbose & VERB_DEBUG )
242  cout << "DEBUG writeGRD: label=" << header->label << endl;
243 
244  ofstream fimg(p->file_name());
245  if ( fimg.fail() ) return -1;
246 
247  fimg.write((char *)header, GRDSIZE);
248  fimg << ss.rdbuf();
249 // fimg << jstr << endl;
250  if ( p->data_pointer() ) fimg.write((char *)p->data_pointer(), p->alloc_size());
251 
252  fimg.close();
253 
254  delete header;
255 
256  return 0;
257 }
258 
readGRD
int readGRD(Bimage *p, int readdata, int img_select)
Reading a Basel GRD map image file format.
Definition: rwGRD.cpp:33
TView
@ TView
Definition: Bimage.h:62
Bimage::symmetry
string & symmetry()
Definition: Bimage.h:603
GRDhead
Definition: rwGRD.h:15
Bimage::get_localtime
tm * get_localtime()
Definition: Bimage.h:260
Bit
@ Bit
Definition: utilities.h:152
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
UCharacter
@ UCharacter
Definition: utilities.h:153
file_util.h
Header file for file checking functions.
fread_large
int fread_large(unsigned char *aptr, size_t pagesize, size_t offset, ifstream *fimg)
Reads blocks of memeory no larger than 1Gb.
Definition: file_util.cpp:347
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:151
Bimage::set_time
void set_time(time_t t)
Definition: Bimage.h:264
GRDSIZE
#define GRDSIZE
Definition: rwGRD.h:13
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
TComplex
@ TComplex
Definition: Bimage.h:60
rwGRD.h
Header file for reading and writing Basel GRD files.
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::meta_data_retain_one_image
void meta_data_retain_one_image(long img_num)
Erases all sub-image records from the meta data except one.
Definition: Bimage.cpp:771
sb
int sb(0)
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
writeGRD
int writeGRD(Bimage *p)
Writing a Basel GRD map image file format.
Definition: rwGRD.cpp:139
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
JSparser::parse
JSvalue parse()
Definition: json.h:416
TVector3
@ TVector3
Definition: Bimage.h:61
verbose
int verbose
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Standard
@ Standard
Definition: Bimage.h:42
Bimage::images
long images()
Definition: Bimage.h:504
JSparser
Definition: json.h:386
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:267
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Bimage::meta_data
JSvalue & meta_data()
Definition: Bimage.h:234
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
LittleIEEE
@ LittleIEEE
Definition: utilities.h:135
BigIEEE
@ BigIEEE
Definition: utilities.h:133
TMulti
@ TMulti
Definition: Bimage.h:66
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
SWAPTRIG
#define SWAPTRIG
Definition: rwimg.h:16
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
LittleVAX
@ LittleVAX
Definition: utilities.h:137
SCharacter
@ SCharacter
Definition: utilities.h:154
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::minimum
double minimum()
Definition: Bimage.h:541
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
Bimage::update_from_meta_data
void update_from_meta_data()
Update sub-image information from the metadata.
Definition: Bimage.cpp:725
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41
Bimage::label
void label(string s)
Definition: Bimage.h:250