00001 #include "M3_format_healpix.h"
00002 #include "fitsio.h"
00003 #include "ccFitsTools.h"
00004
00005
00006 void M3_File_parseFormatString_healpix( M3_File *theFile, M3_FormatStruct_healpix *formatStruct )
00007 {
00008 char *sptr;
00009
00010
00011 formatStruct->order = 'r';
00012 formatStruct->nside = 0;
00013 formatStruct->fieldIndex = 0;
00014
00015 sptr = strchr( theFile->format, '_');
00016 if( sptr)
00017 sptr++;
00018
00019 while( sptr )
00020 {
00021 if( strncmp( sptr, "nside=", 6 ) == 0 )
00022 {
00023 sptr += 6;
00024 formatStruct->nside = atoi(sptr);
00025 }
00026 else if( strncmp( sptr, "fieldIndex=", 11 ) == 0 )
00027 {
00028 sptr += 11;
00029 formatStruct->fieldIndex = atoi(sptr);
00030 }
00031 else if( strncmp( sptr, "order=", 6 ) == 0 )
00032 {
00033 sptr += 6;
00034 if( strncmp( sptr, "ring", 4 ) == 0 )
00035 formatStruct->order = 'r';
00036 else if( strncmp( sptr, "nest", 4) == 0 )
00037 formatStruct->order = 'n';
00038 }
00039 sptr = strchr( sptr, '_');
00040 if(sptr)
00041 sptr++;
00042 }
00043 }
00044
00045 void M3_File_readHeader_healpix( M3_File *theFile, void *data )
00046 {
00047 switch( theFile->fileType )
00048 {
00049 case M3_COORD_FILE_TYPE:
00050 M3_File_readHeader_healpix_coord( theFile, (M3_CoordHeader *)data );
00051 break;
00052 case M3_MAP_FILE_TYPE:
00053 M3_File_readHeader_healpix_map( theFile, (M3_MapHeader *)data );
00054 break;
00055 default:
00056 M3_ErrorCheck(-1, "healpix file format only valid for coord and map files\n", 0, M3_EFLAG_DEFAULT);
00057 }
00058
00059 }
00060 void M3_File_readData_healpix( M3_File *theFile, void *readArg, void *data )
00061 {
00062 switch( theFile->fileType )
00063 {
00064 case M3_COORD_FILE_TYPE:
00065 M3_File_readData_healpix_coord( theFile, *((int32_t*)readArg), (M3_CoordEl *)data );
00066 break;
00067 case M3_MAP_FILE_TYPE:
00068 M3_File_readData_healpix_map( theFile, *((int32_t*)readArg), (M3_MapEl *)data );
00069 break;
00070 default:
00071 M3_ErrorCheck(-1, "healpix file format only valid for coord and map files\n", 0, M3_EFLAG_DEFAULT);
00072 }
00073
00074 }
00075
00076 void M3_File_readHeader_healpix_coord( M3_File *theFile, M3_CoordHeader *header )
00077 {
00078 M3_FormatStruct_healpix formatStruct;
00079
00080 M3_File_parseFormatString_healpix( theFile, &formatStruct );
00081 header->numPixelInClass = 12*formatStruct.nside*formatStruct.nside;
00082
00083 }
00084
00085
00086 void M3_File_readData_healpix_coord( M3_File *theFile, int32_t numReadPixel, M3_CoordEl *data )
00087 {
00088 int32_t i;
00089 double theta, phi;
00090 int32_t nside;
00091 M3_FormatStruct_healpix formatStruct ;
00092 M3_File_parseFormatString_healpix(theFile, &formatStruct );
00093
00094 if( formatStruct.order == 'n')
00095 for( i = 0; i < numReadPixel; i++ )
00096 {
00097 pix2ang_nest( formatStruct.nside, data[i].pixel, &theta, &phi);
00098 data[i].ra = M_1_PI*180.0*phi;
00099 data[i].dec = 90 - M_1_PI*180.0*theta;
00100 }
00101 else
00102 for( i = 0; i < numReadPixel; i++ )
00103 {
00104 pix2ang_ring( formatStruct.nside, data[i].pixel, &theta, &phi);
00105 data[i].ra = M_1_PI*180.0*phi;
00106 data[i].dec = 90 - M_1_PI*180.0*theta;
00107 }
00108 }
00109
00110
00111 void M3_File_readHeader_healpix_map( M3_File *theFile, M3_MapHeader *header )
00112 {
00113 M3_FormatStruct_healpix formatStruct;
00114 fitsfile *thisFitsFile;
00115 int32_t nside;
00116 char errorString[256];
00117 char key[256];
00118 int status = 0;
00119
00120 fits_open_file( &thisFitsFile, theFile->name, READONLY, &status );
00121 M3_ErrorCheck(-1, theFile->name, status == 0, M3_EFLAG_FOPEN_READ );
00122
00123 fits_movabs_hdu( thisFitsFile, 2, NULL, &status);
00124 sprintf( errorString, "Could not go to first fits header in file %s", theFile->name );
00125 M3_ErrorCheck( -1, errorString, status == 0, M3_EFLAG_DEFAULT );
00126
00127 fits_read_key_str( thisFitsFile, "NSIDE", key, NULL, &status );
00128 sprintf( errorString, "Could not read NSIDE flag from healpix map named %s", theFile->name );
00129 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT);
00130 nside = atoi(key);
00131
00132 fits_close_file( thisFitsFile, &status );
00133 sprintf( errorString, "Error closing fits file named %s", theFile->name );
00134 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT );
00135
00136 header->numPixelInMap = header->numPixelInClass = 12*nside*nside;
00137 }
00138
00139 typedef struct
00140 {
00141 char fileName[1024];
00142 char fileFormat[1024];
00143 int32_t lastPixel;
00144 fitsfile *fitsFilePtr;
00145 } M3_staticMapFileRecord;
00146
00147 void M3_File_readData_healpix_map( M3_File *theFile, int32_t numReadPixel, M3_MapEl *data )
00148 {
00149 fitsfile *thisFitsFile;
00150 int status = 0;
00151 int byteSwap, blocks;
00152 char errorString[256] = {0};
00153 char key[256] = {0};
00154 M3_FormatStruct_healpix formatStruct ;
00155 static int64_t diskBufferSize = -1;
00156 char *tempPtr;
00157 int32_t nside, tfields, pixelIndex, firstPixel, lastPixel;
00158 int64_t blockSize, numBlock, bufferLength, numReads, thisBufferLength, i, j, k, l, m;
00159 FILE *fid;
00160 float *buffer, *bufferPtr;
00161 M3_Interval readInterval, bufferInterval;
00162 double *dptr;
00163 static M3_staticMapFileRecord staticMapFileRecord = {0};
00164
00165 if( diskBufferSize == -1 )
00166 {
00167 tempPtr = getenv("M3_DISK_BUFFER_SIZE");
00168 if( tempPtr )
00169 diskBufferSize = strtoll( tempPtr, NULL, 10);
00170 else
00171 diskBufferSize = M3_DISK_BUFFER_SIZE;
00172 }
00173
00174 if( strcmp( theFile->name, staticMapFileRecord->fileName) == 0 )
00175 {
00176 thisFitsFile = staticMapFileRecord->fitsFilePtr;
00177
00178
00179 }
00180
00181
00182 else
00183 {
00184 fits_open_file( &thisFitsFile, theFile->name, READONLY, &status );
00185 M3_ErrorCheck(-1, theFile->name, status == 0, M3_EFLAG_FOPEN_READ );
00186
00187 fits_movabs_hdu( thisFitsFile, 2, NULL, &status);
00188 sprintf( errorString, "Could not go to first fits header in file %s", theFile->name );
00189 M3_ErrorCheck( -1, errorString, status == 0, M3_EFLAG_DEFAULT );
00190 }
00191
00192 M3_File_parseFormatString_healpix(theFile, &formatStruct );
00193
00194 firstPixel = data[0].pixel;
00195 lastPixel = data[numReadPixel-1].pixel;
00196
00197 if( lastPixel - firstPixel + 1 == numReadPixel )
00198 {
00199
00200 dptr = (double*)(data + numReadPixel) - numReadPixel;
00201 read_fits_tableblock( thisFitsFile, firstPixel, lastPixel, NULL, 1, 0, NULL, &formatStruct.fieldIndex, dptr);
00202
00203 for( i = 0, j = firstPixel; i < numReadPixel; i++, j++ )
00204 {
00205 data[i].value = dptr[i];
00206 data[i].pixel = j;
00207 }
00208
00209 fits_close_file( thisFitsFile, &status );
00210 sprintf( errorString, "Error closing fits file named %s", theFile->name );
00211 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT );
00212
00213 }
00214 else
00215 {
00216 fits_read_key_str( thisFitsFile, "NSIDE", key, NULL, &status );
00217 sprintf( errorString, "Could not read NSIDE flag from healpix map named %s", theFile->name );
00218 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT);
00219 nside = atoi(key);
00220
00221 fits_read_key_str( thisFitsFile, "TFIELDS", key, NULL, &status );
00222 sprintf( errorString, "Could not read TFIELDS flag from healpix map named %s", theFile->name );
00223 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT);
00224 tfields = atoi(key);
00225
00226 fits_read_key_str( thisFitsFile, "TFORM1", key, NULL, &status );
00227 sprintf( errorString, "Could not read TFORM1 flag from healpix map named %s", theFile->name );
00228 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT);
00229 if(strncmp( key, "1024E", 5) == 0 )
00230 blocks = 1;
00231 else
00232 blocks = 0;
00233
00234 fits_close_file( thisFitsFile, &status );
00235 sprintf( errorString, "Error closing fits file named %s", theFile->name );
00236 M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT );
00237
00238 buffer = (float *)malloc(diskBufferSize);
00239 M3_ErrorCheck(-1, "M3_File_readData_healpix_map()", (buffer?1:0), M3_EFLAG_MALLOC_FSTRING );
00240
00241 byteSwap = (testEndian()?0:1);
00242 fid = fopen( theFile->name, "r");
00243 skipFitsHeaders( fid, 2 );
00244
00245 if( blocks == 1 )
00246 {
00247 blockSize = tfields*1024*sizeof(float);
00248 numBlock = 12*nside*nside/1024;
00249
00250 readInterval.firstSample = 0;
00251 readInterval.lastSample = numBlock - 1;
00252
00253 M3_bufferIntervalStart( readInterval, diskBufferSize, blockSize, &bufferLength, &numReads );
00254
00255 m = 0;
00256 pixelIndex = 0;
00257
00258 for( j = 0; j < numReads; j++ )
00259 {
00260 M3_bufferIntervalStep( readInterval, bufferLength, j, &bufferInterval );
00261 thisBufferLength = bufferInterval.lastSample - bufferInterval.firstSample + 1;
00262 fread(buffer, blockSize, thisBufferLength, fid );
00263
00264 if( byteSwap )
00265 flipByteOrder( buffer, thisBufferLength*tfields*1024, sizeof(float));
00266
00267 bufferPtr = buffer + 1024*formatStruct.fieldIndex;
00268
00269 for( k = 0; k < thisBufferLength && m < numReadPixel; k++ )
00270 {
00271 for( l = 0; l < 1024 && m < numReadPixel; l++ )
00272 {
00273 if( data[m].pixel == pixelIndex )
00274 {
00275 data[m].value = bufferPtr[l];
00276 m++;
00277 }
00278 pixelIndex++;
00279 }
00280 bufferPtr += tfields*1024;
00281 }
00282 }
00283 }
00284 else
00285 {
00286 readInterval.firstSample = 0;
00287 readInterval.lastSample = 12*nside*nside - 1;
00288
00289 M3_bufferIntervalStart( readInterval, diskBufferSize, tfields*sizeof(float), &bufferLength, &numReads );
00290 m = 0;
00291 pixelIndex = 0;
00292 for( j = 0; j < numReads && m < numReadPixel; j++ )
00293 {
00294 M3_bufferIntervalStep( readInterval, bufferLength, j, &bufferInterval );
00295 thisBufferLength = bufferInterval.lastSample - bufferInterval.firstSample + 1;
00296 fread(buffer, tfields*sizeof(float), thisBufferLength, fid );
00297
00298 if( byteSwap )
00299 flipByteOrder( buffer, thisBufferLength*tfields, sizeof(float) );
00300
00301 for( k = 0; k < thisBufferLength && m < numReadPixel; k++ )
00302 {
00303 if( data[m].pixel == pixelIndex )
00304 {
00305 data[m].value = buffer[k*tfields + formatStruct.fieldIndex];
00306 m++;
00307 }
00308 pixelIndex++;
00309 }
00310 }
00311
00312 }
00313 fclose(fid);
00314 free(buffer);
00315 }
00316 }