ROOT logo
// $Id: THealPixCube.cxx,v 1.5 2009/01/12 20:51:25 oxon Exp $
// Author: Akira Okumura 2008/07/11

/*****************************************************************************
   Copyright (C) 2008-, Akira Okumura
   All rights reserved.
******************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// THealPixCube                                                         //
//                                                                      //
// Cubic HEALPix Image class                                            //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "THealPixCube.h"
#include "THealUtil.h"

ClassImp(THealPixCube)

//______________________________________________________________________________
THealPixCube::THealPixCube(): TNamed()
{
  fN       = 0;
  fHeals   = 0;
  fMaximum = -1111;
  fMinimum = -1111;
  fWaxis.SetName("waxis");
  fWaxis.SetParent(this);
}

//______________________________________________________________________________
THealPixCube::THealPixCube(const char* name, const char* title,
			   Int_t nbins, Double_t wlow, Double_t wup)
  : TNamed(name, title)
{
  fN = (nbins <= 0) ? 1 : nbins;
  fWaxis.Set(fN, wlow, wup);
  fHeals = new THealPix*[fN];
  for(Int_t i = 0; i < fN; i++){
    fHeals[i] = 0;
  } // i
  fMaximum = -1111;
  fMinimum = -1111;
}

//______________________________________________________________________________
THealPixCube::~THealPixCube()
{
  for(Int_t i = 0; i < fN; i++){
    SafeDelete(fHeals[i]);
  } // i
  delete [] fHeals;
  fHeals = 0;
}

//______________________________________________________________________________
THealPix* THealPixCube::GetSlice(Int_t n) const
{
  if(n < 0 || n >= fN){
    return 0;
  } // if
  
  return fHeals[n];
}

//______________________________________________________________________________
THealPix* THealPixCube::operator[](Int_t n) const
{
  if(n < 0 || n >= fN){
    return 0;
  } // if

  return fHeals[n];
}

ClassImp(THealPixCubeF)

//______________________________________________________________________________
THealPixCubeF::THealPixCubeF(): THealPixCube()
{
}

//______________________________________________________________________________
THealPixCubeF::~THealPixCubeF()
{
}

//______________________________________________________________________________
THealPixCubeF::THealPixCubeF(const char* name, const char* title, Int_t order,
			     Int_t nbins, Double_t wlow, Double_t wup,
			     Bool_t nested)
  : THealPixCube(name, title, nbins, wlow, wup)
{
  for(Int_t i = 0; i < fN; i++){
    fHeals[i] = new THealPixF(Form("%s%d", name, i), title, order, nested);
  } // i
}

//______________________________________________________________________________
Int_t THealPixCubeF::GetType() const
{
  // Return type of content as CFITSIO type 'TFLOAT'
  return TFLOAT;
}

//_____________________________________________________________________________
std::string THealPixCubeF::GetTypeString() const
{
  // Return type of content as FITS type string 'E'
  return "E";
}

//______________________________________________________________________________
THealPixCubeF* THealPixCubeF::ReadFits(const char* fname, const char* colname)
{
  THealPix::HealHeader_t head;
  fitsfile* fptr = 0;

  if(!THealPix::ReadFitsHeader(&fptr, fname, colname, head)){
    return 0;
  } // if

  THealPixCubeF* hpcf;
  if(head.nrows*head.repeat == head.npix){ // CMB-like FITS
    hpcf = new THealPixCubeF(fname, colname, head.order, 1, 0, 0, head.isNested);
  } else if(head.nrows == head.npix){ // HEALPix Cube
    hpcf = new THealPixCubeF(fname, colname, head.order, head.repeat, 0, 0, head.isNested);
  } else {
    return 0;
  } // if

  for(Int_t i = 0; i < hpcf->GetN(); i++){
    (*hpcf)[i]->SetUnit(head.tunit);
  } // i

  Int_t status = 0;

  if(head.nrows*head.repeat == head.npix){ // CMB-like FITS
    for(Int_t i = 0; i < head.nrows; i++){
      fits_read_col(fptr, TFLOAT, head.colnum, i + 1, 1, head.repeat, 0,
		    &(((THealPixF*)((*hpcf)[0]))->GetArray()[i*head.repeat]),
		    0, &status);
      if(!THealUtil::FitsReportError(status)){
	delete hpcf;
	return 0;
      } // if
    } // i
  } else if(head.nrows == head.npix){ // HEALPix Cube
    for(Int_t i = 0; i < head.nrows; i++){
      for(Int_t j = 0; j < hpcf->GetN(); j++){
	fits_read_col(fptr, TFLOAT, head.colnum, i + 1, j + 1, 1, 0,
		      &(((THealPixF*)((*hpcf)[j]))->GetArray()[i]), 0, &status);
	if(!THealUtil::FitsReportError(status)){
	  delete hpcf;
	  return 0;
	} // if
      } // j
    } // i
  } // if

  for(Int_t i = 0; i < hpcf->GetN(); i++){
    Double_t entries = 0;
    for(Int_t j = 0; j < (*hpcf)[i]->GetNpix(); j++){
      if((*hpcf)[i]->GetBinContent(j) != 0){
	entries++;
      } // if
    } // j
    (*hpcf)[i]->SetEntries(entries);
  } // i

  return hpcf;
}

ClassImp(THealPixCubeD)

//______________________________________________________________________________
THealPixCubeD::THealPixCubeD(): THealPixCube()
{
}

//______________________________________________________________________________
THealPixCubeD::~THealPixCubeD()
{
}

//______________________________________________________________________________
THealPixCubeD::THealPixCubeD(const char* name, const char* title, Int_t order,
			     Int_t nbins, Double_t wlow, Double_t wup,
			     Bool_t nested)
  : THealPixCube(name, title, nbins, wlow, wup)
{
  for(Int_t i = 0; i < fN; i++){
    fHeals[i] = new THealPixD(Form("%s%d", name, i), title, order, nested);
  } // i
}

//______________________________________________________________________________
Int_t THealPixCubeD::GetType() const
{
  // Return type of content as CFITSIO type 'TDOUBLE'
  return TDOUBLE;
}

//_____________________________________________________________________________
std::string THealPixCubeD::GetTypeString() const
{
  // Return type of content as FITS type string 'D'
  return "D";
}

//______________________________________________________________________________
THealPixCubeD* THealPixCubeD::ReadFits(const char* fname, const char* colname)
{
  THealPix::HealHeader_t head;
  fitsfile* fptr = 0;

  if(!THealPix::ReadFitsHeader(&fptr, fname, colname, head)){
    return 0;
  } // if

  THealPixCubeD* hpcd;
  if(head.nrows*head.repeat == head.npix){ // CMB-like FITS
    hpcd = new THealPixCubeD(fname, colname, head.order, 1, 0, 0, head.isNested);
  } else if(head.nrows == head.npix){ // HEALPix Cube
    hpcd = new THealPixCubeD(fname, colname, head.order, head.repeat, 0, 0, head.isNested);
  } else {
    return 0;
  } // if

  for(Int_t i = 0; i < hpcd->GetN(); i++){
    (*hpcd)[i]->SetUnit(head.tunit);
  } // i

  Int_t status = 0;

  if(head.nrows*head.repeat == head.npix){ // CMB-like FITS
    for(Int_t i = 0; i < head.nrows; i++){
      fits_read_col(fptr, TDOUBLE, head.colnum, i + 1, 1, head.repeat, 0,
		    &(((THealPixD*)((*hpcd)[0]))->GetArray()[i*head.repeat]),
		    0, &status);
      if(!THealUtil::FitsReportError(status)){
	delete hpcd;
	return 0;
      } // if
    } // i
  } else if(head.nrows == head.npix){ // HEALPix Cube
    for(Int_t i = 0; i < head.nrows; i++){
      for(Int_t j = 0; j < hpcd->GetN(); j++){
	fits_read_col(fptr, TDOUBLE, head.colnum, i + 1, j + 1, 1, 0,
		      &(((THealPixD*)((*hpcd)[j]))->GetArray()[i]), 0, &status);
	if(!THealUtil::FitsReportError(status)){
	  delete hpcd;
	  return 0;
	} // if
      } // j
    } // i
  } // if

  for(Int_t i = 0; i < hpcd->GetN(); i++){
    Double_t entries = 0;
    for(Int_t j = 0; j < (*hpcd)[i]->GetNpix(); j++){
      if((*hpcd)[i]->GetBinContent(j) != 0){
	entries++;
      } // if
    } // j
    (*hpcd)[i]->SetEntries(entries);
  } // i

  return hpcd;
}
 THealPixCube.cxx:1
 THealPixCube.cxx:2
 THealPixCube.cxx:3
 THealPixCube.cxx:4
 THealPixCube.cxx:5
 THealPixCube.cxx:6
 THealPixCube.cxx:7
 THealPixCube.cxx:8
 THealPixCube.cxx:9
 THealPixCube.cxx:10
 THealPixCube.cxx:11
 THealPixCube.cxx:12
 THealPixCube.cxx:13
 THealPixCube.cxx:14
 THealPixCube.cxx:15
 THealPixCube.cxx:16
 THealPixCube.cxx:17
 THealPixCube.cxx:18
 THealPixCube.cxx:19
 THealPixCube.cxx:20
 THealPixCube.cxx:21
 THealPixCube.cxx:22
 THealPixCube.cxx:23
 THealPixCube.cxx:24
 THealPixCube.cxx:25
 THealPixCube.cxx:26
 THealPixCube.cxx:27
 THealPixCube.cxx:28
 THealPixCube.cxx:29
 THealPixCube.cxx:30
 THealPixCube.cxx:31
 THealPixCube.cxx:32
 THealPixCube.cxx:33
 THealPixCube.cxx:34
 THealPixCube.cxx:35
 THealPixCube.cxx:36
 THealPixCube.cxx:37
 THealPixCube.cxx:38
 THealPixCube.cxx:39
 THealPixCube.cxx:40
 THealPixCube.cxx:41
 THealPixCube.cxx:42
 THealPixCube.cxx:43
 THealPixCube.cxx:44
 THealPixCube.cxx:45
 THealPixCube.cxx:46
 THealPixCube.cxx:47
 THealPixCube.cxx:48
 THealPixCube.cxx:49
 THealPixCube.cxx:50
 THealPixCube.cxx:51
 THealPixCube.cxx:52
 THealPixCube.cxx:53
 THealPixCube.cxx:54
 THealPixCube.cxx:55
 THealPixCube.cxx:56
 THealPixCube.cxx:57
 THealPixCube.cxx:58
 THealPixCube.cxx:59
 THealPixCube.cxx:60
 THealPixCube.cxx:61
 THealPixCube.cxx:62
 THealPixCube.cxx:63
 THealPixCube.cxx:64
 THealPixCube.cxx:65
 THealPixCube.cxx:66
 THealPixCube.cxx:67
 THealPixCube.cxx:68
 THealPixCube.cxx:69
 THealPixCube.cxx:70
 THealPixCube.cxx:71
 THealPixCube.cxx:72
 THealPixCube.cxx:73
 THealPixCube.cxx:74
 THealPixCube.cxx:75
 THealPixCube.cxx:76
 THealPixCube.cxx:77
 THealPixCube.cxx:78
 THealPixCube.cxx:79
 THealPixCube.cxx:80
 THealPixCube.cxx:81
 THealPixCube.cxx:82
 THealPixCube.cxx:83
 THealPixCube.cxx:84
 THealPixCube.cxx:85
 THealPixCube.cxx:86
 THealPixCube.cxx:87
 THealPixCube.cxx:88
 THealPixCube.cxx:89
 THealPixCube.cxx:90
 THealPixCube.cxx:91
 THealPixCube.cxx:92
 THealPixCube.cxx:93
 THealPixCube.cxx:94
 THealPixCube.cxx:95
 THealPixCube.cxx:96
 THealPixCube.cxx:97
 THealPixCube.cxx:98
 THealPixCube.cxx:99
 THealPixCube.cxx:100
 THealPixCube.cxx:101
 THealPixCube.cxx:102
 THealPixCube.cxx:103
 THealPixCube.cxx:104
 THealPixCube.cxx:105
 THealPixCube.cxx:106
 THealPixCube.cxx:107
 THealPixCube.cxx:108
 THealPixCube.cxx:109
 THealPixCube.cxx:110
 THealPixCube.cxx:111
 THealPixCube.cxx:112
 THealPixCube.cxx:113
 THealPixCube.cxx:114
 THealPixCube.cxx:115
 THealPixCube.cxx:116
 THealPixCube.cxx:117
 THealPixCube.cxx:118
 THealPixCube.cxx:119
 THealPixCube.cxx:120
 THealPixCube.cxx:121
 THealPixCube.cxx:122
 THealPixCube.cxx:123
 THealPixCube.cxx:124
 THealPixCube.cxx:125
 THealPixCube.cxx:126
 THealPixCube.cxx:127
 THealPixCube.cxx:128
 THealPixCube.cxx:129
 THealPixCube.cxx:130
 THealPixCube.cxx:131
 THealPixCube.cxx:132
 THealPixCube.cxx:133
 THealPixCube.cxx:134
 THealPixCube.cxx:135
 THealPixCube.cxx:136
 THealPixCube.cxx:137
 THealPixCube.cxx:138
 THealPixCube.cxx:139
 THealPixCube.cxx:140
 THealPixCube.cxx:141
 THealPixCube.cxx:142
 THealPixCube.cxx:143
 THealPixCube.cxx:144
 THealPixCube.cxx:145
 THealPixCube.cxx:146
 THealPixCube.cxx:147
 THealPixCube.cxx:148
 THealPixCube.cxx:149
 THealPixCube.cxx:150
 THealPixCube.cxx:151
 THealPixCube.cxx:152
 THealPixCube.cxx:153
 THealPixCube.cxx:154
 THealPixCube.cxx:155
 THealPixCube.cxx:156
 THealPixCube.cxx:157
 THealPixCube.cxx:158
 THealPixCube.cxx:159
 THealPixCube.cxx:160
 THealPixCube.cxx:161
 THealPixCube.cxx:162
 THealPixCube.cxx:163
 THealPixCube.cxx:164
 THealPixCube.cxx:165
 THealPixCube.cxx:166
 THealPixCube.cxx:167
 THealPixCube.cxx:168
 THealPixCube.cxx:169
 THealPixCube.cxx:170
 THealPixCube.cxx:171
 THealPixCube.cxx:172
 THealPixCube.cxx:173
 THealPixCube.cxx:174
 THealPixCube.cxx:175
 THealPixCube.cxx:176
 THealPixCube.cxx:177
 THealPixCube.cxx:178
 THealPixCube.cxx:179
 THealPixCube.cxx:180
 THealPixCube.cxx:181
 THealPixCube.cxx:182
 THealPixCube.cxx:183
 THealPixCube.cxx:184
 THealPixCube.cxx:185
 THealPixCube.cxx:186
 THealPixCube.cxx:187
 THealPixCube.cxx:188
 THealPixCube.cxx:189
 THealPixCube.cxx:190
 THealPixCube.cxx:191
 THealPixCube.cxx:192
 THealPixCube.cxx:193
 THealPixCube.cxx:194
 THealPixCube.cxx:195
 THealPixCube.cxx:196
 THealPixCube.cxx:197
 THealPixCube.cxx:198
 THealPixCube.cxx:199
 THealPixCube.cxx:200
 THealPixCube.cxx:201
 THealPixCube.cxx:202
 THealPixCube.cxx:203
 THealPixCube.cxx:204
 THealPixCube.cxx:205
 THealPixCube.cxx:206
 THealPixCube.cxx:207
 THealPixCube.cxx:208
 THealPixCube.cxx:209
 THealPixCube.cxx:210
 THealPixCube.cxx:211
 THealPixCube.cxx:212
 THealPixCube.cxx:213
 THealPixCube.cxx:214
 THealPixCube.cxx:215
 THealPixCube.cxx:216
 THealPixCube.cxx:217
 THealPixCube.cxx:218
 THealPixCube.cxx:219
 THealPixCube.cxx:220
 THealPixCube.cxx:221
 THealPixCube.cxx:222
 THealPixCube.cxx:223
 THealPixCube.cxx:224
 THealPixCube.cxx:225
 THealPixCube.cxx:226
 THealPixCube.cxx:227
 THealPixCube.cxx:228
 THealPixCube.cxx:229
 THealPixCube.cxx:230
 THealPixCube.cxx:231
 THealPixCube.cxx:232
 THealPixCube.cxx:233
 THealPixCube.cxx:234
 THealPixCube.cxx:235
 THealPixCube.cxx:236
 THealPixCube.cxx:237
 THealPixCube.cxx:238
 THealPixCube.cxx:239
 THealPixCube.cxx:240
 THealPixCube.cxx:241
 THealPixCube.cxx:242
 THealPixCube.cxx:243
 THealPixCube.cxx:244
 THealPixCube.cxx:245
 THealPixCube.cxx:246
 THealPixCube.cxx:247
 THealPixCube.cxx:248
 THealPixCube.cxx:249
 THealPixCube.cxx:250
 THealPixCube.cxx:251
 THealPixCube.cxx:252
 THealPixCube.cxx:253
 THealPixCube.cxx:254
 THealPixCube.cxx:255
 THealPixCube.cxx:256
 THealPixCube.cxx:257
 THealPixCube.cxx:258
 THealPixCube.cxx:259
 THealPixCube.cxx:260
 THealPixCube.cxx:261
 THealPixCube.cxx:262
 THealPixCube.cxx:263
 THealPixCube.cxx:264
 THealPixCube.cxx:265
 THealPixCube.cxx:266
 THealPixCube.cxx:267
 THealPixCube.cxx:268
 THealPixCube.cxx:269
 THealPixCube.cxx:270
 THealPixCube.cxx:271
 THealPixCube.cxx:272