1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
|
/***************************************************************************
* traceutils.c:
*
* Generic routines to handle Traces.
*
* Written by Chad Trabant, IRIS Data Management Center
*
* modified: 2015.108
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
static int mst_groupsort_cmp (MSTrace *mst1, MSTrace *mst2, flag quality);
/***************************************************************************
* mst_init:
*
* Initialize and return a MSTrace struct, allocating memory if needed.
* If the specified MSTrace includes data samples they will be freed.
*
* Returns a pointer to a MSTrace struct on success or NULL on error.
***************************************************************************/
MSTrace *
mst_init (MSTrace *mst)
{
/* Free datasamples, prvtptr and stream state if present */
if (mst)
{
if (mst->datasamples)
free (mst->datasamples);
if (mst->prvtptr)
free (mst->prvtptr);
if (mst->ststate)
free (mst->ststate);
}
else
{
mst = (MSTrace *)malloc (sizeof (MSTrace));
}
if (mst == NULL)
{
ms_log (2, "mst_init(): Cannot allocate memory\n");
return NULL;
}
memset (mst, 0, sizeof (MSTrace));
return mst;
} /* End of mst_init() */
/***************************************************************************
* mst_free:
*
* Free all memory associated with a MSTrace struct and set the pointer
* to 0.
***************************************************************************/
void
mst_free (MSTrace **ppmst)
{
if (ppmst && *ppmst)
{
/* Free datasamples if present */
if ((*ppmst)->datasamples)
free ((*ppmst)->datasamples);
/* Free private memory if present */
if ((*ppmst)->prvtptr)
free ((*ppmst)->prvtptr);
/* Free stream processing state if present */
if ((*ppmst)->ststate)
free ((*ppmst)->ststate);
free (*ppmst);
*ppmst = 0;
}
} /* End of mst_free() */
/***************************************************************************
* mst_initgroup:
*
* Initialize and return a MSTraceGroup struct, allocating memory if
* needed. If the supplied MSTraceGroup is not NULL any associated
* memory it will be freed.
*
* Returns a pointer to a MSTraceGroup struct on success or NULL on error.
***************************************************************************/
MSTraceGroup *
mst_initgroup (MSTraceGroup *mstg)
{
MSTrace *mst = 0;
MSTrace *next = 0;
if (mstg)
{
mst = mstg->traces;
while (mst)
{
next = mst->next;
mst_free (&mst);
mst = next;
}
}
else
{
mstg = (MSTraceGroup *)malloc (sizeof (MSTraceGroup));
}
if (mstg == NULL)
{
ms_log (2, "mst_initgroup(): Cannot allocate memory\n");
return NULL;
}
memset (mstg, 0, sizeof (MSTraceGroup));
return mstg;
} /* End of mst_initgroup() */
/***************************************************************************
* mst_freegroup:
*
* Free all memory associated with a MSTraceGroup struct and set the
* pointer to 0.
***************************************************************************/
void
mst_freegroup (MSTraceGroup **ppmstg)
{
MSTrace *mst = 0;
MSTrace *next = 0;
if (*ppmstg)
{
mst = (*ppmstg)->traces;
while (mst)
{
next = mst->next;
mst_free (&mst);
mst = next;
}
free (*ppmstg);
*ppmstg = 0;
}
} /* End of mst_freegroup() */
/***************************************************************************
* mst_findmatch:
*
* Traverse the MSTrace chain starting at 'startmst' until a MSTrace
* is found that matches the given name identifiers. If the dataquality
* byte is not 0 it must also match.
*
* Return a pointer a matching MSTrace otherwise 0 if no match found.
***************************************************************************/
MSTrace *
mst_findmatch (MSTrace *startmst, char dataquality,
char *network, char *station, char *location, char *channel)
{
int idx;
if (!startmst || !network || !station || !location || !channel)
return 0;
while (startmst)
{
if (dataquality && dataquality != startmst->dataquality)
{
startmst = startmst->next;
continue;
}
/* Compare network */
idx = 0;
while (network[idx] == startmst->network[idx])
{
if (network[idx] == '\0')
break;
idx++;
}
if (network[idx] != '\0' || startmst->network[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* Compare station */
idx = 0;
while (station[idx] == startmst->station[idx])
{
if (station[idx] == '\0')
break;
idx++;
}
if (station[idx] != '\0' || startmst->station[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* Compare location */
idx = 0;
while (location[idx] == startmst->location[idx])
{
if (location[idx] == '\0')
break;
idx++;
}
if (location[idx] != '\0' || startmst->location[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* Compare channel */
idx = 0;
while (channel[idx] == startmst->channel[idx])
{
if (channel[idx] == '\0')
break;
idx++;
}
if (channel[idx] != '\0' || startmst->channel[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* A match was found if we made it this far */
break;
}
return startmst;
} /* End of mst_findmatch() */
/***************************************************************************
* mst_findadjacent:
*
* Find a MSTrace in a MSTraceGroup matching the given name
* identifiers, samplerate and is adjacent with a time span. If the
* dataquality byte is not 0 it must also match.
*
* The time tolerance and sample rate tolerance are used to determine
* if traces abut. If timetol is -1.0 the default tolerance of 1/2
* the sample period will be used. If samprratetol is -1.0 the
* default tolerance check of abs(1-sr1/sr2) < 0.0001 is used (defined
* in libmseed.h). If timetol or sampratetol is -2.0 the respective
* tolerance check will not be performed.
*
* The 'whence' flag will be set, when a matching MSTrace is found, to
* indicate where the indicated time span is adjacent to the MSTrace
* using the following values:
* 1: time span fits at the end of the MSTrace
* 2: time span fits at the beginning of the MSTrace
*
* Return a pointer a matching MSTrace and set the 'whence' flag
* otherwise 0 if no match found.
***************************************************************************/
MSTrace *
mst_findadjacent (MSTraceGroup *mstg, flag *whence, char dataquality,
char *network, char *station, char *location, char *channel,
double samprate, double sampratetol,
hptime_t starttime, hptime_t endtime, double timetol)
{
MSTrace *mst = 0;
hptime_t pregap;
hptime_t postgap;
hptime_t hpdelta;
hptime_t hptimetol = 0;
hptime_t nhptimetol = 0;
int idx;
if (!mstg || !whence || !network || !station || !location || !channel)
return 0;
*whence = 0;
/* Calculate high-precision sample period */
hpdelta = (hptime_t) ((samprate) ? (HPTMODULUS / samprate) : 0.0);
/* Calculate high-precision time tolerance */
if (timetol == -1.0)
hptimetol = (hptime_t) (0.5 * hpdelta); /* Default time tolerance is 1/2 sample period */
else if (timetol >= 0.0)
hptimetol = (hptime_t) (timetol * HPTMODULUS);
nhptimetol = (hptimetol) ? -hptimetol : 0;
mst = mstg->traces;
while (mst)
{
/* post/pregap are negative when the record overlaps the trace
* segment and positive when there is a time gap. */
postgap = starttime - mst->endtime - hpdelta;
pregap = mst->starttime - endtime - hpdelta;
/* If not checking the time tolerance decide if beginning or end is a better fit */
if (timetol == -2.0)
{
if (ms_dabs ((double)postgap) < ms_dabs ((double)pregap))
*whence = 1;
else
*whence = 2;
}
else
{
if (postgap <= hptimetol && postgap >= nhptimetol)
{
/* Span fits right at the end of the trace */
*whence = 1;
}
else if (pregap <= hptimetol && pregap >= nhptimetol)
{
/* Span fits right at the beginning of the trace */
*whence = 2;
}
else
{
/* Span does not fit with this Trace */
mst = mst->next;
continue;
}
}
/* Perform samprate tolerance check if requested */
if (sampratetol != -2.0)
{
/* Perform default samprate tolerance check if requested */
if (sampratetol == -1.0)
{
if (!MS_ISRATETOLERABLE (samprate, mst->samprate))
{
mst = mst->next;
continue;
}
}
/* Otherwise check against the specified sample rate tolerance */
else if (ms_dabs (samprate - mst->samprate) > sampratetol)
{
mst = mst->next;
continue;
}
}
/* Compare data qualities */
if (dataquality && dataquality != mst->dataquality)
{
mst = mst->next;
continue;
}
/* Compare network */
idx = 0;
while (network[idx] == mst->network[idx])
{
if (network[idx] == '\0')
break;
idx++;
}
if (network[idx] != '\0' || mst->network[idx] != '\0')
{
mst = mst->next;
continue;
}
/* Compare station */
idx = 0;
while (station[idx] == mst->station[idx])
{
if (station[idx] == '\0')
break;
idx++;
}
if (station[idx] != '\0' || mst->station[idx] != '\0')
{
mst = mst->next;
continue;
}
/* Compare location */
idx = 0;
while (location[idx] == mst->location[idx])
{
if (location[idx] == '\0')
break;
idx++;
}
if (location[idx] != '\0' || mst->location[idx] != '\0')
{
mst = mst->next;
continue;
}
/* Compare channel */
idx = 0;
while (channel[idx] == mst->channel[idx])
{
if (channel[idx] == '\0')
break;
idx++;
}
if (channel[idx] != '\0' || mst->channel[idx] != '\0')
{
mst = mst->next;
continue;
}
/* A match was found if we made it this far */
break;
}
return mst;
} /* End of mst_findadjacent() */
/***************************************************************************
* mst_addmsr:
*
* Add MSRecord time coverage to a MSTrace. The start or end time will
* be updated and samples will be copied if they exist. No checking
* is done to verify that the record matches the trace in any way.
*
* If whence is 1 the coverage will be added at the end of the trace,
* whereas if whence is 2 the coverage will be added at the beginning
* of the trace.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
mst_addmsr (MSTrace *mst, MSRecord *msr, flag whence)
{
int samplesize = 0;
if (!mst || !msr)
return -1;
/* Reallocate data sample buffer if samples are present */
if (msr->datasamples && msr->numsamples >= 0)
{
/* Check that the entire record was decompressed */
if (msr->samplecnt != msr->numsamples)
{
ms_log (2, "mst_addmsr(): Sample counts do not match, record not fully decompressed?\n");
ms_log (2, " The sample buffer will likely contain a discontinuity.\n");
}
if ((samplesize = ms_samplesize (msr->sampletype)) == 0)
{
ms_log (2, "mst_addmsr(): Unrecognized sample type: '%c'\n",
msr->sampletype);
return -1;
}
if (msr->sampletype != mst->sampletype)
{
ms_log (2, "mst_addmsr(): Mismatched sample type, '%c' and '%c'\n",
msr->sampletype, mst->sampletype);
return -1;
}
mst->datasamples = realloc (mst->datasamples,
(size_t) (mst->numsamples * samplesize + msr->numsamples * samplesize));
if (mst->datasamples == NULL)
{
ms_log (2, "mst_addmsr(): Cannot allocate memory\n");
return -1;
}
}
/* Add samples at end of trace */
if (whence == 1)
{
if (msr->datasamples && msr->numsamples >= 0)
{
memcpy ((char *)mst->datasamples + (mst->numsamples * samplesize),
msr->datasamples,
(size_t) (msr->numsamples * samplesize));
mst->numsamples += msr->numsamples;
}
mst->endtime = msr_endtime (msr);
if (mst->endtime == HPTERROR)
{
ms_log (2, "mst_addmsr(): Error calculating record end time\n");
return -1;
}
}
/* Add samples at the beginning of trace */
else if (whence == 2)
{
if (msr->datasamples && msr->numsamples >= 0)
{
/* Move any samples to end of buffer */
if (mst->numsamples > 0)
{
memmove ((char *)mst->datasamples + (msr->numsamples * samplesize),
mst->datasamples,
(size_t) (mst->numsamples * samplesize));
}
memcpy (mst->datasamples,
msr->datasamples,
(size_t) (msr->numsamples * samplesize));
mst->numsamples += msr->numsamples;
}
mst->starttime = msr->starttime;
}
/* If two different data qualities reset the MSTrace.dataquality to 0 */
if (mst->dataquality && msr->dataquality && mst->dataquality != msr->dataquality)
mst->dataquality = 0;
/* Update MSTrace sample count */
mst->samplecnt += msr->samplecnt;
return 0;
} /* End of mst_addmsr() */
/***************************************************************************
* mst_addspan:
*
* Add a time span to a MSTrace. The start or end time will be updated
* and samples will be copied if they are provided. No checking is done to
* verify that the record matches the trace in any way.
*
* If whence is 1 the coverage will be added at the end of the trace,
* whereas if whence is 2 the coverage will be added at the beginning
* of the trace.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
mst_addspan (MSTrace *mst, hptime_t starttime, hptime_t endtime,
void *datasamples, int64_t numsamples, char sampletype,
flag whence)
{
int samplesize = 0;
if (!mst)
return -1;
if (datasamples && numsamples > 0)
{
if ((samplesize = ms_samplesize (sampletype)) == 0)
{
ms_log (2, "mst_addspan(): Unrecognized sample type: '%c'\n",
sampletype);
return -1;
}
if (sampletype != mst->sampletype)
{
ms_log (2, "mst_addspan(): Mismatched sample type, '%c' and '%c'\n",
sampletype, mst->sampletype);
return -1;
}
mst->datasamples = realloc (mst->datasamples,
(size_t) (mst->numsamples * samplesize + numsamples * samplesize));
if (mst->datasamples == NULL)
{
ms_log (2, "mst_addspan(): Cannot allocate memory\n");
return -1;
}
}
/* Add samples at end of trace */
if (whence == 1)
{
if (datasamples && numsamples > 0)
{
memcpy ((char *)mst->datasamples + (mst->numsamples * samplesize),
datasamples,
(size_t) (numsamples * samplesize));
mst->numsamples += numsamples;
}
mst->endtime = endtime;
}
/* Add samples at the beginning of trace */
else if (whence == 2)
{
if (datasamples && numsamples > 0)
{
/* Move any samples to end of buffer */
if (mst->numsamples > 0)
{
memmove ((char *)mst->datasamples + (numsamples * samplesize),
mst->datasamples,
(size_t) (mst->numsamples * samplesize));
}
memcpy (mst->datasamples,
datasamples,
(size_t) (numsamples * samplesize));
mst->numsamples += numsamples;
}
mst->starttime = starttime;
}
/* Update MSTrace sample count */
if (numsamples > 0)
mst->samplecnt += numsamples;
return 0;
} /* End of mst_addspan() */
/***************************************************************************
* mst_addmsrtogroup:
*
* Add data samples from a MSRecord to a MSTrace in a MSTraceGroup by
* searching the group for the approriate MSTrace and either adding data
* to it or creating a new MSTrace if no match found.
*
* Matching traces are found using the mst_findadjacent() routine. If
* the dataquality flag is true the data quality bytes must also match
* otherwise they are ignored.
*
* Return a pointer to the MSTrace updated or 0 on error.
***************************************************************************/
MSTrace *
mst_addmsrtogroup (MSTraceGroup *mstg, MSRecord *msr, flag dataquality,
double timetol, double sampratetol)
{
MSTrace *mst = 0;
hptime_t endtime;
flag whence;
char dq;
if (!mstg || !msr)
return 0;
dq = (dataquality) ? msr->dataquality : 0;
endtime = msr_endtime (msr);
if (endtime == HPTERROR)
{
ms_log (2, "mst_addmsrtogroup(): Error calculating record end time\n");
return 0;
}
/* Find matching, time adjacent MSTrace */
mst = mst_findadjacent (mstg, &whence, dq,
msr->network, msr->station, msr->location, msr->channel,
msr->samprate, sampratetol,
msr->starttime, endtime, timetol);
/* If a match was found update it otherwise create a new MSTrace and
add to end of MSTrace chain */
if (mst)
{
/* Records with no time coverage do not contribute to a trace */
if (msr->samplecnt <= 0 || msr->samprate <= 0.0)
return mst;
if (mst_addmsr (mst, msr, whence))
{
return 0;
}
}
else
{
mst = mst_init (NULL);
mst->dataquality = dq;
strncpy (mst->network, msr->network, sizeof (mst->network));
strncpy (mst->station, msr->station, sizeof (mst->station));
strncpy (mst->location, msr->location, sizeof (mst->location));
strncpy (mst->channel, msr->channel, sizeof (mst->channel));
mst->starttime = msr->starttime;
mst->samprate = msr->samprate;
mst->sampletype = msr->sampletype;
if (mst_addmsr (mst, msr, 1))
{
mst_free (&mst);
return 0;
}
/* Link new MSTrace into the end of the chain */
if (!mstg->traces)
{
mstg->traces = mst;
}
else
{
MSTrace *lasttrace = mstg->traces;
while (lasttrace->next)
lasttrace = lasttrace->next;
lasttrace->next = mst;
}
mstg->numtraces++;
}
return mst;
} /* End of mst_addmsrtogroup() */
/***************************************************************************
* mst_addtracetogroup:
*
* Add a MSTrace to a MSTraceGroup at the end of the MSTrace chain.
*
* Return a pointer to the MSTrace added or 0 on error.
***************************************************************************/
MSTrace *
mst_addtracetogroup (MSTraceGroup *mstg, MSTrace *mst)
{
MSTrace *lasttrace;
if (!mstg || !mst)
return 0;
if (!mstg->traces)
{
mstg->traces = mst;
}
else
{
lasttrace = mstg->traces;
while (lasttrace->next)
lasttrace = lasttrace->next;
lasttrace->next = mst;
}
mst->next = 0;
mstg->numtraces++;
return mst;
} /* End of mst_addtracetogroup() */
/***************************************************************************
* mst_groupheal:
*
* Check if traces in MSTraceGroup can be healed, if contiguous segments
* belong together they will be merged. This routine is only useful
* if the trace group was assembled from segments out of time order
* (e.g. a file of Mini-SEED records not in time order) but forming
* contiguous time coverage. The MSTraceGroup will be sorted using
* mst_groupsort() before healing.
*
* The time tolerance and sample rate tolerance are used to determine
* if the traces are indeed the same. If timetol is -1.0 the default
* tolerance of 1/2 the sample period will be used. If samprratetol
* is -1.0 the default tolerance check of abs(1-sr1/sr2) < 0.0001 is
* used (defined in libmseed.h).
*
* Return number of trace mergings on success otherwise -1 on error.
***************************************************************************/
int
mst_groupheal (MSTraceGroup *mstg, double timetol, double sampratetol)
{
int mergings = 0;
MSTrace *curtrace = 0;
MSTrace *nexttrace = 0;
MSTrace *searchtrace = 0;
MSTrace *prevtrace = 0;
int8_t merged = 0;
double postgap, pregap, delta;
if (!mstg)
return -1;
/* Sort MSTraceGroup before any healing */
if (mst_groupsort (mstg, 1))
return -1;
curtrace = mstg->traces;
while (curtrace)
{
nexttrace = mstg->traces;
prevtrace = mstg->traces;
while (nexttrace)
{
searchtrace = nexttrace;
nexttrace = searchtrace->next;
/* Do not process the same MSTrace we are trying to match */
if (searchtrace == curtrace)
{
prevtrace = searchtrace;
continue;
}
/* Check if this trace matches the curtrace */
if (strcmp (searchtrace->network, curtrace->network) ||
strcmp (searchtrace->station, curtrace->station) ||
strcmp (searchtrace->location, curtrace->location) ||
strcmp (searchtrace->channel, curtrace->channel))
{
prevtrace = searchtrace;
continue;
}
/* Perform default samprate tolerance check if requested */
if (sampratetol == -1.0)
{
if (!MS_ISRATETOLERABLE (searchtrace->samprate, curtrace->samprate))
{
prevtrace = searchtrace;
continue;
}
}
/* Otherwise check against the specified sample rates tolerance */
else if (ms_dabs (searchtrace->samprate - curtrace->samprate) > sampratetol)
{
prevtrace = searchtrace;
continue;
}
merged = 0;
/* post/pregap are negative when searchtrace overlaps curtrace
segment and positive when there is a time gap. */
delta = (curtrace->samprate) ? (1.0 / curtrace->samprate) : 0.0;
postgap = ((double)(searchtrace->starttime - curtrace->endtime) / HPTMODULUS) - delta;
pregap = ((double)(curtrace->starttime - searchtrace->endtime) / HPTMODULUS) - delta;
/* Calculate default time tolerance (1/2 sample period) if needed */
if (timetol == -1.0)
timetol = 0.5 * delta;
/* Fits right at the end of curtrace */
if (ms_dabs (postgap) <= timetol)
{
/* Merge searchtrace with curtrace */
mst_addspan (curtrace, searchtrace->starttime, searchtrace->endtime,
searchtrace->datasamples, searchtrace->numsamples,
searchtrace->sampletype, 1);
/* If no data is present, make sure sample count is updated */
if (searchtrace->numsamples <= 0)
curtrace->samplecnt += searchtrace->samplecnt;
/* If qualities do not match reset the indicator */
if (curtrace->dataquality != searchtrace->dataquality)
curtrace->dataquality = 0;
merged = 1;
}
/* Fits right at the beginning of curtrace */
else if (ms_dabs (pregap) <= timetol)
{
/* Merge searchtrace with curtrace */
mst_addspan (curtrace, searchtrace->starttime, searchtrace->endtime,
searchtrace->datasamples, searchtrace->numsamples,
searchtrace->sampletype, 2);
/* If no data is present, make sure sample count is updated */
if (searchtrace->numsamples <= 0)
curtrace->samplecnt += searchtrace->samplecnt;
/* If qualities do not match reset the indicator */
if (curtrace->dataquality != searchtrace->dataquality)
curtrace->dataquality = 0;
merged = 1;
}
/* If searchtrace was merged with curtrace remove it from the chain */
if (merged)
{
/* Re-link trace chain and free searchtrace */
if (searchtrace == mstg->traces)
mstg->traces = nexttrace;
else
prevtrace->next = nexttrace;
mst_free (&searchtrace);
mstg->numtraces--;
mergings++;
}
else
{
prevtrace = searchtrace;
}
}
curtrace = curtrace->next;
}
return mergings;
} /* End of mst_groupheal() */
/***************************************************************************
* mst_groupsort:
*
* Sort a MSTraceGroup using a mergesort algorithm. MSTrace entries
* are compared using the mst_groupsort_cmp() function.
*
* The mergesort implementation was inspired by the listsort function
* published and copyright 2001 by Simon Tatham.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
mst_groupsort (MSTraceGroup *mstg, flag quality)
{
MSTrace *p, *q, *e, *top, *tail;
int nmerges;
int insize, psize, qsize, i;
if (!mstg)
return -1;
if (!mstg->traces)
return 0;
top = mstg->traces;
insize = 1;
for (;;)
{
p = top;
top = NULL;
tail = NULL;
nmerges = 0; /* count number of merges we do in this pass */
while (p)
{
nmerges++; /* there exists a merge to be done */
/* step `insize' places along from p */
q = p;
psize = 0;
for (i = 0; i < insize; i++)
{
psize++;
q = q->next;
if (!q)
break;
}
/* if q hasn't fallen off end, we have two lists to merge */
qsize = insize;
/* now we have two lists; merge them */
while (psize > 0 || (qsize > 0 && q))
{
/* decide whether next element of merge comes from p or q */
if (psize == 0)
{ /* p is empty; e must come from q. */
e = q;
q = q->next;
qsize--;
}
else if (qsize == 0 || !q)
{ /* q is empty; e must come from p. */
e = p;
p = p->next;
psize--;
}
else if (mst_groupsort_cmp (p, q, quality) <= 0)
{ /* First element of p is lower (or same), e must come from p. */
e = p;
p = p->next;
psize--;
}
else
{ /* First element of q is lower; e must come from q. */
e = q;
q = q->next;
qsize--;
}
/* add the next element to the merged list */
if (tail)
tail->next = e;
else
top = e;
tail = e;
}
/* now p has stepped `insize' places along, and q has too */
p = q;
}
tail->next = NULL;
/* If we have done only one merge, we're finished. */
if (nmerges <= 1) /* allow for nmerges==0, the empty list case */
{
mstg->traces = top;
return 0;
}
/* Otherwise repeat, merging lists twice the size */
insize *= 2;
}
} /* End of mst_groupsort() */
/***************************************************************************
* mst_groupsort_cmp:
*
* Compare two MSTrace entities for the purposes of sorting a
* MSTraceGroup. Criteria for MSTrace comparison are (in order of
* testing): source name, start time, descending endtime (longest
* trace first) and sample rate.
*
* Return 1 if mst1 is "greater" than mst2, otherwise return 0.
***************************************************************************/
static int
mst_groupsort_cmp (MSTrace *mst1, MSTrace *mst2, flag quality)
{
char src1[50], src2[50];
int strcmpval;
if (!mst1 || !mst2)
return -1;
mst_srcname (mst1, src1, quality);
mst_srcname (mst2, src2, quality);
strcmpval = strcmp (src1, src2);
/* If the source names do not match make sure the "greater" string is 2nd,
* otherwise, if source names do match, make sure the later start time is 2nd
* otherwise, if start times match, make sure the earlier end time is 2nd
* otherwise, if end times match, make sure the highest sample rate is 2nd
*/
if (strcmpval > 0)
{
return 1;
}
else if (strcmpval == 0)
{
if (mst1->starttime > mst2->starttime)
{
return 1;
}
else if (mst1->starttime == mst2->starttime)
{
if (mst1->endtime < mst2->endtime)
{
return 1;
}
else if (mst1->endtime == mst2->endtime)
{
if (!MS_ISRATETOLERABLE (mst1->samprate, mst2->samprate) &&
mst1->samprate > mst2->samprate)
{
return 1;
}
}
}
}
return 0;
} /* End of mst_groupsort_cmp() */
/***************************************************************************
* mst_convertsamples:
*
* Convert the data samples associated with an MSTrace to another data
* type. ASCII data samples cannot be converted, if supplied or
* requested an error will be returned.
*
* When converting float & double sample types to integer type a
* simple rounding is applied by adding 0.5 to the sample value before
* converting (truncating) to integer.
*
* If the truncate flag is true data samples will be truncated to
* integers even if loss of sample precision is detected. If the
* truncate flag is false (0) and loss of precision is detected an
* error is returned.
*
* Returns 0 on success, and -1 on failure.
***************************************************************************/
int
mst_convertsamples (MSTrace *mst, char type, flag truncate)
{
int32_t *idata;
float *fdata;
double *ddata;
int64_t idx;
if (!mst)
return -1;
/* No conversion necessary, report success */
if (mst->sampletype == type)
return 0;
if (mst->sampletype == 'a' || type == 'a')
{
ms_log (2, "mst_convertsamples: cannot convert ASCII samples to/from numeric type\n");
return -1;
}
idata = (int32_t *)mst->datasamples;
fdata = (float *)mst->datasamples;
ddata = (double *)mst->datasamples;
/* Convert to 32-bit integers */
if (type == 'i')
{
if (mst->sampletype == 'f') /* Convert floats to integers with simple rounding */
{
for (idx = 0; idx < mst->numsamples; idx++)
{
/* Check for loss of sub-integer */
if (!truncate && (fdata[idx] - (int32_t)fdata[idx]) > 0.000001)
{
ms_log (1, "mst_convertsamples: Warning, loss of precision when converting floats to integers, loss: %g\n",
(fdata[idx] - (int32_t)fdata[idx]));
return -1;
}
idata[idx] = (int32_t) (fdata[idx] + 0.5);
}
}
else if (mst->sampletype == 'd') /* Convert doubles to integers with simple rounding */
{
for (idx = 0; idx < mst->numsamples; idx++)
{
/* Check for loss of sub-integer */
if (!truncate && (ddata[idx] - (int32_t)ddata[idx]) > 0.000001)
{
ms_log (1, "mst_convertsamples: Warning, loss of precision when converting doubles to integers, loss: %g\n",
(ddata[idx] - (int32_t)ddata[idx]));
return -1;
}
idata[idx] = (int32_t) (ddata[idx] + 0.5);
}
/* Reallocate buffer for reduced size needed */
if (!(mst->datasamples = realloc (mst->datasamples, (size_t) (mst->numsamples * sizeof (int32_t)))))
{
ms_log (2, "mst_convertsamples: cannot re-allocate buffer for sample conversion\n");
return -1;
}
}
mst->sampletype = 'i';
} /* Done converting to 32-bit integers */
/* Convert to 32-bit floats */
else if (type == 'f')
{
if (mst->sampletype == 'i') /* Convert integers to floats */
{
for (idx = 0; idx < mst->numsamples; idx++)
fdata[idx] = (float)idata[idx];
}
else if (mst->sampletype == 'd') /* Convert doubles to floats */
{
for (idx = 0; idx < mst->numsamples; idx++)
fdata[idx] = (float)ddata[idx];
/* Reallocate buffer for reduced size needed */
if (!(mst->datasamples = realloc (mst->datasamples, (size_t) (mst->numsamples * sizeof (float)))))
{
ms_log (2, "mst_convertsamples: cannot re-allocate buffer after sample conversion\n");
return -1;
}
}
mst->sampletype = 'f';
} /* Done converting to 32-bit floats */
/* Convert to 64-bit doubles */
else if (type == 'd')
{
if (!(ddata = (double *)malloc ((size_t) (mst->numsamples * sizeof (double)))))
{
ms_log (2, "mst_convertsamples: cannot allocate buffer for sample conversion to doubles\n");
return -1;
}
if (mst->sampletype == 'i') /* Convert integers to doubles */
{
for (idx = 0; idx < mst->numsamples; idx++)
ddata[idx] = (double)idata[idx];
free (idata);
}
else if (mst->sampletype == 'f') /* Convert floats to doubles */
{
for (idx = 0; idx < mst->numsamples; idx++)
ddata[idx] = (double)fdata[idx];
free (fdata);
}
mst->datasamples = ddata;
mst->sampletype = 'd';
} /* Done converting to 64-bit doubles */
return 0;
} /* End of mst_convertsamples() */
/***************************************************************************
* mst_srcname:
*
* Generate a source name string for a specified MSTrace in the
* format: 'NET_STA_LOC_CHAN[_QUAL]'. The quality is added to the
* srcname if the quality flag argument is 1 and mst->dataquality is
* not zero. The passed srcname must have enough room for the
* resulting string.
*
* Returns a pointer to the resulting string or NULL on error.
***************************************************************************/
char *
mst_srcname (MSTrace *mst, char *srcname, flag quality)
{
char *src = srcname;
char *cp = srcname;
if (!mst || !srcname)
return NULL;
/* Build the source name string */
cp = mst->network;
while (*cp)
{
*src++ = *cp++;
}
*src++ = '_';
cp = mst->station;
while (*cp)
{
*src++ = *cp++;
}
*src++ = '_';
cp = mst->location;
while (*cp)
{
*src++ = *cp++;
}
*src++ = '_';
cp = mst->channel;
while (*cp)
{
*src++ = *cp++;
}
if (quality && mst->dataquality)
{
*src++ = '_';
*src++ = mst->dataquality;
}
*src = '\0';
return srcname;
} /* End of mst_srcname() */
/***************************************************************************
* mst_printtracelist:
*
* Print trace list summary information for the specified MSTraceGroup.
*
* By default only print the srcname, starttime and endtime for each
* trace. If details is greater than 0 include the sample rate,
* number of samples and a total trace count. If gaps is greater than
* 0 and the previous trace matches (srcname & samprate) include the
* gap between the endtime of the last trace and the starttime of the
* current trace.
*
* The timeformat flag can either be:
* 0 : SEED time format (year, day-of-year, hour, min, sec)
* 1 : ISO time format (year, month, day, hour, min, sec)
* 2 : Epoch time, seconds since the epoch
***************************************************************************/
void
mst_printtracelist (MSTraceGroup *mstg, flag timeformat,
flag details, flag gaps)
{
MSTrace *mst = 0;
char srcname[50];
char prevsrcname[50];
char stime[30];
char etime[30];
char gapstr[20];
flag nogap;
double gap;
double delta;
double prevsamprate;
hptime_t prevendtime;
int tracecnt = 0;
if (!mstg)
return;
mst = mstg->traces;
/* Print out the appropriate header */
if (details > 0 && gaps > 0)
ms_log (0, " Source Start sample End sample Gap Hz Samples\n");
else if (details <= 0 && gaps > 0)
ms_log (0, " Source Start sample End sample Gap\n");
else if (details > 0 && gaps <= 0)
ms_log (0, " Source Start sample End sample Hz Samples\n");
else
ms_log (0, " Source Start sample End sample\n");
prevsrcname[0] = '\0';
prevsamprate = -1.0;
prevendtime = 0;
while (mst)
{
mst_srcname (mst, srcname, 1);
/* Create formatted time strings */
if (timeformat == 2)
{
snprintf (stime, sizeof (stime), "%.6f", (double)MS_HPTIME2EPOCH (mst->starttime));
snprintf (etime, sizeof (etime), "%.6f", (double)MS_HPTIME2EPOCH (mst->endtime));
}
else if (timeformat == 1)
{
if (ms_hptime2isotimestr (mst->starttime, stime, 1) == NULL)
ms_log (2, "Cannot convert trace start time for %s\n", srcname);
if (ms_hptime2isotimestr (mst->endtime, etime, 1) == NULL)
ms_log (2, "Cannot convert trace end time for %s\n", srcname);
}
else
{
if (ms_hptime2seedtimestr (mst->starttime, stime, 1) == NULL)
ms_log (2, "Cannot convert trace start time for %s\n", srcname);
if (ms_hptime2seedtimestr (mst->endtime, etime, 1) == NULL)
ms_log (2, "Cannot convert trace end time for %s\n", srcname);
}
/* Print trace info at varying levels */
if (gaps > 0)
{
gap = 0.0;
nogap = 0;
if (!strcmp (prevsrcname, srcname) && prevsamprate != -1.0 &&
MS_ISRATETOLERABLE (prevsamprate, mst->samprate))
gap = (double)(mst->starttime - prevendtime) / HPTMODULUS;
else
nogap = 1;
/* Check that any overlap is not larger than the trace coverage */
if (gap < 0.0)
{
delta = (mst->samprate) ? (1.0 / mst->samprate) : 0.0;
if ((gap * -1.0) > (((double)(mst->endtime - mst->starttime) / HPTMODULUS) + delta))
gap = -(((double)(mst->endtime - mst->starttime) / HPTMODULUS) + delta);
}
/* Fix up gap display */
if (nogap)
snprintf (gapstr, sizeof (gapstr), " == ");
else if (gap >= 86400.0 || gap <= -86400.0)
snprintf (gapstr, sizeof (gapstr), "%-3.1fd", (gap / 86400));
else if (gap >= 3600.0 || gap <= -3600.0)
snprintf (gapstr, sizeof (gapstr), "%-3.1fh", (gap / 3600));
else if (gap == 0.0)
snprintf (gapstr, sizeof (gapstr), "-0 ");
else
snprintf (gapstr, sizeof (gapstr), "%-4.4g", gap);
if (details <= 0)
ms_log (0, "%-17s %-24s %-24s %-4s\n",
srcname, stime, etime, gapstr);
else
ms_log (0, "%-17s %-24s %-24s %-s %-3.3g %-" PRId64 "\n",
srcname, stime, etime, gapstr, mst->samprate, mst->samplecnt);
}
else if (details > 0 && gaps <= 0)
ms_log (0, "%-17s %-24s %-24s %-3.3g %-" PRId64 "\n",
srcname, stime, etime, mst->samprate, mst->samplecnt);
else
ms_log (0, "%-17s %-24s %-24s\n", srcname, stime, etime);
if (gaps > 0)
{
strcpy (prevsrcname, srcname);
prevsamprate = mst->samprate;
prevendtime = mst->endtime;
}
tracecnt++;
mst = mst->next;
}
if (tracecnt != mstg->numtraces)
ms_log (2, "mst_printtracelist(): number of traces in trace group is inconsistent\n");
if (details > 0)
ms_log (0, "Total: %d trace segment(s)\n", tracecnt);
} /* End of mst_printtracelist() */
/***************************************************************************
* mst_printsynclist:
*
* Print SYNC trace list summary information for the specified MSTraceGroup.
*
* The SYNC header line will be created using the supplied dccid, if
* the pointer is NULL the string "DCC" will be used instead.
*
* If the subsecond flag is true the segment start and end times will
* include subsecond precision, otherwise they will be truncated to
* integer seconds.
*
***************************************************************************/
void
mst_printsynclist (MSTraceGroup *mstg, char *dccid, flag subsecond)
{
MSTrace *mst = 0;
char stime[30];
char etime[30];
char yearday[32];
time_t now;
struct tm *nt;
if (!mstg)
{
return;
}
/* Generate current time stamp */
now = time (NULL);
nt = localtime (&now);
nt->tm_year += 1900;
nt->tm_yday += 1;
snprintf (yearday, sizeof (yearday), "%04d,%03d", nt->tm_year, nt->tm_yday);
/* Print SYNC header line */
ms_log (0, "%s|%s\n", (dccid) ? dccid : "DCC", yearday);
/* Loope through trace list */
mst = mstg->traces;
while (mst)
{
ms_hptime2seedtimestr (mst->starttime, stime, subsecond);
ms_hptime2seedtimestr (mst->endtime, etime, subsecond);
/* Print SYNC line */
ms_log (0, "%s|%s|%s|%s|%s|%s||%.10g|%" PRId64 "|||||||%s\n",
mst->network, mst->station, mst->location, mst->channel,
stime, etime, mst->samprate, mst->samplecnt,
yearday);
mst = mst->next;
}
} /* End of mst_printsynclist() */
/***************************************************************************
* mst_printgaplist:
*
* Print gap/overlap list summary information for the specified
* MSTraceGroup. Overlaps are printed as negative gaps. The trace
* summary information in the MSTraceGroup is logically inverted so gaps
* for like channels are identified.
*
* If mingap and maxgap are not NULL their values will be enforced and
* only gaps/overlaps matching their implied criteria will be printed.
*
* The timeformat flag can either be:
* 0 : SEED time format (year, day-of-year, hour, min, sec)
* 1 : ISO time format (year, month, day, hour, min, sec)
* 2 : Epoch time, seconds since the epoch
***************************************************************************/
void
mst_printgaplist (MSTraceGroup *mstg, flag timeformat,
double *mingap, double *maxgap)
{
MSTrace *mst;
char src1[50], src2[50];
char time1[30], time2[30];
char gapstr[30];
double gap;
double delta;
double nsamples;
flag printflag;
int gapcnt = 0;
if (!mstg)
return;
if (!mstg->traces)
return;
mst = mstg->traces;
ms_log (0, " Source Last Sample Next Sample Gap Samples\n");
while (mst->next)
{
mst_srcname (mst, src1, 1);
mst_srcname (mst->next, src2, 1);
if (!strcmp (src1, src2))
{
/* Skip MSTraces with 0 sample rate, usually from SOH records */
if (mst->samprate == 0.0)
{
mst = mst->next;
continue;
}
/* Check that sample rates match using default tolerance */
if (!MS_ISRATETOLERABLE (mst->samprate, mst->next->samprate))
{
ms_log (2, "%s Sample rate changed! %.10g -> %.10g\n",
src1, mst->samprate, mst->next->samprate);
}
gap = (double)(mst->next->starttime - mst->endtime) / HPTMODULUS;
/* Check that any overlap is not larger than the trace coverage */
if (gap < 0.0)
{
delta = (mst->next->samprate) ? (1.0 / mst->next->samprate) : 0.0;
if ((gap * -1.0) > (((double)(mst->next->endtime - mst->next->starttime) / HPTMODULUS) + delta))
gap = -(((double)(mst->next->endtime - mst->next->starttime) / HPTMODULUS) + delta);
}
printflag = 1;
/* Check gap/overlap criteria */
if (mingap)
if (gap < *mingap)
printflag = 0;
if (maxgap)
if (gap > *maxgap)
printflag = 0;
if (printflag)
{
nsamples = ms_dabs (gap) * mst->samprate;
if (gap > 0.0)
nsamples -= 1.0;
else
nsamples += 1.0;
/* Fix up gap display */
if (gap >= 86400.0 || gap <= -86400.0)
snprintf (gapstr, sizeof (gapstr), "%-3.1fd", (gap / 86400));
else if (gap >= 3600.0 || gap <= -3600.0)
snprintf (gapstr, sizeof (gapstr), "%-3.1fh", (gap / 3600));
else if (gap == 0.0)
snprintf (gapstr, sizeof (gapstr), "-0 ");
else
snprintf (gapstr, sizeof (gapstr), "%-4.4g", gap);
/* Create formatted time strings */
if (timeformat == 2)
{
snprintf (time1, sizeof (time1), "%.6f", (double)MS_HPTIME2EPOCH (mst->endtime));
snprintf (time2, sizeof (time2), "%.6f", (double)MS_HPTIME2EPOCH (mst->next->starttime));
}
else if (timeformat == 1)
{
if (ms_hptime2isotimestr (mst->endtime, time1, 1) == NULL)
ms_log (2, "Cannot convert trace end time for %s\n", src1);
if (ms_hptime2isotimestr (mst->next->starttime, time2, 1) == NULL)
ms_log (2, "Cannot convert next trace start time for %s\n", src1);
}
else
{
if (ms_hptime2seedtimestr (mst->endtime, time1, 1) == NULL)
ms_log (2, "Cannot convert trace end time for %s\n", src1);
if (ms_hptime2seedtimestr (mst->next->starttime, time2, 1) == NULL)
ms_log (2, "Cannot convert next trace start time for %s\n", src1);
}
ms_log (0, "%-17s %-24s %-24s %-4s %-.8g\n",
src1, time1, time2, gapstr, nsamples);
gapcnt++;
}
}
mst = mst->next;
}
ms_log (0, "Total: %d gap(s)\n", gapcnt);
} /* End of mst_printgaplist() */
/***************************************************************************
* mst_pack:
*
* Pack MSTrace data into Mini-SEED records using the specified record
* length, encoding format and byte order. The datasamples array and
* numsamples field will be adjusted (reduced) based on how many
* samples were packed.
*
* As each record is filled and finished they are passed to
* record_handler which expects 1) a char * to the record, 2) the
* length of the record and 3) a pointer supplied by the original
* caller containing optional private data (handlerdata). It is the
* responsibility of record_handler to process the record, the memory
* will be re-used or freed when record_handler returns.
*
* If the flush flag is > 0 all of the data will be packed into data
* records even though the last one will probably not be filled.
*
* If the mstemplate argument is not NULL it will be used as the
* template for the packed Mini-SEED records. Otherwise a new
* MSRecord will be initialized and populated from values in the
* MSTrace. The reclen, encoding and byteorder arguments take
* precedence over those in the template. The start time, sample
* rate, datasamples, numsamples and sampletype values from the
* template will be preserved.
*
* Returns the number of records created on success and -1 on error.
***************************************************************************/
int
mst_pack (MSTrace *mst, void (*record_handler) (char *, int, void *),
void *handlerdata, int reclen, flag encoding, flag byteorder,
int64_t *packedsamples, flag flush, flag verbose,
MSRecord *mstemplate)
{
MSRecord *msr;
char srcname[50];
int trpackedrecords = 0;
int64_t trpackedsamples = 0;
int samplesize;
int64_t bufsize;
hptime_t preservestarttime = 0;
double preservesamprate = 0.0;
void *preservedatasamples = 0;
int64_t preservenumsamples = 0;
char preservesampletype = 0;
StreamState *preserveststate = 0;
if (packedsamples)
*packedsamples = 0;
/* Allocate stream processing state space if needed */
if (!mst->ststate)
{
mst->ststate = (StreamState *)malloc (sizeof (StreamState));
if (!mst->ststate)
{
ms_log (2, "mst_pack(): Could not allocate memory for StreamState\n");
return -1;
}
memset (mst->ststate, 0, sizeof (StreamState));
}
if (mstemplate)
{
msr = mstemplate;
preservestarttime = msr->starttime;
preservesamprate = msr->samprate;
preservedatasamples = msr->datasamples;
preservenumsamples = msr->numsamples;
preservesampletype = msr->sampletype;
preserveststate = msr->ststate;
}
else
{
msr = msr_init (NULL);
if (msr == NULL)
{
ms_log (2, "mst_pack(): Error initializing msr\n");
return -1;
}
msr->dataquality = 'D';
strcpy (msr->network, mst->network);
strcpy (msr->station, mst->station);
strcpy (msr->location, mst->location);
strcpy (msr->channel, mst->channel);
}
/* Setup MSRecord template for packing */
msr->reclen = reclen;
msr->encoding = encoding;
msr->byteorder = byteorder;
msr->starttime = mst->starttime;
msr->samprate = mst->samprate;
msr->datasamples = mst->datasamples;
msr->numsamples = mst->numsamples;
msr->sampletype = mst->sampletype;
msr->ststate = mst->ststate;
/* Sample count sanity check */
if (mst->samplecnt != mst->numsamples)
{
ms_log (2, "mst_pack(): Sample counts do not match, abort\n");
return -1;
}
/* Pack data */
trpackedrecords = msr_pack (msr, record_handler, handlerdata, &trpackedsamples, flush, verbose);
if (verbose > 1)
{
ms_log (1, "Packed %d records for %s trace\n", trpackedrecords, mst_srcname (mst, srcname, 1));
}
/* Adjust MSTrace start time, data array and sample count */
if (trpackedsamples > 0)
{
/* The new start time was calculated my msr_pack */
mst->starttime = msr->starttime;
samplesize = ms_samplesize (mst->sampletype);
bufsize = (mst->numsamples - trpackedsamples) * samplesize;
if (bufsize)
{
memmove (mst->datasamples,
(char *)mst->datasamples + (trpackedsamples * samplesize),
(size_t)bufsize);
mst->datasamples = realloc (mst->datasamples, (size_t)bufsize);
if (mst->datasamples == NULL)
{
ms_log (2, "mst_pack(): Cannot (re)allocate datasamples buffer\n");
return -1;
}
}
else
{
if (mst->datasamples)
free (mst->datasamples);
mst->datasamples = 0;
}
mst->samplecnt -= trpackedsamples;
mst->numsamples -= trpackedsamples;
}
/* Reinstate preserved values if a template was used */
if (mstemplate)
{
msr->starttime = preservestarttime;
msr->samprate = preservesamprate;
msr->datasamples = preservedatasamples;
msr->numsamples = preservenumsamples;
msr->sampletype = preservesampletype;
msr->ststate = preserveststate;
}
else
{
msr->datasamples = 0;
msr->ststate = 0;
msr_free (&msr);
}
if (packedsamples)
*packedsamples = trpackedsamples;
return trpackedrecords;
} /* End of mst_pack() */
/***************************************************************************
* mst_packgroup:
*
* Pack MSTraceGroup data into Mini-SEED records by calling mst_pack()
* for each MSTrace in the group.
*
* Returns the number of records created on success and -1 on error.
***************************************************************************/
int
mst_packgroup (MSTraceGroup *mstg, void (*record_handler) (char *, int, void *),
void *handlerdata, int reclen, flag encoding, flag byteorder,
int64_t *packedsamples, flag flush, flag verbose,
MSRecord *mstemplate)
{
MSTrace *mst;
int trpackedrecords = 0;
int64_t trpackedsamples = 0;
char srcname[50];
if (!mstg)
{
return -1;
}
if (packedsamples)
*packedsamples = 0;
mst = mstg->traces;
while (mst)
{
if (mst->numsamples <= 0)
{
if (verbose > 1)
{
mst_srcname (mst, srcname, 1);
ms_log (1, "No data samples for %s, skipping\n", srcname);
}
}
else
{
trpackedrecords += mst_pack (mst, record_handler, handlerdata, reclen,
encoding, byteorder, &trpackedsamples, flush,
verbose, mstemplate);
if (trpackedrecords == -1)
break;
if (packedsamples)
*packedsamples += trpackedsamples;
}
mst = mst->next;
}
return trpackedrecords;
} /* End of mst_packgroup() */
|