crinj.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Chad Hanna
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /*----------------------------------------------------------------------- 
00021  * 
00022  * File Name: crinj.c
00023  *
00024  * Author: Ravi Kumar and Chad Hanna
00025  * 
00026  * Revision: $Id: crinj.c,v 1.3 2007/06/08 15:29:44 bema Exp $
00027  * 
00028  *-----------------------------------------------------------------------
00029  */
00030 
00031 
00032 #include <ctype.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <getopt.h>
00038 #include <time.h>
00039 #include <math.h>
00040 #include <lal/LALStdio.h>
00041 #include <lal/LALConstants.h>
00042 #include <lal/Date.h>
00043 #include <lal/TimeDelay.h>
00044 
00045 
00046 #define USAGE \
00047   "lalapps_inspinj [options]\n"\
00048 "\nDefaults are shown in brackets\n\n" \
00049 "  --help                      display this message\n"\
00050 "  --source-file FILE          read source parameters from FILE\n"\
00051 "  --RA-bin-size DEGREES       input right ascension bin size in degrees\n"\
00052 "  --cosDEC-bin-size (0,1]     input Cos(Declination) bin size from +0,1\n"\
00053 "  --logDIST-bin-size (0,3]    input log10(Distance) bin size from +0,3\n"\
00054 "\n"
00055 
00056 RCSID( "$Id: crinj.c,v 1.3 2007/06/08 15:29:44 bema Exp $" );
00057 
00058 #define KPC ( 1e3 * LAL_PC_SI )
00059 #define MPC ( 1e6 * LAL_PC_SI )
00060 #define GPC ( 1e9 * LAL_PC_SI )
00061 
00062 struct {
00063   char   name[16];
00064   double ra;
00065   double dec;
00066   double dist;
00067   double lum;
00068   double fudge;
00069 } *source_data;
00070 
00071 typedef struct {
00072   char NAME[16];
00073   double RA;
00074   double cosDEC;
00075   double logDIST;
00076   double DEC;
00077   double DIST;
00078   double LUM;
00079   double FUDGE;
00080   int RAbin;
00081   int cosDECbin;
00082   int logDISTbin;
00083   unsigned long long ID;
00084 } *binsourcedata;
00085 
00086 
00087 int num_source = 0;
00088 
00089 void read_source_data( char *sourceFileName )
00090 {
00091   char line[256];
00092   FILE *fp;
00093   int i;
00094   fp = fopen( sourceFileName, "r" );
00095 
00096 
00097   if ( ! fp )
00098   {
00099     perror( "read_source_data" );
00100     printf( "Could not find file %s\n", sourceFileName );
00101     exit( 1 );
00102   }
00103 
00104   num_source = 0;
00105   while ( fgets( line, sizeof( line ), fp ) )
00106     if ( line[0] == '#' )
00107       continue;
00108     else 
00109       ++num_source;
00110   rewind( fp );
00111   
00112   source_data = calloc( num_source, sizeof( *source_data ) );
00113 
00114   if ( ! source_data )
00115   {
00116     printf("alloc error\n" );
00117     exit( 1 );
00118   }
00119 
00120   i = 0;
00121   while ( fgets( line, sizeof( line ), fp ) )
00122     if ( line[0] == '#' )
00123       continue;
00124     else
00125     {
00126       char ra_sgn, dec_sgn;
00127       double ra_h, ra_m, dec_d, dec_m;
00128       int c;
00129 
00130       c = sscanf( line, "%s %c%le:%le %c%le:%le %le %le %le",
00131           source_data[i].name, &ra_sgn, &ra_h, &ra_m, &dec_sgn, &dec_d, &dec_m,
00132           &source_data[i].dist, &source_data[i].lum, &source_data[i].fudge );
00133 
00134       if ( c != 10 )
00135       {
00136         fprintf( stderr, "error parsing source datafile %s line %d\n", sourceFileName,i );
00137         exit( 1 );
00138       }
00139 
00140       /* by convention, overall sign is carried only on hours/degrees entry */
00141       source_data[i].ra  = ( ra_h + ra_m / 60.0 ) * LAL_PI / 12.0;
00142       source_data[i].dec = ( dec_d + dec_m / 60.0 ) * LAL_PI / 180.0;
00143 
00144       if ( ra_sgn == '-' )
00145         source_data[i].ra *= -1;
00146       if ( dec_sgn == '-' )
00147         source_data[i].dec *= -1;
00148       ++i;
00149     }
00150   fclose( fp );
00151 }
00152 
00153 int main( int argc, char *argv[] )
00154 {
00155   /* Declare some local variables */
00156 
00157   /* set up inital debugging values */
00158   float RAbinsize = 0;
00159   float cosDECbinsize = 0;
00160   float logDISTbinsize = 0;
00161   FILE *CFP = NULL;                             /* pointer to the cluster file */
00162   char *sourceFileName = NULL;
00163   int i = 0;
00164   int n = 0;
00165   int m = 0;
00166   binsourcedata bin_source_data = NULL;
00167   double totalLUM = 0;
00168   double sumDIST =0;
00169   double sumRA = 0;
00170   double sumcosDEC =0;
00171   
00172  
00173   char   NAME[16];
00174   double RA = 0;
00175   double DEC = 0;
00176   double DIST =0;
00177   double cosDEC = 0;
00178   double logDIST = 0;
00179   double LUM = 0;
00180   double RAbin = 0;
00181   double cosDECbin = 0;
00182   double logDISTbin = 0;
00183   unsigned long long ID = 0;
00184 
00185   char   cNAME[16];
00186   double cRA = 0;
00187   double cDEC = 0;
00188   double cDIST = 0;
00189   double cLUM = 0;
00190   char bigNAME[16];
00191  
00192   int num_c = 0;
00193   
00194 
00195 /*
00196   struct bin_source_data *bin_source_data = NULL;
00197   struct clusteredsourcedata *clustered_source_data = NULL;
00198 */
00199   /* getopt arguments */
00200   struct option long_options[] =
00201   {
00202     {"help",                          no_argument, 0,                'h'},
00203     {"source-file",             required_argument, 0,                'f'},
00204     {"RA-bin-size",             required_argument, 0,                'r'},
00205     {"cosDEC-bin-size",         required_argument, 0,                'c'},
00206     {"logDIST-bin-size",        required_argument, 0,                'l'},
00207     {0, 0, 0, 0}
00208   };
00209   int c;
00210 
00211 
00212   /* parse the arguments */
00213   while ( 1 )
00214   {
00215     /* getopt_long stores long option here */
00216     int option_index = 0;
00217     size_t optarg_len;
00218 
00219     c = getopt_long_only( argc, argv, 
00220         "hf:r:c:l:", long_options, &option_index );
00221 
00222     /* detect the end of the options */
00223     if ( c == - 1 )
00224     {
00225       break;
00226     }
00227 
00228     switch ( c )
00229     {
00230       case 0:
00231         /* if this option set a flag, do nothing else now */
00232         if ( long_options[option_index].flag != 0 )
00233         {
00234           break;
00235         }
00236         else
00237         {
00238           fprintf( stderr, "error parsing option %s with argument %s\n",
00239               long_options[option_index].name, optarg );
00240           exit( 1 );
00241         }
00242         break;
00243 
00244       case 'f':
00245         optarg_len = strlen( optarg ) + 1;
00246         sourceFileName = calloc( 1, optarg_len * sizeof(char) );
00247         memcpy( sourceFileName, optarg, optarg_len * sizeof(char) );
00248         break;
00249 
00250       case 'r':
00251         RAbinsize = atof( optarg );
00252         RAbinsize *= LAL_PI/180.0;
00253         break;
00254 
00255       case 'c':
00256         cosDECbinsize = atof( optarg );
00257         break;
00258 
00259       case 'l':
00260         logDISTbinsize = atof( optarg ); 
00261         break;
00262 
00263       case 'h':
00264         fprintf( stderr, USAGE );
00265         exit( 0 );
00266         break;
00267 
00268       case '?':
00269         fprintf( stderr, USAGE );
00270         exit( 1 );
00271         break;
00272 
00273       default:
00274         fprintf( stderr, "unknown error while parsing options\n" );
00275         fprintf( stderr, USAGE );
00276         exit( 1 );
00277         break;
00278     }
00279   }
00280 
00281   read_source_data( sourceFileName );
00282 
00283   printf("finished reading sourcelist...numsource = %d\n",num_source); 
00284   bin_source_data = calloc(num_source, sizeof(*bin_source_data));
00285   
00286 
00287   /* binning code */
00288    i=0; 
00289   /* bin the source file */
00290   for (i=0;i < num_source; i++){
00291     
00292   memcpy(bin_source_data[i].NAME,source_data[i].name,sizeof(source_data[i].name));
00293 
00294     bin_source_data[i].LUM = source_data[i].lum;
00295     bin_source_data[i].RA = source_data[i].ra;
00296     bin_source_data[i].DEC = source_data[i].dec;
00297     bin_source_data[i].DIST = source_data[i].dist;
00298     if (source_data[i].dec < 0)
00299       bin_source_data[i].cosDEC = -1.0*cos(source_data[i].dec);
00300     else
00301       bin_source_data[i].cosDEC = cos(source_data[i].dec);
00302     bin_source_data[i].logDIST = log10(source_data[i].dist);
00303     bin_source_data[i].RAbin = floor(bin_source_data[i].RA/RAbinsize);
00304     bin_source_data[i].cosDECbin = floor((1.0+bin_source_data[i].cosDEC)/cosDECbinsize);
00305     bin_source_data[i].logDISTbin = floor(bin_source_data[i].logDIST/logDISTbinsize);
00306     bin_source_data[i].ID = 10000*bin_source_data[i].RAbin + 
00307                          100*bin_source_data[i].cosDECbin +
00308                          1000000*bin_source_data[i].logDISTbin;
00309 
00310     }
00311 
00312   /* Sort the list */
00313     
00314     for(n = 0; n < num_source; n++){
00315       for(m = (n+1); m < num_source; m++){
00316         if (bin_source_data[m].ID < bin_source_data[n].ID){
00317           memcpy(NAME, bin_source_data[m].NAME,sizeof(bin_source_data[m].NAME));
00318           LUM = bin_source_data[m].LUM;
00319           RA = bin_source_data[m].RA;
00320           DEC = bin_source_data[m].DEC;
00321           DIST = bin_source_data[m].DIST;
00322           cosDEC = bin_source_data[m].cosDEC;
00323           logDIST = bin_source_data[m].logDIST;
00324           RAbin = bin_source_data[m].RAbin;
00325           cosDECbin = bin_source_data[m].cosDECbin;
00326           logDISTbin = bin_source_data[m].logDISTbin;
00327           ID = bin_source_data[m].ID;
00328           memcpy(bin_source_data[m].NAME, bin_source_data[n].NAME,sizeof(bin_source_data[n].NAME));
00329           bin_source_data[m].LUM = bin_source_data[n].LUM;
00330           bin_source_data[m].RA = bin_source_data[n].RA;
00331           bin_source_data[m].DEC = bin_source_data[n].DEC;
00332           bin_source_data[m].DIST = bin_source_data[n].DIST;
00333           bin_source_data[m].cosDEC = bin_source_data[n].cosDEC;
00334           bin_source_data[m].logDIST = bin_source_data[n].logDIST;
00335           bin_source_data[m].RAbin = bin_source_data[n].RAbin;
00336           bin_source_data[m].cosDECbin = bin_source_data[n].cosDECbin;
00337           bin_source_data[m].logDISTbin = bin_source_data[n].logDISTbin;
00338           bin_source_data[m].ID = bin_source_data[n].ID;
00339           memcpy(bin_source_data[n].NAME, NAME,sizeof(NAME));
00340           bin_source_data[n].LUM = LUM;
00341           bin_source_data[n].RA = RA;
00342           bin_source_data[n].DEC = DEC;
00343           bin_source_data[n].DIST = DIST;
00344           bin_source_data[n].cosDEC = cosDEC;
00345           bin_source_data[n].logDIST = logDIST;
00346           bin_source_data[n].RAbin = RAbin;
00347           bin_source_data[n].cosDECbin = cosDECbin;
00348           bin_source_data[n].logDISTbin = logDISTbin;
00349           bin_source_data[n].ID = ID;
00350 
00351         }
00352       }
00353     }
00354 
00355   /* Clustering and write it to a file called ClusterList.dat*/
00356 
00357  
00358   n = 0;
00359   CFP = fopen("ClusterList.dat", "w");
00360   if (CFP == NULL){
00361     printf("error opening ClusterList.dat for writing\n");
00362     return 1;
00363     }
00364 
00365   
00366   fprintf(CFP, "# Clustered galaxy list\n");
00367   fprintf(CFP, "# NAME\t\t RA\t\t DEC\t\t DIST\t\t LUM\t\t FUDGE\n");
00368 
00369   totalLUM = bin_source_data[0].LUM;
00370   sumDIST = bin_source_data[0].DIST * bin_source_data[0].LUM;
00371   sumRA = bin_source_data[0].RA * bin_source_data[0].LUM;
00372   sumcosDEC = bin_source_data[0].cosDEC * bin_source_data[0].LUM;
00373   memcpy(bigNAME, bin_source_data[0].NAME,sizeof(bin_source_data[0].NAME));
00374   
00375   for(n=1; n<num_source; n++){
00376       if (bin_source_data[n].ID != bin_source_data[n-1].ID){
00377         num_c++;
00378         cLUM = totalLUM;
00379         cRA = sumRA/totalLUM;
00380         /*Check Sign and bin over cos(DEC)*/
00381         if( sumcosDEC < 0)
00382           cDEC = -acos(sumcosDEC/totalLUM);
00383         else
00384           cDEC = acos(sumcosDEC/totalLUM);
00385         cDIST = sumDIST/totalLUM;
00386         memcpy(cNAME, bigNAME,sizeof(bigNAME));
00387         fprintf(CFP, "c%-10s\t %+02d:%0.2f\t %+02d:%0.2f\t %8.1f\t %8.3f\t %.2f\n", 
00388                 cNAME,
00389                 (int) floor(cRA*12.0/LAL_PI), 
00390                 (fmod(cRA*12.0/LAL_PI,1.0)*60.0),
00391                 (int) floor(cDEC*180.0/LAL_PI), 
00392                 (fabs(fmod(cDEC*180.0/LAL_PI,1.0)*60.0)), 
00393                 cDIST, cLUM, 1.00 );
00394         /* initialize */
00395         memcpy(bigNAME, bin_source_data[n].NAME,sizeof(bin_source_data[n].NAME));
00396         totalLUM = bin_source_data[n].LUM;
00397         sumDIST = bin_source_data[n].DIST*bin_source_data[n].LUM;
00398         sumRA = bin_source_data[n].RA*bin_source_data[n].LUM;
00399         sumcosDEC = bin_source_data[n].cosDEC*bin_source_data[n].LUM;
00400       }
00401       else
00402         { 
00403         if (bin_source_data[n].LUM > bin_source_data[n-1].LUM)
00404           memcpy(bigNAME, bin_source_data[n].NAME,sizeof(bin_source_data[n].NAME));
00405         totalLUM += bin_source_data[n].LUM;
00406         sumDIST += bin_source_data[n].DIST*bin_source_data[n].LUM;
00407         sumRA += bin_source_data[n].RA*bin_source_data[n].LUM;
00408         sumcosDEC += bin_source_data[n].cosDEC*bin_source_data[n].LUM;
00409       }
00410   }
00411 
00412 printf("Clustered list written to ClusterList.dat\n");
00413 printf("Clustered list size = %d\n", num_c);
00414 printf("Fraction of sources = %f\n", ((float) num_c) / ((float)num_source));
00415 
00416 
00417 fclose(CFP);
00418 return 0;
00419 }

Generated on Sun Oct 12 02:31:42 2008 for LAL by  doxygen 1.5.2