Commit 38f6a989 authored by Maude Le Jeune's avatar Maude Le Jeune
Browse files

No commit message

No commit message
parent e418e9e2
This diff is collapsed.
#include "atlasmanager.h"
#include <fstream>
#include <sstream>
#include <iomanip>
#include "maptools.h"
using namespace std;
string deg2hms(double deg){
int h, m, s,ms;
h = int(deg/15);
deg = deg-15*h;
m = int(deg*4);
deg = deg-m/4.0;
s = int(deg*240);
deg = deg-s/240.0;
ms = int(deg*240000);
deg = deg-ms/240000.0;
ostringstream hms;
hms.fill('0');
hms <<setw(2) << h <<":"<<setw(2) <<m<<":"<<setw(2) <<s<<"."<<setw(3) <<ms;
return hms.str();
}
string deg2dms(double deg){
int d, m, s,cs;
bool neg = false;
if(deg<0){
neg = true;
deg = -deg;
}
d = int(deg);
deg = deg-d;
m = int(deg*60);
deg = deg-m/60.0;
s = int(deg*3600);
deg = deg-s/3600.0;
cs = int(deg*360000);
deg = deg-cs/360000.0;
ostringstream dms;
dms.fill('0');
if(neg) dms << "-";
else dms << "+";
dms << setw(2) << d <<":"<< setw(2) <<m<<":"<<setw(2)<<s<<"."<<setw(2)<<cs;
return dms.str();
}
//---------------------------SETTERS------------------------------------
void AtlasManager::setCenters(const char * filename){
ifstream entree(filename, ios::in);
if(!entree){
cerr << "Cannot read file " << filename << "\n";
}
int i;
double x, y, z;
vec3 center;
while(entree.good()){
entree >> i >> i >> x >> y >> z;
if(entree.good()){
center = vec3(x,y,z);
center.Normalize();
centers.push_back(center);
}
}
minInterCenterDistance();
}
void AtlasManager::setCenters(vector<pointing> & pcenters){
vector<pointing>::iterator it;
for(it = pcenters.begin(); it != pcenters.end(); it++){
centers.push_back(it->to_vec3());
}
}
void AtlasManager::minInterCenterDistance(){
mICD = 2*pi;
double dist;
for(unsigned int i = 0; i < centers.size(); i++){
for(unsigned int j = i+1; j < centers.size(); j++){
double ang = dotprod(centers[i],centers[j]);
if(ang < 1 && ang > -1){
dist = acos(ang);
if(dist < mICD) mICD = dist;
}
}
}
cout << "choice : " << mICD << " rad\n";
}
void AtlasManager::setMask(Healpix_Map<double> &mask)
{
TangentPlane sqmap;
double min;
double max;
for (int i=0; i<centers.size(); i++)
{
pointing pc;
vec2pnt(centers[i],pc);
sqmap.alloc(mapSize,mapSize,true);
sqmap.gnomonicProjection(mask, pc, mapRes, mapSize);
sqmap.minMax(min, max);
if (min<0.9)
maskindex.push_back(i);
}
}
//---------------------------GETTERS------------------------------------
void AtlasManager::writeApodWin(){
ostringstream outfilename;
outfilename << atlasName << "mask.fits";
apodWin.toFits(outfilename.str());
}
void AtlasManager::getMap(int index, TangentPlane & sqmap, bool dftReady){
pointing pc;
vec2pnt(centers[index],pc);
sqmap.alloc(mapSize,mapSize,dftReady);
sqmap.gnomonicProjection(*sphere, pc, mapRes, mapSize);
//sqmap *= apodWin;
}
bool AtlasManager::outMask(int index){
for (int i=0; i<maskindex.size(); i++)
if (maskindex[i]==index)
return false;
return true;
}
//--------------Coordinates manipulation function---------------------
void AtlasManager::whichMap(pointing pos, vector<int> & mI){
vec3 pixpos = pos.to_vec3();
for(unsigned int mapIndex = 0; mapIndex < centers.size(); mapIndex++){
if(acos(dotprod(pixpos,centers[mapIndex]))<radiusOut)
mI.push_back(mapIndex);
}
}
//-------------------------High Level Functions-----------------------
void AtlasManager::sphere2atlas(){
TangentPlane sqmap(mapSize,mapSize);
TGA_Image im;
for(unsigned int i = 0; i<centers.size(); i++){
pointing pc;
vec2pnt(centers[i],pc);
sqmap.gnomonicProjection(*sphere, pc, mapRes, mapSize);
sqmap *= apodWin;
ostringstream outfilename;
outfilename << atlasName << i << ".fits";
//outfilename << atlasName << i << ".tga";
sqmap.toFits(outfilename.str());
//sqmap.toTGA(im);
//im.write(outfilename.str());
}
}
void AtlasManager::atlas2sphere(){
TangentPlane sqmap;
Healpix_Map<double> weight(sphere->Nside(), sphere->Scheme(), SET_NSIDE);
Healpix_Map<double> tw(sphere->Nside(), sphere->Scheme(), SET_NSIDE);
Healpix_Map<double> tv(sphere->Nside(), sphere->Scheme(), SET_NSIDE);
Healpix_Map<unsigned char> tn(sphere->Nside(), sphere->Scheme(), SET_NSIDE);
sphere->fill(0);
weight.fill(0);
tw.fill(0);
tv.fill(0);
tn.fill(0);
for(int i = 0; i<centers.size(); i++){
ostringstream infilename;
infilename << atlasName << i << ".fits";
sqmap.fromFits(infilename.str());
reprojMap(sqmap, i, *sphere, weight,tv,tw,tn);
}
for(int ipix = 0; ipix<sphere->Npix(); ipix++){
if(weight[ipix] == 0){
(*sphere)[ipix] = Healpix_undef;
//cout << "Warning : pixel " << ipix << " undefined\n";
}else{
(*sphere)[ipix] = (*sphere)[ipix]/weight[ipix];
}
}
}
void AtlasManager::coverage(Healpix_Map<double> &hmap, double radius, bool arcmin){
if (radius != 0){
if (arcmin) radius *= degr2rad/60.0;
vector< int > listpix;
for(int i = 0; i<centers.size(); i++){
pointing pc;
vec2pnt(centers[i],pc);
hmap.query_disc (pc, radius, listpix);
vector<int>::iterator pix;
for(pix = listpix.begin(); pix != listpix.end(); pix++)
hmap[*pix] += 1;
}
}
}
// double AtlasManager::weightRebuild(int mapIndex, vec3 pixpos){
// double dist = acos(dotprod(centers[mapIndex],pixpos));
// if(dist >= rebuildRadiusOut){
// return 0.0;
// }
// if(dist <= rebuildRadiusIn){
// return 1.0;
// }
// else{
// return 0.5 + 0.5*cos((dist-rebuildRadiusIn)/rebuildTrSize);
// }
// }
void AtlasManager::reprojMap(TangentPlane & sqmap, int mapIndex,
Healpix_Map<double> &hmap,
Healpix_Map<double> &weight,
Healpix_Map<double> &tempv,
Healpix_Map<double> &tempw,
Healpix_Map<unsigned char> &tempn){
pointing pntc;
vec2pnt(centers[mapIndex], pntc);
double lon0rad = pntc.phi;
double lat0rad = pi/2-pntc.theta;
rotmatrix rot;
rot.Make_CPAC_Euler_Matrix(lon0rad,-lat0rad,0);
double delta=mapRes;
double start=-(mapSize/2.)*delta;
int ipix[sqmap.size1()*sqmap.size2()];
int npix = 0;
for(int xpix = 0; xpix < sqmap.size1(); xpix++){
for(int ypix = 0; ypix < sqmap.size2(); ypix++){
if(apodWin[xpix][ypix] > 0){
vec3 pnt (1,-(start+ypix*delta), start+xpix*delta);
pnt = rot.Transform(pnt);
pnt.Normalize();
ipix[npix] = hmap.ang2pix(pnt);
tempv[ipix[npix]] += apodWin[xpix][ypix] * sqmap[xpix][ypix];
tempw[ipix[npix]] += apodWin[xpix][ypix];
tempn[ipix[npix++]] += 1;
}
}
}
for(int i = 0; i<npix; i++){
if(tempn[ipix[i]] != 0){
hmap[ipix[i]] += tempv[ipix[i]]/tempn[ipix[i]];
weight[ipix[i]] += tempw[ipix[i]]/tempn[ipix[i]];
tempv[ipix[i]] = 0;
tempw[ipix[i]] = 0;
tempn[ipix[i]] = 0;
}
}
}
// void AtlasManager::rebuildMap(Healpix_Map<double> &map,Healpix_Map<double> &weight ){
// weight = Healpix_Map<double>(map.Nside(), map.Scheme(), SET_NSIDE);
// Healpix_Map<double> inmapv(map.Nside(), map.Scheme(), SET_NSIDE);
// Healpix_Map<double> inmapw(map.Nside(), map.Scheme(), SET_NSIDE);
// weight.fill(0.0f);
// inmapv.fill(0.0f);
// inmapw.fill(0.0f);
// map.fill(0.0f);
// arr2<double> sqmap;
// if(!rebSet) return;
// for(unsigned int mapIndex = 0; mapIndex < centers.size(); mapIndex++){
// cout << mapIndex <<"."<<flush;
// readMap(sqmap,mapIndex);
// reprojMap(sqmap, mapIndex,map,weight, inmapv, inmapw);
// }
// cout << "\n";
// for(int ipix = 0; ipix<map.Npix(); ipix++){
// if(approx(weight[ipix],Healpix_undef) || weight[ipix] == 0){
// map[ipix] = Healpix_undef;
// cout << "Warning : pixel " << ipix << " undefined\n";
// }else{
// map[ipix] = map[ipix]/weight[ipix];
// }
// }
// }
// void AtlasManager::overlap(Healpix_Map<double> &overmap){
// overmap.fill(0.0f);
// for(int mapInd = 0; mapInd < centers.size(); mapInd++){
// cout << mapInd << "."<<flush;
// vec3 cpnt = centers[mapInd];
// vector<int> pixlist;
// vector<int>::iterator ipix;
// overmap.query_disc(cpnt, radiusOut,pixlist);
// pointing pnt;
// vec3 pixpos;
// for(ipix = pixlist.begin(); ipix != pixlist.end(); ipix++){
// pnt = overmap.pix2ang(*ipix);
// pixpos = pnt.to_vec3();
// overmap[*ipix] += apodize(mapInd, pixpos);
// }
// }
// cout << "\n";
// }
// void AtlasManager::buildOverlap(Healpix_Map<double> &overmap){
// if(rebSet){
// overmap.fill(0.0f);
// for(int mapInd = 0; mapInd < centers.size(); mapInd++){
// cout << mapInd << "."<<flush;
// vec3 cpnt = centers[mapInd];
// vector<int> pixlist;
// vector<int>::iterator ipix;
// overmap.query_disc(cpnt, rebuildRadiusOut,pixlist);
// pointing pnt;
// vec3 pixpos;
// for(ipix = pixlist.begin(); ipix != pixlist.end(); ipix++){
// pnt = overmap.pix2ang(*ipix);
// pixpos = pnt.to_vec3();
// overmap[*ipix] += weightRebuild(mapInd, pixpos);
// }
// }
// cout << "\n";
// }
// }
/*
void AtlasManager::setSize(int npix){
if(centerSet && resSet){
mapSize = npix;
radiusOut = atan(0.5*mapSize*mapResRad);
if(radiusOut < mICD){
cout << "WARNING : mapsize maybe insufficiant to cover all sky\n";
}
}
}*/
/*
void AtlasManager::autoSize(){
if(centerSet && resSet){
mapSize = int(2*tan(mICD)/mapResRad);
radiusOut=mICD;
cout << "Auto setting for mapsize : " << mapSize << " pixels\n";
cout << "Radius coverage : " << (mICD * rad2degr * 60) << " arcmin\n";
}
else{}
}*/
/*
void AtlasManager::setApod(double radIn){
if(centerSet && resSet){
radiusOut = atan(0.5*mapSize*mapResRad);
if(radIn < radiusOut){
radiusIn = radIn;
trSize = (radiusOut - radiusIn) / pi;
apodSet = true;
cout << "Setting apod\n";
}
else
cout << "Error : not allowed to set a radius exceeding map size\n";
}
}*/
/*
void AtlasManager::autoApod(){
double rad = atan(0.5*0.9*mapSize*mapResRad);
cout << "Auto setting for apodization inner radius : " << (rad * rad2degr * 60) << " arcmin\n";
setApod(rad);
}*/
/*
void AtlasManager::setRebuild(double radIn){
if(centerSet && resSet){
if(apodSet)
rebuildRadiusOut = radiusIn;
else
rebuildRadiusOut = radiusOut;
if(radIn < rebuildRadiusOut){
rebuildRadiusIn = radIn;
rebuildTrSize = (rebuildRadiusOut - rebuildRadiusIn) / pi;
rebSet = true;
}
else
cout << "Error : not allowed to set a radius exceeding effective map size\n";
}
}*/
/*
void AtlasManager::autoRebuild(){
double rad;
if(apodSet){
rad = 0.9*radiusIn;
}
else{
rad = 0.9*radiusOut;
}
setRebuild(rad);
cout << "Auto setting for rebuild inner radius : " << (rad * rad2degr * 60) << " arcmin\n";
}*/
/*
double AtlasManager::pro_gnoZeroMean (const Healpix_Map<double> &map, int mapIndex, arr2<double> &img, double &minv, double &maxv){
pointing pntc;
vec2pnt(centers[mapIndex], pntc);
double lon0rad = pntc.phi;
double lat0rad = pi/2-pntc.theta;
rotmatrix rot;
rot.Make_CPAC_Euler_Matrix(lon0rad,-lat0rad,0);
double delta=mapRes*degr2rad/60.;
double start=-(mapSize/2.)*delta;
double mean = 0;
img.alloc(mapSize,mapSize);
minv=1e30;
maxv=-1e30;
for (int i=0; i<img.size2(); ++i)
for (int j=0; j<img.size1(); ++j)
{
vec3 pnt (1,-(start+i*delta), start+j*delta);
pnt = rot.Transform(pnt);
if (projMethod==1)
img[j][i] = map.interpolated_value(pnt);
else if (projMethod==2)
img[j][i] = map.interpolated_value2(pnt);
else
img[j][i]=map[map.ang2pix(pnt)];
if (!approx(img[i][j],Healpix_undef))
{
if (img[j][i]<minv) minv=img[j][i];
if (img[j][i]>maxv) maxv=img[j][i];
}
mean += img[j][i];
if(apodSet){
img[j][i]*= apodize(mapIndex,pnt);
}
}
mean = mean / (img.size1() * img.size2());
for(int i=0; i<img.size1(); i++){
for(int j = 0; j<img.size2(); j++)
img[i][j] -= mean;
}
return mean;
}
*/
/*
void AtlasManager::pro_gno (const Healpix_Map<double> &map, int mapIndex, arr2<double> &img, double &minv, double &maxv){
pointing pntc;
vec2pnt(centers[mapIndex], pntc);
double lon0rad = pntc.phi;
double lat0rad = pi/2-pntc.theta;
rotmatrix rot;
rot.Make_CPAC_Euler_Matrix(lon0rad,-lat0rad,0);
double delta=mapRes*degr2rad/60.;
double start=-(mapSize/2.)*delta;
img.alloc(mapSize,mapSize);
minv=1e30;
maxv=-1e30;
for (int i=0; i<img.size2(); ++i)
for (int j=0; j<img.size1(); ++j)
{
vec3 pnt (1,-(start+i*delta), start+j*delta);
pnt = rot.Transform(pnt);
if (projMethod==1)
img[j][i] = map.interpolated_value(pnt);
else if (projMethod==2)
img[j][i] = map.interpolated_value2(pnt);
else
img[j][i]=map[map.ang2pix(pnt)];
if (!approx(img[i][j],Healpix_undef))
{
if (img[j][i]<minv) minv=img[j][i];
if (img[j][i]>maxv) maxv=img[j][i];
}
if(apodSet){
img[j][i]*= apodize(mapIndex,pnt);
}
}
}
*/
/*
double AtlasManager::writeMap(const Healpix_Map<double> &map, int mapIndex, bool zeroMean){
arr2<double> tanmap;
double minv, maxv;
double mean = 0;
if(zeroMean){
mean = pro_gnoZeroMean (map, mapIndex, tanmap, minv, maxv);
}else{
pro_gno (map, mapIndex, tanmap, minv, maxv);
}
pointing pnt;
vec2pnt(centers[mapIndex], pnt);
double latdeg = (pi/2 - pnt.theta)*rad2degr;
double londeg = (pnt.phi)*rad2degr;
fitshandle outfits;
ostringstream outfilename;
outfilename << atlasName << mapIndex << ".fits";
outfits.create (outfilename.str());
outfits.goto_hdu(1);
outfits.insert_image(-32, tanmap);
outfits.add_key("CRVAL1", (double)londeg);
outfits.add_key("CRVAL2", (double)latdeg);
outfits.add_key("RA", deg2hms(londeg));
outfits.add_key("DEC", deg2dms(latdeg));
outfits.add_key("EPOCH", 2000);
outfits.add_key("EQUINOX", 2000);
outfits.add_key("RADECSYS", string("GALACTIC"));
outfits.add_key("CRPIX1", (double)((mapSize+1)/2.));
outfits.add_key("CRPIX2", (double)((mapSize+1)/2.));
outfits.add_key("SECPIX", (double)(mapRes*60));
outfits.add_key("CDELT1", (double)(-mapRes/60.0));
outfits.add_key("CDELT2", (double)(mapRes/60.0));
outfits.add_key("CTYPE1", string("GLON-TAN"));
outfits.add_key("CTYPE2", string("GLAT-TAN"));
outfits.add_key("CD1_1", (double)(-mapRes/60.0));
outfits.add_key("CD1_2", (double)0.0);
outfits.add_key("CD2_1", (double)0.0);
outfits.add_key("CD2_2", (double)(mapRes/60.0));
outfits.close();
return mean;
}*/
/*
void AtlasManager::readMap(arr2<double> & sqmap,int mapIndex){
fitshandle infits;
ostringstream infilename;
infilename << atlasName << mapIndex << ".fits";
readImageFits(infilename.str(), sqmap);
}
*/
/*
void AtlasManager::writeAllMapsAndMeanMap(const Healpix_Map<double> &map, Healpix_Map<double> &meanMap){
vector<double> means;
double mean;
meanMap = Healpix_Map<double>(map.Nside(), map.Scheme(), SET_NSIDE);
Healpix_Map<double> inmapv(map.Nside(), map.Scheme(), SET_NSIDE);
Healpix_Map<double> inmapw(map.Nside(), map.Scheme(), SET_NSIDE);
meanMap.fill(0);
Healpix_Map<double> weight(map.Nside(), map.Scheme(), SET_NSIDE);
weight.fill(0.0f);
inmapv.fill(0.0f);
inmapw.fill(0.0f);
for(unsigned int i = 0; i<centers.size(); i++){
mean = writeMap(map, i, true);
means.push_back(mean);
}
arr2<double> meansqmap;
meansqmap.alloc(mapSize,mapSize);
for(unsigned int mapIndex = 0; mapIndex < centers.size(); mapIndex++){
meansqmap.fill(means[mapIndex]);
reprojMap(meansqmap, mapIndex,meanMap,weight,inmapv, inmapw);
}
for(int ipix = 0; ipix<map.Npix(); ipix++){
if(approx(weight[ipix],Healpix_undef) || weight[ipix] == 0){
meanMap[ipix] = Healpix_undef;
cout << "Warning : pixel " << ipix << " undefined\n";
}else{
meanMap[ipix] =meanMap[ipix]/weight[ipix];
}
}
}
*/
/*
void AtlasManager::substractLowModes(Healpix_Map<double> &hmap, Healpix_Map<double> &lmap){
lmap = Healpix_Map<double>(hmap.Nside(), hmap.Scheme(),SET_NSIDE);
}
*/
// Copyright (C) (2006-2007) Observatoire de Paris
//
// This file is part of TPCT (Tangent Plane C++ Tools) .
// TPCT is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
/*! \file atlasmanager.h
* \brief Defines the class AtlasManager
*
* \author Marc Betoule <marc.betoule@wanadoo.fr>
*/
#ifndef ATLASMANAGER_H
#define ATLASMANAGER_H
<