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 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
|
/*
* Copyright (C) 2015-2018 S[&]T, The Netherlands.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "harp-internal.h"
#include "harp-constants.h"
#include "harp-csv.h"
#include "harp-filter-collocation.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 128
typedef enum profile_resample_type_enum
{
profile_resample_skip,
profile_resample_remove,
profile_resample_linear,
profile_resample_log,
profile_resample_interval
} profile_resample_type;
/** Convert geopotential height to geometric height (= altitude)
* \param gph Geopotential height [m]
* \param latitude Latitude [degree_north]
* \return the altitude [m]
*/
double harp_altitude_from_gph_and_latitude(double gph, double latitude)
{
double altitude;
double g0 = (double)CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE; /* gravitational accel. [m s-2] at latitude 45o32'33'' */
double gsurf; /* gravitational acceleration at surface [m s-2] */
double Rsurf; /* local curvature radius [m] */
gsurf = harp_gravity_at_surface_from_latitude(latitude);
Rsurf = harp_local_curvature_radius_at_surface_from_latitude(latitude);
altitude = g0 * Rsurf * gph / (gsurf * Rsurf - g0 * gph);
return altitude;
}
/** Convert a pressure profile to an altitude profile
* \param num_levels Length of vertical axis
* \param pressure_profile Pressure vertical profile [Pa]
* \param temperature_profile Temperature vertical profile [K]
* \param molar_mass_air Molar mass of total air [g/mol]
* \param surface_pressure Surface pressure [Pa]
* \param surface_height Surface height [m]
* \param latitude Latitude [degree_north]
* \param altitude_profile variable in which the vertical profile will be stored [m]
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
void harp_profile_altitude_from_pressure(long num_levels, const double *pressure_profile,
const double *temperature_profile, const double *molar_mass_air,
double surface_pressure, double surface_height, double latitude,
double *altitude_profile)
{
double z, prev_z = 0, p, prev_p = 0, T, prev_T = 0, M, prev_M = 0, g;
long i;
for (i = 0; i < num_levels; i++)
{
long k = i;
if (pressure_profile[0] < pressure_profile[num_levels - 1])
{
/* vertical axis is from TOA to surface -> invert the loop index */
k = num_levels - 1 - i;
}
p = pressure_profile[k];
M = molar_mass_air[k];
T = temperature_profile[k];
if (i == 0)
{
g = harp_gravity_at_surface_from_latitude(latitude);
z = surface_height + 1e3 * (T / M) * (CONST_MOLAR_GAS / g) * log(surface_pressure / p);
}
else
{
g = harp_gravity_from_latitude_and_height(latitude, prev_z);
z = prev_z + 1e3 * ((prev_T + T) / (prev_M + M)) * (CONST_MOLAR_GAS / g) * log(prev_p / p);
}
altitude_profile[k] = z;
prev_p = p;
prev_M = M;
prev_T = T;
prev_z = z;
}
}
/** Convert geopotential height to geopotential
* \param gph Geopotential height [m]
* \return the geopotential [m2/s2]
*/
double harp_geopotential_from_gph(double gph)
{
return CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE * gph;
}
/** Convert geopotential to geopotential height
* \param geopotential Geopotential [m2/s2]
* \return the geopotential height [m]
*/
double harp_gph_from_geopotential(double geopotential)
{
return geopotential / CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE;
}
/** Convert geometric height (= altitude) to geopotential height
* \param altitude Altitude [m]
* \param latitude Latitude [degree_north]
* \return the geopotential height [m]
*/
double harp_gph_from_altitude_and_latitude(double altitude, double latitude)
{
double gsurf; /* gravitational acceleration at surface [m] */
double Rsurf; /* local curvature radius [m] */
gsurf = harp_gravity_at_surface_from_latitude(latitude);
Rsurf = harp_local_curvature_radius_at_surface_from_latitude(latitude);
return (gsurf / CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE) * Rsurf * altitude / (altitude + Rsurf);
}
/** Convert geometric height (= altitude) to geopotential height
* \param surface_pressure Surface pressure [Pa]
* \param num_levels Length of vertical axis
* \param pressure_bounds Lower and upper pressure [Pa] boundaries for each level {vertical,2} (decreasing order)
* \param altitude_profile Altitude vertical profile [m] (needs to be in increasing order)
* \param latitude Latitude at the surface [degree_north]
* \return the total column mass density [kg/m2]
*/
double harp_column_mass_density_from_surface_pressure_and_profile(double surface_pressure, long num_levels,
const double *pressure_bounds,
const double *altitude_profile, double latitude)
{
double sum1 = 0, sum2 = 0; /* the average gravity g = sum1/sum2 */
long i;
for (i = 0; i < num_levels; i++)
{
double g = harp_gravity_from_latitude_and_height(latitude, altitude_profile[i]);
sum1 += pressure_bounds[2 * i] - pressure_bounds[2 * i + 1];
sum2 += (pressure_bounds[2 * i] - pressure_bounds[2 * i + 1]) / g;
}
return surface_pressure * sum2 / sum1;
}
/** Calculate tropopause level from altitude and temperature grid
* This uses the WMO definition:
* The boundary between the troposphere and the stratosphere, where an abrupt change in lapse rate usually occurs.
* It is defined as the lowest level at which the lapse rate decreases to 2 degC/km or less, provided that the average
* lapse rate between this level and all higher levels within 2 km does not exceed 2 degC/km.
* Only levels between 50000Pa and 5000Pa are considered (which is why pressure is required as an input).
* \param num_levels Length of vertical axis
* \param altitude_profile Altitude vertical profile [m] (needs to be in increasing order)
* \param pressure_profile Pressure vertical profile [Pa] (needs to be in decreasing order)
* \param temperature_profile Temperature vertical profile [K]
* \return the index in the altitude grid that represents the tropopause, or -1 if it was not found.
*/
long harp_tropopause_index_from_altitude_and_temperature(long num_levels, const double *altitude_profile,
const double *pressure_profile,
const double *temperature_profile)
{
double lapse_above;
double lapse_below;
double height;
long i = 1;
while (i < num_levels - 1 && pressure_profile[i] > 50000)
{
i++;
}
if (i >= num_levels - 1)
{
return -1;
}
height = altitude_profile[i] - altitude_profile[i - 1];
if (height < 0)
{
/* altitude needs to be increasing */
return -1;
}
if (height < EPSILON)
{
lapse_below = harp_nan();
}
else
{
lapse_below = (temperature_profile[i - 1] - temperature_profile[i]) / height;
}
while (i < num_levels - 1 && pressure_profile[i] > 5000)
{
height = altitude_profile[i + 1] - altitude_profile[i];
if (height < 0)
{
/* altitude needs to be increasing */
return -1;
}
if (height < EPSILON)
{
/* skip layers that are too small */
lapse_above = lapse_below;
}
else
{
lapse_above = (temperature_profile[i] - temperature_profile[i + 1]) / height;
}
/* A rate of 2 degC/km is the same is 0.002 K/m. */
if (lapse_below > 0.002 && lapse_above <= 0.002)
{
long k = i + 2;
while (k < num_levels && altitude_profile[k] <= altitude_profile[i] + 2000)
{
height = altitude_profile[k] - altitude_profile[i];
if (height >= EPSILON)
{
/* average lapse rate should not exceed 2 degC/km */
if ((temperature_profile[i] - temperature_profile[k]) / height > 0.002)
{
k = -1;
break;
}
}
k++;
}
if (k > i)
{
return i;
}
}
lapse_below = lapse_above;
i++;
}
/* we were not able to find the tropopause -> return NaN */
return -1;
}
/** Convert a pressure profile to a geopotential height profile
* \param num_levels Length of vertical axis
* \param pressure_profile Pressure vertical profile [Pa]
* \param temperature_profile Temperature vertical profile [K]
* \param molar_mass_air Molar mass of total air [g/mol]
* \param surface_pressure Surface pressure [Pa]
* \param surface_height Surface height [m]
* \param gph_profile Variable in which the vertical profile will be stored [m]
*/
void harp_profile_gph_from_pressure(long num_levels, const double *pressure_profile, const double *temperature_profile,
const double *molar_mass_air, double surface_pressure, double surface_height,
double *gph_profile)
{
double z, prev_z = 0, p, prev_p = 0, T, prev_T = 0, M, prev_M = 0;
long i;
for (i = 0; i < num_levels; i++)
{
long k = i;
if (pressure_profile[0] < pressure_profile[num_levels - 1])
{
/* vertical axis is from TOA to surface -> invert the loop index */
k = num_levels - 1 - i;
}
p = pressure_profile[k];
M = molar_mass_air[k];
T = temperature_profile[k];
if (i == 0)
{
z = surface_height + 1e3 * (T / M) * (CONST_MOLAR_GAS / CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE) *
log(surface_pressure / p);
}
else
{
z = prev_z + 1e3 * ((prev_T + T) / (prev_M + M)) * (CONST_MOLAR_GAS / CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE) *
log(prev_p / p);
}
gph_profile[k] = z;
prev_p = p;
prev_M = M;
prev_T = T;
prev_z = z;
}
}
/** Integrate the partial column profile to obtain the column
* \param num_levels Number of levels of the partial column profile
* \param partial_column_profile Partial column profile [molec/m2]
* \return column the integrated column [molec/m2]
*/
double harp_profile_column_from_partial_column(long num_levels, const double *partial_column_profile)
{
double column = 0;
int empty = 1;
long k;
/* Integrate, but ignore NaN values */
for (k = 0; k < num_levels; k++)
{
if (!harp_isnan(partial_column_profile[k]))
{
column += partial_column_profile[k];
empty = 0;
}
}
/* Set column to NaN if all contributions were NaN */
if (empty)
{
return harp_nan();
}
return column;
}
/** Convert an altitude profile to a pressure profile
* \param num_levels Length of vertical axis
* \param altitude_profile Altitude profile [m]
* \param temperature_profile Temperature vertical profile [K]
* \param molar_mass_air Molar mass of total air [g/mol]
* \param surface_pressure Surface pressure [Pa]
* \param surface_height Surface height [m]
* \param latitude Latitude [degree_north]
* \param pressure_profile variable in which the vertical profile will be stored [Pa]
*/
void harp_profile_pressure_from_altitude(long num_levels, const double *altitude_profile,
const double *temperature_profile, const double *molar_mass_air,
double surface_pressure, double surface_height, double latitude,
double *pressure_profile)
{
double z, prev_z = 0, p, prev_p = 0, T, prev_T = 0, M, prev_M = 0, g;
long i;
for (i = 0; i < num_levels; i++)
{
long k = i;
if (altitude_profile[0] > altitude_profile[num_levels - 1])
{
/* vertical axis is from TOA to surface -> invert the loop index */
k = num_levels - 1 - i;
}
z = altitude_profile[k];
M = molar_mass_air[k];
T = temperature_profile[k];
if (i == 0)
{
g = harp_gravity_from_latitude_and_height(latitude, (z + surface_height) / 2);
p = surface_pressure * exp(-1e-3 * (M / T) * (g / CONST_MOLAR_GAS) * (z - surface_height));
}
else
{
g = harp_gravity_from_latitude_and_height(latitude, (prev_z + z) / 2);
p = prev_p * exp(-1e-3 * ((prev_M + M) / (prev_T + T)) * (g / CONST_MOLAR_GAS) * (z - prev_z));
}
pressure_profile[k] = p;
prev_p = p;
prev_M = M;
prev_T = T;
prev_z = z;
}
}
/** Convert a geopotential height profile to a pressure profile
* \param num_levels Length of vertical axis
* \param gph_profile Geopotential height profile [m]
* \param temperature_profile Temperature vertical profile [K]
* \param molar_mass_air Molar mass of total air [g/mol]
* \param surface_pressure Surface pressure [Pa]
* \param surface_height Surface height [m]
* \param pressure_profile Variable in which the vertical profile will be stored [Pa]
*/
void harp_profile_pressure_from_gph(long num_levels, const double *gph_profile, const double *temperature_profile,
const double *molar_mass_air, double surface_pressure, double surface_height,
double *pressure_profile)
{
double z, prev_z = 0, p, prev_p = 0, T, prev_T = 0, M, prev_M = 0;
long i;
for (i = 0; i < num_levels; i++)
{
long k = i;
if (gph_profile[0] > gph_profile[num_levels - 1])
{
/* vertical axis is from TOA to surface -> invert the loop index */
k = num_levels - 1 - i;
}
z = gph_profile[k];
M = molar_mass_air[k];
T = temperature_profile[k];
if (i == 0)
{
p = surface_pressure * exp(-1e-3 * (M / T) * (CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE / CONST_MOLAR_GAS) *
(z - surface_height));
}
else
{
p = prev_p * exp(-1e-3 * ((prev_M + M) / (prev_T + T)) *
(CONST_GRAV_ACCEL_45LAT_WGS84_SPHERE / CONST_MOLAR_GAS) * (z - prev_z));
}
pressure_profile[k] = p;
prev_p = p;
prev_M = M;
prev_T = T;
prev_z = z;
}
}
/** Sum the columns of the 2D averaging kernal to arrive at a 1D column averaging kernel
* The 2D averaging kernel needs to be a partial column number density AVK.
* \param num_levels Number of vertical levels
* \param column_density_avk_2d 2D column number density averaging kernel {num_levels,num_levels}
* \param column_density_avk_1d 1D column number density averaging kernel {num_levels}
*/
void harp_profile_column_avk_from_partial_column_avk(long num_levels, const double *column_density_avk_2d,
double *column_density_avk_1d)
{
long i, j;
for (j = 0; j < num_levels; j++)
{
column_density_avk_1d[j] = column_density_avk_2d[j];
for (i = 1; i < num_levels; i++)
{
column_density_avk_1d[j] += column_density_avk_2d[i * num_levels + j];
}
}
}
/** Convert a partial column avk to a density avk using the altitude boundaries profile
* This is a generic routine to convert partial columns to a densities. It works for all cases where the
* conversion is a matter of dividing the partial column value by the altitude height to get the density value.
* \param num_levels Number of vertical levels
* \param partial_column_avk Partial column avk {vertical,vertical}
* \param altitude_bounds Lower and upper altitude [m] boundaries for each level {vertical,2}
* \param density_avk variable in which the density avk {vertical,vertical} will be stored
*/
void harp_density_avk_from_partial_column_avk_and_altitude_bounds(long num_levels, const double *partial_column_avk,
const double *altitude_bounds, double *density_avk)
{
long i, j;
for (i = 0; i < num_levels; i++)
{
double height = fabs(altitude_bounds[i * 2 + 1] - altitude_bounds[i * 2]);
if (height < EPSILON)
{
for (j = 0; j < num_levels; j++)
{
density_avk[i * num_levels + j] = 0;
}
}
else
{
for (j = 0; j < num_levels; j++)
{
density_avk[i * num_levels + j] = partial_column_avk[i * num_levels + j] / height;
}
}
}
for (j = 0; j < num_levels; j++)
{
double height = fabs(altitude_bounds[j * 2 + 1] - altitude_bounds[j * 2]);
for (i = 0; i < num_levels; i++)
{
density_avk[i * num_levels + j] *= height;
}
}
}
/** Convert a partial column profile to a density profile using the altitude boundaries as provided
* This is a generic routine to convert densities to partial columns. It works for all cases where the conversion is a
* matter of multiplying the density value by the altitude height to get the partial column value.
* \param num_levels Number of vertical levels
* \param density_avk Density avk {vertical,vertical}
* \param altitude_bounds Lower and upper altitude [m] boundaries for each level {vertical,2}
* \param partial_column_avk variable in which the partial column avk {vertical,vertical} will be stored
*/
void harp_partial_column_avk_from_density_avk_and_altitude_bounds(long num_levels, const double *density_avk,
const double *altitude_bounds,
double *partial_column_avk)
{
long i, j;
for (i = 0; i < num_levels; i++)
{
double height = fabs(altitude_bounds[i * 2 + 1] - altitude_bounds[i * 2]);
for (j = 0; j < num_levels; j++)
{
partial_column_avk[i * num_levels + j] = density_avk[i * num_levels + j] * height;
}
}
for (j = 0; j < num_levels; j++)
{
double height = fabs(altitude_bounds[j * 2 + 1] - altitude_bounds[j * 2]);
if (height < EPSILON)
{
for (i = 0; i < num_levels; i++)
{
partial_column_avk[i * num_levels + j] = 0;
}
}
else
{
for (i = 0; i < num_levels; i++)
{
partial_column_avk[i * num_levels + j] /= height;
}
}
}
}
/** Convert a volume mixing ratio avk to a number density avk using the air number density profile
* \param num_levels Number of vertical levels
* \param volume_mixing_ratio_avk Volume mixing ratio avk {vertical,vertical}
* \param number_density_air Number density of air [molec/cm3] {vertical}
* \param number_density_avk variable in which the number density avk [(molec/cm3)/(molec/cm3)] {vertical,vertical}
* will be stored
*/
void harp_number_density_avk_from_volume_mixing_ratio_avk(long num_levels, const double *volume_mixing_ratio_avk,
const double *number_density_air, double *number_density_avk)
{
long i, j;
for (i = 0; i < num_levels; i++)
{
double number_density = number_density_air[i];
for (j = 0; j < num_levels; j++)
{
number_density_avk[i * num_levels + j] = volume_mixing_ratio_avk[i * num_levels + j] * number_density;
}
}
for (j = 0; j < num_levels; j++)
{
double number_density = number_density_air[j];
if (fabs(number_density) < EPSILON)
{
for (i = 0; i < num_levels; i++)
{
number_density_avk[i * num_levels + j] = 0;
}
}
else
{
for (i = 0; i < num_levels; i++)
{
number_density_avk[i * num_levels + j] /= number_density;
}
}
}
}
/** Convert a number density avk to a volume mixing ratio avk using the air number density profile
* \param num_levels Number of vertical levels
* \param number_density_avk Number density avk [(molec/cm3)/(molec/cm3)] {vertical,vertical}
* \param number_density_air Number density of air [molec/cm3] {vertical}
* \param volume_mixing_ratio_avk variable in which the volume mixing ratio avk {vertical,vertical} will be stored
*/
void harp_volume_mixing_ratio_avk_from_number_density_avk(long num_levels, const double *number_density_avk,
const double *number_density_air,
double *volume_mixing_ratio_avk)
{
long i, j;
for (i = 0; i < num_levels; i++)
{
double number_density = number_density_air[i];
if (fabs(number_density) < EPSILON)
{
for (j = 0; j < num_levels; j++)
{
volume_mixing_ratio_avk[i * num_levels + j] = 0;
}
}
else
{
for (j = 0; j < num_levels; j++)
{
volume_mixing_ratio_avk[i * num_levels + j] = number_density_avk[i * num_levels + j] / number_density;
}
}
}
for (j = 0; j < num_levels; j++)
{
double number_density = number_density_air[j];
for (i = 0; i < num_levels; i++)
{
volume_mixing_ratio_avk[i * num_levels + j] *= number_density;
}
}
}
static long get_unpadded_vector_length(double *vector, long vector_length)
{
long i;
for (i = vector_length - 1; i >= 0; i--)
{
if (!harp_isnan(vector[i]))
{
return i + 1;
}
}
return vector_length;
}
/** \addtogroup harp_variable
* @{
*/
/** Vertically smooth the variable using the given averaging kernel and a apriori.
* The variable already needs to be on the same vertical grid as that of the averaging kernel (and a priori).
* The apriori is optional. If provided, the apriori is first subtracted from the variable, then the smoothing is
* performed, and finally the apriori is added again.
* The averaging kernel needs to have dimensions {time,vertical,vertical} and the apriori {time,vertical}.
* The variable to be smoothed needs to have dimensions {time, ..., vertical} (i.e. first dimension must be time and
* the last the vertical dimension; number of dimensions needs to be 2 or higher).
* The vertical axis variable is optional and, if provided, needs to have dimensions {time,vertical}.
* The vertical axis variable will be used to determine the valid number of vertical elements per profile.
* All inputs need to be provided as 'double' data.
* \param variable Variable to which the averaging kernel (and apriori) should be applied.
* \param vertical_axis The variable containing the time dependent vertical grid (optional).
* \param averaging_kernel The variable containing the averaging kernel.
* \param apriori The variable containing the apriori (optional).
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
LIBHARP_API int harp_variable_smooth_vertical(harp_variable *variable, harp_variable *vertical_axis,
harp_variable *averaging_kernel, harp_variable *apriori)
{
double *vector = NULL;
long max_vertical_elements;
long num_blocks;
long k, l;
if (variable == NULL)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "variable is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (averaging_kernel == NULL)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "avk is NULL (%s:%u)", __FILE__, __LINE__);
return -1;
}
if (variable->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for variable");
return -1;
}
if (variable->num_dimensions < 2 || variable->dimension_type[0] != harp_dimension_time ||
variable->dimension_type[variable->num_dimensions - 1] != harp_dimension_vertical)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "variable should have dimensions {time,...,vertical}");
return -1;
}
if (averaging_kernel->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for averaging kernel");
return -1;
}
if (averaging_kernel->num_dimensions != 3 || averaging_kernel->dimension_type[0] != harp_dimension_time ||
averaging_kernel->dimension_type[1] != harp_dimension_vertical ||
averaging_kernel->dimension_type[2] != harp_dimension_vertical)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "averaging kernel should have dimensions {time,vertical,vertical}");
return -1;
}
if (averaging_kernel->dimension[1] != averaging_kernel->dimension[2])
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "vertical dimensions of averaging kernel do not match");
return -1;
}
if (variable->dimension[0] != averaging_kernel->dimension[0] ||
variable->dimension[variable->num_dimensions - 1] != averaging_kernel->dimension[1])
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "variable and avk have inconsistent dimensions");
return -1;
}
max_vertical_elements = averaging_kernel->dimension[1];
if (apriori != NULL)
{
if (apriori->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for apriori");
return -1;
}
if (apriori->num_dimensions != 2 || apriori->dimension_type[0] != harp_dimension_time ||
apriori->dimension_type[1] != harp_dimension_vertical)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "apriori should have dimensions {time,vertical}");
return -1;
}
if (apriori->dimension[0] != averaging_kernel->dimension[0] ||
apriori->dimension[1] != averaging_kernel->dimension[1])
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "apriori and avk have inconsistent dimensions");
return -1;
}
}
if (vertical_axis != NULL)
{
if (vertical_axis->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for axis variable");
return -1;
}
if (vertical_axis->num_dimensions != 2 || vertical_axis->dimension_type[0] != harp_dimension_time ||
vertical_axis->dimension_type[1] != harp_dimension_vertical)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "axis variable should have dimensions {time,vertical}");
return -1;
}
if (vertical_axis->dimension[0] != averaging_kernel->dimension[0] ||
vertical_axis->dimension[1] != averaging_kernel->dimension[1])
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "axis variable and avk have inconsistent dimensions");
return -1;
}
}
/* allocate memory for the temporary vertical profile vector */
vector = malloc(max_vertical_elements * sizeof(double));
if (!vector)
{
harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
max_vertical_elements * sizeof(double), __FILE__, __LINE__);
return -1;
}
/* calculate the number of blocks in this datetime slice of the variable */
num_blocks = variable->num_elements / variable->dimension[0] / max_vertical_elements;
for (k = 0; k < variable->dimension[0]; k++)
{
long num_vertical_elements = max_vertical_elements;
if (vertical_axis != NULL)
{
num_vertical_elements =
get_unpadded_vector_length(&vertical_axis->data.double_data[k * max_vertical_elements],
max_vertical_elements);
}
for (l = 0; l < num_blocks; l++)
{
long blockoffset = (k * num_blocks + l) * max_vertical_elements;
long i, j;
/* store profile in temporary vector */
for (i = 0; i < num_vertical_elements; i++)
{
vector[i] = variable->data.double_data[blockoffset + i];
}
/* subtract a priori */
if (apriori != NULL)
{
for (i = 0; i < num_vertical_elements; i++)
{
vector[i] -= apriori->data.double_data[k * max_vertical_elements + i];
}
}
/* multiply by avk */
for (i = 0; i < num_vertical_elements; i++)
{
if (!harp_isnan(vector[i]))
{
long avk_offset = (k * max_vertical_elements + i) * max_vertical_elements;
long num_valid = 0;
variable->data.double_data[blockoffset + i] = 0;
for (j = 0; j < num_vertical_elements; j++)
{
if (!harp_isnan(vector[j]))
{
variable->data.double_data[blockoffset + i] +=
averaging_kernel->data.double_data[avk_offset + j] * vector[j];
num_valid++;
}
}
/* add the apriori again */
if (apriori != NULL)
{
variable->data.double_data[blockoffset + i] +=
apriori->data.double_data[k * max_vertical_elements + i];
}
else if (num_valid == 0)
{
variable->data.double_data[blockoffset + i] = harp_nan();
}
}
}
}
}
free(vector);
return 0;
}
/**
* @}
*/
/** \addtogroup harp_product
* @{
*/
/** Smooth the product's variables using the vertical grids, avks and a apriori of the collocated product.
*
* The product is first fully regridded (using the vertical dimension) to the vertical grid of the averaging kernel
* (and apriori). Then, the given list of variables is smoothed using the list of AVKs and apriori variables.
*
* \param product Product to smooth.
* \param num_smooth_variables length of smooth_variables.
* \param smooth_variables The names of the variables to smooth.
* \param vertical_axis The name of the variable to use as a vertical axis (pressure/altitude/etc).
* \param vertical_unit The unit in which the vertical_axis will be brought for the regridding.
* \param collocated_product The product containing the collocated measurements and the averaging kernel and a-priori.
*
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
LIBHARP_API int harp_product_smooth_vertical_with_collocated_product(harp_product *product, int num_smooth_variables,
const char **smooth_variables,
const char *vertical_axis,
const char *vertical_unit,
const harp_product *collocated_product)
{
harp_dimension_type local_dimension_type[HARP_NUM_DIM_TYPES];
harp_data_type data_type;
harp_product *temp_product = NULL;
char vertical_bounds_name[MAX_NAME_LENGTH];
char avk_name[MAX_NAME_LENGTH];
char apriori_name[MAX_NAME_LENGTH];
harp_variable *collocation_index = NULL;
harp_variable *vertical_grid = NULL;
harp_variable *vertical_bounds = NULL;
harp_variable *avk = NULL;
harp_variable *apriori = NULL;
harp_variable *variable = NULL;
harp_variable *temp_variable = NULL;
int i;
if (product->dimension[harp_dimension_vertical] == 0)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no vertical dimension");
return -1;
}
/* raise warnings for any variables that were not present */
for (i = 0; i < num_smooth_variables; i++)
{
if (!harp_product_has_variable(product, smooth_variables[i]))
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no variable named '%s'", smooth_variables[i]);
return -1;
}
}
snprintf(vertical_bounds_name, MAX_NAME_LENGTH, "%s_bounds", vertical_axis);
if (harp_product_new(&temp_product) != 0)
{
return -1;
}
data_type = harp_type_int32;
local_dimension_type[0] = harp_dimension_time;
if (harp_product_get_derived_variable(collocated_product, "collocation_index", &data_type, NULL, 1,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
local_dimension_type[0] = harp_dimension_time;
local_dimension_type[1] = harp_dimension_vertical;
local_dimension_type[2] = harp_dimension_independent;
/* vertical grid */
data_type = harp_type_double;
if (harp_product_get_derived_variable(collocated_product, vertical_axis, &data_type, vertical_unit, 2,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
/* vertical grid bounds */
if (harp_product_get_derived_variable(collocated_product, vertical_bounds_name, &data_type, vertical_unit, 3,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
local_dimension_type[2] = harp_dimension_vertical;
for (i = 0; i < num_smooth_variables; i++)
{
snprintf(avk_name, MAX_NAME_LENGTH, "%s_avk", smooth_variables[i]);
snprintf(apriori_name, MAX_NAME_LENGTH, "%s_apriori", smooth_variables[i]);
harp_product_get_variable_by_name(product, smooth_variables[i], &variable);
/* avk */
if (harp_product_get_derived_variable(collocated_product, avk_name, &data_type, "", 3, local_dimension_type,
&temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
/* apriori profile */
if (harp_product_get_derived_variable(collocated_product, apriori_name, &data_type, variable->unit, 2,
local_dimension_type, &temp_variable) == 0)
{
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
}
}
/* Get the source product's collocation index variable */
if (harp_product_get_variable_by_name(product, "collocation_index", &collocation_index) != 0)
{
return -1;
}
/* sort/filter the reduced collocated product so the samples are in the same order as in 'product' */
if (harp_product_filter_by_index(temp_product, "collocation_index", collocation_index->num_elements,
collocation_index->data.int32_data) != 0)
{
harp_product_delete(temp_product);
return -1;
}
harp_product_get_variable_by_name(temp_product, vertical_axis, &vertical_grid);
harp_product_get_variable_by_name(temp_product, vertical_bounds_name, &vertical_bounds);
if (harp_product_regrid_with_axis_variable(product, vertical_grid, vertical_bounds) != 0)
{
harp_product_delete(temp_product);
return -1;
}
for (i = 0; i < num_smooth_variables; i++)
{
snprintf(avk_name, MAX_NAME_LENGTH, "%s_avk", smooth_variables[i]);
snprintf(apriori_name, MAX_NAME_LENGTH, "%s_apriori", smooth_variables[i]);
harp_product_get_variable_by_name(product, smooth_variables[i], &variable);
harp_product_get_variable_by_name(temp_product, avk_name, &avk);
apriori = NULL;
if (harp_product_has_variable(temp_product, apriori_name))
{
harp_product_get_variable_by_name(temp_product, apriori_name, &apriori);
}
if (harp_variable_smooth_vertical(variable, vertical_grid, avk, apriori) != 0)
{
harp_product_delete(temp_product);
return -1;
}
}
harp_product_delete(temp_product);
return 0;
}
/** Smooth the product's variables (from dataset a in the collocation result) using the vertical grids,
* avks and a apriori of collocated products in dataset b.
*
* The product is first fully regridded (using the vertical dimension) to the vertical grid of the averaging kernel
* (and apriori). Then, the given list of variables is smoothed using the list of AVKs and apriori variables.
*
* \param product Product to smooth.
* \param num_smooth_variables length of smooth_variables.
* \param smooth_variables The names of the variables to smooth.
* \param vertical_axis The name of the variable to use as a vertical axis (pressure/altitude/etc).
* \param vertical_unit The unit in which the vertical_axis will be brought for the regridding.
* \param collocation_result The collocation result used to locate the matching vertical grids/avks/apriori.
* The collocation result is assumed to have the appropriate metadata available for all matches (dataset b).
*
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
LIBHARP_API int harp_product_smooth_vertical_with_collocated_dataset(harp_product *product, int num_smooth_variables,
const char **smooth_variables,
const char *vertical_axis,
const char *vertical_unit,
const harp_collocation_result *collocation_result)
{
harp_collocation_result *filtered_collocation_result = NULL;
harp_data_type data_type = harp_type_double;
harp_product *merged_product = NULL;
char vertical_bounds_name[MAX_NAME_LENGTH];
char avk_name[MAX_NAME_LENGTH];
char apriori_name[MAX_NAME_LENGTH];
harp_variable *variable = NULL;
harp_variable *collocation_index = NULL;
harp_variable *vertical_grid = NULL;
harp_variable *vertical_bounds = NULL;
harp_variable *avk = NULL;
harp_variable *apriori = NULL;
long i;
if (product->dimension[harp_dimension_vertical] == 0)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no vertical dimension");
return -1;
}
/* raise warnings for any variables that were not present */
for (i = 0; i < num_smooth_variables; i++)
{
if (!harp_product_has_variable(product, smooth_variables[i]))
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no variable named '%s'", smooth_variables[i]);
return -1;
}
}
/* Get the source product's collocation index variable */
if (harp_product_get_variable_by_name(product, "collocation_index", &collocation_index) != 0)
{
return -1;
}
/* copy the collocation result for filtering */
if (harp_collocation_result_shallow_copy(collocation_result, &filtered_collocation_result) != 0)
{
return -1;
}
/* Reduce the collocation result to only pairs that include the source product */
if (harp_collocation_result_filter_for_collocation_indices(filtered_collocation_result,
collocation_index->num_elements,
collocation_index->data.int32_data) != 0)
{
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
if (filtered_collocation_result->num_pairs != collocation_index->num_elements)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product and collocation result are inconsistent");
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
snprintf(vertical_bounds_name, MAX_NAME_LENGTH, "%s_bounds", vertical_axis);
for (i = 0; i < filtered_collocation_result->dataset_b->num_products; i++)
{
harp_dimension_type local_dimension_type[HARP_NUM_DIM_TYPES];
harp_product *collocated_product;
long j;
if (harp_collocation_result_get_filtered_product_b(filtered_collocation_result,
filtered_collocation_result->dataset_b->source_product[i],
&collocated_product) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
if (collocated_product == NULL || harp_product_is_empty(collocated_product))
{
continue;
}
local_dimension_type[0] = harp_dimension_time;
local_dimension_type[1] = harp_dimension_vertical;
local_dimension_type[2] = harp_dimension_independent;
/* vertical grid */
if (harp_product_add_derived_variable(collocated_product, vertical_axis, &data_type, vertical_unit, 2,
local_dimension_type) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
/* vertical grid bounds */
if (harp_product_add_derived_variable(collocated_product, vertical_bounds_name, &data_type, vertical_unit, 3,
local_dimension_type) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
local_dimension_type[2] = harp_dimension_vertical;
for (j = 0; j < num_smooth_variables; j++)
{
snprintf(avk_name, MAX_NAME_LENGTH, "%s_avk", smooth_variables[j]);
snprintf(apriori_name, MAX_NAME_LENGTH, "%s_apriori", smooth_variables[j]);
harp_product_get_variable_by_name(product, smooth_variables[j], &variable);
/* avk */
if (harp_product_add_derived_variable(collocated_product, avk_name, &data_type, "", 3, local_dimension_type)
!= 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
/* apriori profile */
harp_product_add_derived_variable(collocated_product, apriori_name, &data_type, variable->unit, 2,
local_dimension_type);
/* it is Ok if the apriori cannot be derived (we ignore the return value of the function) */
}
/* strip collocated product to the variables that we need */
for (j = collocated_product->num_variables - 1; j >= 0; j--)
{
const char *name = collocated_product->variable[j]->name;
if (strcmp(name, "collocation_index") != 0 && strcmp(name, vertical_axis) != 0 &&
strcmp(name, vertical_bounds_name) != 0 && strstr(name, "_avk") == NULL &&
strstr(name, "_apriori") == NULL)
{
if (harp_product_remove_variable(collocated_product, collocated_product->variable[j]) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
}
}
if (merged_product == NULL)
{
merged_product = collocated_product;
}
else
{
if (harp_product_append(merged_product, collocated_product) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
harp_product_delete(collocated_product);
}
}
if (merged_product == NULL)
{
harp_collocation_result_shallow_delete(filtered_collocation_result);
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "collocated dataset does not contain any matching pairs");
return -1;
}
/* sort/filter the merged product so the samples are in the same order as in 'product' */
if (harp_product_filter_by_index(merged_product, "collocation_index", collocation_index->num_elements,
collocation_index->data.int32_data) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
harp_product_get_variable_by_name(merged_product, vertical_axis, &vertical_grid);
harp_product_get_variable_by_name(merged_product, vertical_bounds_name, &vertical_bounds);
if (harp_product_regrid_with_axis_variable(product, vertical_grid, vertical_bounds) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
for (i = 0; i < num_smooth_variables; i++)
{
snprintf(avk_name, MAX_NAME_LENGTH, "%s_avk", smooth_variables[i]);
snprintf(apriori_name, MAX_NAME_LENGTH, "%s_apriori", smooth_variables[i]);
harp_product_get_variable_by_name(product, smooth_variables[i], &variable);
harp_product_get_variable_by_name(merged_product, avk_name, &avk);
apriori = NULL;
if (harp_product_has_variable(merged_product, apriori_name))
{
harp_product_get_variable_by_name(merged_product, apriori_name, &apriori);
}
if (harp_variable_smooth_vertical(variable, vertical_grid, avk, apriori) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
}
/* cleanup */
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return 0;
}
/** Derive vertical column smoothed with column averaging kernel and optional a-priori
* First a partial column profile will be derived from the product.
* This partial column profile will be regridded to the column averaging kernel grid.
* The regridded column profile will then be combined with the column averaging kernel and optional apriori profile
* to create an integrated smoothed vertical column.
* All inputs need to be provided as 'double' data.
* \param product Product from which to derive a smoothed integrated vertical column.
* \param name Name of the variable that should be created.
* \param unit Unit (optional) of the variable that should be created.
* \param vertical_grid Variable containing the vertical grid of the column avk.
* \param vertical_bounds Variable containig the grid boundaries of the column avk (optional).
* \param column_avk Column averaging kernel variable.
* \param apriori Apriori profile (optional).
* \param variable Pointer to the C variable where the derived HARP variable will be stored.
*
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
LIBHARP_API int harp_product_get_smoothed_column(harp_product *product, const char *name, const char *unit,
harp_variable *vertical_grid, harp_variable *vertical_bounds,
harp_variable *column_avk, harp_variable *apriori,
harp_variable **variable)
{
harp_data_type data_type = harp_type_double;
harp_dimension_type grid_dim_type[2];
harp_product *regrid_product;
harp_variable *column_variable;
harp_variable *partcol_variable;
harp_variable *source_grid;
harp_variable *source_bounds;
long num_vertical_elements;
long i, j;
if (product->dimension[harp_dimension_vertical] == 0)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no vertical dimension");
return -1;
}
if (vertical_grid->num_dimensions < 1 ||
vertical_grid->dimension_type[vertical_grid->num_dimensions - 1] != harp_dimension_vertical)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "vertical grid has invalid dimensions");
return -1;
}
if (vertical_grid->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for vertical grid");
return -1;
}
/* vertical_bounds are checked by harp_product_regrid_with_axis_variable() */
if (column_avk->num_dimensions < 1 ||
column_avk->dimension_type[column_avk->num_dimensions - 1] != harp_dimension_vertical)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "column avk has invalid dimensions");
return -1;
}
num_vertical_elements = vertical_grid->dimension[vertical_grid->num_dimensions - 1];
if (column_avk->dimension[column_avk->num_dimensions - 1] != num_vertical_elements)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "column avk and vertical grid have inconsistent dimensions");
return -1;
}
if (column_avk->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for column avk");
return -1;
}
if (apriori != NULL)
{
if (apriori->data_type != harp_type_double)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid data type for apriori");
return -1;
}
if (apriori->num_dimensions != column_avk->num_dimensions)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "apriori profile and column avk have inconsistent dimensions");
return -1;
}
for (i = 0; i < apriori->num_dimensions; i++)
{
if (apriori->dimension_type[i] != column_avk->dimension_type[i] ||
apriori->dimension[i] != column_avk->dimension[i])
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT,
"apriori profile and column avk have inconsistent dimensions");
return -1;
}
}
}
if (harp_product_new(®rid_product) != 0)
{
return -1;
}
/* retrieve partial column profile from source product */
if (harp_product_get_derived_variable(product, name, &data_type, unit, column_avk->num_dimensions,
column_avk->dimension_type, &partcol_variable) != 0)
{
harp_product_delete(regrid_product);
return -1;
}
if (harp_product_add_variable(regrid_product, partcol_variable) != 0)
{
harp_variable_delete(partcol_variable);
harp_product_delete(regrid_product);
return -1;
}
grid_dim_type[0] = harp_dimension_time;
grid_dim_type[1] = harp_dimension_vertical;
/* Add axis variables for the source grid to the temporary product */
if (harp_product_get_derived_variable(product, vertical_grid->name, &vertical_grid->data_type, vertical_grid->unit,
1, &grid_dim_type[1], &source_grid) != 0)
{
/* Failed to derive time independent. Try time dependent. */
if (harp_product_get_derived_variable(product, vertical_grid->name, &vertical_grid->data_type,
vertical_grid->unit, 2, grid_dim_type, &source_grid) != 0)
{
harp_product_delete(regrid_product);
return -1;
}
}
if (harp_product_add_variable(regrid_product, source_grid) != 0)
{
harp_variable_delete(source_grid);
harp_product_delete(regrid_product);
return -1;
}
if (harp_product_get_derived_bounds_for_grid(product, source_grid, &source_bounds) != 0)
{
harp_product_delete(regrid_product);
return -1;
}
if (harp_product_add_variable(regrid_product, source_bounds) != 0)
{
harp_variable_delete(source_bounds);
harp_product_delete(regrid_product);
return -1;
}
if (harp_product_regrid_with_axis_variable(regrid_product, vertical_grid, vertical_bounds) != 0)
{
harp_product_delete(regrid_product);
return -1;
}
if (harp_variable_new(name, harp_type_double, column_avk->num_dimensions - 1, column_avk->dimension_type,
column_avk->dimension, &column_variable) != 0)
{
harp_product_delete(regrid_product);
return -1;
}
if (harp_variable_set_unit(column_variable, unit) != 0)
{
harp_variable_delete(column_variable);
harp_product_delete(regrid_product);
return -1;
}
for (i = 0; i < column_variable->num_elements; i++)
{
int is_valid = 0;
/* multiply partial column profile with column averaging kernel */
column_variable->data.double_data[i] = 0;
for (j = 0; j < num_vertical_elements; j++)
{
if (!harp_isnan(partcol_variable->data.double_data[i * num_vertical_elements + j]))
{
column_variable->data.double_data[i] +=
partcol_variable->data.double_data[i * num_vertical_elements + j] *
column_avk->data.double_data[i * num_vertical_elements + j];
is_valid = 1;
/* subtract the apriori */
if (apriori != NULL && !harp_isnan(apriori->data.double_data[i * num_vertical_elements + j]))
{
column_variable->data.double_data[i] -=
column_avk->data.double_data[i * num_vertical_elements + j] *
apriori->data.double_data[i * num_vertical_elements + j];
}
}
/* add the apriori */
if (apriori != NULL && !harp_isnan(apriori->data.double_data[i * num_vertical_elements + j]))
{
column_variable->data.double_data[i] += apriori->data.double_data[i * num_vertical_elements + j];
is_valid = 1;
}
}
if (!is_valid)
{
column_variable->data.double_data[i] = harp_nan();
}
}
harp_product_delete(regrid_product);
*variable = column_variable;
return 0;
}
/** Derive a vertical column smoothed with column averaging kernel and a-priori from the collocated product
*
* \param product Product to regrid.
* \param name Name of the variable that should be created.
* \param unit Unit (optional) of the variable that should be created.
* \param num_dimensions Number of dimensions of the variable that should be created.
* \param dimension_type Type of dimension for each of the dimensions of the variable that should be created.
* \param vertical_axis The name of the variable to use as a vertical axis (pressure/altitude/etc).
* \param vertical_unit The unit in which the vertical_axis will be brought for the regridding.
* \param collocated_product The product containing the collocated measurements and the averaging kernel and a-priori.
* \param variable Pointer to the C variable where the derived HARP variable will be stored.
*
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
LIBHARP_API int harp_product_get_smoothed_column_using_collocated_product(harp_product *product, const char *name,
const char *unit, int num_dimensions,
const harp_dimension_type *dimension_type,
const char *vertical_axis,
const char *vertical_unit,
const harp_product *collocated_product,
harp_variable **variable)
{
harp_dimension_type local_dimension_type[HARP_NUM_DIM_TYPES];
harp_data_type data_type;
harp_product *temp_product = NULL;
char vertical_bounds_name[MAX_NAME_LENGTH];
char column_avk_name[MAX_NAME_LENGTH];
char apriori_name[MAX_NAME_LENGTH];
harp_variable *collocation_index = NULL;
harp_variable *vertical_grid = NULL;
harp_variable *vertical_bounds = NULL;
harp_variable *column_avk = NULL;
harp_variable *apriori = NULL;
harp_variable *temp_variable = NULL;
int i;
if (num_dimensions == 0 || dimension_type[0] != harp_dimension_time)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT,
"first dimension of requested smoothed vertical column should be the time dimension");
return -1;
}
if (num_dimensions >= HARP_NUM_DIM_TYPES)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "number of dimensions (%d) too large (%s:%u)", num_dimensions,
__FILE__, __LINE__);
return -1;
}
if (product->dimension[harp_dimension_vertical] == 0)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no vertical dimension");
return -1;
}
snprintf(vertical_bounds_name, MAX_NAME_LENGTH, "%s_bounds", vertical_axis);
snprintf(column_avk_name, MAX_NAME_LENGTH, "%s_avk", name);
snprintf(apriori_name, MAX_NAME_LENGTH, "%s_apriori", name);
if (harp_product_new(&temp_product) != 0)
{
return -1;
}
data_type = harp_type_int32;
local_dimension_type[0] = harp_dimension_time;
if (harp_product_get_derived_variable(collocated_product, "collocation_index", &data_type, NULL, 1,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
local_dimension_type[0] = harp_dimension_time;
local_dimension_type[1] = harp_dimension_vertical;
local_dimension_type[2] = harp_dimension_independent;
/* vertical grid */
data_type = harp_type_double;
if (harp_product_get_derived_variable(collocated_product, vertical_axis, &data_type, vertical_unit, 2,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
/* vertical grid bounds */
if (harp_product_get_derived_variable(collocated_product, vertical_bounds_name, &data_type, vertical_unit, 3,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
for (i = 0; i < num_dimensions; i++)
{
local_dimension_type[i] = dimension_type[i];
}
local_dimension_type[num_dimensions] = harp_dimension_vertical;
/* column avk */
if (harp_product_get_derived_variable(collocated_product, column_avk_name, &data_type, "", num_dimensions + 1,
local_dimension_type, &temp_variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
/* apriori profile */
if (harp_product_get_derived_variable(collocated_product, apriori_name, &data_type, unit, num_dimensions + 1,
local_dimension_type, &temp_variable) == 0)
{
if (harp_product_add_variable(temp_product, temp_variable) != 0)
{
harp_variable_delete(temp_variable);
harp_product_delete(temp_product);
return -1;
}
}
/* Get the source product's collocation index variable */
if (harp_product_get_variable_by_name(product, "collocation_index", &collocation_index) != 0)
{
return -1;
}
/* sort/filter the reduced collocated product so the samples are in the same order as in 'product' */
if (harp_product_filter_by_index(temp_product, "collocation_index", collocation_index->num_elements,
collocation_index->data.int32_data) != 0)
{
harp_product_delete(temp_product);
return -1;
}
harp_product_get_variable_by_name(temp_product, vertical_axis, &vertical_grid);
harp_product_get_variable_by_name(temp_product, vertical_bounds_name, &vertical_bounds);
harp_product_get_variable_by_name(temp_product, column_avk_name, &column_avk);
if (harp_product_has_variable(temp_product, apriori_name))
{
harp_product_get_variable_by_name(temp_product, apriori_name, &apriori);
}
if (harp_product_get_smoothed_column(product, name, unit, vertical_grid, vertical_bounds, column_avk, apriori,
variable) != 0)
{
harp_product_delete(temp_product);
return -1;
}
harp_product_delete(temp_product);
return 0;
}
/** Derive a vertical column smoothed with column averaging kernel and a-priori from collocated products in dataset b
*
* \param product Product to regrid.
* \param name Name of the variable that should be created.
* \param unit Unit (optional) of the variable that should be created.
* \param num_dimensions Number of dimensions of the variable that should be created.
* \param dimension_type Type of dimension for each of the dimensions of the variable that should be created.
* \param vertical_axis The name of the variable to use as a vertical axis (pressure/altitude/etc).
* \param vertical_unit The unit in which the vertical_axis will be brought for the regridding.
* \param collocation_result The collocation result used to find matching variables.
* The collocation result is assumed to have the appropriate metadata available for all matches (dataset b).
* \param variable Pointer to the C variable where the derived HARP variable will be stored.
*
* \return
* \arg \c 0, Success.
* \arg \c -1, Error occurred (check #harp_errno).
*/
LIBHARP_API int harp_product_get_smoothed_column_using_collocated_dataset(harp_product *product, const char *name,
const char *unit, int num_dimensions,
const harp_dimension_type *dimension_type,
const char *vertical_axis,
const char *vertical_unit,
const harp_collocation_result
*collocation_result, harp_variable **variable)
{
harp_collocation_result *filtered_collocation_result = NULL;
harp_data_type data_type = harp_type_double;
harp_product *merged_product = NULL;
char vertical_bounds_name[MAX_NAME_LENGTH];
char column_avk_name[MAX_NAME_LENGTH];
char apriori_name[MAX_NAME_LENGTH];
harp_variable *collocation_index = NULL;
harp_variable *vertical_grid = NULL;
harp_variable *vertical_bounds = NULL;
harp_variable *column_avk = NULL;
harp_variable *apriori = NULL;
long i;
if (num_dimensions == 0 || dimension_type[0] != harp_dimension_time)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT,
"first dimension of requested smoothed vertical column should be the time dimension");
return -1;
}
if (num_dimensions >= HARP_NUM_DIM_TYPES)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "number of dimensions (%d) too large (%s:%u)", num_dimensions,
__FILE__, __LINE__);
return -1;
}
if (product->dimension[harp_dimension_vertical] == 0)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product has no vertical dimension");
return -1;
}
/* Get the source product's collocation index variable */
if (harp_product_get_variable_by_name(product, "collocation_index", &collocation_index) != 0)
{
return -1;
}
/* copy the collocation result for filtering */
if (harp_collocation_result_shallow_copy(collocation_result, &filtered_collocation_result) != 0)
{
return -1;
}
/* Reduce the collocation result to only pairs that include the source product */
if (harp_collocation_result_filter_for_collocation_indices(filtered_collocation_result,
collocation_index->num_elements,
collocation_index->data.int32_data) != 0)
{
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
if (filtered_collocation_result->num_pairs != collocation_index->num_elements)
{
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "product and collocation result are inconsistent");
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
snprintf(vertical_bounds_name, MAX_NAME_LENGTH, "%s_bounds", vertical_axis);
snprintf(column_avk_name, MAX_NAME_LENGTH, "%s_avk", name);
snprintf(apriori_name, MAX_NAME_LENGTH, "%s_apriori", name);
for (i = 0; i < filtered_collocation_result->dataset_b->num_products; i++)
{
harp_dimension_type local_dimension_type[HARP_NUM_DIM_TYPES];
harp_product *collocated_product;
long j;
if (harp_collocation_result_get_filtered_product_b(filtered_collocation_result,
filtered_collocation_result->dataset_b->source_product[i],
&collocated_product) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
if (collocated_product == NULL || harp_product_is_empty(collocated_product))
{
continue;
}
local_dimension_type[0] = harp_dimension_time;
local_dimension_type[1] = harp_dimension_vertical;
local_dimension_type[2] = harp_dimension_independent;
/* vertical grid */
if (harp_product_add_derived_variable(collocated_product, vertical_axis, &data_type, vertical_unit, 2,
local_dimension_type) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
/* vertical grid bounds */
if (harp_product_add_derived_variable(collocated_product, vertical_bounds_name, &data_type, vertical_unit, 3,
local_dimension_type) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
for (j = 0; j < num_dimensions; j++)
{
local_dimension_type[j] = dimension_type[j];
}
local_dimension_type[num_dimensions] = harp_dimension_vertical;
/* column avk */
if (harp_product_add_derived_variable(collocated_product, column_avk_name, &data_type, "", num_dimensions + 1,
local_dimension_type) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
/* apriori profile */
harp_product_add_derived_variable(collocated_product, apriori_name, &data_type, unit, num_dimensions + 1,
local_dimension_type);
/* it is Ok if the apriori cannot be derived (we ignore the return value of the function) */
/* strip collocated product to just the variables that we need */
for (j = collocated_product->num_variables - 1; j >= 0; j--)
{
const char *name = collocated_product->variable[j]->name;
if (strcmp(name, "collocation_index") != 0 && strcmp(name, vertical_axis) != 0 &&
strcmp(name, vertical_bounds_name) != 0 && strcmp(name, column_avk_name) != 0 &&
strcmp(name, apriori_name) != 0)
{
if (harp_product_remove_variable(collocated_product, collocated_product->variable[j]) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
}
}
if (merged_product == NULL)
{
merged_product = collocated_product;
}
else
{
if (harp_product_append(merged_product, collocated_product) != 0)
{
harp_product_delete(collocated_product);
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
harp_product_delete(collocated_product);
}
}
if (merged_product == NULL)
{
harp_collocation_result_shallow_delete(filtered_collocation_result);
harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "collocated dataset does not contain any matching pairs");
return -1;
}
/* sort/filter the merged product so the samples are in the same order as in 'product' */
if (harp_product_filter_by_index(merged_product, "collocation_index", collocation_index->num_elements,
collocation_index->data.int32_data) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
harp_product_get_variable_by_name(merged_product, vertical_axis, &vertical_grid);
harp_product_get_variable_by_name(merged_product, vertical_bounds_name, &vertical_bounds);
harp_product_get_variable_by_name(merged_product, column_avk_name, &column_avk);
if (harp_product_has_variable(merged_product, apriori_name))
{
harp_product_get_variable_by_name(merged_product, apriori_name, &apriori);
}
if (harp_product_get_smoothed_column(product, name, unit, vertical_grid, vertical_bounds, column_avk, apriori,
variable) != 0)
{
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return -1;
}
/* cleanup */
harp_product_delete(merged_product);
harp_collocation_result_shallow_delete(filtered_collocation_result);
return 0;
}
/**
* @}
*/
|