mad2hfits.c

00001 /*******************************************************************************
00002 *   M3:  mad2hfits.c                                                  *
00003 *                                                                              *
00004 *   Version 1.0 May 2004                                                       *
00005 *                                                                              *
00006 *   Copyright (C) 2004  C.M. Cantalupo                                         *
00007 *                                                                              *
00008 *   This program is free software; you can redistribute it and/or modify       *
00009 *   it under the terms of the GNU General Public License as published by       *
00010 *   the Free Software Foundation; either version 2 of the License, or          *
00011 *   (at your option) any later version.                                        *
00012 *                                                                              *
00013 *   This program is distributed in the hope that it will be useful,            *
00014 *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00015 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00016 *   GNU General Public License for more details.                               *
00017 *                                                                              *
00018 *   You should have received a copy of the GNU General Public License          *
00019 *   along with this program; if not, write to the Free Software                *
00020 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  *
00021 *                                                                              *
00022 *******************************************************************************/
00023 
00024 
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <math.h>
00029 #include "M3_system.h"
00030 #include "M3_error.h"
00031 #include "hpic.h"
00032 
00033 
00034 
00035 typedef struct
00036 {
00037   int32_t pixel;
00038   double value;
00039 } M3_MapEl;
00040 
00041 
00042 int main( int argc, char **argv )
00043 {
00044   int32_t header[2];
00045   int32_t header2[2];
00046   char inFileNameI[256];
00047   char inFileNameQ[256];
00048   char inFileNameU[256];
00049   char outFileName[256];
00050   M3_MapEl *data;
00051   FILE *fid;
00052   int64_t i, j;
00053   int nside;
00054   int isPolar;
00055   hpic_float *hpicMap[3] = {0};
00056   hpic_keys *hpicKeys;
00057 
00058   if( argc != 3 && argc != 5 )
00059   {
00060     fprintf( stderr, "Usage:  %s  { inFile | inFileI inFileQ inFileU } outFile\n\n", argv[0] );
00061     return(0);
00062   }
00063 
00064   if( argc == 3 )
00065   {
00066     strcpy( inFileNameI, argv[1]);
00067     strcpy( outFileName, argv[2]);
00068     isPolar = 0;
00069   }
00070   else
00071   {
00072     strcpy( inFileNameI, argv[1] );
00073     strcpy( inFileNameQ, argv[2] );
00074     strcpy( inFileNameU, argv[3] );
00075     strcpy( outFileName, argv[4] );
00076     isPolar = 1;
00077   }
00078 
00079 
00080   hpicKeys = hpic_keys_alloc();
00081 
00082   hpic_keys_iadd( hpicKeys, "POLAR", isPolar, "Pixelisation included (True/False)");
00083   hpic_keys_sadd( hpicKeys, "TTYPE1", "SIGNAL0 ", "label for field   1");
00084   
00085   if( isPolar )
00086   {
00087     hpic_keys_sadd( hpicKeys, "TTYPE2", "SIGNAL1 ", "label for field   2");
00088     hpic_keys_sadd( hpicKeys, "TTYPE3", "SIGNAL2 ", "label for field   3");
00089   }
00090   
00091 
00092   fid = fopen( inFileNameI, "r");
00093   M3_ErrorCheck( -1, inFileNameI, fid, M3_EFLAG_FOPEN_READ);
00094 
00095   fread( header, sizeof(int32_t), 2, fid);
00096 
00097   nside = sqrt(header[0]/12);
00098 
00099   data = (M3_MapEl *)malloc(sizeof(M3_MapEl)*header[1]);
00100   M3_ErrorCheck(-1, "main", data, M3_EFLAG_MALLOC_FSTRING );
00101 
00102   for( j = 0; j < 1 + 2*isPolar; j++ )
00103   {
00104     fread( data, sizeof(M3_MapEl), header[1], fid);
00105     if( getenv("MAD2HFITS_NEST") )
00106       hpicMap[j] = hpic_float_alloc( nside, HPIC_NEST, HPIC_COORD_G, HPIC_STND );
00107     else
00108       hpicMap[j] = hpic_float_alloc( nside, HPIC_RING, HPIC_COORD_G, HPIC_STND );
00109     
00110 
00111     for( i = 0; i < header[1]; i++ )
00112       hpic_float_set( hpicMap[j], data[i].pixel, data[i].value );
00113 
00114     fclose(fid);
00115     fid = NULL;
00116 
00117     if( isPolar && j == 0 )
00118     {
00119       fid = fopen( inFileNameQ, "r");
00120       M3_ErrorCheck(-1, inFileNameQ, fid, M3_EFLAG_MALLOC_FSTRING );
00121     }
00122     else if( isPolar && j == 1 )
00123     {
00124       fid = fopen( inFileNameU, "r");
00125       M3_ErrorCheck(-1, inFileNameQ, fid, M3_EFLAG_MALLOC_FSTRING );
00126     }
00127 
00128     if(fid)
00129     {
00130       fread( header2, sizeof(int32_t), 2, fid );
00131       M3_ErrorCheck(-1, "Number of pixels in class does not match between files.", header[0] == header2[0], M3_EFLAG_DEFAULT );
00132       if( header2[1] > header[1] )
00133       {
00134         data = (M3_MapEl *)realloc(data, sizeof(M3_MapEl)*header2[1] );
00135         M3_ErrorCheck(-1, "main", data, M3_EFLAG_MALLOC_FSTRING );
00136         header[1] = header2[1];
00137       }
00138     }
00139   }
00140 
00141   if( isPolar )
00142   {
00143     hpic_cmb_write_fullTQU( outFileName, hpicMap[0], hpicMap[1], hpicMap[2], "MADCAP3", "mad2hfits", hpicKeys );
00144   }
00145   else
00146   {
00147     hpic_cmb_write_full( outFileName, hpicMap[0], "MADCAP3", "mad2hfits", hpicKeys );
00148   }
00149 
00150 
00151   for( j = 0; j < 3; j++ )
00152     if( hpicMap[j] )
00153       hpic_float_free( hpicMap[j] );
00154   free(data);
00155 
00156   hpic_keys_free( hpicKeys );
00157 
00158 }
00159 
00160     

Generated on Mon Nov 24 10:05:12 2008 for M3 by  doxygen 1.5.3-20071008