isce3 0.25.0
Loading...
Searching...
No Matches
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
25using namespace std;
26
27template <class type>
28class 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
98template <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
107template <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
114template <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/*
123template <class type>
124inline 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 }*/
143template <class type>
144inline 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
182template <class type>
183inline DataPatch<type>::~DataPatch()
184{
185 // DPRINT("DataPatch::~DataPatch()")
186 if(data != NULL) delete [] data;
187 if(data_lines != NULL) delete [] data_lines;
188}
189
190//------------------------------------------------------------------------------
191
192template <class type> inline
193void 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
207template <class type> inline
208void 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
222template <class type> inline
223void 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
232template <class type> inline
233void 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/*
242template <class type>
243inline 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//------------------------------------------------------------------------------
255template <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 goto cleanup;
270 }
271 }
272 cerr << "DataPatch::dump: number of pixels: " << actual_nr_pixels << endl;
273cleanup:
274 fclose(fp);
275}
276//------------------------------------------------------------------------------
277template <class type> inline void DataPatch<type>::write (char *filename, int append)
278
279{
280 FILE *fp;
281 if(append){
282 if (!(fp = fopen(filename, "a"))) {
283 cerr << "DataPatch::write: error on opening: " << filename << endl;
284 return;
285 }
286 }else{
287 if (!(fp = fopen(filename, "w"))) {
288 cerr << "DataPatch::write: error on opening: " << filename << endl;
289 return;
290 }
291 }
292
293 // write the content to file :
294 for (int line = actual_start_line; line < actual_nr_lines; line++) {
295 for (int pixel = actual_start_pixel; pixel < actual_nr_pixels; pixel++)
296 if (fwrite ((void*)&data_lines [line][pixel], sizeof(type), 1, fp) != 1) {
297 cerr << "DataPatch::write: error on writing: " << filename << endl;
298 goto cleanup;
299 }
300 }
301 cerr << "DataPatch::write: number of pixels: " << actual_nr_pixels << endl;
302cleanup:
303 fclose(fp);
304}
305
306
307//------------------------------------------------------------------------------
308template <class type> inline void DataPatch<type>::dumpall (char *filename)
309{
310 FILE *fp;
311 if (!(fp = fopen(filename, "w"))) {
312 cerr << "DataPatch::dump: error on opening: " << filename << endl;
313 return;
314 }
315
316 // dump the content to file :
317 for (int line = 0; line < nr_lines; line++) {
318 for (int pixel = 0; pixel < nr_pixels; pixel++)
319 if (fwrite ((void*)&data_lines [line][pixel], sizeof(type), 1, fp) != 1) {
320 cerr << "DataPatch::dump: error on writing: " << filename << endl;
321 goto cleanup;
322 }
323 }
324 cerr << "DataPatch::dumpall: number of pixels: " << nr_pixels << endl;
325cleanup:
326 fclose(fp);
327}
328
329//------------------------------------------------------------------------------
330// STATISTIC FUNCTIONS
331//------------------------------------------------------------------------------
332
333template<class type> inline type DataPatch<type>::mean(){
334 int cnt = 0;
335 type sum = 0.0;
336 for (int line = actual_start_line; line < actual_start_line + actual_nr_lines; line++) {
337 for (int pixel = actual_start_pixel; pixel < actual_start_pixel+actual_nr_pixels; pixel++) {
338 sum += data_lines [line][pixel];
339 cnt++;
340 }
341 }
342 if (cnt != 0) sum /= cnt;
343 return sum;
344}
Definition DataPatch.h:28

Generated for ISCE3.0 by doxygen 1.13.2.