isce3  0.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
DataPatch.h
1 // Copyright (c) 2017-, California Institute of Technology ("Caltech"). U.S.
2 // Government sponsorship acknowledged.
3 // All rights reserved.
4 //
5 // Author(s):
6 //
7 //
8 // ======================================================================
9 //
10 // FILENAME: DataPatch.h
11 //
12 //
13 // ======================================================================
14 
15 #pragma once
16 
17 #include "stdlib.h"
18 #include <complex>
19 #include <iostream>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 //------------------------------------------------------------------------------
24 
25 using namespace std;
26 
27 template <class type>
28 class DataPatch {
29 
30  // friend std::ostream& operator<< (std::ostream&, DataPatch& patch);
31 
32  private:
33 
34  DataPatch(const DataPatch & patch);
35  DataPatch & operator = (const DataPatch & patch);
36 
37 
38  protected:
39  int nr_lines; // maximum line extent of patch
40  int nr_pixels; // maximum pixel extent of patch
41 
42  int actual_start_line; // start of "good" data inside patch
43  int actual_nr_lines; // number of "good" data lines inside patch
44 
45  int actual_start_pixel; // start of "good" pixels inside patch
46  int actual_nr_pixels; // number of "good" data pixels inside patch
47 
48  int extern_start_line; // corresponding to actual_start_line in the file
49  int extern_start_pixel; // corresponding to actual_start_pixel in the file
50 
51  type *data; // pointer to the data
52  type **data_lines; // pointer to array of line pointers
53 
54  public:
55 
56  //DataPatch(); //added by craig
57  DataPatch(int pixels, int lines);
58 
59  virtual type operator() (int x,int y);
60 
61  virtual type operator[] (int k);
62  virtual const type operator[] (int k) const;
63 
64  virtual ~DataPatch();
65 
66 
67  void set_actual_lines (int act_start_line, int act_nr_lines);
68 
69  void set_actual_pixels (int act_start_pixel, int act_nr_pixels);
70 
71  void set_extern_start_line (int start_line);
72 
73  void set_extern_start_pixel (int start_pixel);
74 
75  int get_nr_lines () {return nr_lines;}
76  int get_nr_pixels () {return nr_pixels;}
77  int get_actual_start_line () {return actual_start_line;}
78  int get_actual_nr_lines () {return actual_nr_lines;}
79  int get_actual_start_pixel () {return actual_start_pixel;}
80  int get_actual_nr_pixels () {return actual_nr_pixels;}
81  int get_extern_start_line () {return extern_start_line;}
82  int get_extern_start_pixel () {return extern_start_pixel;}
83 
84  type *get_data_ptr () {return data;}
85  type **get_data_lines_ptr () {return data_lines;}
86 
87  type mean();
88 
89  void dump (char *filename); // dumps the used part of patch to filename
90  void dumpall(char *filename);
91  void write (char *filename, int append=0); // dumps the used part of patch to filename
92 
93 };
94 
95 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
97 
98 template <class type> inline type DataPatch<type>::operator() (int y,int x) {
99  // DPRINT("operator()\n");
100  if(y<0 || y>=nr_lines)
101  cerr << "DataPatch::operator()(int x,int y), Invalid par y: " << y << endl;
102  if(x<0 || x>=nr_pixels)
103  cerr << "DataPatch::operator()(int x,int y), Invalid par x: " << x << endl;
104  return data_lines[y][x];
105 }
106 
107 template <class type> inline type DataPatch<type>::operator[] (int k) {
108  // DPRINT("operator[]\n");
109  if(k < 0 || k >=nr_lines*nr_pixels)
110  cerr << "DataPatch::operator[](int k), Invalid par k: " << k << endl;
111  return data[k];
112 }
113 
114 template <class type> inline const type DataPatch<type>::operator[] (int k) const {
115  // DPRINT("operator[]\n");
116  if(k < 0 || k >=nr_lines*nr_pixels)
117  cerr << "DataPatch::operator[](int k), Invalid par k: " << k << endl;
118  return data[k];
119 }
120 
121 
122 /*
123 template <class type>
124 inline DataPatch<type>::DataPatch()
125 {
126  // DPRINT("DataPatch::DataPatch()\n");
127  nr_lines = 0;
128  nr_pixels = 0;
129 
130  actual_start_line = 0;
131  actual_nr_lines = 0;
132 
133  actual_start_pixel = 0;
134  actual_nr_pixels = 0;
135 
136  extern_start_line = 0;
137  extern_start_pixel = 0;
138 
139  data = NULL;
140  data_lines = NULL;
141 
142  }*/
143 template <class type>
144 inline DataPatch<type>::DataPatch (int pixels, int lines)
145 {
146  // DPRINT("DataPatch::DataPatch(%d,%d)\n",pixels,lines);
147  nr_lines = lines;
148  nr_pixels = pixels;
149 
150  actual_start_line = 0;
151  actual_nr_lines = nr_lines;
152 
153  actual_start_pixel = 0;
154  actual_nr_pixels = nr_pixels;
155 
156  extern_start_line = 0;
157  extern_start_pixel = 0;
158 
159  data = NULL;
160  data_lines = NULL;
161 
162  if (nr_lines <= 0)
163  cerr << "DataPatch:invalid nr_lines: " << nr_lines << endl;
164  if (nr_pixels <= 0)
165  cerr << "DataPatch:invalid nr_pixels: " << nr_lines << endl;
166 
167  data = new type[nr_lines*nr_pixels];
168  data_lines = new type *[nr_lines];
169 
170  if (data == NULL)
171  cerr <<"DataPatch: unable to allocate memory: " << sizeof(type)*nr_lines*nr_pixels << endl;
172 
173  if (data_lines == NULL)
174  cerr << "DataPatch: unable to allocate memory: " << sizeof(type)*nr_lines << endl;
175 
176  for(int ii = 0; ii < nr_lines; ii++)
177  data_lines[ii] = data+ii*nr_pixels;
178 }
179 
180 //------------------------------------------------------------------------------
181 
182 template <class type>
184 {
185  // DPRINT("DataPatch::~DataPatch()")
186  if(data != NULL) delete [] data;
187  if(data_lines != NULL) delete [] data_lines;
188 }
189 
190 //------------------------------------------------------------------------------
191 
192 template <class type> inline
193 void DataPatch<type>::set_actual_lines (int act_start_line, int act_nr_lines)
194 {
195  if (act_start_line < 0)
196  cerr << "DataPatch:invalid actual_start_line: " << act_start_line << endl;
197  if (act_nr_lines <= 0)
198  cerr << "DataPatch:invalid actual_nr_lines: " << act_nr_lines << endl;
199  if (act_start_line + act_nr_lines > nr_lines)
200  cerr << "DataPatch:invalid actual_lines: " << act_start_line+act_nr_lines << endl;
201  actual_start_line = act_start_line;
202  actual_nr_lines = act_nr_lines;
203 }
204 
205 //------------------------------------------------------------------------------
206 
207 template <class type> inline
208 void DataPatch<type>::set_actual_pixels (int act_start_pixel, int act_nr_pixels)
209 {
210  if (act_start_pixel < 0)
211  cerr << "DataPatch:invalid actual_start_pixel: " << act_start_pixel << endl;
212  if (act_nr_pixels <= 0)
213  cerr << "DataPatch:invalid actual_nr_pixels: " << act_nr_pixels << endl;
214  if (act_start_pixel + act_nr_pixels > nr_pixels)
215  cerr << "DataPatch:invalid actual_pixels: " << act_start_pixel+act_nr_pixels << endl;
216  actual_start_pixel = act_start_pixel;
217  actual_nr_pixels = act_nr_pixels;
218 }
219 
220 //------------------------------------------------------------------------------
221 
222 template <class type> inline
223 void DataPatch<type>::set_extern_start_line (int start_line)
224 {
225  if (start_line < 0)
226  cerr << "DataPatch:invalid extern_start_line: " << start_line << endl;
227  extern_start_line = start_line;
228 }
229 
230 //------------------------------------------------------------------------------
231 
232 template <class type> inline
233 void DataPatch<type>::set_extern_start_pixel (int start_pixel)
234 {
235  if (start_pixel < 0)
236  cerr << "DataPatch:invalid extern_start_pixel: "<< start_pixel << endl;
237  extern_start_pixel = start_pixel;
238 }
239 
240 //------------------------------------------------------------------------------
241 /*
242 template <class type>
243 inline std::ostream &operator<< (std::ostream &stream, DataPatch<type>& patch)
244 {
245  stream << "nr_lines " << patch.nr_lines << std::endl;
246  stream << "nr_pixels " << patch.nr_pixels << std::endl;
247  stream << "actual_start_line " << patch.actual_start_line << std::endl;
248  stream << "actual_start_pixel " << patch.actual_start_pixel << std::endl;
249  stream << "actual_nr_lines " << patch.actual_nr_lines << std::endl;
250  stream << "actual_nr_pixels " << patch.actual_nr_pixels << std::endl;
251  return stream;
252 }
253 */
254 //------------------------------------------------------------------------------
255 template <class type> inline void DataPatch<type>::dump (char *filename)
256 
257 {
258  FILE *fp;
259  if (!(fp = fopen(filename, "w"))) {
260  cerr << "DataPatch::dump: error on opening: " << filename << endl;
261  return;
262  }
263 
264  // dump the content to file :
265  for (int line = actual_start_line; line < actual_nr_lines; line++) {
266  for (int pixel = actual_start_pixel; pixel < actual_nr_pixels; pixel++)
267  if (fwrite ((void*)&data_lines [line][pixel], sizeof(type), 1, fp) != 1) {
268  cerr << "DataPatch::dump: error on writing: " << filename << endl;
269  return;
270  }
271  }
272  cerr << "DataPatch::dump: number of pixels: " << actual_nr_pixels << endl;
273 }
274 //------------------------------------------------------------------------------
275 template <class type> inline void DataPatch<type>::write (char *filename, int append)
276 
277 {
278  FILE *fp;
279  if(append){
280  if (!(fp = fopen(filename, "a"))) {
281  cerr << "DataPatch::write: error on opening: " << filename << endl;
282  return;
283  }
284  }else{
285  if (!(fp = fopen(filename, "w"))) {
286  cerr << "DataPatch::write: error on opening: " << filename << endl;
287  return;
288  }
289  }
290 
291  // write the content to file :
292  for (int line = actual_start_line; line < actual_nr_lines; line++) {
293  for (int pixel = actual_start_pixel; pixel < actual_nr_pixels; pixel++)
294  if (fwrite ((void*)&data_lines [line][pixel], sizeof(type), 1, fp) != 1) {
295  cerr << "DataPatch::write: error on writing: " << filename << endl;
296  return;
297  }
298  }
299  //cerr << "DataPatch::write: number of pixels: " << actual_nr_pixels << endl;
300  fclose(fp);
301 }
302 
303 
304 //------------------------------------------------------------------------------
305 template <class type> inline void DataPatch<type>::dumpall (char *filename)
306 {
307  FILE *fp;
308  if (!(fp = fopen(filename, "w"))) {
309  cerr << "DataPatch::dump: error on opening: " << filename << endl;
310  return;
311  }
312 
313  // dump the content to file :
314  for (int line = 0; line < nr_lines; line++) {
315  for (int pixel = 0; pixel < nr_pixels; pixel++)
316  if (fwrite ((void*)&data_lines [line][pixel], sizeof(type), 1, fp) != 1) {
317  cerr << "DataPatch::dump: error on writing: " << filename << endl;
318  return;
319  }
320  }
321  cerr << "DataPatch::dumpall: number of pixels: " << nr_pixels << endl;
322 }
323 
324 //------------------------------------------------------------------------------
325 // STATISTIC FUNCTIONS
326 //------------------------------------------------------------------------------
327 
328 template<class type> inline type DataPatch<type>::mean(){
329  int cnt = 0;
330  type sum = 0.0;
331  for (int line = actual_start_line; line < actual_start_line + actual_nr_lines; line++) {
332  for (int pixel = actual_start_pixel; pixel < actual_start_pixel+actual_nr_pixels; pixel++) {
333  sum += data_lines [line][pixel];
334  cnt++;
335  }
336  }
337  if (cnt != 0) sum /= cnt;
338  return sum;
339 }
Definition: DataPatch.h:28

Generated for ISCE3.0 by doxygen 1.8.5.