GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GFitsTableByteCol.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GFitsTableByteCol.cpp - FITS table Byte column class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2017 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GFitsTableByteCol.cpp
23  * @brief FITS table byte column class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <string>
32 #include "GException.hpp"
33 #include "GTools.hpp"
34 #include "GFitsCfitsio.hpp"
35 #include "GFitsTableByteCol.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_INSERT "GFitsTableByteCol::insert(int&, int&)"
39 #define G_REMOVE "GFitsTableByteCol::remove(int&, int&)"
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 
47 
48 /*==========================================================================
49  = =
50  = Constructors/destructors =
51  = =
52  ==========================================================================*/
53 
54 /***********************************************************************//**
55  * @brief Constructor
56  ***************************************************************************/
58 {
59  // Initialise class members for clean destruction
60  init_members();
61 
62  // Return
63  return;
64 }
65 
66 
67 /***********************************************************************//**
68  * @brief Constructor
69  *
70  * @param[in] name Name of column.
71  * @param[in] nrows Number of rows in column.
72  * @param[in] size Vector size of column.
73  ***************************************************************************/
74 GFitsTableByteCol::GFitsTableByteCol(const std::string& name,
75  const int& nrows,
76  const int& size) :
77  GFitsTableCol(name, nrows, size, 2)
78 {
79  // Initialise class members for clean destruction
80  init_members();
81 
82  // Return
83  return;
84 }
85 
86 
87 /***********************************************************************//**
88  * @brief Copy constructor
89  *
90  * @param[in] column Table column.
91  ***************************************************************************/
93  GFitsTableCol(column)
94 {
95  // Initialise class members for clean destruction
96  init_members();
97 
98  // Copy members
99  copy_members(column);
100 
101  // Return
102  return;
103 }
104 
105 
106 /***********************************************************************//**
107  * @brief Destructor
108  ***************************************************************************/
110 {
111  // Free members
112  free_members();
113 
114  // Return
115  return;
116 }
117 
118 
119 /*==========================================================================
120  = =
121  = Operators =
122  = =
123  ==========================================================================*/
124 
125 /***********************************************************************//**
126  * @brief Assignment operator
127  *
128  * @param[in] column Table column.
129  * @return Table column.
130  ***************************************************************************/
132 {
133  // Execute only if object is not identical
134  if (this != &column) {
135 
136  // Copy base class members
137  this->GFitsTableCol::operator=(column);
138 
139  // Free members
140  free_members();
141 
142  // Initialise private members for clean destruction
143  init_members();
144 
145  // Copy members
146  copy_members(column);
147 
148  } // endif: object was not identical
149 
150  // Return this object
151  return *this;
152 }
153 
154 
155 /***********************************************************************//**
156  * @brief Column data access operator
157  *
158  * @param[in] row Row of column to access.
159  * @param[in] inx Vector index in column row to access.
160  *
161  * Provides access to data in a column.
162  ***************************************************************************/
163 unsigned char& GFitsTableByteCol::operator()(const int& row, const int& inx)
164 {
165  // If data are not available then load them now
166  if (m_data == NULL) fetch_data();
167 
168  // Return data bin
169  return m_data[offset(row, inx)];
170 }
171 
172 
173 /***********************************************************************//**
174  * @brief Column data access operator (const variant)
175  *
176  * @param[in] row Row of column to access.
177  * @param[in] inx Vector index in column row to access.
178  *
179  * Provides access to data in a column.
180  ***************************************************************************/
181 const unsigned char& GFitsTableByteCol::operator()(const int& row, const int& inx) const
182 {
183  // If data are not available then load them now
184  if (m_data == NULL) fetch_data();
185 
186  // Return data bin
187  return m_data[offset(row, inx)];
188 }
189 
190 
191 /*==========================================================================
192  = =
193  = Public methods =
194  = =
195  ==========================================================================*/
196 
197 /***********************************************************************//**
198  * @brief Clear instance
199  *
200  * This method properly resets the object to an initial state.
201  ***************************************************************************/
203 {
204  // Free class members (base and derived classes, derived class first)
205  free_members();
207 
208  // Initialise members
210  init_members();
211 
212  // Return
213  return;
214 }
215 
216 
217 /***********************************************************************//**
218  * @brief Clone column
219  ***************************************************************************/
221 {
222  return new GFitsTableByteCol(*this);
223 }
224 
225 
226 /***********************************************************************//**
227  * @brief Get string value
228  *
229  * @param[in] row Table row.
230  * @param[in] inx Table column vector index.
231  *
232  * Returns value of specified row and vector index as string.
233  ***************************************************************************/
234 std::string GFitsTableByteCol::string(const int& row, const int& inx) const
235 {
236  // If data are not available then load them now
237  if (m_data == NULL) fetch_data();
238 
239  // Return value
240  return (gammalib::str(m_data[offset(row,inx)]));
241 }
242 
243 
244 /***********************************************************************//**
245  * @brief Get double precision value
246  *
247  * @param[in] row Table row.
248  * @param[in] inx Table column vector index.
249  *
250  * Returns value of specified row and vector index as double precision.
251  ***************************************************************************/
252 double GFitsTableByteCol::real(const int& row, const int& inx) const
253 {
254  // If data are not available then load them now
255  if (m_data == NULL) fetch_data();
256 
257  // Convert unsigned char into double
258  double value = (double)m_data[offset(row,inx)];
259 
260  // Return value
261  return value;
262 }
263 
264 
265 /***********************************************************************//**
266  * @brief Get integer value
267  *
268  * @param[in] row Table row.
269  * @param[in] inx Table column vector index.
270  *
271  * Returns value of specified row and vector index as integer.
272  ***************************************************************************/
273 int GFitsTableByteCol::integer(const int& row, const int& inx) const
274 {
275  // If data are not available then load them now
276  if (m_data == NULL) fetch_data();
277 
278  // Convert unsigned char into int
279  int value = (int)m_data[offset(row,inx)];
280 
281  // Return value
282  return value;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Insert rows in column
288  *
289  * @param[in] row Row after which rows should be inserted (0=first row).
290  * @param[in] nrows Number of rows to be inserted.
291  *
292  * @exception GException::fits_invalid_row
293  * Specified row is invalid.
294  *
295  * Inserts rows into a FITS table. This implies that the column will be
296  * loaded into memory.
297  ***************************************************************************/
298 void GFitsTableByteCol::insert(const int& row, const int& nrows)
299 {
300  // Make sure that row is valid
301  if (row < 0 || row > m_length) {
302  throw GException::fits_invalid_row(G_INSERT, row, m_length);
303  }
304 
305  // Continue only if there are rows to be inserted
306  if (nrows > 0) {
307 
308  // If we have no rows yet then simply set the length to the
309  // number of rows to be inserted
310  if (m_length == 0) {
311  m_length = nrows;
313  alloc_data();
314  init_data();
315  }
316 
317  // ... otherwise fetch data, allocate new data and copy over
318  // the existing items
319  else {
320 
321  // If data are not available then load them now
322  if (m_data == NULL) fetch_data();
323 
324  // Compute new column length
325  int length = m_length + nrows;
326 
327  // Calculate size of new memory
328  m_size = m_number * length;
329 
330  // Allocate new data to hold the column
331  unsigned char* new_data = new unsigned char[m_size];
332 
333  // Compute the number of elements before the insertion point,
334  // the number of elements that get inserted, and the total
335  // number of elements after the insertion point
336  int n_before = m_number * row;
337  int n_insert = m_number * nrows;
338  int n_after = m_number * (m_length - row);
339 
340  // Copy and initialise data
341  unsigned char* src = m_data;
342  unsigned char* dst = new_data;
343  for (int i = 0; i < n_before; ++i) {
344  *dst++ = *src++;
345  }
346  for (int i = 0; i < n_insert; ++i) {
347  *dst++ = 0;
348  }
349  for (int i = 0; i < n_after; ++i) {
350  *dst++ = *src++;
351  }
352 
353  // Free old data
354  if (m_data != NULL) delete [] m_data;
355 
356  // Set pointer to new data and store length
357  m_data = new_data;
358  m_length = length;
359 
360  } // endelse: there were already data
361 
362  } // endfor: there were rows to be inserted
363 
364  // Return
365  return;
366 }
367 
368 
369 /***********************************************************************//**
370  * @brief Remove rows from column
371  *
372  * @param[in] row Row after which rows should be removed (0=first row).
373  * @param[in] nrows Number of rows to be removed.
374  *
375  * @exception GException::fits_invalid_row
376  * Specified row is invalid.
377  * @exception GException::fits_invalid_nrows
378  * Invalid number of rows specified.
379  *
380  * This method removes rows from a FITS table. This implies that the column
381  * will be loaded into memory.
382  ***************************************************************************/
383 void GFitsTableByteCol::remove(const int& row, const int& nrows)
384 {
385  // Make sure that row is valid
386  if (row < 0 || row >= m_length) {
388  }
389 
390  // Make sure that we don't remove beyond the limit
391  if (nrows < 0 || nrows > m_length-row) {
393  }
394 
395  // Continue only if there are rows to be removed
396  if (nrows > 0) {
397 
398  // If data are not available then load them now
399  if (m_data == NULL) fetch_data();
400 
401  // Compute new column length
402  int length = m_length - nrows;
403 
404  // Calculate size of new memory
405  m_size = m_number * length;
406 
407  // If we have rows remaining then allocate new data to hold
408  // the column
409  if (m_size > 0) {
410 
411  // Allocate new data to hold the column
412  unsigned char* new_data = new unsigned char[m_size];
413 
414  // Compute the number of elements before the removal point,
415  // the number of elements that get removed, and the total
416  // number of elements after the removal point
417  int n_before = m_number * row;
418  int n_remove = m_number * nrows;
419  int n_after = m_number * (length - row);
420 
421  // Copy data
422  unsigned char* src = m_data;
423  unsigned char* dst = new_data;
424  for (int i = 0; i < n_before; ++i) {
425  *dst++ = *src++;
426  }
427  src += n_remove;
428  for (int i = 0; i < n_after; ++i) {
429  *dst++ = *src++;
430  }
431 
432  // Free old data
433  if (m_data != NULL) delete [] m_data;
434 
435  // Set pointer to new data and store length
436  m_data = new_data;
437  m_length = length;
438 
439  } // endif: there are still elements after removal
440 
441  // ... otherwise just remove all data
442  else {
443 
444  // Free old data
445  if (m_data != NULL) delete [] m_data;
446 
447  // Set pointer to new data and store length
448  m_data = NULL;
449  m_length = length;
450  }
451 
452  } // endfor: there were rows to be removed
453 
454  // Return
455  return;
456 }
457 
458 
459 /***********************************************************************//**
460  * @brief Set nul value
461  *
462  * @param[in] value Nul value.
463  *
464  * @todo To correctly reflect the nul value in the data, the column should
465  * be reloaded. However, the column may have been changed, so in principle
466  * saving is needed. However, we may not want to store the data, hence saving
467  * is also not desired. We thus have to develop a method to update the
468  * column information for a new nul value in place ...
469  ***************************************************************************/
470 void GFitsTableByteCol::nulval(const unsigned char* value)
471 {
472  // Allocate nul value
473  alloc_nulval(value);
474 
475  // Update column
476 // if (m_data != NULL) {
477 // save();
478 // load();
479 // }
480 
481  // Return
482  return;
483 }
484 
485 
486 /*==========================================================================
487  = =
488  = Private methods =
489  = =
490  ==========================================================================*/
491 
492 /***********************************************************************//**
493  * @brief Initialise class members
494  ***************************************************************************/
496 {
497  // Initialise members
498  m_type = __TBYTE;
499  m_data = NULL;
500  m_nulval = NULL;
501 
502  // Return
503  return;
504 }
505 
506 
507 /***********************************************************************//**
508  * @brief Copy class members
509  *
510  * @param[in] column Column.
511  *
512  * Sets the content of the vector column by copying from another column.
513  * If the code is compiled with the small memory option, and if the source
514  * column has not yet been loaded, then we only load the column temporarily
515  * for copying purposes and release it again once copying is finished.
516  ***************************************************************************/
518 {
519  // Fetch data if necessary
520  bool not_loaded = (!column.is_loaded());
521  if (not_loaded) {
522  column.fetch_data();
523  }
524 
525  // Copy attributes
526  m_type = column.m_type;
527  m_size = column.m_size;
528  m_varlen = column.m_varlen;
529  m_rowstart = column.m_rowstart;
530 
531  // Copy column data
532  if (column.m_data != NULL && m_size > 0) {
533  alloc_data();
534  for (int i = 0; i < m_size; ++i) {
535  m_data[i] = column.m_data[i];
536  }
537  }
538 
539  // Copy NULL value
540  alloc_nulval(column.m_nulval);
541 
542  // Small memory option: release column if it was fetch above
543  #if defined(G_SMALL_MEMORY)
544  if (not_loaded) {
545  const_cast<GFitsTableByteCol*>(&column)->release_data();
546  }
547  #endif
548 
549  // Return
550  return;
551 }
552 
553 
554 /***********************************************************************//**
555  * @brief Delete class members
556  ***************************************************************************/
558 {
559  // Free memory
560  if (m_data != NULL) delete [] m_data;
561  if (m_nulval != NULL) delete m_nulval;
562 
563  // Mark memory as freed
564  m_data = NULL;
565  m_nulval = NULL;
566 
567  // Return
568  return;
569 }
570 
571 
572 /***********************************************************************//**
573  * @brief Returns format string of ASCII table
574  ***************************************************************************/
575 std::string GFitsTableByteCol::ascii_format(void) const
576 {
577  // Initialize format string
578  std::string format;
579 
580  // Set type code
581  format.append("I4");
582 
583  // Set width
584  //format.append(gammalib::str(m_width));
585 
586  // Return format
587  return format;
588 }
589 
590 
591 /***********************************************************************//**
592  * @brief Allocates column data
593  ***************************************************************************/
595 {
596  // Free any existing memory
597  if (m_data != NULL) delete [] m_data;
598 
599  // Mark pointer as free
600  m_data = NULL;
601 
602  // Allocate new data
603  if (m_size > 0) {
604  m_data = new unsigned char[m_size];
605  }
606 
607  // Return
608  return;
609 }
610 
611 
612 /***********************************************************************//**
613  * @brief Fetch column data
614  ***************************************************************************/
616 {
617  // Load column (circumvent const correctness)
618  const_cast<GFitsTableByteCol*>(this)->load_column();
619 
620  // Return
621  return;
622 }
623 
624 
625 /***********************************************************************//**
626  * @brief Resize column data
627  *
628  * @param[in] index Start index.
629  * @param[in] number Number of elements to add/remove.
630  *
631  * Adds or removes elements from specified index on. Adding is done if
632  * @p number is a positive number, removing if @p number is negative.
633  * Note that the method does not change the validity of the arguments.
634  * This needs to be done by the client.
635  ***************************************************************************/
636 void GFitsTableByteCol::resize_data(const int& index, const int& number)
637 {
638  // Continue only if number of elements is not zero
639  if (number != 0) {
640 
641  // If data are not available then load them now
642  if (m_data == NULL) fetch_data();
643 
644  // If elements should be removed then do not allocate new memory
645  // but just move elements forward and change the logical size of
646  // memory. Only if all elements should be removed the memory is
647  // released.
648  if (number < 0) {
649  int left = index - number;
650  unsigned char* dst = m_data + index;
651  unsigned char* src = m_data + left;
652  int num = m_size - left;
653  for (int i = 0; i < num; ++i) {
654  *dst++ = *src++;
655  }
656  m_size += number;
657  if (m_size < 1) {
658  release_data();
659  }
660  }
661 
662  // If elements should be added then allocate new memory, copy over
663  // the old data and initialise the new elements
664  else {
665  int left = m_size - index;
666  m_size += number;
667  unsigned char* new_data = new unsigned char[m_size];
668  unsigned char* dst = new_data;
669  unsigned char* src = m_data;
670  for (int i = 0; i < index; ++i) {
671  *dst++ = *src++;
672  }
673  for (int i = 0; i < number; ++i) {
674  *dst++ = 0;
675  }
676  for (int i = 0; i < left; ++i) {
677  *dst++ = *src++;
678  }
679  if (m_data != NULL) delete [] m_data;
680  m_data = new_data;
681  }
682 
683  } // endif: number was non-zero
684 
685  // Return
686  return;
687 }
688 
689 
690 /***********************************************************************//**
691  * @brief Release column data
692  ***************************************************************************/
694 {
695  // Free any existing memory
696  if (m_data != NULL) delete [] m_data;
697 
698  // Mark pointer as free and reset loaded vector size
699  m_data = NULL;
700  m_size = 0;
701 
702  // Return
703  return;
704 }
705 
706 
707 /***********************************************************************//**
708  * @brief Allocates null value
709  ***************************************************************************/
710 void GFitsTableByteCol::alloc_nulval(const unsigned char* value)
711 {
712  // Free any existing memory
713  if (m_nulval != NULL) delete m_nulval;
714 
715  // Mark pointer as free
716  m_nulval = NULL;
717 
718  // If we have valid value, allocate and set nul value
719  if (value != NULL) {
720  m_nulval = new unsigned char;
721  *m_nulval = *value;
722  }
723 
724  // Return
725  return;
726 }
727 
728 
729 /***********************************************************************//**
730  * @brief Initialise column data
731  ***************************************************************************/
733 {
734  // Initialise data if they exist
735  if (m_data != NULL) {
736  for (int i = 0; i < m_size; ++i) {
737  m_data[i] = 0;
738  }
739  }
740 
741  // Return
742  return;
743 }
virtual GFitsTableByteCol * clone(void) const
Clone column.
int m_type
Column type.
void free_members(void)
Delete class members.
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition: GTools.cpp:1046
virtual void init_data(void)
Initialise column data.
virtual void clear(void)
Clear instance.
int m_number
Number of elements in column.
void free_members(void)
Delete class members.
GFitsTableCol & operator=(const GFitsTableCol &column)
Assignment operator.
virtual void insert(const int &row, const int &nrows)
Insert rows in column.
FITS table Byte column.
int m_varlen
Maximum number of elements in variable-length.
virtual int integer(const int &row, const int &col=0) const
Get integer value.
Gammalib tools definition.
unsigned char * m_nulval
NULL value.
unsigned char * m_data
Data vector.
virtual void load_column(void)
Load table column from FITS file.
#define __TBYTE
virtual void release_data(void)
Release column data.
const int & number(void) const
Returns number of elements in column.
void alloc_nulval(const unsigned char *value)
Allocates null value.
virtual ~GFitsTableByteCol(void)
Destructor.
void init_members(void)
Initialise class members.
CFITSIO interface header.
Abstract interface for FITS table column.
std::vector< int > m_rowstart
Start index of each row.
std::string left(const std::string &s, const int &n, const char &c= ' ')
Left justify string to achieve a length of n characters.
Definition: GTools.cpp:949
unsigned char & operator()(const int &row, const int &inx=0)
Column data access operator.
void init_members(void)
Initialise class members.
virtual void alloc_data(void)
Allocates column data.
#define G_INSERT
#define G_REMOVE
virtual void resize_data(const int &index, const int &number)
Resize column data.
GFitsTableByteCol & operator=(const GFitsTableByteCol &column)
Assignment operator.
int m_length
Length of column (number of rows)
virtual double real(const int &row, const int &col=0) const
Get double precision value.
virtual std::string ascii_format(void) const
Returns format string of ASCII table.
int m_size
Size of allocated data area (0 if not loaded)
GFitsTableByteCol(void)
Constructor.
virtual int offset(const int &row, const int &inx) const
Compute offset of column element in memory.
virtual bool is_loaded(void) const
Checks if column has been loaded.
Exception handler interface definition.
unsigned char * nulval(void)
Returns pointer to nul value.
virtual void remove(const int &row, const int &nrows)
Remove rows from column.
FITS table Byte column class interface definition.
void copy_members(const GFitsTableByteCol &column)
Copy class members.
virtual std::string string(const int &row, const int &col=0) const
Get string value.
const int & nrows(void) const
Returns number of rows in column.
virtual void fetch_data(void) const
Fetch column data.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413