00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 #define GENERALMESHTESTC_EMEM 1
00109 #define GENERALMESHTESTC_ERNG 2
00110 #define GENERALMESHTESTC_EFIO 3
00111 #define GENERALMESHTESTC_EOPT 4
00112 #define GENERALMESHTESTC_EMET 5
00113
00114 #define GENERALMESHTESTC_MSGEMEM "memory (de)allocation error"
00115 #define GENERALMESHTESTC_MSGERNG "value out of range"
00116 #define GENERALMESHTESTC_MSGEFIO "file I/O error"
00117 #define GENERALMESHTESTC_MSGEOPT "unknown command-line option"
00118 #define GENERALMESHTESTC_MSGEMET "determinant of projected metric negative"
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 #include <math.h>
00150 #include <stdio.h>
00151 #include <lal/AVFactories.h>
00152 #include <lal/LALXMGRInterface.h>
00153 #include <lal/PtoleMetric.h>
00154 #include <lal/StackMetric.h>
00155 #include <lal/TwoDMesh.h>
00156 #include <lal/LALBarycenter.h>
00157
00158
00159
00160 NRCSID( GENERALMESHTESTC, "$Id: GeneralMeshTest.c,v 1.9 2007/06/08 14:41:52 bema Exp $" );
00161
00162 #define MIN_DURATION (86400./LAL_TWOPI)
00163 #define MAX_DURATION (3.16e7)
00164 #define MIN_FREQ 1e2
00165 #define MAX_FREQ 1e4
00166 #define MAX_NODES 1e6
00167
00168
00169
00170 REAL4 ra_min;
00171 REAL4 ra_max;
00172 REAL4 dec_min;
00173 REAL4 dec_max;
00174
00175
00176 char *optarg = NULL;
00177 int lalDebugLevel = 0;
00178 int metric_code;
00179
00180
00181
00182
00183
00184
00185 void getRange( LALStatus *, REAL4 [2], REAL4, void * );
00186 void getMetric( LALStatus *, REAL4 [3], REAL4 [2], void * );
00187
00188
00189 static SkyPosition center;
00190 REAL4 radius;
00191
00192 REAL8Vector *tevlambda;
00193
00194
00195 int main( int argc, char **argv )
00196 {
00197 static LALStatus stat;
00198 INT2 opt;
00199 BOOLEAN errors;
00200 BOOLEAN grace;
00201 BOOLEAN nonGrace;
00202 TwoDMeshNode *firstNode;
00203 static TwoDMeshParamStruc mesh;
00204 static PtoleMetricIn search;
00205 REAL4 mismatch;
00206 int begin;
00207 REAL4 duration;
00208 REAL4 fMax;
00209 FILE *fp;
00210 static MetricParamStruc tevparam;
00211 PulsarTimesParamStruc tevpulse;
00212
00213 EphemerisData *eph;
00214 int detector;
00215
00216
00217 float a, b, c, d, e, f;
00218 BOOLEAN rectangular;
00219 char earth[] = "earth00-04.dat";
00220 char sun[] = "sun00-04.dat";
00221
00222
00223
00224 metric_code = 1;
00225 errors = 0;
00226 grace = 0;
00227 nonGrace = 0;
00228 begin = 731265908;
00229 duration = 1e5;
00230 fMax = 1e3;
00231 mismatch = .02;
00232
00233 center.system = COORDINATESYSTEM_EQUATORIAL;
00234 center.longitude = (24.1/60)*LAL_PI_180;
00235 center.latitude = -(72+5./60)*LAL_PI_180;
00236 radius = 24.0/60*LAL_PI_180;
00237 detector = 4;
00238 ra_min = 0.0;
00239 ra_max = LAL_PI_2;
00240 dec_min = 0.0;
00241 dec_max = LAL_PI_2;
00242 rectangular = 0;
00243
00244
00245 while( (opt = getopt( argc, argv, "a:b:c:d:ef:l:m:pr:t:x" )) != -1 )
00246 {
00247 switch( opt )
00248 {
00249 case '?':
00250 return GENERALMESHTESTC_EOPT;
00251 case 'a':
00252 metric_code = atoi( optarg );
00253 break;
00254 case 'b':
00255 begin = atoi( optarg );
00256 break;
00257 case 'c':
00258 if( sscanf( optarg, "%f:%f:%f:%f:%f:%f", &a, &b, &c, &d, &e, &f ) != 6)
00259 {
00260 fprintf( stderr, "coordinates should be hh:mm:ss:dd:mm:ss\n" );
00261 return GENERALMESHTESTC_EOPT;
00262 }
00263 center.longitude = (15*a+b/4+c/240)*LAL_PI_180;
00264 center.latitude = (d+e/60+f/3600)*LAL_PI_180;
00265 break;
00266 case 'd':
00267 detector = atoi( optarg );
00268 break;
00269 case 'e':
00270 lalDebugLevel = 1;
00271 break;
00272 case 'f':
00273 fMax = atof( optarg );
00274 break;
00275 case 'l':
00276 if( sscanf( optarg, "%f:%f:%f:%f",
00277 &ra_min, &ra_max, &dec_min, &dec_max) != 4)
00278 {
00279 fprintf( stderr, "coordinates should be ra_min, ra_max, dec_min, dec_max, all in degrees\n" );
00280 return GENERALMESHTESTC_EOPT;
00281 }
00282 ra_min *= LAL_PI_180;
00283 ra_max *= LAL_PI_180;
00284 dec_min *= LAL_PI_180;
00285 dec_max *= LAL_PI_180;
00286 rectangular = 1;
00287 radius = 0;
00288 break;
00289 case 'm':
00290 mismatch = atof( optarg );
00291 break;
00292 case 'p':
00293 nonGrace = 1;
00294 break;
00295 case 'r':
00296 if( rectangular == 1 )
00297 break;
00298 radius = LAL_PI_180/60*atof( optarg );
00299 if( radius < 0 ) {
00300 fprintf( stderr, "%s line %d: %s\n", __FILE__, __LINE__,
00301 GENERALMESHTESTC_MSGERNG );
00302 return GENERALMESHTESTC_ERNG;
00303 }
00304 break;
00305 case 't':
00306 duration = atof( optarg );
00307 if( duration < MIN_DURATION || duration > MAX_DURATION ) {
00308 fprintf( stderr, "%s line %d: %s\n", __FILE__, __LINE__,
00309 GENERALMESHTESTC_MSGERNG );
00310 return GENERALMESHTESTC_ERNG;
00311 }
00312 break;
00313 case 'x':
00314 grace = 1;
00315 break;
00316 }
00317 }
00318
00319
00320
00321 mesh.mThresh = mismatch;
00322 mesh.nIn = MAX_NODES;
00323 mesh.getRange = getRange;
00324 mesh.getMetric = getMetric;
00325 if(metric_code==1)
00326 mesh.metricParams = (void *) &search;
00327 if(metric_code==2 || metric_code==3)
00328 mesh.metricParams = (void *) &tevparam;
00329 if( radius == 0 )
00330 {
00331 mesh.domain[0] = dec_min;
00332 mesh.domain[1] = dec_max;
00333
00334 if(metric_code==1)
00335 mesh.rangeParams = (void *) &search;
00336 if(metric_code==2 || metric_code==3)
00337 {
00338 mesh.rangeParams = (void *) &tevparam;
00339 }
00340 }
00341 else
00342 {
00343 mesh.domain[0] = center.latitude - radius;
00344 mesh.domain[1] = center.latitude + radius;
00345 mesh.rangeParams = NULL;
00346 }
00347
00348
00349
00350 if(detector==1)
00351 tevpulse.site = &lalCachedDetectors[LALDetectorIndexLHODIFF];
00352 if(detector==2)
00353 tevpulse.site = &lalCachedDetectors[LALDetectorIndexLLODIFF];
00354 if(detector==3)
00355 tevpulse.site = &lalCachedDetectors[LALDetectorIndexVIRGODIFF];
00356 if(detector==4)
00357 tevpulse.site = &lalCachedDetectors[LALDetectorIndexGEO600DIFF];
00358 if(detector==5)
00359 tevpulse.site = &lalCachedDetectors[LALDetectorIndexTAMA300DIFF];
00360
00361 search.site = tevpulse.site;
00362 tevpulse.latitude = search.site->frDetector.vertexLatitudeRadians;
00363 tevpulse.longitude = search.site->frDetector.vertexLongitudeRadians;
00364
00365
00366
00367 search.position.system = COORDINATESYSTEM_EQUATORIAL;
00368 search.spindown = NULL;
00369 search.epoch.gpsSeconds = begin;
00370 search.epoch.gpsNanoSeconds = 0;
00371 search.duration = duration;
00372 search.maxFreq = fMax;
00373
00374
00375
00376 tevlambda = NULL;
00377 LALDCreateVector( &stat, &tevlambda, 3 );
00378 tevlambda->data[0] = fMax;
00379 tevparam.constants = &tevpulse;
00380 tevparam.n = 1;
00381 tevparam.errors = 0;
00382 tevparam.start = 0;
00383 tevpulse.t0 = 0.0;
00384 tevpulse.epoch.gpsSeconds = begin;
00385 tevpulse.epoch.gpsNanoSeconds = 0;
00386 tevparam.deltaT = duration;
00387
00388
00389
00390 LALGetEarthTimes( &stat, &tevpulse );
00391
00392
00393 eph = (EphemerisData *)LALMalloc(sizeof(EphemerisData));
00394 eph->ephiles.earthEphemeris = earth;
00395 eph->ephiles.sunEphemeris = sun;
00396 eph->leap = 13;
00397
00398
00399 LALInitBarycenter( &stat, eph );
00400
00401 tevpulse.ephemeris = eph;
00402
00403
00404 if(metric_code==1)
00405 {
00406 printf("Using PtoleMetric()\n");
00407 }
00408 if(metric_code==2)
00409 {
00410 printf("Using CoherentMetric() with the BTBaryPtolemaic timing function\n");
00411 tevparam.dtCanon = LALDTBaryPtolemaic;
00412 }
00413 if(metric_code==3)
00414 {
00415 printf("Using CoherentMetric() with the DTEphemeris timing function\n");
00416 tevparam.dtCanon = LALDTEphemeris;
00417 }
00418
00419
00420
00421 firstNode = NULL;
00422 LALCreateTwoDMesh( &stat, &firstNode, &mesh );
00423 if( stat.statusCode )
00424 return stat.statusCode;
00425 printf( "created %d nodes\n", mesh.nOut );
00426 if( mesh.nOut == MAX_NODES )
00427 printf( "This overflowed your limit. Try a smaller search.\n" );
00428
00429
00430
00431 if(grace)
00432 {
00433 TwoDMeshNode *node;
00434 fp = popen( "xmgrace -pipe", "w" );
00435 if( !fp )
00436 return GENERALMESHTESTC_EFIO;
00437 fprintf( fp, "@xaxis label \"Right ascension (degrees)\"\n" );
00438 fprintf( fp, "@yaxis label \"Declination (degrees)\"\n" );
00439 fprintf( fp, "@s0 line type 0\n");
00440 fprintf( fp, "@s0 symbol 8\n");
00441 fprintf( fp, "@s0 symbol size 0.300000\n");
00442 for( node = firstNode; node; node = node->next )
00443 fprintf( fp, "%e %e\n",
00444 (double)((node->y)*180/LAL_PI), (double)((node->x)*180/LAL_PI));
00445 fclose( fp );
00446 }
00447
00448
00449
00450 if( nonGrace )
00451 {
00452 TwoDMeshNode *node;
00453 fp = fopen( "mesh.dat", "w" );
00454 if( !fp )
00455 return GENERALMESHTESTC_EFIO;
00456
00457 for( node = firstNode; node; node = node->next )
00458 fprintf( fp, "%e %e\n",
00459 (double)((node->y)*180/LAL_PI), (double)((node->x)*180/LAL_PI));
00460 fclose( fp );
00461 }
00462
00463
00464 LALDestroyTwoDMesh( &stat, &firstNode, &mesh.nOut );
00465 printf( "destroyed %d nodes\n", mesh.nOut );
00466 if( stat.statusCode )
00467 return GENERALMESHTESTC_EMEM;
00468
00469 LALDDestroyVector( &stat, &tevlambda );
00470
00471 LALFree( eph->ephemE );
00472 if( stat.statusCode )
00473 {
00474 printf( "%s line %d: %s\n", __FILE__, __LINE__,
00475 GENERALMESHTESTC_MSGEMEM );
00476 return GENERALMESHTESTC_EMEM;
00477 }
00478 LALFree( eph->ephemS );
00479 if( stat.statusCode )
00480 {
00481 printf( "%s line %d: %s\n", __FILE__, __LINE__,
00482 GENERALMESHTESTC_MSGEMEM );
00483 return GENERALMESHTESTC_EMEM;
00484 }
00485 LALFree( eph );
00486 if( stat.statusCode )
00487 {
00488 printf( "%s line %d: %s\n", __FILE__, __LINE__,
00489 GENERALMESHTESTC_MSGEMEM );
00490 return GENERALMESHTESTC_EMEM;
00491 }
00492
00493
00494 LALCheckMemoryLeaks();
00495 return 0;
00496 }
00497
00498
00499
00500
00501 void getRange( LALStatus *stat, REAL4 y[2], REAL4 x, void *unused )
00502 {
00503
00504 INITSTATUS( stat, "getRange", GENERALMESHTESTC );
00505 ATTATCHSTATUSPTR( stat );
00506
00507
00508 y[0] = center.longitude - sqrt( pow( radius*1.001, 2 )
00509 - pow( x-center.latitude, 2 ) );
00510 y[1] = center.longitude + sqrt( pow( radius*1.001, 2 )
00511 - pow( x-center.latitude, 2 ) );
00512
00513 if( unused )
00514 {
00515 y[0] = ra_min;
00516 y[1] = ra_max;
00517 }
00518
00519
00520
00521
00522
00523
00524
00525 DETATCHSTATUSPTR( stat );
00526 RETURN( stat );
00527 }
00528
00529
00530
00531
00532 void getMetric( LALStatus *stat,
00533 REAL4 g[3],
00534 REAL4 x[2],
00535 void *params )
00536 {
00537
00538 REAL8Vector *metric = NULL;
00539 PtoleMetricIn *Ppatch = NULL;
00540 MetricParamStruc *Cpatch = NULL;
00541 REAL8 determinant;
00542
00543
00544 if(metric_code==1)
00545 Ppatch = params;
00546
00547 if(metric_code==2 || metric_code==3)
00548 Cpatch = params;
00549
00550
00551
00552 INITSTATUS( stat, "getMetric", GENERALMESHTESTC );
00553 ATTATCHSTATUSPTR( stat );
00554 TRY( LALDCreateVector( stat->statusPtr, &metric, 6 ), stat );
00555
00556
00557 if(metric_code==1)
00558 {
00559 Ppatch->position.longitude = x[1];
00560 Ppatch->position.latitude = x[0];
00561 }
00562
00563 if(metric_code==2 || metric_code==3)
00564 {
00565 tevlambda->data[1] = x[1];
00566 tevlambda->data[2] = x[0];
00567 }
00568
00569
00570
00571 if(metric_code==1)
00572 LALPtoleMetric( stat->statusPtr, metric, Ppatch );
00573 if(metric_code==2 || metric_code==3)
00574 LALCoherentMetric( stat->statusPtr, metric, tevlambda, Cpatch );
00575
00576 BEGINFAIL( stat )
00577 TRY( LALDDestroyVector( stat->statusPtr, &metric ), stat );
00578 ENDFAIL( stat );
00579 LALProjectMetric( stat->statusPtr, metric, 0 );
00580 BEGINFAIL( stat )
00581 TRY( LALDDestroyVector( stat->statusPtr, &metric ), stat );
00582 ENDFAIL( stat );
00583
00584 determinant = metric->data[5]*metric->data[2]-pow(metric->data[4],2.0);
00585 if(determinant < 0.0)
00586 {
00587 printf( "%s line %d: %s\n", __FILE__, __LINE__,
00588 GENERALMESHTESTC_MSGEMET );
00589 return;
00590 }
00591
00592
00593
00594
00595 g[1] = metric->data[2];
00596 g[0] = metric->data[5];
00597 g[2] = metric->data[4];
00598
00599
00600
00601 TRY( LALDDestroyVector( stat->statusPtr, &metric ), stat );
00602 DETATCHSTATUSPTR( stat );
00603 RETURN( stat );
00604
00605
00606 }