Changeset 2048 for trunk/psLib/src/astronomy/psTime.c
- Timestamp:
- Oct 11, 2004, 3:34:09 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/astronomy/psTime.c (modified) (41 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/astronomy/psTime.c
r2032 r2048 11 11 * @author Ross Harman, MHPCC 12 12 * 13 * @version $Revision: 1.3 0$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-10- 09 00:17:14$13 * @version $Revision: 1.31 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-10-12 01:34:09 $ 15 15 * 16 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 27 27 #include "psAbort.h" 28 28 #include "psImage.h" 29 #include "psCoord.h" 29 30 30 31 #include "psAstronomyErrors.h" 32 33 #ifndef SER7_FILE 34 #define SER7_FILE "../../data/ser7.dat" 35 #endif 36 37 #ifndef TAIUTC_FILE 38 #define TAIUTC_FILE "../../data/tai-utc.dat" 39 #endif 31 40 32 41 /** Sidereal angular conversion from seconds to radians for GMST in seconds (i.e. pi/(180*240)) */ … … 61 70 62 71 /** Preprocessor macro to check for null psTime struct */ 63 #define CHECK_NULL_TIME(TIME,RETURN) \64 if(TIME == NULL) { \65 psError(__func__,"NULL value not allowed"); \66 return RETURN; \72 #define CHECK_NULL_TIME(TIME,RETURN) \ 73 if(TIME == NULL) { \ 74 psError(__func__,"NULL value not allowed"); \ 75 return RETURN; \ 67 76 } 68 77 69 78 /** Preprocessor macro to allocate psTime struct */ 70 #define ALLOC_TIME(TIME) \71 TIME = (psTime*)psAlloc(sizeof(psTime)); \72 if (TIME == NULL) { \73 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); \74 } \75 TIME->sec = 0; \76 TIME->usec = 0; \79 #define ALLOC_TIME(TIME) \ 80 TIME = (psTime*)psAlloc(sizeof(psTime)); \ 81 if (TIME == NULL) { \ 82 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); \ 83 } \ 84 TIME->sec = 0; \ 85 TIME->usec = 0; \ 77 86 TIME->type = PS_TIME_TAI; 78 87 79 80 psImage* readTaiUtcFile(char *fileName) 88 /** Read TAI/UTC data file. 89 * 90 * Reads TAI/UTC data file. 91 * 92 * @return psImage*: Two-dimensional array with table data. 93 */ 94 static psImage* readTaiUtcFile( 95 const char *fileName ///< Name of file to read. 96 ); 97 98 99 /** Read SER7 data file. 100 * 101 * Reads SER7 data file. 102 * 103 * @return psImage*: Two-dimensional array with table data. 104 */ 105 static psImage* readSer7File( 106 const char *fileName ///< Name of file to read. 107 ); 108 109 110 /** Read finals data file. 111 * 112 * Reads finals data file. 113 * 114 * @return psImage*: Two-dimensional array with table data. 115 */ 116 /* not yet used 117 static psImage* readFinalsFile( 118 char *fileName ///< Name of file to read. 119 ); 120 */ 121 122 /** Read EOPC data file. 123 * 124 * Reads EOPC data file. 125 * 126 * @return psImage*: Two-dimensional array with table data. 127 */ 128 /* not yet used 129 static psImage* readEopcFile( 130 char *fileName ///< Name of file to read. 131 ); 132 */ 133 134 /** Lookup and interpolate TAI-UTC data 135 * 136 * Interpolates TAI-UTC data, given time in MJD format. 137 * 138 * @return double: Interpolated value. 139 */ 140 static double lookupTaiUtcTable( 141 const psTime *time ///< time to lookup. 142 ); 143 144 /** Lookup and interpolate polar or UT1-UTC data 145 * 146 * Interpolates TAI-UT1 or polar coordinate data, given time in MJD format. 147 * 148 * @return double: Interpolated value. 149 */ 150 static double lookupSer7Table( 151 const psTime *time, ///< time to lookup. 152 int col ///< Column to lookup. 153 ); 154 155 static psImage* readTaiUtcFile(const char *fileName) 81 156 { 82 157 char line[LINESIZE]; … … 113 188 *(unsigned int *)&table->numRows = j; 114 189 190 p_psMemSetPersistent(table,true); 191 p_psMemSetPersistent(table->data.V,true); 192 p_psMemSetPersistent(table->rawDataBuffer,true); 193 115 194 return table; 116 195 } 117 196 118 psImage* readSer7File(char *fileName)197 static psImage* readSer7File(const char *fileName) 119 198 { 120 199 bool beginRecord = false; … … 160 239 *(unsigned int *)&table->numRows = j; 161 240 241 p_psMemSetPersistent(table,true); 242 p_psMemSetPersistent(table->data.V,true); 243 p_psMemSetPersistent(table->rawDataBuffer,true); 244 162 245 return table; 163 246 } 164 247 165 psImage* readEopcFile(char *fileName) 248 /* Not yet used... 249 static psImage* readEopcFile(char *fileName) 166 250 { 167 251 char line[LINESIZE]; … … 171 255 psImage *table = NULL; 172 256 FILE *fd = NULL; 173 174 257 258 175 259 fd=fopen(fileName, "r"); 176 260 if(fd==NULL) { … … 178 262 return NULL ; 179 263 } 180 264 181 265 // Table shouldn't be larger than 2500 rows. All columns are read. 182 266 table = psImageAlloc(11, maxLines, PS_TYPE_F64); … … 186 270 } 187 271 } 188 272 189 273 while(fgets(line, LINESIZE, fd) != NULL) { 190 274 if(j>=maxLines) { … … 197 281 ptr, ptr+1, ptr+2, ptr+3, ptr+4, ptr+5, ptr+6, ptr+7, ptr+8, ptr+9, ptr+10); 198 282 } 199 283 200 284 *(unsigned int *)&table->numRows = j; 201 285 202 286 return table; 203 287 } 204 205 psImage* readFinalsFile(char *fileName)288 289 static psImage* readFinalsFile(char *fileName) 206 290 { 207 291 char line[LINESIZE]; … … 210 294 psImage *table = NULL; 211 295 FILE *fd = NULL; 212 213 296 297 214 298 fd=fopen(fileName, "r"); 215 299 if(fd==NULL) { … … 217 301 return NULL ; 218 302 } 219 303 220 304 // Table shouldn't be larger than 15000 rows. Select columns are read. 221 305 table = psImageAlloc(6, maxLines, PS_TYPE_F64); … … 226 310 psFree(table); 227 311 } 228 312 229 313 psF64 *ptr = table->data.F64[j++]; 230 314 ptr = table->data.F64[j++]; 231 315 232 316 // Sometimes there are blank entries in this table, so check for a space to determine if there is no entry 233 317 if(*(line+7) == ' ') { // MJD … … 236 320 *ptr = atof(line+7); 237 321 } 238 322 239 323 if(*(line+18) == ' ') { // Polar motion X 240 324 *(ptr+1) = 0.0; … … 242 326 *(ptr+1) = atof(line+18); 243 327 } 244 328 245 329 if(*(line+27) == ' ') { // Polar motion X error 246 330 *(ptr+2) = 0.0; … … 248 332 *(ptr+2) = atof(line+27); 249 333 } 250 334 251 335 if(*(line+37) == ' ') { // Polar motion Y 252 336 *(ptr+3) = 0.0; … … 254 338 *(ptr+3) = atof(line+37); 255 339 } 256 340 257 341 if(*(line+46) == ' ') { // Polar motion Y error 258 342 *(ptr+4) = 0.0; … … 260 344 *(ptr+4) = atof(line+46); 261 345 } 262 346 263 347 if(*(line+58) == ' ') { // UT1-UTC 264 348 *(ptr+5) = 0.0; … … 267 351 } 268 352 } 269 353 270 354 *(unsigned int *)&table->numRows = j; 271 355 272 356 return table; 273 357 } 274 275 276 double lookupTaiUtcTable(psImage *table,psTime *time)358 */ 359 360 static double lookupTaiUtcTable(const psTime *time) 277 361 { 278 362 int hiIdx = 0; … … 286 370 double const2 = 0.0; 287 371 double const3 = 0.0; 288 372 static psImage* table = NULL; 373 374 if (table == NULL) { 375 table = readTaiUtcFile(TAIUTC_FILE); 376 if (table == NULL) { 377 psAbort(__func__,"Failed to read tai-utc.dat file"); 378 } 379 } 289 380 290 381 // Number of rows is the number of rows of data read from the data file … … 337 428 } 338 429 339 double lookupSer7Table(psImage *table,psTime *time, int col)430 static double lookupSer7Table(const psTime *time, int col) 340 431 { 341 432 int hiIdx = 0; … … 345 436 double denom = 0.0; 346 437 double mjdUtc = 0.0; 347 438 static psImage* table = NULL; 439 440 if (table == NULL) { 441 table = readSer7File(SER7_FILE); 442 if (table == NULL) { 443 psAbort(__func__,"Failed to read the ser7.dat file."); 444 } 445 } 348 446 349 447 // Number of rows is the number of rows of data read from the data file … … 404 502 // Add most leapseconds to UTC time to get TAI time if necessary 405 503 if(type == PS_TIME_TAI) { 406 time->sec += ps GetTAIDelta(time);504 time->sec += psTimeGetTAIDelta(time); 407 505 } 408 506 … … 420 518 } 421 519 422 delta = ps GetTAIDelta(time);520 delta = psTimeGetTAIDelta(time); 423 521 424 522 if(type == PS_TIME_UTC) { … … 461 559 taiTime = psMemIncrRefCounter(time); 462 560 ALLOC_TIME(utcTime); 463 utcTime->sec = taiTime->sec - ps GetTAIDelta(time);561 utcTime->sec = taiTime->sec - psTimeGetTAIDelta(time); 464 562 utcTime->usec = taiTime->usec; 465 563 utcTime->type = PS_TIME_UTC; … … 467 565 utcTime = psMemIncrRefCounter(time); 468 566 ALLOC_TIME(taiTime); 469 taiTime->sec = utcTime->sec + ps GetTAIDelta(time);567 taiTime->sec = utcTime->sec + psTimeGetTAIDelta(time); 470 568 taiTime->usec = utcTime->usec; 471 569 taiTime->type = PS_TIME_TAI; … … 475 573 ALLOC_TIME(ut1UtcDelta); 476 574 ut1UtcDelta->type = PS_TIME_UTC; 477 ut1UtcDelta->usec = ps GetUT1Delta(utcTime)*1e6;575 ut1UtcDelta->usec = psTimeGetUT1Delta(utcTime)*1e6; 478 576 ut1Time = psTimeAdd(utcTime, ut1UtcDelta); 479 577 … … 516 614 } 517 615 518 double psGetUT1Delta(psTime *time) 519 { 520 static psImage* iersTable = NULL; 521 double ut1UtcDeltaUsec = 0.0; 522 523 524 if (iersTable == NULL) { 525 iersTable = readSer7File("../../data/ser7.dat"); 526 p_psMemSetPersistent(iersTable,true); 527 p_psMemSetPersistent(iersTable->data.V,true); 528 p_psMemSetPersistent(iersTable->rawDataBuffer,true); 529 } 530 531 ut1UtcDeltaUsec = lookupSer7Table(iersTable, time, 6)*1.0e6; 532 533 return ut1UtcDeltaUsec; 534 } 535 536 double psGetTAIDelta(psTime *time) 537 { 538 double delta = 0.0; 539 static psImage* taiUtcTable = NULL; 540 541 542 if (taiUtcTable == NULL) { 543 taiUtcTable = readTaiUtcFile("../../data/tai-utc.dat"); 544 p_psMemSetPersistent(taiUtcTable,true); 545 p_psMemSetPersistent(taiUtcTable->data.V,true); 546 p_psMemSetPersistent(taiUtcTable->rawDataBuffer,true); 547 } 548 delta = lookupTaiUtcTable(taiUtcTable, time); 549 550 return delta; 551 } 552 553 psF64 psTimeLeapseconds(const psTime *time1, const psTime *time2) 554 { 555 psF64 diff = 0.0; 616 double psTimeGetUT1Delta(const psTime *time) 617 { 618 return lookupSer7Table(time, 6); 619 } 620 621 struct psSphere* psTimeGetPoleCoords(const psTime* time) 622 { 623 624 double x = lookupSer7Table((psTime*)time, 4); 625 double y = lookupSer7Table((psTime*)time, 5); 626 627 struct psSphere* output = psAlloc(sizeof(psSphere)); 628 output->r = x * M_PI / 648000.0; // x in arcsec converted to radians, i.e., x/60/60*M_PI/180 629 output->d = y * M_PI / 648000.0; 630 631 return output; 632 } 633 634 double psTimeGetTAIDelta(const psTime *time) 635 { 636 return lookupTaiUtcTable(time); 637 } 638 639 long psTimeLeapseconds(const psTime *time1, const psTime *time2) 640 { 641 long diff = 0; 556 642 557 643 // NULL error checks … … 559 645 CHECK_NULL_TIME(time2,0); 560 646 561 diff = fabs(psGetTAIDelta((psTime*)time1)-psGetTAIDelta((psTime*)time2));647 diff = abs(psTimeGetTAIDelta((psTime*)time1)-psTimeGetTAIDelta((psTime*)time2)); 562 648 563 649 return diff; 564 650 } 565 651 566 double psTimeToJD( psTime *time)652 double psTimeToJD(const psTime *time) 567 653 { 568 654 double jd = 0.0; … … 582 668 } 583 669 584 double psTimeToMJD( psTime *time)670 double psTimeToMJD(const psTime *time) 585 671 { 586 672 double mjd = 0.0; … … 600 686 } 601 687 602 char* psTimeToISO (psTime *time)688 char* psTimeToISOTime(const psTime *time) 603 689 { 604 690 int ms = 0; … … 634 720 } 635 721 636 struct timeval psTimeToTimeval( psTime *time)722 struct timeval psTimeToTimeval(const psTime *time) 637 723 { 638 724 struct timeval timevalTime; … … 648 734 } 649 735 650 struct tm* psTimeToTM( psTime *time)736 struct tm* psTimeToTM(const psTime *time) 651 737 { 652 738 long cent = 0; … … 711 797 } 712 798 713 psTime* ps JDToTime(double time)799 psTime* psTimeFromJD(double time) 714 800 { 715 801 double days = 0.0; … … 734 820 } 735 821 736 psTime* ps MJDToTime(double time)822 psTime* psTimeFromMJD(double time) 737 823 { 738 824 double days = 0.0; … … 757 843 } 758 844 759 psTime* ps ISOToTime(char *time)845 psTime* psTimeFromISOTime(const char *time) 760 846 { 761 847 char tempString[MAX_TIME_STRING_LENGTH]; … … 826 912 827 913 // Convert tm time to psTime 828 outTime = psT MToTime(&tmTime);914 outTime = psTimeFromTM(&tmTime); 829 915 outTime->usec = millisecond * 1000; 830 916 … … 832 918 } 833 919 834 psTime* psTime valToTime(struct timeval *time)920 psTime* psTimeFromTimeval(const struct timeval *time) 835 921 { 836 922 psTime *outTime = NULL; … … 850 936 } 851 937 852 psTime* psT MToTime(struct tm* time)938 psTime* psTimeFromTM(const struct tm* time) 853 939 { 854 940 long year; … … 905 991 } 906 992 907 psTime* psTimeAdd( psTime *tai1,psTime *tai2)993 psTime* psTimeAdd(const psTime *tai1, const psTime *tai2) 908 994 { 909 995 psTime *outTime = NULL; … … 929 1015 } 930 1016 931 psTime* psTimeSub (psTime *tai1,psTime *tai2)1017 psTime* psTimeSubtract(const psTime *tai1, const psTime *tai2) 932 1018 { 933 1019 psTime *outTime = NULL; … … 953 1039 } 954 1040 955 psTime* psTimeDelta( psTime *tai1,psTime *tai2)1041 psTime* psTimeDelta(const psTime *tai1, const psTime *tai2) 956 1042 { 957 1043 psTime *outTime = NULL;
Note:
See TracChangeset
for help on using the changeset viewer.
