/usr/share/doc/netcdf-doc/netcdf-tutorial.html is in netcdf-doc 1:4.1.3-7ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 | <html lang="en">
<head>
<title>The NetCDF Tutorial</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The NetCDF Tutorial">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="top" href="#Top">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 2005-2009 University Corporation for
Atmospheric Research
Permission is granted to make and distribute verbatim copies of this
manual provided that the copyright notice and these paragraphs are
preserved on all copies. The software and any accompanying written
materials are provided ``as is'' without warranty of any kind. UCAR
expressly disclaims all warranties of any kind, either expressed or
implied, including but not limited to the implied warranties of
merchantability and fitness for a particular purpose.
The Unidata Program Center is managed by the University
Corporation for Atmospheric Research and sponsored by the National
Science Foundation. Any opinions, findings, conclusions, or
recommendations expressed in this publication are those of the
author(s) and do not necessarily reflect the views of the National
Science Foundation.
Mention of any commercial company or product in this document
does not constitute an endorsement by the Unidata Program Center.
Unidata does not authorize any use of information from this
publication for advertising or publicity purposes.-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<h1 class="settitle">The NetCDF Tutorial</h1>
<div class="node">
<a name="Top"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Intro">Intro</a>,
Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
</div>
<h2 class="unnumbered">NetCDF Tutorial</h2>
<p>This tutorial aims to give a quick and painless introduction to
netCDF.
<p>For the basic concepts of netCDF see <a href="#Intro">Intro</a>. Read this to
understand the netCDF data models and how to use netCDF.
<p>For sets of examples of increasing complexity see <a href="#Examples">Examples</a>. The
example programs are provided for each of four netCDF API languages,
C, C++, F77, and F90. (No new netCDF-4 API extensions are used in
these examples.)
<p>For a quick reference to the important functions in the netCDF-3 C,
C++, Fortran 77, and Fortran 90 APIs, with hyper-links to the full
documentation of each function, see <a href="#Useful-Functions">Useful Functions</a>.
<p>To learn about the new features of netCDF-4, see
<a href="#API_002dExtensions">API-Extensions</a>. For some examples see <a href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>.
<p>NetCDF interface guides are available for C (see <a href="netcdf-c.html#Top">The NetCDF C Interface Guide</a>), C++ (see <a href="netcdf-cxx.html#Top">The NetCDF C++ Interface Guide</a>), Fortran 77
(see <a href="netcdf-f77.html#Top">The NetCDF Fortran 77 Interface Guide</a>), and
Fortran 90 (see <a href="netcdf-f90.html#Top">The NetCDF Fortran 90 Interface Guide</a>).
<p>This document applies to netCDF version 4.1.3; it was last
updated on 30 June 2011.
<ul class="menu">
<li><a accesskey="1" href="#Intro">Intro</a>: What is NetCDF?
<li><a accesskey="2" href="#Examples">Examples</a>: Examples of increasing complexity.
<li><a accesskey="3" href="#Useful-Functions">Useful Functions</a>: Quick reference to useful netCDF functions.
<li><a accesskey="4" href="#API_002dExtensions">API-Extensions</a>: New features added in netCDF-4.
<li><a accesskey="5" href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>
<li><a accesskey="6" href="#Combined-Index">Combined Index</a>
</li></ul>
<p>--- The Detailed Node Listing ---
<p>What is NetCDF?
</p>
<ul class="menu">
<li><a accesskey="7" href="#Data-Model">Data Model</a>: How netCDF classic sees data.
<li><a accesskey="8" href="#Common-Data-Model">Common Data Model</a>: The new, expanded data model.
<li><a accesskey="9" href="#Errors">Errors</a>: When things go tragically awry.
<li><a href="#Unlimited-Dimensions">Unlimited Dimensions</a>: Arbitrarily extending a dimension.
<li><a href="#Fill-Values">Fill Values</a>: Handling missing data.
<li><a href="#Tools">Tools</a>: Useful tools for netCDF files.
<li><a href="#APIs">APIs</a>: Programming languages and netCDF.
<li><a href="#Documentation">Documentation</a>: Introducing the netCDF documentation!
<li><a href="#Versions">Versions</a>: Different versions of netCDF.
</li></ul>
<p>Example Programs
</p>
<ul class="menu">
<li><a href="#simple_005fxy">simple_xy</a>: A very simple netCDF file.
<li><a href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>: A more complex file with more metadata.
<li><a href="#pres_005ftemp_005f4D">pres_temp_4D</a>: A 4D file with an unlimited dimension.
</li></ul>
<p>The simple_xy Example
</p>
<ul class="menu">
<li><a href="#simple_005fxy-in-C">simple_xy in C</a>
<li><a href="#simple_005fxy-in-F77">simple_xy in F77</a>
<li><a href="#simple_005fxy-in-F90">simple_xy in F90</a>
<li><a href="#simple_005fxy-in-C_002b_002b">simple_xy in C++</a>
</li></ul>
<p>simple_xy_wr.c and simple_xy_rd.c
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fwr_002ec">simple_xy_wr.c</a>
<li><a href="#simple_005fxy_005frd_002ec">simple_xy_rd.c</a>
</li></ul>
<p>simple_xy_wr.f and simple_xy_rd.f
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fwr_002ef">simple_xy_wr.f</a>
<li><a href="#simple_005fxy_005frd_002ef">simple_xy_rd.f</a>
</li></ul>
<p>simple_xy_wr.f90 and simple_xy_rd.f90
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fwr_002ef90">simple_xy_wr.f90</a>
<li><a href="#simple_005fxy_005frd_002ef90">simple_xy_rd.f90</a>
</li></ul>
<p>simple_xy_wr.cpp and simple_xy_rd.cpp
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fwr_002ecpp">simple_xy_wr.cpp</a>
<li><a href="#simple_005fxy_005frd_002ecpp">simple_xy_rd.cpp</a>
</li></ul>
<p>The sfc_pres_temp Example
</p>
<ul class="menu">
<li><a href="#sfc_005fpres_005ftemp-in-C">sfc_pres_temp in C</a>
<li><a href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>
<li><a href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>
<li><a href="#sfc_005fpres_005ftemp-in-C_002b_002b">sfc_pres_temp in C++</a>
</li></ul>
<p>sfc_pres_temp_wr.c and sfc_pres_temp_rd.c
</p>
<ul class="menu">
<li><a href="#sfc_005fpres_005ftemp_005fwr_002ec">sfc_pres_temp_wr.c</a>
<li><a href="#sfc_005fpres_005ftemp_005frd_002ec">sfc_pres_temp_rd.c</a>
</li></ul>
<p>sfc_pres_temp_wr.f and sfc_pres_temp_rd.f
</p>
<ul class="menu">
<li><a href="#sfc_005fpres_005ftemp_005fwr_002ef">sfc_pres_temp_wr.f</a>
<li><a href="#sfc_005fpres_005ftemp_005frd_002ef">sfc_pres_temp_rd.f</a>
</li></ul>
<p>sfc_pres_temp_wr.f90 and sfc_pres_temp_rd.f90
</p>
<ul class="menu">
<li><a href="#sfc_005fpres_005ftemp_005fwr_002ef90">sfc_pres_temp_wr.f90</a>
<li><a href="#sfc_005fpres_005ftemp_005frd_002ef90">sfc_pres_temp_rd.f90</a>
</li></ul>
<p>sfc_pres_temp_wr.cpp and sfc_pres_temp_rd.cpp
</p>
<ul class="menu">
<li><a href="#sfc_005fpres_005ftemp_005fwr_002ecpp">sfc_pres_temp_wr.cpp</a>
<li><a href="#sfc_005fpres_005ftemp_005frd_002ecpp">sfc_pres_temp_rd.cpp</a>
</li></ul>
<p>The pres_temp_4D Example
</p>
<ul class="menu">
<li><a href="#pres_005ftemp_005f4D-in-C">pres_temp_4D in C</a>
<li><a href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>
<li><a href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>
<li><a href="#pres_005ftemp_005f4D-in-C_002b_002b">pres_temp_4D in C++</a>
</li></ul>
<p>pres_temp_4D_wr.c and pres_temp_4D_rd.c
</p>
<ul class="menu">
<li><a href="#pres_005ftemp_005f4D_005fwr_002ec">pres_temp_4D_wr.c</a>
<li><a href="#pres_005ftemp_005f4D_005frd_002ec">pres_temp_4D_rd.c</a>
</li></ul>
<p>pres_temp_4D_wr.f and pres_temp_4D_rd.f
</p>
<ul class="menu">
<li><a href="#pres_005ftemp_005f4D_005fwr_002ef">pres_temp_4D_wr.f</a>
<li><a href="#pres_005ftemp_005f4D_005frd_002ef">pres_temp_4D_rd.f</a>
</li></ul>
<p>pres_temp_4D_wr.f90 and pres_temp_4D_rd.f90
</p>
<ul class="menu">
<li><a href="#pres_005ftemp_005f4D_005fwr_002ef90">pres_temp_4D_wr.f90</a>
<li><a href="#pres_005ftemp_005f4D_005frd_002ef90">pres_temp_4D_rd.f90</a>
</li></ul>
<p>pres_temp_4D_wr.cpp and pres_temp_4D_rd.cpp
</p>
<ul class="menu">
<li><a href="#pres_005ftemp_005f4D_005fwr_002ecpp">pres_temp_4D_wr.cpp</a>
<li><a href="#pres_005ftemp_005f4D_005frd_002ecpp">pres_temp_4D_rd.cpp</a>
</li></ul>
<p>The Functions You Need in NetCDF-3
</p>
<ul class="menu">
<li><a href="#Creation">Creation</a>: Creating netCDF files, adding metadata.
<li><a href="#Reading">Reading</a>: Reading netCDF files of known structure.
<li><a href="#Inquiry-Functions">Inquiry Functions</a>: Learning about an unknown netCDF file.
<li><a href="#Subsets">Subsets</a>: Reading and writing Subsets of data.
</li></ul>
<p>Creating New Files and Metadata, an Overview
</p>
<ul class="menu">
<li><a href="#Creation-in-C">Creation in C</a>
<li><a href="#Creation-in-F77">Creation in F77</a>
<li><a href="#Creation-in-F90">Creation in F90</a>
<li><a href="#Creation-in-C_002b_002b">Creation in C++</a>
</li></ul>
<p>Numbering of NetCDF IDs
</p>
<ul class="menu">
<li><a href="#Reading-in-C">Reading in C</a>
<li><a href="#Reading-in-F77">Reading in F77</a>
<li><a href="#Reading-in-F90">Reading in F90</a>
<li><a href="#Reading-in-C_002b_002b">Reading in C++</a>
</li></ul>
<p>Reading NetCDF Files of Unknown Structure
</p>
<ul class="menu">
<li><a href="#Inquiry-in-C">Inquiry in C</a>
<li><a href="#Inquiry-in-F77">Inquiry in F77</a>
<li><a href="#Inquiry-in-F90">Inquiry in F90</a>
<li><a href="#Inquiry-in-C_002b_002b">Inquiry in C++</a>
</li></ul>
<p>Reading and Writing Subsets of Data
</p>
<ul class="menu">
<li><a href="#Subsetting-in-C">Subsetting in C</a>
<li><a href="#Subsetting-in-F77">Subsetting in F77</a>
<li><a href="#Subsetting-in-F90">Subsetting in F90</a>
<li><a href="#Subsetting-in-C_002b_002b">Subsetting in C++</a>
</li></ul>
<p>API Extensions Introduced with NetCDF-4
</p>
<ul class="menu">
<li><a href="#Interoperability">Interoperability</a>: Reading and writing HDF5 files.
<li><a href="#Multiple_002dUnlimited_002dDimensions">Multiple-Unlimited-Dimensions</a>: Use more than one unlimited dimension.
<li><a href="#Groups">Groups</a>: Organizing data hierarchically.
<li><a href="#Compound_002dTypes">Compound-Types</a>: Creating data type like C structs.
<li><a href="#Opaque_002dTypes">Opaque-Types</a>: Creating a data type of known size.
<li><a href="#VLEN_002dType">VLEN-Type</a>: Variable length arrays.
<li><a href="#Strings">Strings</a>: Storing strings of data.
<li><a href="#New_002dinq_002dFunctions">New-inq-Functions</a>: Functions to help explore a file.
<li><a href="#Parallel">Parallel</a>: How to get parallel I/O.
<li><a href="#Future">Future</a>: What's coming next!
</li></ul>
<p>Collective/Independent Access
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fpar-in-C">simple_xy_par in C</a>
</li></ul>
<p>simple_xy_par_wr.c and simple_xy_par_rd.c
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fpar_005fwr_002ef90">simple_xy_par_wr.f90</a>
<li><a href="#simple_005fxy_005fpar_005frd_002ef90">simple_xy_par_rd.f90</a>
</li></ul>
<p>NetCDF-4 Examples
</p>
<ul class="menu">
<li><a href="#simple_005fnc4">simple_nc4</a>
<li><a href="#simple_005fxy_005fnc4">simple_xy_nc4</a>
</li></ul>
<p>The simple_nc4 Example
</p>
<ul class="menu">
<li><a href="#simple_005fnc4-in-C">simple_nc4 in C</a>
</li></ul>
<p>simple_nc4_wr.c and simple_nc4_rd.c
</p>
<ul class="menu">
<li><a href="#simple_005fnc4_005fwr_002ec">simple_nc4_wr.c</a>
<li><a href="#simple_005fnc4_005frd_002ec">simple_nc4_rd.c</a>
</li></ul>
<p>The simple_xy_nc4 Example
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fnc4-in-C">simple_xy_nc4 in C</a>
<li><a href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>
<li><a href="#simple_005fxy_005fnc4-in-F90">simple_xy_nc4 in F90</a>
</li></ul>
<p>simple_xy_nc4_wr.c and simple_xy_nc4_rd.c
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fnc4_005fwr_002ec">simple_xy_nc4_wr.c</a>
<li><a href="#simple_005fxy_005fnc4_005frd_002ec">simple_xy_nc4_rd.c</a>
</li></ul>
<p>simple_xy_nc4_wr.f and simple_xy_nc4_rd.f
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fnc4_005fwr_002ef">simple_xy_nc4_wr.f</a>
<li><a href="#simple_005fxy_005fnc4_005frd_002ef">simple_xy_nc4_rd.f</a>
</li></ul>
<p>simple_xy_nc4_wr.f90 and simple_xy_nc4_rd.f90
</p>
<ul class="menu">
<li><a href="#simple_005fxy_005fnc4_005fwr_002ef90">simple_xy_nc4_wr.f90</a>
<li><a href="#simple_005fxy_005fnc4_005frd_002ef90">simple_xy_nc4_rd.f90</a>
</ul>
<div class="node">
<a name="Intro"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Examples">Examples</a>,
Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">1 What is NetCDF?</h2>
<p><a name="index-netCDF_002c-definition-1"></a><a name="index-Unidata-2"></a><a name="index-UCAR-3"></a>
NetCDF is a set of data formats, programming interfaces, and software
libraries that help read and write scientific data files.
<p>NetCDF was developed and is maintained at Unidata, part of the
University Corporation for Atmospheric Research (UCAR) Office of
Programs (UOP). Unidata is funded primarily by the National Science
Foundation.
<p>Unidata provides data and software tools for use in geoscience
education and research. For more information see the web sites of
Unidata (<a href="http://www.unidata.ucar.edu">http://www.unidata.ucar.edu</a>), UOP (<a href="http://www.uop.ucar.edu">http://www.uop.ucar.edu</a>),
and UCAR (<a href="http://www.ucar.edu">http://www.ucar.edu</a>).
<p>This tutorial may serve as an introduction to netCDF. Full netCDF
documentation is available on-line (see <a href="#Documentation">Documentation</a>).
<ul class="menu">
<li><a accesskey="1" href="#Data-Model">Data Model</a>: How netCDF classic sees data.
<li><a accesskey="2" href="#Common-Data-Model">Common Data Model</a>: The new, expanded data model.
<li><a accesskey="3" href="#Errors">Errors</a>: When things go tragically awry.
<li><a accesskey="4" href="#Unlimited-Dimensions">Unlimited Dimensions</a>: Arbitrarily extending a dimension.
<li><a accesskey="5" href="#Fill-Values">Fill Values</a>: Handling missing data.
<li><a accesskey="6" href="#Tools">Tools</a>: Useful tools for netCDF files.
<li><a accesskey="7" href="#APIs">APIs</a>: Programming languages and netCDF.
<li><a accesskey="8" href="#Documentation">Documentation</a>: Introducing the netCDF documentation!
<li><a accesskey="9" href="#Versions">Versions</a>: Different versions of netCDF.
</ul>
<div class="node">
<a name="Data-Model"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Common-Data-Model">Common Data Model</a>,
Previous: <a rel="previous" accesskey="p" href="#Intro">Intro</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.1 The Classic NetCDF Data Model</h3>
<p><a name="index-data-model-4"></a><a name="index-variable-5"></a><a name="index-dimension-6"></a><a name="index-attribute-7"></a>
The classic netCDF data model consists of <dfn>variables</dfn>,
<dfn>dimensions</dfn>, and <dfn>attributes</dfn>. This way of thinking about
data was introduced with the very first netCDF release, and is still
the core of all netCDF files.
<p>(In version 4.0, the netCDF data model has been expanded. See <a href="#Common-Data-Model">Common Data Model</a>.)
<dl>
<dt><code>Variables</code><dd>N-dimensional arrays of data. Variables in netCDF files can be one of
six types (char, byte, short, int, float, double). For more information
see <a href="netcdf.html#Variables">Variables</a>.
<br><dt><code>Dimensions</code><dd>describe the axes of the data arrays. A dimension has a name and a
length. An unlimited dimension has a length that
can be expanded at any time, as more data are written to
it. NetCDF files can contain at most one unlimited dimension. For more
information see <a href="netcdf.html#Dimensions">Dimensions</a>.
<br><dt><code>Attributes</code><dd>annotate variables or files with small notes or supplementary
metadata. Attributes are always scalar values or 1D arrays, which can be
associated with either a variable or the file as a whole. Although
there is no enforced limit, the user is expected to keep attributes
small. For more information see <a href="netcdf.html#Attributes">Attributes</a>.
</dl>
<p>For more information on the netCDF data model see <a href="netcdf.html#The-NetCDF-Data-Model">The NetCDF Data Model</a>.
<h4 class="subsection">1.1.1 Meteorological Example</h4>
<p>NetCDF can be used to store many kinds of data, but it was originally
developed for the Earth science community.
<p>NetCDF views the world of scientific data in the same way that an
atmospheric scientist might: as sets of related arrays. There are
various physical quantities (such as pressure and temperature) located
at points at a particular latitude, longitude, vertical level, and
time.
<p>A scientist might also like to store supporting information, such as
the units, or some information about how the data were produced.
<p>The axis information (latitude, longitude, level, and time) would be
stored as netCDF dimensions. Dimensions have a length and a name.
<p>The physical quantities (pressure, temperature) would be stored as
netCDF variables. Variables are N-dimensional arrays of data, with a
name and an associated set of netCDF dimensions.
<p>It is also customary to add one variable for each dimension, to hold
the values along that axis. These variables are called “coordinate
variables.” The latitude coordinate variable would be a
one-dimensional variable (with latitude as its dimension), and it
would hold the latitude values at each point along the axis.
<p>The additional bits of metadata would be stored as netCDF attributes.
<p>Attributes are always single values or one-dimensional arrays. (This
works out well for a string, which is a one-dimensional array of ASCII
characters.)
<p>The pres_temp_4D example in this tutorial shows how to write and read
a file containing some four-dimensional pressure and temperature data,
including all the metadata needed. See <a href="#pres_005ftemp_005f4D">pres_temp_4D</a>.
<div class="node">
<a name="Common-Data-Model"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Errors">Errors</a>,
Previous: <a rel="previous" accesskey="p" href="#Data-Model">Data Model</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.2 The Common Data Model and NetCDF-4</h3>
<p><a name="index-common-data-model-8"></a><a name="index-groups-9"></a><a name="index-compound-types-10"></a><a name="index-user_002ddefined-types-11"></a><a name="index-netCDF_002d4-model-extensions-12"></a>
With netCDF-4, the netCDF data model has been extended, in a backwards
compatible way.
<p>The new data model, which is known as the “Common Data Model” is
part of an effort here at Unidata to find a common engineering
language for the development of scientific data solutions. It contains
the variables, dimensions, and attributes of the classic data model,
but adds:
<ul>
<li>groups
A way of hierarchically organizing data, similar to directories in a
Unix file system.
<li>user-defined types
The user can now define compound types (like C structures), enumeration
types, variable length arrays, and opaque types.
</ul>
<p>These features may only be used when working with a netCDF-4/HDF5
file. Files created in classic or 64-bit offset format cannot support
groups or user-defined types.
<p>With netCDF-4/HDF5 files, the user may define groups, which may
contain variables, dimensions, and attributes. In this way, a group
acts as a container for the classic netCDF dataset. But netCDF-4/HDF5
files can have many groups, organized hierarchically.
<p>Each file begins with at least one group, the root group. The user may
then add more groups, receiving a new ncid for each group created.
<p>Since each group functions as a complete netCDF classic dataset, it is
possible to have variables with the same name in two or more different
groups, within the same netCDF-4/HDF5 data file.
<p>Dimensions have a special scope: they may be seen by all variables in
their group, and all descendant groups. This allows the user to define
dimensions in a top-level group, and use them in many sub-groups.
<p>Since it may be necessary to write code which works with all types of
netCDF data files, we also introduce the ability to create
netCDF-4/HDF5 files which follow all the rules of the classic netCDF
model. That is, these files are in HDF5, but will not support multiple
unlimited dimensions, user-defined types, groups, etc. They act just
like a classic netCDF file.
<div class="node">
<a name="Errors"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Unlimited-Dimensions">Unlimited Dimensions</a>,
Previous: <a rel="previous" accesskey="p" href="#Common-Data-Model">Common Data Model</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.3 NetCDF Error Handling</h3>
<p>Each netCDF function in the C, Fortran 77, and Fortran 90 APIs returns
0 on success, in the tradition of C. (For C++, see below).
<p>When programming with netCDF in these languages, always check return
values of every netCDF API call. The return code can be looked up in
netcdf.h (for C programmers) or netcdf.inc (for Fortran programmers),
or you can use the strerror function to print out an error
message. (See <a href="netcdf-c.html#nc_005fstrerror">nc_strerror</a>/<a href="netcdf-f77.html#NF_005fSTRERROR">NF_STRERROR</a>/<a href="netcdf-f90.html#NF90_005fSTRERROR">NF90_STRERROR</a>).
<p>In general, if a function returns an error code, you can assume it
didn't do what you hoped it would. The exception is the NC_ERANGE
error, which is returned by any of the reading or writing functions
when one or more of the values read or written exceeded the range for
the type. (For example if you were to try to read 1000 into an
unsigned byte.)
<p>In the case of NC_ERANGE errors, the netCDF library completes the
read/write operation, and then returns the error. The type conversion
is handled like a C type conversion, whether or not it is within
range. This may yield bad data, but the netCDF library just returns
NC_ERANGE and leaves it up to the user to handle. (For more
information about type conversion see <a href="netcdf-c.html#Type-Conversion">Type Conversion</a>).
<p>Error handling in C++ is different. For some objects, the is_valid()
method should be called. Other error handling is controlled by the
NcError class. For more information see <a href="netcdf-cxx.html#Class-NcError">Class NcError</a>.
<p>For a complete list of netCDF error codes see <a href="netcdf-c.html#Error-Codes">Error Codes</a>.
<div class="node">
<a name="Unlimited-Dimensions"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Fill-Values">Fill Values</a>,
Previous: <a rel="previous" accesskey="p" href="#Errors">Errors</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.4 Unlimited Dimensions</h3>
<p>Sometimes you don't know the size of all dimensions when you create a
file, or you would like to arbitrarily extend the file along one of
the dimensions.
<p>For example, model output usually has a time dimension. Rather than
specifying that there will be forty-two output times when creating the
file, you might like to create it with one time, and then add
data for additional times, until you wanted to stop.
<p>For this purpose netCDF provides the unlimited dimension. By
specifying a length of “unlimited” when defining a dimension, you
indicate to netCDF that the dimension may be extended, and its
length may increase.
<p>In netCDF classic files, there can only be one unlimited dimension,
and it must be declared first in the list of dimensions for a
variable.
<p>For programmers, the unlimited dimension will correspond with the
slowest-varying dimension. In C this is the first dimension of an
array, in Fortran, the last.
<p>The third example in this tutorial, pres_temp_4D, demonstrates how to
write and read data one time step at a time along an unlimited
dimension in a classic netCDF file. See <a href="#pres_005ftemp_005f4D">pres_temp_4D</a>.
<p>In netCDF-4/HDF5 files, any number of unlimited dimensions may be
used, and there is no restriction as to where they appear in a
variable's list of dimension IDs.
<p>For more detailed information about dimensions see <a href="netcdf.html#Dimensions">Dimensions</a>.
<div class="node">
<a name="Fill-Values"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Tools">Tools</a>,
Previous: <a rel="previous" accesskey="p" href="#Unlimited-Dimensions">Unlimited Dimensions</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.5 Fill Values</h3>
<p>Sometimes there are missing values in the data, and some value is
needed to represent them.
<p>For example, what value do you put in a sea-surface temperature
variable for points over land?
<p>In netCDF, you can create an attribute for the variable (and of the
same type as the variable) called “_FillValue” that contains a
value that you have used for missing data. Applications that read the
data file can use this to know how to represent these values.
<p>Using attributes it is possible to capture metadata that would
otherwise be separated from the data. Various conventions have been
established. By using a set of conventions, a data producer is more
likely to produce files that can be easily shared within the research
community, and that contain enough details to be useful as a long-term
archive. Conventions also make it easier to develop software that
interprets information represented in data, because a convention
selects one conventional way to represent information when multiple
equivalent representations are possible.
<p>For more information on _FillValue and other attribute conventions,
see <a href="netcdf.html#Attribute-Conventions">Attribute Conventions</a>.
<p>Climate and meteorological users are urged to follow the Climate and
Forecast (CF) metadata conventions when producing data files. For more
information about the CF conventions, see <a href="http://cf-pcmdi.llnl.gov">http://cf-pcmdi.llnl.gov</a>.
<p>For information about creating attributes, see <a href="#Creation">Creation</a>.
<div class="node">
<a name="Tools"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#APIs">APIs</a>,
Previous: <a rel="previous" accesskey="p" href="#Fill-Values">Fill Values</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.6 Tools for Manipulating NetCDF Files</h3>
<p><a name="index-ncdump-13"></a><a name="index-ncgen-14"></a><a name="index-third_002dparty-tools-15"></a><a name="index-software-for-netCDF-16"></a>
Many existing software applications can read and manipulate netCDF
files. Before writing your own program, check to see if any existing
programs meet your needs.
<p>Two utilities come with the netCDF distribution: ncdump and
ncgen. The ncdump command reads a netCDF file and outputs text in
a format called CDL. The ncgen command reads a text file in CDL
format, and generates a netCDF data file.
<p>One common use for ncdump is to examine the metadata of a netCDF file,
to see what it contains. At the beginning of each example in this
tutorial, an ncdump of the resulting data file is
shown. See <a href="#simple_005fxy">simple_xy</a>.
<p>For more information about ncdump and ncgen see <a href="netcdf.html#NetCDF-Utilities">NetCDF Utilities</a>.
<p>The following general-purpose tools have been found to be useful in
many situations. Some of the tools on this list are developed at
Unidata. The others are developed elsewhere, and we can make no
guarantees about their continued availability or success. All of these
tools are open-source.
<p><table summary="">
<tr align="left"><td valign="top" width="20%">UDUNITS
</td><td valign="top" width="40%">Unidata library to help with scientific units.
</td><td valign="top" width="40%"><a href="http://www.unidata.ucar.edu/software/udunits">http://www.unidata.ucar.edu/software/udunits</a>
<p><br></td></tr><tr align="left"><td valign="top" width="20%">IDV
</td><td valign="top" width="40%">Unidata's Integrated Data Viewer, a 3D visualization and analysis
package (Java based).
</td><td valign="top" width="40%"><a href="http://www.unidata.ucar.edu/software/idv">http://www.unidata.ucar.edu/software/idv</a>
<p><br></td></tr><tr align="left"><td valign="top" width="20%">NCL
</td><td valign="top" width="40%">NCAR Command Language, a graphics and data manipulation package.
</td><td valign="top" width="40%"><a href="http://www.ncl.ucar.edu">http://www.ncl.ucar.edu</a>
<p><br></td></tr><tr align="left"><td valign="top" width="20%">GrADS
</td><td valign="top" width="40%">The Grid Analysis and Display System package.
</td><td valign="top" width="40%"><a href="http://grads.iges.org/grads/grads.html">http://grads.iges.org/grads/grads.html</a>
<p><br></td></tr><tr align="left"><td valign="top" width="20%">NCO
</td><td valign="top" width="40%">NetCDF Command line Operators, tools to manipulate netCDF files.
</td><td valign="top" width="40%"><a href="http://nco.sourceforge.net">http://nco.sourceforge.net</a>
<br></td></tr></table>
<p>For a list of netCDF tools that we know about see
<a href="http://www.unidata.ucar.edu/netcdf/software.html">http://www.unidata.ucar.edu/netcdf/software.html</a>. If you know of any that should be
added to this list, send email to support-netcdf@unidata.ucar.edu.
<div class="node">
<a name="APIs"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Documentation">Documentation</a>,
Previous: <a rel="previous" accesskey="p" href="#Tools">Tools</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.7 The NetCDF Programming APIs</h3>
<p><a name="index-tools-for-manipulating-netCDF-17"></a><a name="index-language-APIs-for-netCDF-18"></a><a name="index-perl_002c-netCDF-API-19"></a><a name="index-ruby_002c-netCDF-API-20"></a><a name="index-python_002c-netCDF-API-21"></a><a name="index-C_002b_002b_002c-netCDF-API-22"></a><a name="index-C_002c-netCDF-API-23"></a><a name="index-Fortran-77_002c-netCDF-API-24"></a><a name="index-Fortran-90_002c-netCDF-API-25"></a><a name="index-V2-API-26"></a>
Unidata supports netCDF APIs in C, C++, Fortran 77, Fortran 90, and
Java.
<p>The Java API is a complete implementation of netCDF in Java. It is
distributed independently of the other APIs. For more information see
the netCDF Java page: <a href="http://www.unidata.ucar.edu/software/netcdf-java">http://www.unidata.ucar.edu/software/netcdf-java</a>. If you are
writing web server software, you should certainly be doing so in Java.
<p>The C, C++, Fortran 77 and Fortran 90 APIs are distributed and
installed when the netCDF C library is built, if compilers exist to
build them, and if they are not turned off when configuring the netCDF
build.
<p>The C++ and Fortran APIs depend on the C API. Due to the nature of C++
and Fortran 90, users of those languages can also use the C and
Fortran 77 APIs (respectively) directly.
<p>In the netCDF-4.0 beta release, only the C API is well-tested. The
Fortran APIs include support for netCDF-4 advanced features, but need
more testing, which will be added in a future release of netCDF.
<p>The C++ API can handle netCDF-4.0/HDF5 files, but can not yet handle
advanced netCDF-4 features. The successor to the current C++ API is
under active development, and will include support for netCDF-4
advanced features.
<p>Full documentation exists for each API (see <a href="#Documentation">Documentation</a>).
<p>In addition, many other language APIs exist, including Perl, Python,
and Ruby. Most of these APIs were written and supported by netCDF
users. Some of them are listed on the netCDF software page, see
<a href="http://www.unidata.ucar.edu/netcdf/software.html">http://www.unidata.ucar.edu/netcdf/software.html</a>. Since these generally use the C
API, they should work well with netCDF-4/HDF5 files, but the
maintainers of the APIs must add support for netCDF-4 advanced
features.
<p>In addition to the main netCDF-3 C API, there is an additional (older)
C API, the netCDF-2 API. This API produces exactly the same files as
the netCDF-3 API - only the API is different. (That is, users can
create either classic format files, the default, or 64-bit offset
files, or netCDF-4/HDF5 files.)
<p>The version 2 API was the API before netCDF-3.0 came out. It is still
fully supported, however. Programs written to the version 2 API will
continue to work.
<p>Users writing new programs should use the netCDF-3 API, which contains
better type checking, better error handling, and better
documentation.
<p>The netCDF-2 API is provided for backward compatibility. Documentation
for the netCDF-2 API can be found on the netCDF website, see
<a href="http://www.unidata.ucar.edu/netcdf/old_docs/really_old/guide_toc.html">http://www.unidata.ucar.edu/netcdf/old_docs/really_old/guide_toc.html</a>.
<div class="node">
<a name="Documentation"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Versions">Versions</a>,
Previous: <a rel="previous" accesskey="p" href="#APIs">APIs</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.8 NetCDF Documentation</h3>
<p>This tutorial is brief. A much more complete description of netCDF can
be found in The NetCDF Users Guide. It fully describes the netCDF model and
format. For more information see <a href="netcdf.html#Top">The NetCDF Users Guide</a>.
<p>The netCDF distribution, in various forms, can be obtained from the
netCDF web site: <a href="http://www.unidata.ucar.edu/netcdf">http://www.unidata.ucar.edu/netcdf</a>.
<p>A porting and installation guide for the C, C++, Fortran 77, and
Fortran 90 APIs describes how to build these APIs on a variety of
platforms. See <a href="netcdf-install.html#Top">The NetCDF Installation and Porting Guide</a>.
<p>Language specific programming guides are available for netCDF for the
C, C++, Fortran 77, Fortran 90, and Java APIs:
<dl>
<dt><code>C</code><dd><a href="netcdf-c.html#Top">The NetCDF C Interface Guide</a>.
<br><dt><code>C++</code><dd><a href="netcdf-cxx.html#Top">The NetCDF C++ Interface Guide</a>.
<br><dt><code>Fortran 77</code><dd><a href="netcdf-f77.html#Top">The NetCDF Fortran 77 Interface Guide</a>.
<br><dt><code>Fortran 90</code><dd><a href="netcdf-f90.html#Top">The NetCDF Fortran 90 Interface Guide</a>.
<br><dt><code>Java</code><dd><a href="http://www.unidata.ucar.edu/software/netcdf-java/v2.1/NetcdfJavaUserManual.htm">http://www.unidata.ucar.edu/software/netcdf-java/v2.1/NetcdfJavaUserManual.htm</a>.
</dl>
<p>Man pages for the C, F77, and F90 interfaces, and ncgen and ncdump,
are available on the documentation page of the netCDF web site
(<a href="http://www.unidata.ucar.edu/netcdf/docs">http://www.unidata.ucar.edu/netcdf/docs</a>), and are installed with the netCDF
distribution.
<p>The latest version of all netCDF documentation can always be found at
the documentation page of the netCDF web site: <a href="http://www.unidata.ucar.edu/netcdf/docs">http://www.unidata.ucar.edu/netcdf/docs</a>
<div class="node">
<a name="Versions"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Documentation">Documentation</a>,
Up: <a rel="up" accesskey="u" href="#Intro">Intro</a>
</div>
<h3 class="section">1.9 A Note on NetCDF Versions and Formats</h3>
<p><a name="index-classic-format-27"></a><a name="index-g_t64_002dbit-offset-format-28"></a>
NetCDF has changed (and improved) over its lifetime. That means
the user must have some understanding of netCDF versions.
<p>To add to the confusion, there are versions for the APIs, and also for
the data files that they produce. The API version is the version
number that appears in the tarball file that is downloaded from the
netCDF website. For example this document applied to API version
4.1.3.
<p>The good news is that all netCDF files ever written can always be read
by the latest netCDF release. That is, we guarantee backward data
compatibility.
<h4 class="subsection">1.9.1 Classic Format</h4>
<p>The default format is classic format. This is the original netCDF
binary format - the format that the netCDF library has been using for
almost 20 years, since its introduction with the first release of
netCDF. No special flag is needed to create a file in classic format;
it is the default.
<p>Classic format has some strict limitations for files larger than two
gigabytes. (see <a href="netcdf.html#NetCDF-Classic-Format-Limitations">NetCDF Classic Format Limitations</a>).
<h4 class="subsection">1.9.2 64-bit Offset Format</h4>
<p>In December, 2004, version 3.6.0 of the netCDF library was
released. It allows users to use a new version of the netCDF file
format which greatly expands the sizes of variables and files which
may be written.
<p>The format which was introduced in 3.6.0 is called “64-bit Offset
Format.”
<p>Create files in this format by passing the 64-bit offset format flag
to the create call (for example, in C, set the NC_64BIT_OFFSET flag
when calling the function nc_create. (see <a href="netcdf-c.html#nc_005fcreate">nc_create</a>).
<p>64-bit offset is very useful for very large data files (over two
gigabytes), however these files can only be shared with those who have
upgraded to version 3.6.0 (or better) of netCDF. Earlier versions of
netCDF will not be able to read these files.
<h4 class="subsection">1.9.3 NetCDF-4/HDF5 Format</h4>
<p>With version 4.0 of netCDF, we introduce another new data format:
netCDF-4/HDF5 format. This format is HDF5, with full use of the new
dimension scales, creation ordering, and other features of HDF5 added
in its version 1.8.0 release.
<p>As with 64-bit offset, this format is turned on when the file is
created. (For example, with the nf_netcdf4 flag in the nf_create
function. see <a href="netcdf-f77.html#nf_005fcreate">nf_create</a>).
<h4 class="subsection">1.9.4 Sharing Data</h4>
<p>The classic format is the most portable. Classic format files can be
read correctly by any version of netCDF. A netCDF-4 user can create a
classic file, and share it with a user who has not upgraded netCDF
since the version 2.3 in 1994.
<p>64-bit offset format files can be read by any user who has at least
version 3.6.0 of the netCDF API (released in Dec., 2004).
<p>Users must have netCDF 4.0 to read netCDF-4/HDF5 files. However,
netCDF-4 does produce backward compatible classic and 64-bit offset
format files. That is, a netCDF-4.0 user can create a classic format
file, and share it with researchers who are still using a old version
of netCDF. Similarly a netCDF-4.0 user can read any existing netCDF
data file, whatever version of netCDF was used to create it.
<h4 class="subsection">1.9.5 Classic Model</h4>
<p>The original netCDF API represents a data model as well as a
programming API. That is, the idea of variables, attributes, and the
six data types (char, byte, short, integer, float, and double),
comprises a model of how data may be stored.
<p>The netCDF-4 release expands this model with groups, user-defined
types, and new base types. New functions have been added to the APIs
to accommodate these extensions, but once they are used, the file can
no longer be output as a classic format file.
<p>That is, once you use groups in your code, you can only produce
netCDF-4/HDF5 files. If you try to change the format to classic, you
will get an error when you attempt to use any of the group functions.
<p>Since it is convenient to be able to produce files of all formats from
the same code (restricting oneself to the classic data model), a flag
has been added which, when used in the creation of a netCDF-4/HDF5
file, will instruct the library to disallow the use of any advanced
features in the file.
<p>This is referred to as a “classic model” netCDF-4/HDF5 file.
<p>To get a classic model file, use the classic model flag when creating
the file. For example, in Fortran 77, use the nf_classic_model flag
when calling nf_create (see <a href="netcdf-f77.html#nf_005fcreate">nf_create</a>).
<p>For more information about format issues see <a href="netcdf.html#Format">The NetCDF Users Guide</a>.
<div class="node">
<a name="Examples"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Useful-Functions">Useful Functions</a>,
Previous: <a rel="previous" accesskey="p" href="#Intro">Intro</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">2 Example Programs</h2>
<p><a name="index-example-programs-29"></a><a name="index-simple_005fxy-example-30"></a><a name="index-sfc_005fpres_005ftemp-example-31"></a><a name="index-pres_005ftemp_005f4D-example-32"></a>
The netCDF example programs show how to use netCDF.
<p>In the netCDF distribution, the “examples” directory contains
examples in C, Fortran 77, Fortran 90, C++, and CDL.
<p>There are three sets of netCDF-3 example programs in each
language. Each language has its own subdirectory under the
“examples” directory (for example, the Fortran 77 examples are in
“examples/F77”).
<p>There is also one example for netCDF-4, which is only provided in the
C language. This example will only be run if the –enable-netcdf-4
option was used with configure.
<p>The examples are built and run with the “make check” command. (For
more information on building netCDF, see <a href="netcdf-install.html#Top">The NetCDF Installation and Porting Guide</a>).
<p>The examples create, and then read, example data files of increasing
complexity.
<p>The corresponding examples in each language create identical netCDF
data files. For example, the C program sfc_pres_temp_wr.c produces the
same data file as the Fortran 77 program sfc_pres_temp_wr.f.
<p>For convenience, the complete source code in each language can be
found in this tutorial, as well as in the netCDF distribution.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy">simple_xy</a>: A very simple netCDF file.
<li><a accesskey="2" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>: A more complex file with more metadata.
<li><a accesskey="3" href="#pres_005ftemp_005f4D">pres_temp_4D</a>: A 4D file with an unlimited dimension.
</ul>
<div class="node">
<a name="simple_xy"></a>
<a name="simple_005fxy"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>,
Previous: <a rel="previous" accesskey="p" href="#Examples">Examples</a>,
Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
</div>
<h3 class="section">2.1 The simple_xy Example</h3>
<p>This example is an unrealistically simple netCDF file, to demonstrate
the minimum operation of the netCDF APIs. Users should seek to make
their netCDF files more self-describing than this primitive example.
<p>As in all the netCDF tutorial examples, this example file is created
by C, Fortran 77, Fortran 90, and C++ programs, and by ncgen, which
creates it from a CDL script. All examples create identical files,
“simple_xy.nc.”
<p>The programs that create this sample file all have the base name
“simple_xy_wr”, with different extensions depending on the
language.
<p>Therefore the example files that create simple_xy.nc can be found in:
C/simple_xy_wr.c, F77/simple_xy_wr.f, F90/simple_xy_wr.f90,
CXX/simple_xy_wr.cpp, and CDL/simple_xy_wr.cdl.
<p>Corresponding read programs (C/simple_xy_rd.c, etc.) read the
simple_xy.nc data file, and ensure that it contains the correct
values.
<p>The simple_xy.nc data file contains two dimensions, “x” and “y”,
and one netCDF variable, “data.”
<p>The utility ncdump can be used to show the contents of netCDF
files. By default, ncdump shows the CDL description of the file. This
CDL description can be fed into ncgen to create the data file.
<p>The CDL for this example is shown below. For more information on
ncdump and ncgen see <a href="netcdf.html#NetCDF-Utilities">NetCDF Utilities</a>.
<pre class="example"> netcdf simple_xy {
dimensions:
x = 6 ;
y = 12 ;
variables:
int data(x, y) ;
data:
data =
0, 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 ;
}
</pre>
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy-in-C">simple_xy in C</a>
<li><a accesskey="2" href="#simple_005fxy-in-F77">simple_xy in F77</a>
<li><a accesskey="3" href="#simple_005fxy-in-F90">simple_xy in F90</a>
<li><a accesskey="4" href="#simple_005fxy-in-C_002b_002b">simple_xy in C++</a>
</ul>
<div class="node">
<a name="simple_xy-in-C"></a>
<a name="simple_005fxy-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy-in-F77">simple_xy in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy">simple_xy</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy">simple_xy</a>
</div>
<h4 class="subsection">2.1.1 simple_xy_wr.c and simple_xy_rd.c</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/C.
<p>The example program simple_xy_wr.c creates the example
data file simple_xy.nc. The example program simple_xy_rd.c reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fwr_002ec">simple_xy_wr.c</a>
<li><a accesskey="2" href="#simple_005fxy_005frd_002ec">simple_xy_rd.c</a>
</ul>
<div class="node">
<a name="simple_xy_wr.c"></a>
<a name="simple_005fxy_005fwr_002ec"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005frd_002ec">simple_xy_rd.c</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-C">simple_xy in C</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-C">simple_xy in C</a>
</div>
<h5 class="subsubsection">2.1.1.1 simple_xy_wr.c</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is a very simple example which writes a 2D array of
sample data. To handle this in netCDF we create two shared
dimensions, "x" and "y", and a netCDF variable, called "data".
This example demonstrates the netCDF C API. This is part of the
netCDF tutorial, which can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$Id: simple_xy_wr.c,v 1.12 2007/02/14 20:59:21 ed Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "simple_xy.nc"
/* We are writing 2D data, a 6 x 12 grid. */
#define NDIMS 2
#define NX 6
#define NY 12
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
/* When we create netCDF variables and dimensions, we get back an
* ID for each one. */
int ncid, x_dimid, y_dimid, varid;
int dimids[NDIMS];
/* This is the data array we will write. It will be filled with a
* progression of numbers for this example. */
int data_out[NX][NY];
/* Loop indexes, and error handling. */
int x, y, retval;
/* Create some pretend data. If this wasn't an example program, we
* would have some real data to write, for example, model
* output. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
data_out[x][y] = x * NY + y;
/* Always check the return code of every netCDF function call. In
* this example program, any retval which is not equal to NC_NOERR
* (0) will cause the program to print an error message and exit
* with a non-zero return code. */
/* Create the file. The NC_CLOBBER parameter tells netCDF to
* overwrite this file, if it already exists.*/
if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid)))
ERR(retval);
/* Define the dimensions. NetCDF will hand back an ID for each. */
if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
ERR(retval);
/* The dimids array is used to pass the IDs of the dimensions of
* the variable. */
dimids[0] = x_dimid;
dimids[1] = y_dimid;
/* Define the variable. The type of the variable in this case is
* NC_INT (4-byte integer). */
if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
dimids, &varid)))
ERR(retval);
/* End define mode. This tells netCDF we are done defining
* metadata. */
if ((retval = nc_enddef(ncid)))
ERR(retval);
/* Write the pretend data to the file. Although netCDF supports
* reading and writing subsets of data, in this case we write all
* the data in one operation. */
if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
ERR(retval);
/* Close the file. This frees up any internal netCDF resources
* associated with the file, and flushes any buffers. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file simple_xy.nc!\n");
return 0;
}
</pre>
<div class="node">
<a name="simple_xy_rd.c"></a>
<a name="simple_005fxy_005frd_002ec"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fwr_002ec">simple_xy_wr.c</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-C">simple_xy in C</a>
</div>
<h5 class="subsubsection">2.1.1.2 simple_xy_rd.c</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is a simple example which reads a small dummy array, which was
written by simple_xy_wr.c. This is intended to illustrate the use
of the netCDF C API.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$Id: simple_xy_rd.c,v 1.9 2006/08/17 23:00:55 russ Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will read. */
#define FILE_NAME "simple_xy.nc"
/* We are reading 2D data, a 6 x 12 grid. */
#define NX 6
#define NY 12
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
/* This will be the netCDF ID for the file and data variable. */
int ncid, varid;
int data_in[NX][NY];
/* Loop indexes, and error handling. */
int x, y, retval;
/* Open the file. NC_NOWRITE tells netCDF we want read-only access
* to the file.*/
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);
/* Get the varid of the data variable, based on its name. */
if ((retval = nc_inq_varid(ncid, "data", &varid)))
ERR(retval);
/* Read the data. */
if ((retval = nc_get_var_int(ncid, varid, &data_in[0][0])))
ERR(retval);
/* Check the data. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
if (data_in[x][y] != x * NY + y)
return ERRCODE;
/* Close the file, freeing all resources. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
return 0;
}
</pre>
<div class="node">
<a name="simple_xy-in-F77"></a>
<a name="simple_005fxy-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy-in-F90">simple_xy in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-C">simple_xy in C</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy">simple_xy</a>
</div>
<h4 class="subsection">2.1.2 simple_xy_wr.f and simple_xy_rd.f</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/F77.
<p>The example program simple_xy_wr.f creates the example
data file simple_xy.nc. The example program simple_xy_rd.f reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fwr_002ef">simple_xy_wr.f</a>
<li><a accesskey="2" href="#simple_005fxy_005frd_002ef">simple_xy_rd.f</a>
</ul>
<div class="node">
<a name="simple_xy_wr.f"></a>
<a name="simple_005fxy_005fwr_002ef"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005frd_002ef">simple_xy_rd.f</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-F77">simple_xy in F77</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-F77">simple_xy in F77</a>
</div>
<h5 class="subsubsection">2.1.2.1 simple_xy_wr.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is a very simple example which writes a 2D array of
C sample data. To handle this in netCDF we create two shared
C dimensions, "x" and "y", and a netCDF variable, called "data".
C This example demonstrates the netCDF Fortran 77 API. This is part
C of the netCDF tutorial, which can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: simple_xy_wr.f,v 1.11 2008/08/20 22:29:56 russ Exp $
program simple_xy_wr
implicit none
include 'netcdf.inc'
C This is the name of the data file we will create.
character*(*) FILE_NAME
parameter (FILE_NAME='simple_xy.nc')
C We are writing 2D data, a 12 x 6 grid.
integer NDIMS
parameter (NDIMS=2)
integer NX, NY
parameter (NX = 6, NY = 12)
C When we create netCDF files, variables and dimensions, we get back
C an ID for each one.
integer ncid, varid, dimids(NDIMS)
integer x_dimid, y_dimid
C This is the data array we will write. It will just be filled with
C a progression of integers for this example.
integer data_out(NY, NX)
C Loop indexes, and error handling.
integer x, y, retval
C Create some pretend data. If this wasn't an example program, we
C would have some real data to write, for example, model output.
do x = 1, NX
do y = 1, NY
data_out(y, x) = (x - 1) * NY + (y - 1)
end do
end do
C Always check the return code of every netCDF function call. In
C this example program, any retval which is not equal to nf_noerr
C (0) will call handle_err, which prints a netCDF error message, and
C then exits with a non-zero return code.
C Create the netCDF file. The nf_clobber parameter tells netCDF to
C overwrite this file, if it already exists.
retval = nf_create(FILE_NAME, NF_CLOBBER, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the dimensions. NetCDF will hand back an ID for each.
retval = nf_def_dim(ncid, "x", NX, x_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, "y", NY, y_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
C The dimids array is used to pass the IDs of the dimensions of
C the variables. Note that in fortran arrays are stored in
C column-major format.
dimids(2) = x_dimid
dimids(1) = y_dimid
C Define the variable. The type of the variable in this case is
C NF_INT (4-byte integer).
retval = nf_def_var(ncid, "data", NF_INT, NDIMS, dimids, varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C End define mode. This tells netCDF we are done defining metadata.
retval = nf_enddef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Write the pretend data to the file. Although netCDF supports
C reading and writing subsets of data, in this case we write all the
C data in one operation.
retval = nf_put_var_int(ncid, varid, data_out)
if (retval .ne. nf_noerr) call handle_err(retval)
C Close the file. This frees up any internal netCDF resources
C associated with the file, and flushes any buffers.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
print *,'*** SUCCESS writing example file simple_xy.nc!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="simple_xy_rd.f"></a>
<a name="simple_005fxy_005frd_002ef"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fwr_002ef">simple_xy_wr.f</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-F77">simple_xy in F77</a>
</div>
<h5 class="subsubsection">2.1.2.2 simple_xy_rd.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is a simple example which reads a small dummy array, from a
C netCDF data file created by the companion program simple_xy_wr.f.
C This is intended to illustrate the use of the netCDF fortran 77
C API. This example program is part of the netCDF tutorial, which can
C be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: simple_xy_rd.f,v 1.8 2007/02/14 20:59:20 ed Exp $
program simple_xy_rd
implicit none
include 'netcdf.inc'
C This is the name of the data file we will read.
character*(*) FILE_NAME
parameter (FILE_NAME='simple_xy.nc')
C We are reading 2D data, a 12 x 6 grid.
integer NX, NY
parameter (NX = 6, NY = 12)
integer data_in(NY, NX)
C This will be the netCDF ID for the file and data variable.
integer ncid, varid
C Loop indexes, and error handling.
integer x, y, retval
C Open the file. NF_NOWRITE tells netCDF we want read-only access to
C the file.
retval = nf_open(FILE_NAME, NF_NOWRITE, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Get the varid of the data variable, based on its name.
retval = nf_inq_varid(ncid, 'data', varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Read the data.
retval = nf_get_var_int(ncid, varid, data_in)
if (retval .ne. nf_noerr) call handle_err(retval)
C Check the data.
do x = 1, NX
do y = 1, NY
if (data_in(y, x) .ne. (x - 1) * NY + (y - 1)) then
print *, 'data_in(', y, ', ', x, ') = ', data_in(y, x)
stop 2
end if
end do
end do
C Close the file, freeing all resources.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
print *,'*** SUCCESS reading example file ', FILE_NAME, '!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="simple_xy-in-F90"></a>
<a name="simple_005fxy-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy-in-C_002b_002b">simple_xy in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-F77">simple_xy in F77</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy">simple_xy</a>
</div>
<h4 class="subsection">2.1.3 simple_xy_wr.f90 and simple_xy_rd.f90</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/F90.
<p>The example program simple_xy_wr.f90 creates the example
data file simple_xy.nc. The example program simple_xy_rd.f90 reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fwr_002ef90">simple_xy_wr.f90</a>
<li><a accesskey="2" href="#simple_005fxy_005frd_002ef90">simple_xy_rd.f90</a>
</ul>
<div class="node">
<a name="simple_xy_wr.f90"></a>
<a name="simple_005fxy_005fwr_002ef90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005frd_002ef90">simple_xy_rd.f90</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-F90">simple_xy in F90</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-F90">simple_xy in F90</a>
</div>
<h5 class="subsubsection">2.1.3.1 simple_xy_wr.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is a very simple example which writes a 2D array of
! sample data. To handle this in netCDF we create two shared
! dimensions, "x" and "y", and a netCDF variable, called "data".
! This example demonstrates the netCDF Fortran 90 API. This is part
! of the netCDF tutorial, which can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_wr.f90,v 1.11 2010/04/06 19:32:08 ed Exp $
program simple_xy_wr
use netcdf
implicit none
! This is the name of the data file we will create.
character (len = *), parameter :: FILE_NAME = "simple_xy.nc"
! We are writing 2D data, a 12 x 6 grid.
integer, parameter :: NDIMS = 2
integer, parameter :: NX = 6, NY = 12
! When we create netCDF files, variables and dimensions, we get back
! an ID for each one.
integer :: ncid, varid, dimids(NDIMS)
integer :: x_dimid, y_dimid
! This is the data array we will write. It will just be filled with
! a progression of integers for this example.
integer, dimension(:,:), allocatable :: data_out
! Loop indexes, and error handling.
integer :: x, y
! Allocate memory for data.
allocate(data_out(NY, NX))
! Create some pretend data. If this wasn't an example program, we
! would have some real data to write, for example, model output.
do x = 1, NX
do y = 1, NY
data_out(y, x) = (x - 1) * NY + (y - 1)
end do
end do
! Always check the return code of every netCDF function call. In
! this example program, wrapping netCDF calls with "call check()"
! makes sure that any return which is not equal to nf90_noerr (0)
! will print a netCDF error message and exit.
! Create the netCDF file. The nf90_clobber parameter tells netCDF to
! overwrite this file, if it already exists.
call check( nf90_create(FILE_NAME, NF90_CLOBBER, ncid) )
! Define the dimensions. NetCDF will hand back an ID for each.
call check( nf90_def_dim(ncid, "x", NX, x_dimid) )
call check( nf90_def_dim(ncid, "y", NY, y_dimid) )
! The dimids array is used to pass the IDs of the dimensions of
! the variables. Note that in fortran arrays are stored in
! column-major format.
dimids = (/ y_dimid, x_dimid /)
! Define the variable. The type of the variable in this case is
! NF90_INT (4-byte integer).
call check( nf90_def_var(ncid, "data", NF90_INT, dimids, varid) )
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
! Write the pretend data to the file. Although netCDF supports
! reading and writing subsets of data, in this case we write all the
! data in one operation.
call check( nf90_put_var(ncid, varid, data_out) )
! Close the file. This frees up any internal netCDF resources
! associated with the file, and flushes any buffers.
call check( nf90_close(ncid) )
print *, "*** SUCCESS writing example file simple_xy.nc! "
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program simple_xy_wr
</pre>
<div class="node">
<a name="simple_xy_rd.f90"></a>
<a name="simple_005fxy_005frd_002ef90"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fwr_002ef90">simple_xy_wr.f90</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-F90">simple_xy in F90</a>
</div>
<h5 class="subsubsection">2.1.3.2 simple_xy_rd.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is a simple example which reads a small dummy array, from a
! netCDF data file created by the companion program simple_xy_wr.f90.
! This is intended to illustrate the use of the netCDF fortran 90
! API. This example program is part of the netCDF tutorial, which can
! be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_rd.f90,v 1.11 2010/04/06 19:32:09 ed Exp $
program simple_xy_rd
use netcdf
implicit none
! This is the name of the data file we will read.
character (len = *), parameter :: FILE_NAME = "simple_xy.nc"
! We are reading 2D data, a 12 x 6 grid.
integer, parameter :: NX = 6, NY = 12
integer, dimension(:,:), allocatable :: data_in
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid
! Loop indexes, and error handling.
integer :: x, y
! Allocate memory for data.
allocate(data_in(NY, NX))
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check( nf90_open(FILE_NAME, NF90_NOWRITE, ncid) )
! Get the varid of the data variable, based on its name.
call check( nf90_inq_varid(ncid, "data", varid) )
! Read the data.
call check( nf90_get_var(ncid, varid, data_in) )
! Check the data.
do x = 1, NX
do y = 1, NY
if (data_in(y, x) /= (x - 1) * NY + (y - 1)) then
print *, "data_in(", y, ", ", x, ") = ", data_in(y, x)
stop "Stopped"
end if
end do
end do
! Close the file, freeing all resources.
call check( nf90_close(ncid) )
print *,"*** SUCCESS reading example file ", FILE_NAME, "! "
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program simple_xy_rd
</pre>
<div class="node">
<a name="simple_xy-in-C++"></a>
<a name="simple_005fxy-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-F90">simple_xy in F90</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy">simple_xy</a>
</div>
<h4 class="subsection">2.1.4 simple_xy_wr.cpp and simple_xy_rd.cpp</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/CXX.
<p>The example program simple_xy_wr.cpp creates the example
data file simple_xy.nc. The example program simple_xy_rd.cpp reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fwr_002ecpp">simple_xy_wr.cpp</a>
<li><a accesskey="2" href="#simple_005fxy_005frd_002ecpp">simple_xy_rd.cpp</a>
</ul>
<div class="node">
<a name="simple_xy_wr.cpp"></a>
<a name="simple_005fxy_005fwr_002ecpp"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005frd_002ecpp">simple_xy_rd.cpp</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy-in-C_002b_002b">simple_xy in C++</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-C_002b_002b">simple_xy in C++</a>
</div>
<h5 class="subsubsection">2.1.4.1 simple_xy_wr.cpp</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is a very simple example which writes a 2D array of
sample data. To handle this in netCDF we create two shared
dimensions, "x" and "y", and a netCDF variable, called "data".
This example is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: simple_xy_wr.cpp,v 1.15 2007/01/19 12:52:13 ed Exp $
*/
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
// We are writing 2D data, a 6 x 12 grid.
static const int NX = 6;
static const int NY = 12;
// Return this in event of a problem.
static const int NC_ERR = 2;
int
main(void)
{
// This is the data array we will write. It will just be filled
// with a progression of numbers for this example.
int dataOut[NX][NY];
// Create some pretend data. If this wasn't an example program, we
// would have some real data to write, for example, model output.
for(int i = 0; i < NX; i++)
for(int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
// Create the file. The Replace parameter tells netCDF to overwrite
// this file, if it already exists.
NcFile dataFile("simple_xy.nc", NcFile::Replace);
// You should always check whether a netCDF file creation or open
// constructor succeeded.
if (!dataFile.is_valid())
{
cout << "Couldn't open file!\n";
return NC_ERR;
}
// For other method calls, the default behavior of the C++ API is
// to exit with a message if there is an error. If that behavior
// is OK, there is no need to check return values in simple cases
// like the following.
// When we create netCDF dimensions, we get back a pointer to an
// NcDim for each one.
NcDim* xDim = dataFile.add_dim("x", NX);
NcDim* yDim = dataFile.add_dim("y", NY);
// Define a netCDF variable. The type of the variable in this case
// is ncInt (32-bit integer).
NcVar *data = dataFile.add_var("data", ncInt, xDim, yDim);
// Write the pretend data to the file. Although netCDF supports
// reading and writing subsets of data, in this case we write all
// the data in one operation.
data->put(&dataOut[0][0], NX, NY);
// The file will be automatically close when the NcFile object goes
// out of scope. This frees up any internal netCDF resources
// associated with the file, and flushes any buffers.
cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
return 0;
}
</pre>
<div class="node">
<a name="simple_xy_rd.cpp"></a>
<a name="simple_005fxy_005frd_002ecpp"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fwr_002ecpp">simple_xy_wr.cpp</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy-in-C_002b_002b">simple_xy in C++</a>
</div>
<h5 class="subsubsection">2.1.4.2 simple_xy_rd.cpp</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is a very simple example which reads a 2D array of
sample data produced by simple_xy_wr.cpp.
This example is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: simple_xy_rd.cpp,v 1.13 2007/01/19 12:52:13 ed Exp $
*/
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
// We are reading 2D data, a 6 x 12 grid.
static const int NX = 6;
static const int NY = 12;
// Return this in event of a problem.
static const int NC_ERR = 2;
int main(void)
{
// This is the array we will read.
int dataIn[NX][NY];
// Open the file. The ReadOnly parameter tells netCDF we want
// read-only access to the file.
NcFile dataFile("simple_xy.nc", NcFile::ReadOnly);
// You should always check whether a netCDF file open or creation
// constructor succeeded.
if (!dataFile.is_valid())
{
cout << "Couldn't open file!\n";
return NC_ERR;
}
// For other method calls, the default behavior of the C++ API is
// to exit with a message if there is an error. If that behavior
// is OK, there is no need to check return values in simple cases
// like the following.
// Retrieve the variable named "data"
NcVar *data = dataFile.get_var("data");
// Read all the values from the "data" variable into memory.
data->get(&dataIn[0][0], NX, NY);
// Check the values.
for (int i = 0; i < NX; i++)
for (int j = 0; j < NY; j++)
if (dataIn[i][j] != i * NY + j)
return NC_ERR;
// The netCDF file is automatically closed by the NcFile destructor
cout << "*** SUCCESS reading example file simple_xy.nc!" << endl;
return 0;
}
</pre>
<div class="node">
<a name="sfc_pres_temp"></a>
<a name="sfc_005fpres_005ftemp"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D">pres_temp_4D</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy">simple_xy</a>,
Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
</div>
<h3 class="section">2.2 The sfc_pres_temp Example</h3>
<p>This example has been constructed for the meteorological mind.
<p>Suppose you have some data you want to write to a netCDF file. For
example, you have one time step of surface temperature and surface
pressure, on a 6 x 12 latitude longitude grid.
<p>To store this in netCDF, create a file, add two dimensions (latitude
and longitude) and two variables (pressure and temperature).
<p>In this example we add some netCDF attributes, as is typical in
scientific applications, to further describe the data. In this case we
add a units attribute to every netCDF variable.
<p>In this example we also add additional netCDF variables to describe
the coordinate system. These “coordinate variables” allow us to
specify the latitudes and longitudes that describe the data grid.
<p>The CDL version of the data file, generated by ncdump, is shown below.
<p>For more information on ncdump and ncgen see <a href="netcdf.html#NetCDF-Utilities">NetCDF Utilities</a>.
<pre class="example"> netcdf sfc_pres_temp {
dimensions:
latitude = 6 ;
longitude = 12 ;
variables:
float latitude(latitude) ;
latitude:units = "degrees_north" ;
float longitude(longitude) ;
longitude:units = "degrees_east" ;
float pressure(latitude, longitude) ;
pressure:units = "hPa" ;
float temperature(latitude, longitude) ;
temperature:units = "celsius" ;
data:
latitude = 25, 30, 35, 40, 45, 50 ;
longitude = -125, -120, -115, -110, -105, -100, -95, -90, -85, -80, -75, -70 ;
pressure =
900, 906, 912, 918, 924, 930, 936, 942, 948, 954, 960, 966,
901, 907, 913, 919, 925, 931, 937, 943, 949, 955, 961, 967,
902, 908, 914, 920, 926, 932, 938, 944, 950, 956, 962, 968,
903, 909, 915, 921, 927, 933, 939, 945, 951, 957, 963, 969,
904, 910, 916, 922, 928, 934, 940, 946, 952, 958, 964, 970,
905, 911, 917, 923, 929, 935, 941, 947, 953, 959, 965, 971 ;
temperature =
9, 10.5, 12, 13.5, 15, 16.5, 18, 19.5, 21, 22.5, 24, 25.5,
9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 21.25, 22.75, 24.25,
25.75,
9.5, 11, 12.5, 14, 15.5, 17, 18.5, 20, 21.5, 23, 24.5, 26,
9.75, 11.25, 12.75, 14.25, 15.75, 17.25, 18.75, 20.25, 21.75, 23.25, 24.75,
26.25,
10, 11.5, 13, 14.5, 16, 17.5, 19, 20.5, 22, 23.5, 25, 26.5,
10.25, 11.75, 13.25, 14.75, 16.25, 17.75, 19.25, 20.75, 22.25, 23.75,
25.25, 26.75 ;
}
</pre>
<ul class="menu">
<li><a accesskey="1" href="#sfc_005fpres_005ftemp-in-C">sfc_pres_temp in C</a>
<li><a accesskey="2" href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>
<li><a accesskey="3" href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>
<li><a accesskey="4" href="#sfc_005fpres_005ftemp-in-C_002b_002b">sfc_pres_temp in C++</a>
</ul>
<div class="node">
<a name="sfc_pres_temp-in-C"></a>
<a name="sfc_005fpres_005ftemp-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>
</div>
<h4 class="subsection">2.2.1 sfc_pres_temp_wr.c and sfc_pres_temp_rd.c</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/C.
<p>The example program sfc_pres_temp_wr.c creates the example data file
sfc_pres_temp.nc. The example program sfc_pres_temp_rd.c reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#sfc_005fpres_005ftemp_005fwr_002ec">sfc_pres_temp_wr.c</a>
<li><a accesskey="2" href="#sfc_005fpres_005ftemp_005frd_002ec">sfc_pres_temp_rd.c</a>
</ul>
<div class="node">
<a name="sfc_pres_temp_wr.c"></a>
<a name="sfc_005fpres_005ftemp_005fwr_002ec"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp_005frd_002ec">sfc_pres_temp_rd.c</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-C">sfc_pres_temp in C</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-C">sfc_pres_temp in C</a>
</div>
<h5 class="subsubsection">2.2.1.1 sfc_pres_temp_wr.c</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This example writes some surface pressure and temperatures. It is
intended to illustrate the use of the netCDF C API. The companion
program sfc_pres_temp_rd.c shows how to read the netCDF data file
created by this program.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$Id: sfc_pres_temp_wr.c,v 1.3 2006/06/13 20:46:16 ed Exp $
*/
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "sfc_pres_temp.nc"
/* We are writing 2D data, a 6 x 12 lat-lon grid. We will need two
* netCDF dimensions. */
#define NDIMS 2
#define NLAT 6
#define NLON 12
#define LAT_NAME "latitude"
#define LON_NAME "longitude"
/* Names of things. */
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
#define UNITS "units"
#define DEGREES_EAST "degrees_east"
#define DEGREES_NORTH "degrees_north"
/* These are used to construct some example data. */
#define SAMPLE_PRESSURE 900
#define SAMPLE_TEMP 9.0
#define START_LAT 25.0
#define START_LON -125.0
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
int
main()
{
int ncid, lon_dimid, lat_dimid, pres_varid, temp_varid;
/* In addition to the latitude and longitude dimensions, we will also
create latitude and longitude netCDF variables which will hold the
actual latitudes and longitudes. Since they hold data about the
coordinate system, the netCDF term for these is: "coordinate
variables." */
int lat_varid, lon_varid;
int dimids[NDIMS];
/* We will write surface temperature and pressure fields. */
float pres_out[NLAT][NLON];
float temp_out[NLAT][NLON];
float lats[NLAT], lons[NLON];
/* It's good practice for each netCDF variable to carry a "units"
* attribute. */
char pres_units[] = "hPa";
char temp_units[] = "celsius";
/* Loop indexes. */
int lat, lon;
/* Error handling. */
int retval;
/* Create some pretend data. If this wasn't an example program, we
* would have some real data to write, for example, model
* output. */
for (lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5.*lat;
for (lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5.*lon;
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
{
pres_out[lat][lon] = SAMPLE_PRESSURE + (lon * NLAT + lat);
temp_out[lat][lon] = SAMPLE_TEMP + .25 * (lon * NLAT + lat);
}
/* Create the file. */
if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid)))
ERR(retval);
/* Define the dimensions. */
if ((retval = nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)))
ERR(retval);
/* Define coordinate netCDF variables. They will hold the
coordinate information, that is, the latitudes and longitudes. A
varid is returned for each.*/
if ((retval = nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, &lat_dimid,
&lat_varid)))
ERR(retval);
if ((retval = nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, &lon_dimid,
&lon_varid)))
ERR(retval);
/* Define units attributes for coordinate vars. This attaches a
text attribute to each of the coordinate variables, containing
the units. Note that we are not writing a trailing NULL, just
"units", because the reading program may be fortran which does
not use null-terminated strings. In general it is up to the
reading C program to ensure that it puts null-terminators on
strings where necessary.*/
if ((retval = nc_put_att_text(ncid, lat_varid, UNITS,
strlen(DEGREES_NORTH), DEGREES_NORTH)))
ERR(retval);
if ((retval = nc_put_att_text(ncid, lon_varid, UNITS,
strlen(DEGREES_EAST), DEGREES_EAST)))
ERR(retval);
/* Define the netCDF variables. The dimids array is used to pass
the dimids of the dimensions of the variables.*/
dimids[0] = lat_dimid;
dimids[1] = lon_dimid;
if ((retval = nc_def_var(ncid, PRES_NAME, NC_FLOAT, NDIMS,
dimids, &pres_varid)))
ERR(retval);
if ((retval = nc_def_var(ncid, TEMP_NAME, NC_FLOAT, NDIMS,
dimids, &temp_varid)))
ERR(retval);
/* Define units attributes for vars. */
if ((retval = nc_put_att_text(ncid, pres_varid, UNITS,
strlen(pres_units), pres_units)))
ERR(retval);
if ((retval = nc_put_att_text(ncid, temp_varid, UNITS,
strlen(temp_units), temp_units)))
ERR(retval);
/* End define mode. */
if ((retval = nc_enddef(ncid)))
ERR(retval);
/* Write the coordinate variable data. This will put the latitudes
and longitudes of our data grid into the netCDF file. */
if ((retval = nc_put_var_float(ncid, lat_varid, &lats[0])))
ERR(retval);
if ((retval = nc_put_var_float(ncid, lon_varid, &lons[0])))
ERR(retval);
/* Write the pretend data. This will write our surface pressure and
surface temperature data. The arrays of data are the same size
as the netCDF variables we have defined. */
if ((retval = nc_put_var_float(ncid, pres_varid, &pres_out[0][0])))
ERR(retval);
if ((retval = nc_put_var_float(ncid, temp_varid, &temp_out[0][0])))
ERR(retval);
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file sfc_pres_temp.nc!\n");
return 0;
}
</pre>
<div class="node">
<a name="sfc_pres_temp_rd.c"></a>
<a name="sfc_005fpres_005ftemp_005frd_002ec"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp_005fwr_002ec">sfc_pres_temp_wr.c</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-C">sfc_pres_temp in C</a>
</div>
<h5 class="subsubsection">2.2.1.2 sfc_pres_temp_rd.c</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is an example which reads some surface pressure and
temperatures. The data file read by this program is produced by the
companion program sfc_pres_temp_wr.c. It is intended to illustrate
the use of the netCDF C API.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$Id: sfc_pres_temp_rd.c,v 1.5 2007/02/14 20:59:21 ed Exp $
*/
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
/* This is the name of the data file we will read. */
#define FILE_NAME "sfc_pres_temp.nc"
/* We are reading 2D data, a 6 x 12 lat-lon grid. */
#define NDIMS 2
#define NLAT 6
#define NLON 12
#define LAT_NAME "latitude"
#define LON_NAME "longitude"
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
/* These are used to calculate the values we expect to find. */
#define SAMPLE_PRESSURE 900
#define SAMPLE_TEMP 9.0
#define START_LAT 25.0
#define START_LON -125.0
/* For the units attributes. */
#define UNITS "units"
#define PRES_UNITS "hPa"
#define TEMP_UNITS "celsius"
#define LAT_UNITS "degrees_north"
#define LON_UNITS "degrees_east"
#define MAX_ATT_LEN 80
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
int
main()
{
int ncid, pres_varid, temp_varid;
int lat_varid, lon_varid;
/* We will read surface temperature and pressure fields. */
float pres_in[NLAT][NLON];
float temp_in[NLAT][NLON];
/* For the lat lon coordinate variables. */
float lats_in[NLAT], lons_in[NLON];
/* To check the units attributes. */
char pres_units_in[MAX_ATT_LEN], temp_units_in[MAX_ATT_LEN];
char lat_units_in[MAX_ATT_LEN], lon_units_in[MAX_ATT_LEN];
/* We will learn about the data file and store results in these
program variables. */
int ndims_in, nvars_in, ngatts_in, unlimdimid_in;
/* Loop indexes. */
int lat, lon;
/* Error handling. */
int retval;
/* Open the file. */
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);
/* There are a number of inquiry functions in netCDF which can be
used to learn about an unknown netCDF file. NC_INQ tells how
many netCDF variables, dimensions, and global attributes are in
the file; also the dimension id of the unlimited dimension, if
there is one. */
if ((retval = nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in,
&unlimdimid_in)))
ERR(retval);
/* In this case we know that there are 2 netCDF dimensions, 4
netCDF variables, no global attributes, and no unlimited
dimension. */
if (ndims_in != 2 || nvars_in != 4 || ngatts_in != 0 ||
unlimdimid_in != -1) return 2;
/* Get the varids of the latitude and longitude coordinate
* variables. */
if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
ERR(retval);
if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
ERR(retval);
/* Read the coordinate variable data. */
if ((retval = nc_get_var_float(ncid, lat_varid, &lats_in[0])))
ERR(retval);
if ((retval = nc_get_var_float(ncid, lon_varid, &lons_in[0])))
ERR(retval);
/* Check the coordinate variable data. */
for (lat = 0; lat < NLAT; lat++)
if (lats_in[lat] != START_LAT + 5.*lat)
return 2;
for (lon = 0; lon < NLON; lon++)
if (lons_in[lon] != START_LON + 5.*lon)
return 2;
/* Get the varids of the pressure and temperature netCDF
* variables. */
if ((retval = nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
ERR(retval);
if ((retval = nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
ERR(retval);
/* Read the data. Since we know the contents of the file we know
* that the data arrays in this program are the correct size to
* hold all the data. */
if ((retval = nc_get_var_float(ncid, pres_varid, &pres_in[0][0])))
ERR(retval);
if ((retval = nc_get_var_float(ncid, temp_varid, &temp_in[0][0])))
ERR(retval);
/* Check the data. */
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
if (pres_in[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat) ||
temp_in[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
return 2;
/* Each of the netCDF variables has a "units" attribute. Let's read
them and check them. */
if ((retval = nc_get_att_text(ncid, lat_varid, UNITS, lat_units_in)))
ERR(retval);
if (strncmp(lat_units_in, LAT_UNITS, strlen(LAT_UNITS)))
return 2;
if ((retval = nc_get_att_text(ncid, lon_varid, UNITS, lon_units_in)))
ERR(retval);
if (strncmp(lon_units_in, LON_UNITS, strlen(LON_UNITS)))
return 2;
if ((retval = nc_get_att_text(ncid, pres_varid, UNITS, pres_units_in)))
ERR(retval);
if (strncmp(pres_units_in, PRES_UNITS, strlen(PRES_UNITS)))
return 2;
if ((retval = nc_get_att_text(ncid, temp_varid, UNITS, temp_units_in)))
ERR(retval);
if (strncmp(temp_units_in, TEMP_UNITS, strlen(TEMP_UNITS))) return 2;
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS reading example file sfc_pres_temp.nc!\n");
return 0;
}
</pre>
<div class="node">
<a name="sfc_pres_temp-in-F77"></a>
<a name="sfc_005fpres_005ftemp-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-C">sfc_pres_temp in C</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>
</div>
<h4 class="subsection">2.2.2 sfc_pres_temp_wr.f and sfc_pres_temp_rd.f</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/F77.
<p>The example program sfc_pres_temp_wr.f creates the example data file
sfc_pres_temp.nc. The example program sfc_pres_temp_rd.f reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#sfc_005fpres_005ftemp_005fwr_002ef">sfc_pres_temp_wr.f</a>
<li><a accesskey="2" href="#sfc_005fpres_005ftemp_005frd_002ef">sfc_pres_temp_rd.f</a>
</ul>
<div class="node">
<a name="sfc_pres_temp_wr.f"></a>
<a name="sfc_005fpres_005ftemp_005fwr_002ef"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp_005frd_002ef">sfc_pres_temp_rd.f</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>
</div>
<h5 class="subsubsection">2.2.2.1 sfc_pres_temp_wr.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This example writes some surface pressure and temperatures. It is
C intended to illustrate the use of the netCDF fortran 77 API. The
C companion program sfc_pres_temp_rd.f shows how to read the netCDF
C data file created by this program.
C This program is part of the netCDF tutorial:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: sfc_pres_temp_wr.f,v 1.10 2007/02/14 20:59:20 ed Exp $
program sfc_pres_temp_wr
implicit none
include 'netcdf.inc'
C This is the name of the data file we will create.
character*(*) FILE_NAME
parameter (FILE_NAME='sfc_pres_temp.nc')
integer ncid
C We are writing 2D data, a 12 x 6 lon-lat grid. We will need two
C netCDF dimensions.
integer NDIMS
parameter (NDIMS=2)
integer NLATS, NLONS
parameter (NLATS = 6, NLONS = 12)
character*(*) LAT_NAME, LON_NAME
parameter (LAT_NAME='latitude', LON_NAME='longitude')
integer lon_dimid, lat_dimid
C In addition to the latitude and longitude dimensions, we will also
C create latitude and longitude netCDF variables which will hold the
C actual latitudes and longitudes. Since they hold data about the
C coordinate system, the netCDF term for these is: "coordinate
C variables."
real lats(NLATS), lons(NLONS)
integer lat_varid, lon_varid
real START_LAT, START_LON
parameter (START_LAT = 25.0, START_LON = -125.0)
C We will write surface temperature and pressure fields.
character*(*) PRES_NAME, TEMP_NAME
parameter (PRES_NAME='pressure')
parameter (TEMP_NAME='temperature')
integer pres_varid, temp_varid
integer dimids(NDIMS)
C It's good practice for each variable to carry a "units" attribute.
character*(*) UNITS
parameter (UNITS = 'units')
character*(*) PRES_UNITS, TEMP_UNITS, LAT_UNITS, LON_UNITS
parameter (PRES_UNITS = 'hPa', TEMP_UNITS = 'celsius')
parameter (LAT_UNITS = 'degrees_north')
parameter (LON_UNITS = 'degrees_east')
C We will create some pressure and temperature data to write out.
real pres_out(NLONS, NLATS), temp_out(NLONS, NLATS)
real SAMPLE_PRESSURE
parameter (SAMPLE_PRESSURE = 900.0)
real SAMPLE_TEMP
parameter (SAMPLE_TEMP = 9.0)
C Loop indices.
integer lat, lon
C Error handling.
integer retval
C Create pretend data. If this were not an example program, we would
C have some real data to write, for example, model output.
do lat = 1, NLATS
lats(lat) = START_LAT + (lat - 1) * 5.0
end do
do lon = 1, NLONS
lons(lon) = START_LON + (lon - 1) * 5.0
end do
do lon = 1, NLONS
do lat = 1, NLATS
pres_out(lon, lat) = SAMPLE_PRESSURE +
+ (lon - 1) * NLATS + (lat - 1)
temp_out(lon, lat) = SAMPLE_TEMP +
+ .25 * ((lon - 1) * NLATS + (lat - 1))
end do
end do
C Create the file.
retval = nf_create(FILE_NAME, nf_clobber, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the dimensions.
retval = nf_def_dim(ncid, LAT_NAME, NLATS, lat_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, LON_NAME, NLONS, lon_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the coordinate variables. They will hold the coordinate
C information, that is, the latitudes and longitudes. A varid is
C returned for each.
retval = nf_def_var(ncid, LAT_NAME, NF_REAL, 1, lat_dimid,
+ lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, LON_NAME, NF_REAL, 1, lon_dimid,
+ lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Assign units attributes to coordinate var data. This attaches a
C text attribute to each of the coordinate variables, containing the
C units.
retval = nf_put_att_text(ncid, lat_varid, UNITS, len(LAT_UNITS),
+ LAT_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, lon_varid, UNITS, len(LON_UNITS),
+ LON_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the netCDF variables. The dimids array is used to pass the
C dimids of the dimensions of the netCDF variables.
dimids(1) = lon_dimid
dimids(2) = lat_dimid
retval = nf_def_var(ncid, PRES_NAME, NF_REAL, NDIMS, dimids,
+ pres_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, TEMP_NAME, NF_REAL, NDIMS, dimids,
+ temp_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Assign units attributes to the pressure and temperature netCDF
C variables.
retval = nf_put_att_text(ncid, pres_varid, UNITS, len(PRES_UNITS),
+ PRES_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, temp_varid, UNITS, len(TEMP_UNITS),
+ TEMP_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
C End define mode.
retval = nf_enddef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Write the coordinate variable data. This will put the latitudes
C and longitudes of our data grid into the netCDF file.
retval = nf_put_var_real(ncid, lat_varid, lats)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_var_real(ncid, lon_varid, lons)
if (retval .ne. nf_noerr) call handle_err(retval)
C Write the pretend data. This will write our surface pressure and
C surface temperature data. The arrays of data are the same size as
C the netCDF variables we have defined.
retval = nf_put_var_real(ncid, pres_varid, pres_out)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_var_real(ncid, temp_varid, temp_out)
if (retval .ne. nf_noerr) call handle_err(retval)
C Close the file.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C If we got this far, everything worked as expected. Yipee!
print *,'*** SUCCESS writing example file sfc_pres_temp.nc!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="sfc_pres_temp_rd.f"></a>
<a name="sfc_005fpres_005ftemp_005frd_002ef"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp_005fwr_002ef">sfc_pres_temp_wr.f</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>
</div>
<h5 class="subsubsection">2.2.2.2 sfc_pres_temp_rd.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is an example which reads some surface pressure and
C temperatures. The data file read by this program is produced
C comapnion program sfc_pres_temp_wr.f. It is intended to illustrate
C the use of the netCDF fortran 77 API.
C This program is part of the netCDF tutorial:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: sfc_pres_temp_rd.f,v 1.9 2007/02/14 20:59:20 ed Exp $
program sfc_pres_temp_rd
implicit none
include 'netcdf.inc'
C This is the name of the data file we will read.
character*(*) FILE_NAME
parameter (FILE_NAME='sfc_pres_temp.nc')
integer ncid
C We are reading 2D data, a 12 x 6 lon-lat grid.
integer NDIMS
parameter (NDIMS=2)
integer NLATS, NLONS
parameter (NLATS = 6, NLONS = 12)
character*(*) LAT_NAME, LON_NAME
parameter (LAT_NAME='latitude', LON_NAME='longitude')
integer lat_dimid, lon_dimid
C For the lat lon coordinate netCDF variables.
real lats(NLATS), lons(NLONS)
integer lat_varid, lon_varid
C We will read surface temperature and pressure fields.
character*(*) PRES_NAME, TEMP_NAME
parameter (PRES_NAME='pressure')
parameter (TEMP_NAME='temperature')
integer pres_varid, temp_varid
integer dimids(NDIMS)
C To check the units attributes.
character*(*) UNITS
parameter (UNITS = 'units')
character*(*) PRES_UNITS, TEMP_UNITS, LAT_UNITS, LON_UNITS
parameter (PRES_UNITS = 'hPa', TEMP_UNITS = 'celsius')
parameter (LAT_UNITS = 'degrees_north')
parameter (LON_UNITS = 'degrees_east')
integer MAX_ATT_LEN
parameter (MAX_ATT_LEN = 80)
character*(MAX_ATT_LEN) pres_units_in, temp_units_in
character*(MAX_ATT_LEN) lat_units_in, lon_units_in
integer att_len
C Read the data into these arrays.
real pres_in(NLONS, NLATS), temp_in(NLONS, NLATS)
C These are used to calculate the values we expect to find.
real START_LAT, START_LON
parameter (START_LAT = 25.0, START_LON = -125.0)
real SAMPLE_PRESSURE
parameter (SAMPLE_PRESSURE = 900.0)
real SAMPLE_TEMP
parameter (SAMPLE_TEMP = 9.0)
C We will learn about the data file and store results in these
C program variables.
integer ndims_in, nvars_in, ngatts_in, unlimdimid_in
C Loop indices
integer lat, lon
C Error handling
integer retval
C Open the file.
retval = nf_open(FILE_NAME, nf_nowrite, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C There are a number of inquiry functions in netCDF which can be
C used to learn about an unknown netCDF file. NF_INQ tells how many
C netCDF variables, dimensions, and global attributes are in the
C file; also the dimension id of the unlimited dimension, if there
C is one.
retval = nf_inq(ncid, ndims_in, nvars_in, ngatts_in,
+ unlimdimid_in)
if (retval .ne. nf_noerr) call handle_err(retval)
C In this case we know that there are 2 netCDF dimensions, 4 netCDF
C variables, no global attributes, and no unlimited dimension.
if (ndims_in .ne. 2 .or. nvars_in .ne. 4 .or. ngatts_in .ne. 0
+ .or. unlimdimid_in .ne. -1) stop 2
C Get the varids of the latitude and longitude coordinate variables.
retval = nf_inq_varid(ncid, LAT_NAME, lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_varid(ncid, LON_NAME, lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Read the latitude and longitude data.
retval = nf_get_var_real(ncid, lat_varid, lats)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_get_var_real(ncid, lon_varid, lons)
if (retval .ne. nf_noerr) call handle_err(retval)
C Check to make sure we got what we expected.
do lat = 1, NLATS
if (lats(lat) .ne. START_LAT + (lat - 1) * 5.0) stop 2
end do
do lon = 1, NLONS
if (lons(lon) .ne. START_LON + (lon - 1) * 5.0) stop 2
end do
C Get the varids of the pressure and temperature netCDF variables.
retval = nf_inq_varid(ncid, PRES_NAME, pres_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_varid(ncid, TEMP_NAME, temp_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Read the surface pressure and temperature data from the file.
C Since we know the contents of the file we know that the data
C arrays in this program are the correct size to hold all the data.
retval = nf_get_var_real(ncid, pres_varid, pres_in)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_get_var_real(ncid, temp_varid, temp_in)
if (retval .ne. nf_noerr) call handle_err(retval)
C Check the data. It should be the same as the data we wrote.
do lon = 1, NLONS
do lat = 1, NLATS
if (pres_in(lon, lat) .ne. SAMPLE_PRESSURE +
+ (lon - 1) * NLATS + (lat - 1)) stop 2
if (temp_in(lon, lat) .ne. SAMPLE_TEMP +
+ .25 * ((lon - 1) * NLATS + (lat - 1))) stop 2
end do
end do
C Each of the netCDF variables has a "units" attribute. Let's read
C them and check them.
retval = nf_get_att_text(ncid, lat_varid, UNITS, lat_units_in)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_attlen(ncid, lat_varid, UNITS, att_len)
if (retval .ne. nf_noerr) call handle_err(retval)
if (lat_units_in(1:att_len) .ne. LAT_UNITS) stop 2
retval = nf_get_att_text(ncid, lon_varid, UNITS, lon_units_in)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_attlen(ncid, lon_varid, UNITS, att_len)
if (retval .ne. nf_noerr) call handle_err(retval)
if (lon_units_in(1:att_len) .ne. LON_UNITS) stop 2
retval = nf_get_att_text(ncid, pres_varid, UNITS, pres_units_in)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_attlen(ncid, pres_varid, UNITS, att_len)
if (retval .ne. nf_noerr) call handle_err(retval)
if (pres_units_in(1:att_len) .ne. PRES_UNITS) stop 2
retval = nf_get_att_text(ncid, temp_varid, UNITS, temp_units_in)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_attlen(ncid, temp_varid, UNITS, att_len)
if (retval .ne. nf_noerr) call handle_err(retval)
if (temp_units_in(1:att_len) .ne. TEMP_UNITS) stop 2
C Close the file. This frees up any internal netCDF resources
C associated with the file.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C If we got this far, everything worked as expected. Yipee!
print *,'*** SUCCESS reading example file sfc_pres_temp.nc!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="sfc_pres_temp-in-F90"></a>
<a name="sfc_005fpres_005ftemp-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp-in-C_002b_002b">sfc_pres_temp in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-F77">sfc_pres_temp in F77</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>
</div>
<h4 class="subsection">2.2.3 sfc_pres_temp_wr.f90 and sfc_pres_temp_rd.f90</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/F90.
<p>The example program sfc_pres_temp_wr.f90 creates the example data file
sfc_pres_temp.nc. The example program sfc_pres_temp_rd.f90 reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#sfc_005fpres_005ftemp_005fwr_002ef90">sfc_pres_temp_wr.f90</a>
<li><a accesskey="2" href="#sfc_005fpres_005ftemp_005frd_002ef90">sfc_pres_temp_rd.f90</a>
</ul>
<div class="node">
<a name="sfc_pres_temp_wr.f90"></a>
<a name="sfc_005fpres_005ftemp_005fwr_002ef90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp_005frd_002ef90">sfc_pres_temp_rd.f90</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>
</div>
<h5 class="subsubsection">2.2.3.1 sfc_pres_temp_wr.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This example writes some surface pressure and temperatures. It is
! intended to illustrate the use of the netCDF fortran 90 API. The
! companion program sfc_pres_temp_rd.f90 shows how to read the netCDF
! data file created by this program.
! This program is part of the netCDF tutorial:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: sfc_pres_temp_wr.f90,v 1.12 2010/04/06 19:32:09 ed Exp $
program sfc_pres_temp_wr
use netcdf
implicit none
! This is the name of the data file we will create.
character (len = *), parameter :: FILE_NAME = "sfc_pres_temp.nc"
integer :: ncid
! We are writing 2D data, a 12 x 6 lon-lat grid. We will need two
! netCDF dimensions.
integer, parameter :: NDIMS = 2
integer, parameter :: NLATS = 6, NLONS = 12
character (len = *), parameter :: LAT_NAME = "latitude"
character (len = *), parameter :: LON_NAME = "longitude"
integer :: lat_dimid, lon_dimid
! In addition to the latitude and longitude dimensions, we will also
! create latitude and longitude netCDF variables which will hold the
! actual latitudes and longitudes. Since they hold data about the
! coordinate system, the netCDF term for these is: "coordinate
! variables."
real :: lats(NLATS), lons(NLONS)
integer :: lat_varid, lon_varid
real, parameter :: START_LAT = 25.0, START_LON = -125.0
! We will write surface temperature and pressure fields.
character (len = *), parameter :: PRES_NAME="pressure"
character (len = *), parameter :: TEMP_NAME="temperature"
integer :: pres_varid, temp_varid
integer :: dimids(NDIMS)
! It's good practice for each variable to carry a "units" attribute.
character (len = *), parameter :: UNITS = "units"
character (len = *), parameter :: PRES_UNITS = "hPa"
character (len = *), parameter :: TEMP_UNITS = "celsius"
character (len = *), parameter :: LAT_UNITS = "degrees_north"
character (len = *), parameter :: LON_UNITS = "degrees_east"
! We will create some pressure and temperature data to write out.
real, dimension(:,:), allocatable :: temp_out
real, dimension(:,:), allocatable :: pres_out
real, parameter :: SAMPLE_PRESSURE = 900.0
real, parameter :: SAMPLE_TEMP = 9.0
! Loop indices
integer :: lat, lon
! Allocate memory.
allocate(pres_out(NLONS, NLATS))
allocate(temp_out(NLONS, NLATS))
! Create pretend data. If this were not an example program, we would
! have some real data to write, for example, model output.
do lat = 1, NLATS
lats(lat) = START_LAT + (lat - 1) * 5.0
end do
do lon = 1, NLONS
lons(lon) = START_LON + (lon - 1) * 5.0
end do
do lon = 1, NLONS
do lat = 1, NLATS
pres_out(lon, lat) = SAMPLE_PRESSURE + (lon - 1) * NLATS + (lat - 1)
temp_out(lon, lat) = SAMPLE_TEMP + .25 * ((lon - 1) * NLATS + (lat - 1))
end do
end do
! Create the file.
call check( nf90_create(FILE_NAME, nf90_clobber, ncid) )
! Define the dimensions.
call check( nf90_def_dim(ncid, LAT_NAME, NLATS, lat_dimid) )
call check( nf90_def_dim(ncid, LON_NAME, NLONS, lon_dimid) )
! Define the coordinate variables. They will hold the coordinate
! information, that is, the latitudes and longitudes. A varid is
! returned for each.
call check( nf90_def_var(ncid, LAT_NAME, NF90_REAL, lat_dimid, lat_varid) )
call check( nf90_def_var(ncid, LON_NAME, NF90_REAL, lon_dimid, lon_varid) )
! Assign units attributes to coordinate var data. This attaches a
! text attribute to each of the coordinate variables, containing the
! units.
call check( nf90_put_att(ncid, lat_varid, UNITS, LAT_UNITS) )
call check( nf90_put_att(ncid, lon_varid, UNITS, LON_UNITS) )
! Define the netCDF variables. The dimids array is used to pass the
! dimids of the dimensions of the netCDF variables.
dimids = (/ lon_dimid, lat_dimid /)
call check( nf90_def_var(ncid, PRES_NAME, NF90_REAL, dimids, pres_varid) )
call check( nf90_def_var(ncid, TEMP_NAME, NF90_REAL, dimids, temp_varid) )
! Assign units attributes to the pressure and temperature netCDF
! variables.
call check( nf90_put_att(ncid, pres_varid, UNITS, PRES_UNITS) )
call check( nf90_put_att(ncid, temp_varid, UNITS, TEMP_UNITS) )
! End define mode.
call check( nf90_enddef(ncid) )
! Write the coordinate variable data. This will put the latitudes
! and longitudes of our data grid into the netCDF file.
call check( nf90_put_var(ncid, lat_varid, lats) )
call check( nf90_put_var(ncid, lon_varid, lons) )
! Write the pretend data. This will write our surface pressure and
! surface temperature data. The arrays of data are the same size as
! the netCDF variables we have defined.
call check( nf90_put_var(ncid, pres_varid, pres_out) )
call check( nf90_put_var(ncid, temp_varid, temp_out) )
! Close the file.
call check( nf90_close(ncid) )
! If we got this far, everything worked as expected. Yipee!
print *,"*** SUCCESS writing example file sfc_pres_temp.nc!"
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program sfc_pres_temp_wr
</pre>
<div class="node">
<a name="sfc_pres_temp_rd.f90"></a>
<a name="sfc_005fpres_005ftemp_005frd_002ef90"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp_005fwr_002ef90">sfc_pres_temp_wr.f90</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>
</div>
<h5 class="subsubsection">2.2.3.2 sfc_pres_temp_rd.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is an example which reads some surface pressure and
! temperatures. The data file read by this program is produced
! comapnion program sfc_pres_temp_wr.f90. It is intended to illustrate
! the use of the netCDF fortran 90 API.
! This program is part of the netCDF tutorial:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: sfc_pres_temp_rd.f90,v 1.10 2010/04/06 19:32:09 ed Exp $
program sfc_pres_temp_rd
use netcdf
implicit none
! This is the name of the data file we will read.
character (len = *), parameter :: FILE_NAME = "sfc_pres_temp.nc"
integer :: ncid
! We are reading 2D data, a 12 x 6 lon-lat grid.
integer, parameter :: NDIMS = 2
integer, parameter :: NLATS = 6, NLONS = 12
character (len = *), parameter :: LAT_NAME = "latitude"
character (len = *), parameter :: LON_NAME = "longitude"
! For the lat lon coordinate netCDF variables.
real :: lats(NLATS), lons(NLONS)
integer :: lat_varid, lon_varid
! We will read surface temperature and pressure fields.
character (len = *), parameter :: PRES_NAME = "pressure"
character (len = *), parameter :: TEMP_NAME = "temperature"
integer :: pres_varid, temp_varid
! To check the units attributes.
character (len = *), parameter :: UNITS = "units"
character (len = *), parameter :: PRES_UNITS = "hPa"
character (len = *), parameter :: TEMP_UNITS = "celsius"
character (len = *), parameter :: LAT_UNITS = "degrees_north"
character (len = *), parameter :: LON_UNITS = "degrees_east"
integer, parameter :: MAX_ATT_LEN = 80
integer :: att_len
character*(MAX_ATT_LEN) :: pres_units_in, temp_units_in
character*(MAX_ATT_LEN) :: lat_units_in, lon_units_in
! Read the data into these arrays.
real, dimension(:,:), allocatable :: pres_in
real, dimension(:,:), allocatable :: temp_in
! These are used to calculate the values we expect to find.
real, parameter :: START_LAT = 25.0, START_LON = -125.0
real, parameter :: SAMPLE_PRESSURE = 900.0
real, parameter :: SAMPLE_TEMP = 9.0
! We will learn about the data file and store results in these
! program variables.
integer :: ndims_in, nvars_in, ngatts_in, unlimdimid_in
! Loop indices
integer :: lat, lon
! Allocate memory.
allocate(pres_in(NLONS, NLATS))
allocate(temp_in(NLONS, NLATS))
! Open the file.
call check( nf90_open(FILE_NAME, nf90_nowrite, ncid) )
! There are a number of inquiry functions in netCDF which can be
! used to learn about an unknown netCDF file. NF90_INQ tells how many
! netCDF variables, dimensions, and global attributes are in the
! file; also the dimension id of the unlimited dimension, if there
! is one.
call check( nf90_inquire(ncid, ndims_in, nvars_in, ngatts_in, unlimdimid_in) )
! In this case we know that there are 2 netCDF dimensions, 4 netCDF
! variables, no global attributes, and no unlimited dimension.
if (ndims_in /= 2 .or. nvars_in /= 4 .or. ngatts_in /= 0 &
.or. unlimdimid_in /= -1) stop 2
! Get the varids of the latitude and longitude coordinate variables.
call check( nf90_inq_varid(ncid, LAT_NAME, lat_varid) )
call check( nf90_inq_varid(ncid, LON_NAME, lon_varid) )
! Read the latitude and longitude data.
call check( nf90_get_var(ncid, lat_varid, lats) )
call check( nf90_get_var(ncid, lon_varid, lons) )
! Check to make sure we got what we expected.
do lat = 1, NLATS
if (lats(lat) /= START_LAT + (lat - 1) * 5.0) stop 2
end do
do lon = 1, NLONS
if (lons(lon) /= START_LON + (lon - 1) * 5.0) stop 2
end do
! Get the varids of the pressure and temperature netCDF variables.
call check( nf90_inq_varid(ncid, PRES_NAME, pres_varid) )
call check( nf90_inq_varid(ncid, TEMP_NAME, temp_varid) )
! Read the surface pressure and temperature data from the file.
! Since we know the contents of the file we know that the data
! arrays in this program are the correct size to hold all the data.
call check( nf90_get_var(ncid, pres_varid, pres_in) )
call check( nf90_get_var(ncid, temp_varid, temp_in) )
! Check the data. It should be the same as the data we wrote.
do lon = 1, NLONS
do lat = 1, NLATS
if (pres_in(lon, lat) /= SAMPLE_PRESSURE + &
(lon - 1) * NLATS + (lat - 1)) stop 2
if (temp_in(lon, lat) /= SAMPLE_TEMP + &
.25 * ((lon - 1) * NLATS + (lat - 1))) stop 2
end do
end do
! Each of the netCDF variables has a "units" attribute. Let's read
! them and check them.
call check( nf90_get_att(ncid, lat_varid, UNITS, lat_units_in) )
call check( nf90_inquire_attribute(ncid, lat_varid, UNITS, len = att_len) )
if (lat_units_in(1:att_len) /= LAT_UNITS) stop 2
call check( nf90_get_att(ncid, lon_varid, UNITS, lon_units_in) )
call check( nf90_inquire_attribute(ncid, lon_varid, UNITS, len = att_len) )
if (lon_units_in(1:att_len) /= LON_UNITS) stop 2
call check( nf90_get_att(ncid, pres_varid, UNITS, pres_units_in) )
call check( nf90_inquire_attribute(ncid, pres_varid, UNITS, len = att_len) )
if (pres_units_in(1:att_len) /= PRES_UNITS) stop 2
call check( nf90_get_att(ncid, temp_varid, UNITS, temp_units_in) )
call check( nf90_inquire_attribute(ncid, temp_varid, UNITS, len = att_len) )
if (temp_units_in(1:att_len) /= TEMP_UNITS) stop 2
! Close the file. This frees up any internal netCDF resources
! associated with the file.
call check( nf90_close(ncid) )
! If we got this far, everything worked as expected. Yipee!
print *,"*** SUCCESS reading example file sfc_pres_temp.nc!"
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program sfc_pres_temp_rd
</pre>
<div class="node">
<a name="sfc_pres_temp-in-C++"></a>
<a name="sfc_005fpres_005ftemp-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-F90">sfc_pres_temp in F90</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>
</div>
<h4 class="subsection">2.2.4 sfc_pres_temp_wr.cpp and sfc_pres_temp_rd.cpp</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/CXX.
<p>The example program sfc_pres_temp_wr.cpp creates the example data file
sfc_pres_temp.nc. The example program sfc_pres_temp_rd.cpp reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#sfc_005fpres_005ftemp_005fwr_002ecpp">sfc_pres_temp_wr.cpp</a>
<li><a accesskey="2" href="#sfc_005fpres_005ftemp_005frd_002ecpp">sfc_pres_temp_rd.cpp</a>
</ul>
<div class="node">
<a name="sfc_pres_temp_wr.cpp"></a>
<a name="sfc_005fpres_005ftemp_005fwr_002ecpp"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#sfc_005fpres_005ftemp_005frd_002ecpp">sfc_pres_temp_rd.cpp</a>,
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp-in-C_002b_002b">sfc_pres_temp in C++</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-C_002b_002b">sfc_pres_temp in C++</a>
</div>
<h5 class="subsubsection">2.2.4.1 sfc_pres_temp_wr.cpp</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This example writes some surface pressure and temperatures. It is
intended to illustrate the use of the netCDF C++ API. The companion
program sfc_pres_temp_rd.cpp shows how to read the netCDF data file
created by this program.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: sfc_pres_temp_wr.cpp,v 1.12 2007/01/19 12:52:13 ed Exp $
*/
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
// We are writing 2D data, a 6 x 12 lat-lon grid. We will need two
// netCDF dimensions.
static const int NLAT = 6;
static const int NLON = 12;
// These are used to construct some example data.
static const float SAMPLE_PRESSURE = 900;
static const float SAMPLE_TEMP = 9.0;
static const float START_LAT = 25.0;
static const float START_LON = -125.0;
// Return this to OS if there is a failure.
static const int NC_ERR = 2;
int main(void)
{
// These will hold our pressure and temperature data.
float presOut[NLAT][NLON];
float tempOut[NLAT][NLON];
// These will hold our latitudes and longitudes.
float lats[NLAT];
float lons[NLON];
// Create some pretend data. If this wasn't an example program, we
// would have some real data to write, for example, model
// output.
for(int lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5. * lat;
for(int lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5. * lon;
for (int lat = 0; lat < NLAT; lat++)
for(int lon = 0; lon < NLON; lon++)
{
presOut[lat][lon] = SAMPLE_PRESSURE + (lon * NLAT + lat);
tempOut[lat][lon] = SAMPLE_TEMP + .25 * (lon * NLAT + lat);
}
// Change the error behavior of the netCDF C++ API by creating an
// NcError object. Until it is destroyed, this NcError object will
// ensure that the netCDF C++ API silently returns error codes
// on any failure, and leaves any other error handling to the
// calling program. In the case of this example, we just exit with
// an NC_ERR error code.
NcError err(NcError::silent_nonfatal);
// Create the file. The Replace parameter tells netCDF to overwrite
// this file, if it already exists.
NcFile dataFile("sfc_pres_temp.nc", NcFile::Replace);
// Check to see if the file was created.
if(!dataFile.is_valid())
return NC_ERR;
// Define the dimensions. NetCDF will hand back an ncDim object for
// each.
NcDim *latDim, *lonDim;
if (!(latDim = dataFile.add_dim("latitude", NLAT)))
return NC_ERR;
if (!(lonDim = dataFile.add_dim("longitude", NLON)))
return NC_ERR;
// In addition to the latitude and longitude dimensions, we will
// also create latitude and longitude netCDF variables which will
// hold the actual latitudes and longitudes. Since they hold data
// about the coordinate system, the netCDF term for these is:
// "coordinate variables."
NcVar *latVar, *lonVar;
if (!(latVar = dataFile.add_var("latitude", ncFloat, latDim)))
return NC_ERR;
if (!(lonVar = dataFile.add_var("longitude", ncFloat, lonDim)))
return NC_ERR;
// Define units attributes for coordinate vars. This attaches a
// text attribute to each of the coordinate variables, containing
// the units.
if (!lonVar->add_att("units", "degrees_east"))
return NC_ERR;
if (!latVar->add_att("units", "degrees_north"))
return NC_ERR;
// Define the netCDF data variables.
NcVar *presVar, *tempVar;
if (!(presVar = dataFile.add_var("pressure", ncFloat, latDim, lonDim)))
return NC_ERR;
if (!(tempVar = dataFile.add_var("temperature", ncFloat, latDim, lonDim)))
return NC_ERR;
// Define units attributes for variables.
if (!presVar->add_att("units", "hPa"))
return NC_ERR;
if (!tempVar->add_att("units", "celsius"))
return NC_ERR;
// Write the coordinate variable data. This will put the latitudes
// and longitudes of our data grid into the netCDF file.
if (!latVar->put(lats, NLAT))
return NC_ERR;
if (!lonVar->put(lons, NLON))
return NC_ERR;
// Write the pretend data. This will write our surface pressure and
// surface temperature data. The arrays of data are the same size
// as the netCDF variables we have defined, and below we write them
// each in one step.
if (!presVar->put(&presOut[0][0], NLAT, NLON))
return NC_ERR;
if (!tempVar->put(&tempOut[0][0], NLAT, NLON))
return NC_ERR;
// The file is automatically closed by the destructor. This frees
// up any internal netCDF resources associated with the file, and
// flushes any buffers.
cout << "*** SUCCESS writing example file sfc_pres_temp.nc!" << endl;
return 0;
}
</pre>
<div class="node">
<a name="sfc_pres_temp_rd.cpp"></a>
<a name="sfc_005fpres_005ftemp_005frd_002ecpp"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp_005fwr_002ecpp">sfc_pres_temp_wr.cpp</a>,
Up: <a rel="up" accesskey="u" href="#sfc_005fpres_005ftemp-in-C_002b_002b">sfc_pres_temp in C++</a>
</div>
<h5 class="subsubsection">2.2.4.2 sfc_pres_temp_rd.cpp</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is an example which reads some surface pressure and
temperatures. The data file read by this program is produced
companion program sfc_pres_temp_wr.cxx. It is intended to
illustrate the use of the netCDF C++ API.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: sfc_pres_temp_rd.cpp,v 1.17 2008/05/16 13:42:28 ed Exp $
*/
#include <iostream>
#include <cstring>
#include <netcdfcpp.h>
using namespace std;
// We are reading 2D data, a 6 x 12 lat-lon grid.
static const int NLAT = 6;
static const int NLON = 12;
// These are used to calculate the values we expect to find.
static const float SAMPLE_PRESSURE = 900;
static const float SAMPLE_TEMP = 9.0;
static const float START_LAT = 25.0;
static const float START_LON = -125.0;
// Return this code to the OS in case of failure.
static const int NC_ERR = 2;
int main(void)
{
// These will hold our pressure and temperature data.
float presIn[NLAT][NLON];
float tempIn[NLAT][NLON];
// These will hold our latitudes and longitudes.
float latsIn[NLAT];
float lonsIn[NLON];
// Change the error behavior of the netCDF C++ API by creating an
// NcError object. Until it is destroyed, this NcError object will
// ensure that the netCDF C++ API silently returns error codes on
// any failure, and leaves any other error handling to the calling
// program. In the case of this example, we just exit with an
// NC_ERR error code.
NcError err(NcError::silent_nonfatal);
// Open the file and check to make sure it's valid.
NcFile dataFile("sfc_pres_temp.nc", NcFile::ReadOnly);
if(!dataFile.is_valid())
return NC_ERR;
// There are a number of inquiry functions in netCDF which can be
// used to learn about an unknown netCDF file. In this case we know
// that there are 2 netCDF dimensions, 4 netCDF variables, no
// global attributes, and no unlimited dimension.
if (dataFile.num_dims() != 2 || dataFile.num_vars() != 4 ||
dataFile.num_atts() != 0 || dataFile.rec_dim() != 0)
return NC_ERR;
// We get back a pointer to each NcVar we request. Get the
// latitude and longitude coordinate variables.
NcVar *latVar, *lonVar;
if (!(latVar = dataFile.get_var("latitude")))
return NC_ERR;
if (!(lonVar = dataFile.get_var("longitude")))
return NC_ERR;
// Read the latitude and longitude coordinate variables into arrays
// latsIn and lonsIn.
if (!latVar->get(latsIn, NLAT))
return NC_ERR;
if (!lonVar->get(lonsIn, NLON))
return NC_ERR;
// Check the coordinate variable data.
for(int lat = 0; lat < NLAT; lat++)
if (latsIn[lat] != START_LAT + 5. * lat)
return NC_ERR;
// Check longitude values.
for (int lon = 0; lon < NLON; lon++)
if (lonsIn[lon] != START_LON + 5. * lon)
return NC_ERR;
// We get back a pointer to each NcVar we request.
NcVar *presVar, *tempVar;
if (!(presVar = dataFile.get_var("pressure")))
return NC_ERR;
if (!(tempVar = dataFile.get_var("temperature")))
return NC_ERR;
// Read the data. Since we know the contents of the file we know
// that the data arrays in this program are the correct size to
// hold all the data.
if (!presVar->get(&presIn[0][0], NLAT, NLON))
return NC_ERR;
if (!tempVar->get(&tempIn[0][0], NLAT, NLON))
return NC_ERR;
// Check the data.
for (int lat = 0; lat < NLAT; lat++)
for (int lon = 0; lon < NLON; lon++)
if (presIn[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat)
|| tempIn[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
return NC_ERR;
// Each of the netCDF variables has a "units" attribute. Let's read
// them and check them.
NcAtt *att;
char *units;
if (!(att = latVar->get_att("units")))
return NC_ERR;
units = att->as_string(0);
if (strncmp(units, "degrees_north", strlen("degrees_north")))
return NC_ERR;
// Attributes and attribute values should be deleted by the caller
// when no longer needed, to prevent memory leaks.
delete units;
delete att;
if (!(att = lonVar->get_att("units")))
return NC_ERR;
units = att->as_string(0);
if (strncmp(units, "degrees_east", strlen("degrees_east")))
return NC_ERR;
delete units;
delete att;
if (!(att = presVar->get_att("units")))
return NC_ERR;
units = att->as_string(0);
if (strncmp(units, "hPa", strlen("hPa")))
return NC_ERR;
delete units;
delete att;
if (!(att = tempVar->get_att("units")))
return NC_ERR;
units = att->as_string(0);
if (strncmp(units, "celsius", strlen("celsius")))
return NC_ERR;
delete units;
delete att;
// The file will be automatically closed by the destructor. This
// frees up any internal netCDF resources associated with the file,
// and flushes any buffers.
cout << "*** SUCCESS reading example file sfc_pres_temp.nc!" << endl;
return 0;
}
</pre>
<div class="node">
<a name="pres_temp_4D"></a>
<a name="pres_005ftemp_005f4D"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#sfc_005fpres_005ftemp">sfc_pres_temp</a>,
Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
</div>
<h3 class="section">2.3 The pres_temp_4D Example</h3>
<p>This example expands on the previous example by making our
two-dimensional data into four-dimensional data, adding a vertical
level axis and an unlimited time step axis.
<p>Additionally, in this example the data are written and read one
time step at a time, as is typical in scientific applications that use
the unlimited dimension.
<p>The sample data file created by pres_temp_4D_wr can be examined with
the utility ncdump. The output is shown below. For more information on
ncdump see <a href="netcdf.html#NetCDF-Utilities">NetCDF Utilities</a>.
<pre class="example"> netcdf pres_temp_4D {
dimensions:
level = 2 ;
latitude = 6 ;
longitude = 12 ;
time = UNLIMITED ; // (2 currently)
variables:
float latitude(latitude) ;
latitude:units = "degrees_north" ;
float longitude(longitude) ;
longitude:units = "degrees_east" ;
float pressure(time, level, latitude, longitude) ;
pressure:units = "hPa" ;
float temperature(time, level, latitude, longitude) ;
temperature:units = "celsius" ;
data:
latitude = 25, 30, 35, 40, 45, 50 ;
longitude = -125, -120, -115, -110, -105, -100, -95, -90, -85, -80, -75, -70 ;
pressure =
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,
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 ;
temperature =
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,
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 ;
}
</pre>
<ul class="menu">
<li><a accesskey="1" href="#pres_005ftemp_005f4D-in-C">pres_temp_4D in C</a>
<li><a accesskey="2" href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>
<li><a accesskey="3" href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>
<li><a accesskey="4" href="#pres_005ftemp_005f4D-in-C_002b_002b">pres_temp_4D in C++</a>
</ul>
<div class="node">
<a name="pres_temp_4D-in-C"></a>
<a name="pres_005ftemp_005f4D-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D">pres_temp_4D</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D">pres_temp_4D</a>
</div>
<h4 class="subsection">2.3.1 pres_temp_4D_wr.c and pres_temp_4D_rd.c</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/C.
<p>The example program pres_temp_4D_wr.c creates the example data file
pres_temp_4D.nc. The example program pres_temp_4D_rd.c reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#pres_005ftemp_005f4D_005fwr_002ec">pres_temp_4D_wr.c</a>
<li><a accesskey="2" href="#pres_005ftemp_005f4D_005frd_002ec">pres_temp_4D_rd.c</a>
</ul>
<div class="node">
<a name="pres_temp_4D_wr.c"></a>
<a name="pres_005ftemp_005f4D_005fwr_002ec"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D_005frd_002ec">pres_temp_4D_rd.c</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-C">pres_temp_4D in C</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-C">pres_temp_4D in C</a>
</div>
<h5 class="subsubsection">2.3.1.1 pres_temp_4D_wr.c</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is an example program which writes some 4D pressure and
temperatures. It is intended to illustrate the use of the netCDF
C API. The companion program pres_temp_4D_rd.c shows how
to read the netCDF data file created by this program.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$Id: pres_temp_4D_wr.c,v 1.5 2006/11/04 21:13:04 russ Exp $
*/
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "pres_temp_4D.nc"
/* We are writing 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
timesteps of data. */
#define NDIMS 4
#define NLAT 6
#define NLON 12
#define LAT_NAME "latitude"
#define LON_NAME "longitude"
#define NREC 2
#define REC_NAME "time"
#define LVL_NAME "level"
#define NLVL 2
/* Names of things. */
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
#define UNITS "units"
#define DEGREES_EAST "degrees_east"
#define DEGREES_NORTH "degrees_north"
/* These are used to construct some example data. */
#define SAMPLE_PRESSURE 900
#define SAMPLE_TEMP 9.0
#define START_LAT 25.0
#define START_LON -125.0
/* For the units attributes. */
#define UNITS "units"
#define PRES_UNITS "hPa"
#define TEMP_UNITS "celsius"
#define LAT_UNITS "degrees_north"
#define LON_UNITS "degrees_east"
#define MAX_ATT_LEN 80
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
int
main()
{
/* IDs for the netCDF file, dimensions, and variables. */
int ncid, lon_dimid, lat_dimid, lvl_dimid, rec_dimid;
int lat_varid, lon_varid, pres_varid, temp_varid;
int dimids[NDIMS];
/* The start and count arrays will tell the netCDF library where to
write our data. */
size_t start[NDIMS], count[NDIMS];
/* Program variables to hold the data we will write out. We will only
need enough space to hold one timestep of data; one record. */
float pres_out[NLVL][NLAT][NLON];
float temp_out[NLVL][NLAT][NLON];
/* These program variables hold the latitudes and longitudes. */
float lats[NLAT], lons[NLON];
/* Loop indexes. */
int lvl, lat, lon, rec, i = 0;
/* Error handling. */
int retval;
/* Create some pretend data. If this wasn't an example program, we
* would have some real data to write, for example, model
* output. */
for (lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5.*lat;
for (lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5.*lon;
for (lvl = 0; lvl < NLVL; lvl++)
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
{
pres_out[lvl][lat][lon] = SAMPLE_PRESSURE + i;
temp_out[lvl][lat][lon] = SAMPLE_TEMP + i++;
}
/* Create the file. */
if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid)))
ERR(retval);
/* Define the dimensions. The record dimension is defined to have
* unlimited length - it can grow as needed. In this example it is
* the time dimension.*/
if ((retval = nc_def_dim(ncid, LVL_NAME, NLVL, &lvl_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, REC_NAME, NC_UNLIMITED, &rec_dimid)))
ERR(retval);
/* Define the coordinate variables. We will only define coordinate
variables for lat and lon. Ordinarily we would need to provide
an array of dimension IDs for each variable's dimensions, but
since coordinate variables only have one dimension, we can
simply provide the address of that dimension ID (&lat_dimid) and
similarly for (&lon_dimid). */
if ((retval = nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, &lat_dimid,
&lat_varid)))
ERR(retval);
if ((retval = nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, &lon_dimid,
&lon_varid)))
ERR(retval);
/* Assign units attributes to coordinate variables. */
if ((retval = nc_put_att_text(ncid, lat_varid, UNITS,
strlen(DEGREES_NORTH), DEGREES_NORTH)))
ERR(retval);
if ((retval = nc_put_att_text(ncid, lon_varid, UNITS,
strlen(DEGREES_EAST), DEGREES_EAST)))
ERR(retval);
/* The dimids array is used to pass the dimids of the dimensions of
the netCDF variables. Both of the netCDF variables we are
creating share the same four dimensions. In C, the
unlimited dimension must come first on the list of dimids. */
dimids[0] = rec_dimid;
dimids[1] = lvl_dimid;
dimids[2] = lat_dimid;
dimids[3] = lon_dimid;
/* Define the netCDF variables for the pressure and temperature
* data. */
if ((retval = nc_def_var(ncid, PRES_NAME, NC_FLOAT, NDIMS,
dimids, &pres_varid)))
ERR(retval);
if ((retval = nc_def_var(ncid, TEMP_NAME, NC_FLOAT, NDIMS,
dimids, &temp_varid)))
ERR(retval);
/* Assign units attributes to the netCDF variables. */
if ((retval = nc_put_att_text(ncid, pres_varid, UNITS,
strlen(PRES_UNITS), PRES_UNITS)))
ERR(retval);
if ((retval = nc_put_att_text(ncid, temp_varid, UNITS,
strlen(TEMP_UNITS), TEMP_UNITS)))
ERR(retval);
/* End define mode. */
if ((retval = nc_enddef(ncid)))
ERR(retval);
/* Write the coordinate variable data. This will put the latitudes
and longitudes of our data grid into the netCDF file. */
if ((retval = nc_put_var_float(ncid, lat_varid, &lats[0])))
ERR(retval);
if ((retval = nc_put_var_float(ncid, lon_varid, &lons[0])))
ERR(retval);
/* These settings tell netcdf to write one timestep of data. (The
setting of start[0] inside the loop below tells netCDF which
timestep to write.) */
count[0] = 1;
count[1] = NLVL;
count[2] = NLAT;
count[3] = NLON;
start[1] = 0;
start[2] = 0;
start[3] = 0;
/* Write the pretend data. This will write our surface pressure and
surface temperature data. The arrays only hold one timestep worth
of data. We will just rewrite the same data for each timestep. In
a real application, the data would change between timesteps. */
for (rec = 0; rec < NREC; rec++)
{
start[0] = rec;
if ((retval = nc_put_vara_float(ncid, pres_varid, start, count,
&pres_out[0][0][0])))
ERR(retval);
if ((retval = nc_put_vara_float(ncid, temp_varid, start, count,
&temp_out[0][0][0])))
ERR(retval);
}
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file %s!\n", FILE_NAME);
return 0;
}
</pre>
<div class="node">
<a name="pres_temp_4D_rd.c"></a>
<a name="pres_005ftemp_005f4D_005frd_002ec"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D_005fwr_002ec">pres_temp_4D_wr.c</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-C">pres_temp_4D in C</a>
</div>
<h5 class="subsubsection">2.3.1.2 pres_temp_4D_rd.c</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is an example which reads some 4D pressure and
temperatures. The data file read by this program is produced by the
companion program pres_temp_4D_wr.c. It is intended to illustrate
the use of the netCDF C API.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c
$Id: pres_temp_4D_rd.c,v 1.5 2006/06/26 20:37:31 russ Exp $
*/
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
/* This is the name of the data file we will read. */
#define FILE_NAME "pres_temp_4D.nc"
/* We are reading 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
timesteps of data. */
#define NDIMS 4
#define NLAT 6
#define NLON 12
#define LAT_NAME "latitude"
#define LON_NAME "longitude"
#define NREC 2
#define REC_NAME "time"
#define LVL_NAME "level"
#define NLVL 2
/* Names of things. */
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
#define UNITS "units"
#define DEGREES_EAST "degrees_east"
#define DEGREES_NORTH "degrees_north"
/* These are used to calculate the values we expect to find. */
#define SAMPLE_PRESSURE 900
#define SAMPLE_TEMP 9.0
#define START_LAT 25.0
#define START_LON -125.0
/* For the units attributes. */
#define UNITS "units"
#define PRES_UNITS "hPa"
#define TEMP_UNITS "celsius"
#define LAT_UNITS "degrees_north"
#define LON_UNITS "degrees_east"
#define MAX_ATT_LEN 80
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
int
main()
{
int ncid, pres_varid, temp_varid;
int lat_varid, lon_varid;
/* The start and count arrays will tell the netCDF library where to
read our data. */
size_t start[NDIMS], count[NDIMS];
/* Program variables to hold the data we will read. We will only
need enough space to hold one timestep of data; one record. */
float pres_in[NLVL][NLAT][NLON];
float temp_in[NLVL][NLAT][NLON];
/* These program variables hold the latitudes and longitudes. */
float lats[NLAT], lons[NLON];
/* Loop indexes. */
int lvl, lat, lon, rec, i = 0;
/* Error handling. */
int retval;
/* Open the file. */
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);
/* Get the varids of the latitude and longitude coordinate
* variables. */
if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
ERR(retval);
if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
ERR(retval);
/* Read the coordinate variable data. */
if ((retval = nc_get_var_float(ncid, lat_varid, &lats[0])))
ERR(retval);
if ((retval = nc_get_var_float(ncid, lon_varid, &lons[0])))
ERR(retval);
/* Check the coordinate variable data. */
for (lat = 0; lat < NLAT; lat++)
if (lats[lat] != START_LAT + 5.*lat)
return 2;
for (lon = 0; lon < NLON; lon++)
if (lons[lon] != START_LON + 5.*lon)
return 2;
/* Get the varids of the pressure and temperature netCDF
* variables. */
if ((retval = nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
ERR(retval);
if ((retval = nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
ERR(retval);
/* Read the data. Since we know the contents of the file we know
* that the data arrays in this program are the correct size to
* hold one timestep. */
count[0] = 1;
count[1] = NLVL;
count[2] = NLAT;
count[3] = NLON;
start[1] = 0;
start[2] = 0;
start[3] = 0;
/* Read and check one record at a time. */
for (rec = 0; rec < NREC; rec++)
{
start[0] = rec;
if ((retval = nc_get_vara_float(ncid, pres_varid, start,
count, &pres_in[0][0][0])))
ERR(retval);
if ((retval = nc_get_vara_float(ncid, temp_varid, start,
count, &temp_in[0][0][0])))
ERR(retval);
/* Check the data. */
i = 0;
for (lvl = 0; lvl < NLVL; lvl++)
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
{
if (pres_in[lvl][lat][lon] != SAMPLE_PRESSURE + i)
return 2;
if (temp_in[lvl][lat][lon] != SAMPLE_TEMP + i)
return 2;
i++;
}
} /* next record */
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS reading example file pres_temp_4D.nc!\n");
return 0;
}
</pre>
<div class="node">
<a name="pres_temp_4D-in-F77"></a>
<a name="pres_005ftemp_005f4D-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-C">pres_temp_4D in C</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D">pres_temp_4D</a>
</div>
<h4 class="subsection">2.3.2 pres_temp_4D_wr.f and pres_temp_4D_rd.f</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/F77.
<p>The example program pres_temp_4D_wr.f creates the example data file
pres_temp_4D.nc. The example program pres_temp_4D_rd.f reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#pres_005ftemp_005f4D_005fwr_002ef">pres_temp_4D_wr.f</a>
<li><a accesskey="2" href="#pres_005ftemp_005f4D_005frd_002ef">pres_temp_4D_rd.f</a>
</ul>
<div class="node">
<a name="pres_temp_4D_wr.f"></a>
<a name="pres_005ftemp_005f4D_005fwr_002ef"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D_005frd_002ef">pres_temp_4D_rd.f</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>
</div>
<h5 class="subsubsection">2.3.2.1 pres_temp_4D_wr.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is an example program which writes some 4D pressure and
C temperatures. It is intended to illustrate the use of the netCDF
C fortran 77 API. The companion program pres_temp_4D_rd.f shows how
C to read the netCDF data file created by this program.
C This program is part of the netCDF tutorial:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: pres_temp_4D_wr.f,v 1.12 2007/02/14 20:59:20 ed Exp $
program pres_temp_4D_wr
implicit none
include 'netcdf.inc'
C This is the name of the data file we will create.
character*(*) FILE_NAME
parameter (FILE_NAME = 'pres_temp_4D.nc')
integer ncid
C We are writing 4D data, a 12 x 6 x 2 lon-lat-lvl grid, with 2
C timesteps of data.
integer NDIMS, NRECS
parameter (NDIMS = 4, NRECS = 2)
integer NLVLS, NLATS, NLONS
parameter (NLVLS = 2, NLATS = 6, NLONS = 12)
character*(*) LVL_NAME, LAT_NAME, LON_NAME, REC_NAME
parameter (LVL_NAME = 'level')
parameter (LAT_NAME = 'latitude', LON_NAME = 'longitude')
parameter (REC_NAME = 'time')
integer lvl_dimid, lon_dimid, lat_dimid, rec_dimid
C The start and count arrays will tell the netCDF library where to
C write our data.
integer start(NDIMS), count(NDIMS)
C These program variables hold the latitudes and longitudes.
real lats(NLATS), lons(NLONS)
integer lon_varid, lat_varid
C We will create two netCDF variables, one each for temperature and
C pressure fields.
character*(*) PRES_NAME, TEMP_NAME
parameter (PRES_NAME='pressure')
parameter (TEMP_NAME='temperature')
integer pres_varid, temp_varid
integer dimids(NDIMS)
C We recommend that each variable carry a "units" attribute.
character*(*) UNITS
parameter (UNITS = 'units')
character*(*) PRES_UNITS, TEMP_UNITS, LAT_UNITS, LON_UNITS
parameter (PRES_UNITS = 'hPa', TEMP_UNITS = 'celsius')
parameter (LAT_UNITS = 'degrees_north')
parameter (LON_UNITS = 'degrees_east')
C Program variables to hold the data we will write out. We will only
C need enough space to hold one timestep of data; one record.
real pres_out(NLONS, NLATS, NLVLS)
real temp_out(NLONS, NLATS, NLVLS)
real SAMPLE_PRESSURE
parameter (SAMPLE_PRESSURE = 900.0)
real SAMPLE_TEMP
parameter (SAMPLE_TEMP = 9.0)
C Use these to construct some latitude and longitude data for this
C example.
integer START_LAT, START_LON
parameter (START_LAT = 25.0, START_LON = -125.0)
C Loop indices.
integer lvl, lat, lon, rec, i
C Error handling.
integer retval
C Create pretend data. If this wasn't an example program, we would
C have some real data to write, for example, model output.
do lat = 1, NLATS
lats(lat) = START_LAT + (lat - 1) * 5.0
end do
do lon = 1, NLONS
lons(lon) = START_LON + (lon - 1) * 5.0
end do
i = 0
do lvl = 1, NLVLS
do lat = 1, NLATS
do lon = 1, NLONS
pres_out(lon, lat, lvl) = SAMPLE_PRESSURE + i
temp_out(lon, lat, lvl) = SAMPLE_TEMP + i
i = i + 1
end do
end do
end do
C Create the file.
retval = nf_create(FILE_NAME, nf_clobber, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the dimensions. The record dimension is defined to have
C unlimited length - it can grow as needed. In this example it is
C the time dimension.
retval = nf_def_dim(ncid, LVL_NAME, NLVLS, lvl_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, LAT_NAME, NLATS, lat_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, LON_NAME, NLONS, lon_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, REC_NAME, NF_UNLIMITED, rec_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the coordinate variables. We will only define coordinate
C variables for lat and lon. Ordinarily we would need to provide
C an array of dimension IDs for each variable's dimensions, but
C since coordinate variables only have one dimension, we can
C simply provide the address of that dimension ID (lat_dimid) and
C similarly for (lon_dimid).
retval = nf_def_var(ncid, LAT_NAME, NF_REAL, 1, lat_dimid,
+ lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, LON_NAME, NF_REAL, 1, lon_dimid,
+ lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Assign units attributes to coordinate variables.
retval = nf_put_att_text(ncid, lat_varid, UNITS, len(LAT_UNITS),
+ LAT_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, lon_varid, UNITS, len(LON_UNITS),
+ LON_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
C The dimids array is used to pass the dimids of the dimensions of
C the netCDF variables. Both of the netCDF variables we are creating
C share the same four dimensions. In Fortran, the unlimited
C dimension must come last on the list of dimids.
dimids(1) = lon_dimid
dimids(2) = lat_dimid
dimids(3) = lvl_dimid
dimids(4) = rec_dimid
C Define the netCDF variables for the pressure and temperature data.
retval = nf_def_var(ncid, PRES_NAME, NF_REAL, NDIMS, dimids,
+ pres_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, TEMP_NAME, NF_REAL, NDIMS, dimids,
+ temp_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Assign units attributes to the netCDF variables.
retval = nf_put_att_text(ncid, pres_varid, UNITS, len(PRES_UNITS),
+ PRES_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, temp_varid, UNITS, len(TEMP_UNITS),
+ TEMP_UNITS)
if (retval .ne. nf_noerr) call handle_err(retval)
C End define mode.
retval = nf_enddef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Write the coordinate variable data. This will put the latitudes
C and longitudes of our data grid into the netCDF file.
retval = nf_put_var_real(ncid, lat_varid, lats)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_var_real(ncid, lon_varid, lons)
if (retval .ne. nf_noerr) call handle_err(retval)
C These settings tell netcdf to write one timestep of data. (The
C setting of start(4) inside the loop below tells netCDF which
C timestep to write.)
count(1) = NLONS
count(2) = NLATS
count(3) = NLVLS
count(4) = 1
start(1) = 1
start(2) = 1
start(3) = 1
C Write the pretend data. This will write our surface pressure and
C surface temperature data. The arrays only hold one timestep worth
C of data. We will just rewrite the same data for each timestep. In
C a real application, the data would change between timesteps.
do rec = 1, NRECS
start(4) = rec
retval = nf_put_vara_real(ncid, pres_varid, start, count,
+ pres_out)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_vara_real(ncid, temp_varid, start, count,
+ temp_out)
if (retval .ne. nf_noerr) call handle_err(retval)
end do
C Close the file. This causes netCDF to flush all buffers and make
C sure your data are really written to disk.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
print *,'*** SUCCESS writing example file', FILE_NAME, '!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="pres_temp_4D_rd.f"></a>
<a name="pres_005ftemp_005f4D_005frd_002ef"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D_005fwr_002ef">pres_temp_4D_wr.f</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>
</div>
<h5 class="subsubsection">2.3.2.2 pres_temp_4D_rd.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is an example which reads some 4D pressure and
C temperatures. The data file read by this program is produced by
C the companion program pres_temp_4D_wr.f. It is intended to
C illustrate the use of the netCDF Fortran 77 API.
C This program is part of the netCDF tutorial:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: pres_temp_4D_rd.f,v 1.12 2007/02/14 20:59:20 ed Exp $
program pres_temp_4D_rd
implicit none
include 'netcdf.inc'
C This is the name of the data file we will read.
character*(*) FILE_NAME
parameter (FILE_NAME='pres_temp_4D.nc')
integer ncid
C We are reading 4D data, a 12 x 6 x 2 lon-lat-lvl grid, with 2
C timesteps of data.
integer NDIMS, NRECS
parameter (NDIMS = 4, NRECS = 2)
integer NLVLS, NLATS, NLONS
parameter (NLVLS = 2, NLATS = 6, NLONS = 12)
character*(*) LVL_NAME, LAT_NAME, LON_NAME, REC_NAME
parameter (LVL_NAME = 'level')
parameter (LAT_NAME = 'latitude', LON_NAME = 'longitude')
parameter (REC_NAME = 'time')
integer lvl_dimid, lon_dimid, lat_dimid, rec_dimid
C The start and count arrays will tell the netCDF library where to
C read our data.
integer start(NDIMS), count(NDIMS)
C In addition to the latitude and longitude dimensions, we will also
C create latitude and longitude variables which will hold the actual
C latitudes and longitudes. Since they hold data about the
C coordinate system, the netCDF term for these is: "coordinate
C variables."
real lats(NLATS), lons(NLONS)
integer lon_varid, lat_varid
C We will read surface temperature and pressure fields. In netCDF
C terminology these are called "variables."
character*(*) PRES_NAME, TEMP_NAME
parameter (PRES_NAME='pressure')
parameter (TEMP_NAME='temperature')
integer pres_varid, temp_varid
integer dimids(NDIMS)
C We recommend that each variable carry a "units" attribute.
character*(*) UNITS
parameter (UNITS = 'units')
character*(*) PRES_UNITS, TEMP_UNITS, LAT_UNITS, LON_UNITS
parameter (PRES_UNITS = 'hPa', TEMP_UNITS = 'celsius')
parameter (LAT_UNITS = 'degrees_north')
parameter (LON_UNITS = 'degrees_east')
C Program variables to hold the data we will read in. We will only
C need enough space to hold one timestep of data; one record.
real pres_in(NLONS, NLATS, NLVLS)
real temp_in(NLONS, NLATS, NLVLS)
real SAMPLE_PRESSURE
parameter (SAMPLE_PRESSURE = 900.0)
real SAMPLE_TEMP
parameter (SAMPLE_TEMP = 9.0)
C Use these to calculate the values we expect to find.
integer START_LAT, START_LON
parameter (START_LAT = 25.0, START_LON = -125.0)
C Loop indices.
integer lvl, lat, lon, rec, i
C Error handling.
integer retval
C Open the file.
retval = nf_open(FILE_NAME, nf_nowrite, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Get the varids of the latitude and longitude coordinate variables.
retval = nf_inq_varid(ncid, LAT_NAME, lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_varid(ncid, LON_NAME, lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Read the latitude and longitude data.
retval = nf_get_var_real(ncid, lat_varid, lats)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_get_var_real(ncid, lon_varid, lons)
if (retval .ne. nf_noerr) call handle_err(retval)
C Check to make sure we got what we expected.
do lat = 1, NLATS
if (lats(lat) .ne. START_LAT + (lat - 1) * 5.0) stop 2
end do
do lon = 1, NLONS
if (lons(lon) .ne. START_LON + (lon - 1) * 5.0) stop 2
end do
C Get the varids of the pressure and temperature netCDF variables.
retval = nf_inq_varid(ncid, PRES_NAME, pres_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_varid(ncid, TEMP_NAME, temp_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Read 1 record of NLONS*NLATS*NLVLS values, starting at the
C beginning of the record (the (1, 1, 1, rec) element in the netCDF
C file).
count(1) = NLONS
count(2) = NLATS
count(3) = NLVLS
count(4) = 1
start(1) = 1
start(2) = 1
start(3) = 1
C Read the surface pressure and temperature data from the file, one
C record at a time.
do rec = 1, NRECS
start(4) = rec
retval = nf_get_vara_real(ncid, pres_varid, start, count,
$ pres_in)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_get_vara_real(ncid, temp_varid, start, count,
$ temp_in)
if (retval .ne. nf_noerr) call handle_err(retval)
i = 0
do lvl = 1, NLVLS
do lat = 1, NLATS
do lon = 1, NLONS
if (pres_in(lon, lat, lvl) .ne. SAMPLE_PRESSURE + i)
$ stop 2
if (temp_in(lon, lat, lvl) .ne. SAMPLE_TEMP + i)
$ stop 2
i = i + 1
end do
end do
end do
C next record
end do
C Close the file. This frees up any internal netCDF resources
C associated with the file.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C If we got this far, everything worked as expected. Yipee!
print *,'*** SUCCESS reading example file pres_temp_4D.nc!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="pres_temp_4D-in-F90"></a>
<a name="pres_005ftemp_005f4D-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D-in-C_002b_002b">pres_temp_4D in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-F77">pres_temp_4D in F77</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D">pres_temp_4D</a>
</div>
<h4 class="subsection">2.3.3 pres_temp_4D_wr.f90 and pres_temp_4D_rd.f90</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/F90.
<p>The example program pres_temp_4D_wr.f90 creates the example data file
pres_temp_4D.nc. The example program pres_temp_4D_rd.f90 reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#pres_005ftemp_005f4D_005fwr_002ef90">pres_temp_4D_wr.f90</a>
<li><a accesskey="2" href="#pres_005ftemp_005f4D_005frd_002ef90">pres_temp_4D_rd.f90</a>
</ul>
<div class="node">
<a name="pres_temp_4D_wr.f90"></a>
<a name="pres_005ftemp_005f4D_005fwr_002ef90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D_005frd_002ef90">pres_temp_4D_rd.f90</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>
</div>
<h5 class="subsubsection">2.3.3.1 pres_temp_4D_wr.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is an example program which writes some 4D pressure and
! temperatures. It is intended to illustrate the use of the netCDF
! fortran 90 API. The companion program pres_temp_4D_rd.f shows how
! to read the netCDF data file created by this program.
! This program is part of the netCDF tutorial:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: pres_temp_4D_wr.f90,v 1.10 2010/04/06 19:32:09 ed Exp $
program pres_temp_4D_wr
use netcdf
implicit none
! This is the name of the data file we will create.
character (len = *), parameter :: FILE_NAME = "pres_temp_4D.nc"
integer :: ncid
! We are writing 4D data, a 12 x 6 x 2 lon-lat-lvl grid, with 2
! timesteps of data.
integer, parameter :: NDIMS = 4, NRECS = 2
integer, parameter :: NLVLS = 2, NLATS = 6, NLONS = 12
character (len = *), parameter :: LVL_NAME = "level"
character (len = *), parameter :: LAT_NAME = "latitude"
character (len = *), parameter :: LON_NAME = "longitude"
character (len = *), parameter :: REC_NAME = "time"
integer :: lvl_dimid, lon_dimid, lat_dimid, rec_dimid
! The start and count arrays will tell the netCDF library where to
! write our data.
integer :: start(NDIMS), count(NDIMS)
! These program variables hold the latitudes and longitudes.
real :: lats(NLATS), lons(NLONS)
integer :: lon_varid, lat_varid
! We will create two netCDF variables, one each for temperature and
! pressure fields.
character (len = *), parameter :: PRES_NAME="pressure"
character (len = *), parameter :: TEMP_NAME="temperature"
integer :: pres_varid, temp_varid
integer :: dimids(NDIMS)
! We recommend that each variable carry a "units" attribute.
character (len = *), parameter :: UNITS = "units"
character (len = *), parameter :: PRES_UNITS = "hPa"
character (len = *), parameter :: TEMP_UNITS = "celsius"
character (len = *), parameter :: LAT_UNITS = "degrees_north"
character (len = *), parameter :: LON_UNITS = "degrees_east"
! Program variables to hold the data we will write out. We will only
! need enough space to hold one timestep of data; one record.
real, dimension(:,:,:), allocatable :: pres_out
real, dimension(:,:,:), allocatable :: temp_out
real, parameter :: SAMPLE_PRESSURE = 900.0
real, parameter :: SAMPLE_TEMP = 9.0
! Use these to construct some latitude and longitude data for this
! example.
real, parameter :: START_LAT = 25.0, START_LON = -125.0
! Loop indices
integer :: lvl, lat, lon, rec, i
! Allocate memory.
allocate(pres_out(NLONS, NLATS, NLVLS))
allocate(temp_out(NLONS, NLATS, NLVLS))
! Create pretend data. If this were not an example program, we would
! have some real data to write, for example, model output.
do lat = 1, NLATS
lats(lat) = START_LAT + (lat - 1) * 5.0
end do
do lon = 1, NLONS
lons(lon) = START_LON + (lon - 1) * 5.0
end do
i = 0
do lvl = 1, NLVLS
do lat = 1, NLATS
do lon = 1, NLONS
pres_out(lon, lat, lvl) = SAMPLE_PRESSURE + i
temp_out(lon, lat, lvl) = SAMPLE_TEMP + i
i = i + 1
end do
end do
end do
! Create the file.
call check( nf90_create(FILE_NAME, nf90_clobber, ncid) )
! Define the dimensions. The record dimension is defined to have
! unlimited length - it can grow as needed. In this example it is
! the time dimension.
call check( nf90_def_dim(ncid, LVL_NAME, NLVLS, lvl_dimid) )
call check( nf90_def_dim(ncid, LAT_NAME, NLATS, lat_dimid) )
call check( nf90_def_dim(ncid, LON_NAME, NLONS, lon_dimid) )
call check( nf90_def_dim(ncid, REC_NAME, NF90_UNLIMITED, rec_dimid) )
! Define the coordinate variables. We will only define coordinate
! variables for lat and lon. Ordinarily we would need to provide
! an array of dimension IDs for each variable's dimensions, but
! since coordinate variables only have one dimension, we can
! simply provide the address of that dimension ID (lat_dimid) and
! similarly for (lon_dimid).
call check( nf90_def_var(ncid, LAT_NAME, NF90_REAL, lat_dimid, lat_varid) )
call check( nf90_def_var(ncid, LON_NAME, NF90_REAL, lon_dimid, lon_varid) )
! Assign units attributes to coordinate variables.
call check( nf90_put_att(ncid, lat_varid, UNITS, LAT_UNITS) )
call check( nf90_put_att(ncid, lon_varid, UNITS, LON_UNITS) )
! The dimids array is used to pass the dimids of the dimensions of
! the netCDF variables. Both of the netCDF variables we are creating
! share the same four dimensions. In Fortran, the unlimited
! dimension must come last on the list of dimids.
dimids = (/ lon_dimid, lat_dimid, lvl_dimid, rec_dimid /)
! Define the netCDF variables for the pressure and temperature data.
call check( nf90_def_var(ncid, PRES_NAME, NF90_REAL, dimids, pres_varid) )
call check( nf90_def_var(ncid, TEMP_NAME, NF90_REAL, dimids, temp_varid) )
! Assign units attributes to the netCDF variables.
call check( nf90_put_att(ncid, pres_varid, UNITS, PRES_UNITS) )
call check( nf90_put_att(ncid, temp_varid, UNITS, TEMP_UNITS) )
! End define mode.
call check( nf90_enddef(ncid) )
! Write the coordinate variable data. This will put the latitudes
! and longitudes of our data grid into the netCDF file.
call check( nf90_put_var(ncid, lat_varid, lats) )
call check( nf90_put_var(ncid, lon_varid, lons) )
! These settings tell netcdf to write one timestep of data. (The
! setting of start(4) inside the loop below tells netCDF which
! timestep to write.)
count = (/ NLONS, NLATS, NLVLS, 1 /)
start = (/ 1, 1, 1, 1 /)
! Write the pretend data. This will write our surface pressure and
! surface temperature data. The arrays only hold one timestep worth
! of data. We will just rewrite the same data for each timestep. In
! a real :: application, the data would change between timesteps.
do rec = 1, NRECS
start(4) = rec
call check( nf90_put_var(ncid, pres_varid, pres_out, start = start, &
count = count) )
call check( nf90_put_var(ncid, temp_varid, temp_out, start = start, &
count = count) )
end do
! Close the file. This causes netCDF to flush all buffers and make
! sure your data are really written to disk.
call check( nf90_close(ncid) )
print *,"*** SUCCESS writing example file ", FILE_NAME, "!"
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program pres_temp_4D_wr
</pre>
<div class="node">
<a name="pres_temp_4D_rd.f90"></a>
<a name="pres_005ftemp_005f4D_005frd_002ef90"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D_005fwr_002ef90">pres_temp_4D_wr.f90</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>
</div>
<h5 class="subsubsection">2.3.3.2 pres_temp_4D_rd.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is an example which reads some 4D pressure and
! temperatures. The data file read by this program is produced by
! the companion program pres_temp_4D_wr.f90. It is intended to
! illustrate the use of the netCDF Fortran 90 API.
! This program is part of the netCDF tutorial:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: pres_temp_4D_rd.f90,v 1.9 2010/04/06 19:32:09 ed Exp $
program pres_temp_4D_rd
use netcdf
implicit none
! This is the name of the data file we will read.
character (len = *), parameter :: FILE_NAME = "pres_temp_4D.nc"
integer :: ncid
! We are reading 4D data, a 12 x 6 x 2 lon-lat-lvl grid, with 2
! timesteps of data.
integer, parameter :: NDIMS = 4, NRECS = 2
integer, parameter :: NLVLS = 2, NLATS = 6, NLONS = 12
character (len = *), parameter :: LVL_NAME = "level"
character (len = *), parameter :: LAT_NAME = "latitude"
character (len = *), parameter :: LON_NAME = "longitude"
character (len = *), parameter :: REC_NAME = "time"
! The start and count arrays will tell the netCDF library where to
! read our data.
integer :: start(NDIMS), count(NDIMS)
! In addition to the latitude and longitude dimensions, we will also
! create latitude and longitude variables which will hold the actual
! latitudes and longitudes. Since they hold data about the
! coordinate system, the netCDF term for these is: "coordinate
! variables."
real :: lats(NLATS), lons(NLONS)
integer :: lon_varid, lat_varid
! We will read surface temperature and pressure fields. In netCDF
! terminology these are called "variables."
character (len = *), parameter :: PRES_NAME="pressure"
character (len = *), parameter :: TEMP_NAME="temperature"
integer :: pres_varid, temp_varid
! We recommend that each variable carry a "units" attribute.
character (len = *), parameter :: UNITS = "units"
character (len = *), parameter :: PRES_UNITS = "hPa", TEMP_UNITS = "celsius"
character (len = *), parameter :: LAT_UNITS = "degrees_north"
character (len = *), parameter :: LON_UNITS = "degrees_east"
! Program variables to hold the data we will read in. We will only
! need enough space to hold one timestep of data; one record.
! Allocate memory for data.
real, dimension(:,:,:), allocatable :: pres_in
real, dimension(:,:,:), allocatable :: temp_in
real, parameter :: SAMPLE_PRESSURE = 900.0
real, parameter :: SAMPLE_TEMP = 9.0
! Use these to calculate the values we expect to find.
real, parameter :: START_LAT = 25.0, START_LON = -125.0
! Loop indices
integer :: lvl, lat, lon, rec, i
! Allocate memory.
allocate(pres_in(NLONS, NLATS, NLVLS))
allocate(temp_in(NLONS, NLATS, NLVLS))
! Open the file.
call check( nf90_open(FILE_NAME, nf90_nowrite, ncid) )
! Get the varids of the latitude and longitude coordinate variables.
call check( nf90_inq_varid(ncid, LAT_NAME, lat_varid) )
call check( nf90_inq_varid(ncid, LON_NAME, lon_varid) )
! Read the latitude and longitude data.
call check( nf90_get_var(ncid, lat_varid, lats) )
call check( nf90_get_var(ncid, lon_varid, lons) )
! Check to make sure we got what we expected.
do lat = 1, NLATS
if (lats(lat) /= START_LAT + (lat - 1) * 5.0) stop 2
end do
do lon = 1, NLONS
if (lons(lon) /= START_LON + (lon - 1) * 5.0) stop 2
end do
! Get the varids of the pressure and temperature netCDF variables.
call check( nf90_inq_varid(ncid, PRES_NAME, pres_varid) )
call check( nf90_inq_varid(ncid, TEMP_NAME, temp_varid) )
! Read 1 record of NLONS*NLATS*NLVLS values, starting at the beginning
! of the record (the (1, 1, 1, rec) element in the netCDF file).
count = (/ NLONS, NLATS, NLVLS, 1 /)
start = (/ 1, 1, 1, 1 /)
! Read the surface pressure and temperature data from the file, one
! record at a time.
do rec = 1, NRECS
start(4) = rec
call check( nf90_get_var(ncid, pres_varid, pres_in, start = start, &
count = count) )
call check( nf90_get_var(ncid, temp_varid, temp_in, start, count) )
i = 0
do lvl = 1, NLVLS
do lat = 1, NLATS
do lon = 1, NLONS
if (pres_in(lon, lat, lvl) /= SAMPLE_PRESSURE + i) stop 2
if (temp_in(lon, lat, lvl) /= SAMPLE_TEMP + i) stop 2
i = i + 1
end do
end do
end do
! next record
end do
! Close the file. This frees up any internal netCDF resources
! associated with the file.
call check( nf90_close(ncid) )
! If we got this far, everything worked as expected. Yipee!
print *,"*** SUCCESS reading example file ", FILE_NAME, "!"
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program pres_temp_4D_rd
</pre>
<div class="node">
<a name="pres_temp_4D-in-C++"></a>
<a name="pres_005ftemp_005f4D-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-F90">pres_temp_4D in F90</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D">pres_temp_4D</a>
</div>
<h4 class="subsection">2.3.4 pres_temp_4D_wr.cpp and pres_temp_4D_rd.cpp</h4>
<p>These example programs can be found in the netCDF distribution, under
examples/CXX.
<p>The example program pres_temp_4D_wr.cpp creates the example data file
pres_temp_4D.nc. The example program pres_temp_4D_rd.cpp reads the
data file.
<ul class="menu">
<li><a accesskey="1" href="#pres_005ftemp_005f4D_005fwr_002ecpp">pres_temp_4D_wr.cpp</a>
<li><a accesskey="2" href="#pres_005ftemp_005f4D_005frd_002ecpp">pres_temp_4D_rd.cpp</a>
</ul>
<div class="node">
<a name="pres_temp_4D_wr.cpp"></a>
<a name="pres_005ftemp_005f4D_005fwr_002ecpp"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#pres_005ftemp_005f4D_005frd_002ecpp">pres_temp_4D_rd.cpp</a>,
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D-in-C_002b_002b">pres_temp_4D in C++</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-C_002b_002b">pres_temp_4D in C++</a>
</div>
<h5 class="subsubsection">2.3.4.1 pres_temp_4D_wr.cpp</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is an example program which writes some 4D pressure and
temperatures. This example demonstrates the netCDF C++ API.
This is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: pres_temp_4D_wr.cpp,v 1.11 2007/01/19 12:52:13 ed Exp $
*/
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
// We are writing 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
// timesteps of data.
static const int NLVL = 2;
static const int NLAT = 6;
static const int NLON = 12;
static const int NREC = 2;
// These are used to construct some example data.
static const float SAMPLE_PRESSURE = 900.0;
static const float SAMPLE_TEMP = 9.0;
static const float START_LAT = 25.0;
static const float START_LON = -125.0;
// Return this code to the OS in case of failure.
static const int NC_ERR = 2;
int main()
{
// These arrays will store the latitude and longitude values.
float lats[NLAT],lons[NLON];
// These arrays will hold the data we will write out. We will
// only need enough space to hold one timestep of data; one record.
float pres_out[NLVL][NLAT][NLON];
float temp_out[NLVL][NLAT][NLON];
int i = 0;
// Create some pretend data. If this wasn't an example program, we
// would have some real data to write for example, model output.
for (int lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5. * lat;
for (int lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5. * lon;
for (int lvl = 0; lvl < NLVL; lvl++)
for (int lat = 0; lat < NLAT; lat++)
for (int lon = 0; lon < NLON; lon++)
{
pres_out[lvl][lat][lon] = SAMPLE_PRESSURE + i;
temp_out[lvl][lat][lon] = SAMPLE_TEMP + i++;
}
// Change the error behavior of the netCDF C++ API by creating an
// NcError object. Until it is destroyed, this NcError object will
// ensure that the netCDF C++ API returns error codes on any
// failure, prints an error message, and leaves any other error
// handling to the calling program. In the case of this example, we
// just exit with an NC_ERR error code.
NcError err(NcError::verbose_nonfatal);
// Create the file.
NcFile dataFile("pres_temp_4D.nc", NcFile::Replace);
// Check to see if the file was created.
if(!dataFile.is_valid())
return NC_ERR;
// Define the dimensions. NetCDF will hand back an ncDim object for
// each.
NcDim *lvlDim, *latDim, *lonDim, *recDim;
if (!(lvlDim = dataFile.add_dim("level", NLVL)))
return NC_ERR;
if (!(latDim = dataFile.add_dim("latitude", NLAT)))
return NC_ERR;
if (!(lonDim = dataFile.add_dim("longitude", NLON)))
return NC_ERR;
// Add an unlimited dimension...
if (!(recDim = dataFile.add_dim("time")))
return NC_ERR;
// Define the coordinate variables.
NcVar *latVar, *lonVar;
if (!(latVar = dataFile.add_var("latitude", ncFloat, latDim)))
return NC_ERR;
if (!(lonVar = dataFile.add_var("longitude", ncFloat, lonDim)))
return NC_ERR;
// Define units attributes for coordinate vars. This attaches a
// text attribute to each of the coordinate variables, containing
// the units.
if (!latVar->add_att("units", "degrees_north"))
return NC_ERR;
if (!lonVar->add_att("units", "degrees_east"))
return NC_ERR;
// Define the netCDF variables for the pressure and temperature
// data.
NcVar *presVar, *tempVar;
if (!(presVar = dataFile.add_var("pressure", ncFloat, recDim,
lvlDim, latDim, lonDim)))
return NC_ERR;
if (!(tempVar = dataFile.add_var("temperature", ncFloat, recDim,
lvlDim, latDim, lonDim)))
return NC_ERR;
// Define units attributes for data variables.
if (!presVar->add_att("units", "hPa"))
return NC_ERR;
if (!tempVar->add_att("units", "celsius"))
return NC_ERR;
// Write the coordinate variable data to the file.
if (!latVar->put(lats, NLAT))
return NC_ERR;
if (!lonVar->put(lons, NLON))
return NC_ERR;
// Write the pretend data. This will write our surface pressure and
// surface temperature data. The arrays only hold one timestep
// worth of data. We will just rewrite the same data for each
// timestep. In a real application, the data would change between
// timesteps.
for (int rec = 0; rec < NREC; rec++)
{
if (!presVar->put_rec(&pres_out[0][0][0], rec))
return NC_ERR;
if (!tempVar->put_rec(&temp_out[0][0][0], rec))
return NC_ERR;
}
// The file is automatically closed by the destructor. This frees
// up any internal netCDF resources associated with the file, and
// flushes any buffers.
cout << "*** SUCCESS writing example file pres_temp_4D.nc!" << endl;
return 0;
}
</pre>
<div class="node">
<a name="pres_temp_4D_rd.cpp"></a>
<a name="pres_005ftemp_005f4D_005frd_002ecpp"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#pres_005ftemp_005f4D_005fwr_002ecpp">pres_temp_4D_wr.cpp</a>,
Up: <a rel="up" accesskey="u" href="#pres_005ftemp_005f4D-in-C_002b_002b">pres_temp_4D in C++</a>
</div>
<h5 class="subsubsection">2.3.4.2 pres_temp_4D_rd.cpp</h5>
<pre class="example"> /* This is part of the netCDF package.
Copyright 2006 University Corporation for Atmospheric Research/Unidata.
See COPYRIGHT file for conditions of use.
This is an example which reads some 4D pressure and temperature
values. The data file read by this program is produced by the
companion program pres_temp_4D_wr.cpp. It is intended to illustrate
the use of the netCDF C++ API.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
Full documentation of the netCDF C++ API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
$Id: pres_temp_4D_rd.cpp,v 1.13 2007/02/14 20:59:21 ed Exp $
*/
#include <iostream>
#include <netcdfcpp.h>
using namespace std;
// We are writing 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
// timesteps of data.
static const int NLVL = 2;
static const int NLAT = 6;
static const int NLON = 12;
static const int NREC = 2;
// These are used to construct some example data.
static const float SAMPLE_PRESSURE = 900.0;
static const float SAMPLE_TEMP = 9.0;
static const float START_LAT = 25.0;
static const float START_LON = -125.0;
// Return this code to the OS in case of failure.
static const int NC_ERR = 2;
int main()
{
// These arrays will store the latitude and longitude values.
float lats[NLAT], lons[NLON];
// These arrays will hold the data we will read in. We will only
// need enough space to hold one timestep of data; one record.
float pres_in[NLVL][NLAT][NLON];
float temp_in[NLVL][NLAT][NLON];
// Change the error behavior of the netCDF C++ API by creating an
// NcError object. Until it is destroyed, this NcError object will
// ensure that the netCDF C++ API returns error codes on any
// failure, prints an error message, and leaves any other error
// handling to the calling program. In the case of this example, we
// just exit with an NC_ERR error code.
NcError err(NcError::verbose_nonfatal);
// Open the file.
NcFile dataFile("pres_temp_4D.nc", NcFile::ReadOnly);
// Check to see if the file was opened.
if(!dataFile.is_valid())
return NC_ERR;
// Get pointers to the latitude and longitude variables.
NcVar *latVar, *lonVar;
if (!(latVar = dataFile.get_var("latitude")))
return NC_ERR;
if (!(lonVar = dataFile.get_var("longitude")))
return NC_ERR;
// Get the lat/lon data from the file.
if (!latVar->get(lats, NLAT))
return NC_ERR;
if (!lonVar->get(lons, NLON))
return NC_ERR;
// Check the coordinate variable data.
for (int lat = 0; lat < NLAT; lat++)
if (lats[lat] != START_LAT + 5. * lat)
return NC_ERR;
for (int lon = 0; lon < NLON; lon++)
if (lons[lon] != START_LON + 5. * lon)
return NC_ERR;
// Get pointers to the pressure and temperature variables.
NcVar *presVar, *tempVar;
if (!(presVar = dataFile.get_var("pressure")))
return NC_ERR;
if (!(tempVar = dataFile.get_var("temperature")))
return NC_ERR;
// Read the data. Since we know the contents of the file we know
// that the data arrays in this program are the correct size to
// hold one timestep.
for (int rec = 0; rec < NREC; rec++)
{
// Read the data one record at a time.
if (!presVar->set_cur(rec, 0, 0, 0))
return NC_ERR;
if (!tempVar->set_cur(rec, 0, 0, 0))
return NC_ERR;
// Get 1 record of NLVL by NLAT by NLON values for each variable.
if (!presVar->get(&pres_in[0][0][0], 1, NLVL, NLAT, NLON))
return NC_ERR;
if (!tempVar->get(&temp_in[0][0][0], 1, NLVL, NLAT, NLON))
return NC_ERR;
// Check the data.
int i = 0;
for (int lvl = 0; lvl < NLVL; lvl++)
for (int lat = 0; lat < NLAT; lat++)
for (int lon = 0; lon < NLON; lon++)
if (pres_in[lvl][lat][lon] != SAMPLE_PRESSURE + i ||
temp_in[lvl][lat][lon] != SAMPLE_TEMP + i++)
return NC_ERR;
} // next record
// The file is automatically closed by the destructor. This frees
// up any internal netCDF resources associated with the file, and
// flushes any buffers.
cout << "*** SUCCESS reading example file pres_temp_4D.nc!" << endl;
return 0;
}
</pre>
<div class="node">
<a name="Useful-Functions"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#API_002dExtensions">API-Extensions</a>,
Previous: <a rel="previous" accesskey="p" href="#Examples">Examples</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">3 The Functions You Need in NetCDF-3</h2>
<p>The netCDF-3 C and Fortran APIs each have over 100 functions, but most
users need only a handful. Listed below are the essential netCDF
functions for four important tasks in netCDF: creating new files,
reading existing files, learning about a netCDF file of unknown
structure, and reading and writing subsets of data.
<p>In each case the functions are presented for each of the four language
APIs: C, Fortran 77, Fortran 90, and C++, with hyper-links to the
detailed documentation of each function.
<ul class="menu">
<li><a accesskey="1" href="#Creation">Creation</a>: Creating netCDF files, adding metadata.
<li><a accesskey="2" href="#Reading">Reading</a>: Reading netCDF files of known structure.
<li><a accesskey="3" href="#Inquiry-Functions">Inquiry Functions</a>: Learning about an unknown netCDF file.
<li><a accesskey="4" href="#Subsets">Subsets</a>: Reading and writing Subsets of data.
</ul>
<div class="node">
<a name="Creation"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Reading">Reading</a>,
Previous: <a rel="previous" accesskey="p" href="#Useful-Functions">Useful Functions</a>,
Up: <a rel="up" accesskey="u" href="#Useful-Functions">Useful Functions</a>
</div>
<h3 class="section">3.1 Creating New Files and Metadata, an Overview</h3>
<p><a name="index-creating-netCDF-files-33"></a>
To construct a netCDF file you need to:
<dl>
<dt><code>create the file</code><dd>Specify the name, optionally the format: classic (the default) or
64bit-offset.
<br><dt><code>define metadata</code><dd>Specify the names and types of dimensions, data variables, and
attributes.
<br><dt><code>write data</code><dd>Write arrays of data from program variables to the netCDF file. Arrays
of data may be written all at once, or in subsets.
<br><dt><code>close the file</code><dd>Close the file to flush all buffers to the disk and free all resources
allocated for this file.
</dl>
<ul class="menu">
<li><a accesskey="1" href="#Creation-in-C">Creation in C</a>
<li><a accesskey="2" href="#Creation-in-F77">Creation in F77</a>
<li><a accesskey="3" href="#Creation-in-F90">Creation in F90</a>
<li><a accesskey="4" href="#Creation-in-C_002b_002b">Creation in C++</a>
</ul>
<div class="node">
<a name="Creation-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Creation-in-F77">Creation in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#Creation">Creation</a>,
Up: <a rel="up" accesskey="u" href="#Creation">Creation</a>
</div>
<h4 class="subsection">3.1.1 Creating a NetCDF File in C</h4>
<p><a name="index-creating-files-in-C-34"></a><a name="index-nc_005fcreate-35"></a><a name="index-nc_005fdef_005fdim-36"></a><a name="index-nc_005fdef_005fvar-37"></a><a name="index-nc_005fput_005fatt-38"></a><a name="index-nc_005fenddef-39"></a><a name="index-nc_005fput_005fvara-40"></a><a name="index-nc_005fclose-41"></a>
Use nc_create to create a file. Then use nc_def_dim to define each
shared dimension. The data variables are then specified with
nc_def_var. Any attributes are added with nc_put_att. Finally, call
nc_enddef to tell the library that you are done defining the metadata,
and ready to start writing the data.
<p>After all data are written to the file, call nc_close to ensure that
all buffers are flushed, and any resources associated with the
open file are returned to the operating system.
<p>For a very simple example, See <a href="#simple_005fxy-in-C">simple_xy in C</a>.
<p>For a typical sequence of calls to the C versions of these functions,
see See <a href="netcdf-c.html#Creating">Creating a NetCDF Dataset</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fcreate">nc_create</a>
</td><td valign="top" width="50%">create a new netCDF file
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fdef_005fdim">nc_def_dim</a>
</td><td valign="top" width="50%">define a dimension
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fdef_005fvar">nc_def_var</a>
</td><td valign="top" width="50%">define a variable
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fput_005fatt_005f-type">nc_put_att_ type</a>
</td><td valign="top" width="50%">write attributes
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fenddef">nc_enddef</a>
</td><td valign="top" width="50%">leave define mode
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fput_005fvara_005f-type">nc_put_vara_ type</a>
</td><td valign="top" width="50%">write arrays of data
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-c.html#nc_005fclose">nc_close</a>
</td><td valign="top" width="50%">close a file
<br></td></tr></table>
<div class="node">
<a name="Creation-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Creation-in-F90">Creation in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#Creation-in-C">Creation in C</a>,
Up: <a rel="up" accesskey="u" href="#Creation">Creation</a>
</div>
<h4 class="subsection">3.1.2 Creating a NetCDF File in Fortran 77</h4>
<p><a name="index-creating-files-in-Fortran-42"></a><a name="index-NF_005fCREATE-43"></a><a name="index-NF_005fDEF_005fDIM-44"></a><a name="index-NF_005fDEF_005fVAR-45"></a><a name="index-NF_005fPUT_005fATT_005f-type-46"></a><a name="index-NF_005fENDDEF-47"></a><a name="index-NF_005fPUT_005fVARA-48"></a><a name="index-NF_005fCLOSE-49"></a>
Use NF_CREATE to create a file. Then use NF_DEF_DIM to define each
shared dimension. The data variables are then specified with
NF_DEF_VAR. Any attributes are added with NF_PUT_ATT. Finally, call
NF_ENDDEF to tell the library that you are done defining the metadata,
and ready to start writing the data.
<p>After all data are written to the file, call NF_CLOSE to ensure that
all buffers are flushed, and any resources associated with the
open file are returned to the operating system.
<p>For a typical sequence of calls see <a href="netcdf-f77.html#Creating">Creating a NetCDF Dataset</a>.
<p>Fortran users take note: the netCDF Fortran 77 API consists of
wrappers around the functions of the netCDF C library. There is no
Fortran 77 code in netCDF except for these wrappers, and tests to
ensure that the wrappers work.
<p>The name of each Fortran function shows the outline of the C function
it wraps (for example, NF_CREATE is a wrapper around nc_create).
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fCREATE">NF_CREATE</a>
</td><td valign="top" width="50%">create a new netCDF file
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fDEF_005fDIM">NF_DEF_DIM</a>
</td><td valign="top" width="50%">define a dimension
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fDEF_005fVAR">NF_DEF_VAR</a>
</td><td valign="top" width="50%">define a variable
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fPUT_005fATT_005f-type">NF_PUT_ATT_ type</a>
</td><td valign="top" width="50%">write an attribute
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fENDDEF">NF_ENDDEF</a>
</td><td valign="top" width="50%">end define mode
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fPUT_005fVARA_005f-type">NF_PUT_VARA_ type</a>
</td><td valign="top" width="50%">write arrays of data
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fCLOSE">NF_CLOSE</a>
</td><td valign="top" width="50%">close the netCDF file
<br></td></tr></table>
<div class="node">
<a name="Creation-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Creation-in-C_002b_002b">Creation in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#Creation-in-F77">Creation in F77</a>,
Up: <a rel="up" accesskey="u" href="#Creation">Creation</a>
</div>
<h4 class="subsection">3.1.3 Creating a NetCDF File in Fortran 90</h4>
<p><a name="index-creating-files-in-Fortran-50"></a><a name="index-NF90_005fCREATE-51"></a><a name="index-NF90_005fDEF_005fDIM-52"></a><a name="index-NF90_005fDEF_005fVAR-53"></a><a name="index-NF90_005fPUT_005fATT_005f-type-54"></a><a name="index-NF90_005fENDDEF-55"></a><a name="index-NF90_005fPUT_005fVARA-56"></a><a name="index-NF90_005fCLOSE-57"></a>
Use NF90_CREATE to create a file. Then use NF90_DEF_DIM to define each
shared dimension. The data variables are then specified with
NF90_DEF_VAR. Any attributes are added with NF90_PUT_ATT. Finally, call
NF90_ENDDEF to tell the library that you are done defining the metadata,
and ready to start writing the data.
<p>After all data are written to the file, call NF90_CLOSE to ensure that
all buffers are flushed, and any resources associated with the
open file are returned to the operating system.
<p>For a typical sequence of calls see <a href="netcdf-f90.html#Creating">Creating a NetCDF Dataset</a>.
<p>The netCDF Fortran 90 API calls the Fortran 77 API, which in
turn calls the netCDF C library.
<p>The name of each Fortran function shows the outline of the F77 function
it wraps (for example, NF90_CREATE is a wrapper around NF_CREATE). The F77
functions are, in turn, wrappers around the C functions.
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fCREATE">NF90_CREATE</a>
</td><td valign="top" width="50%">create a netCDF file
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fDEF_005fDIM">NF90_DEF_DIM</a>
</td><td valign="top" width="50%">define a dimension
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fDEF_005fVAR">NF90_DEF_VAR</a>
</td><td valign="top" width="50%">define a variable
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fPUT_005fATT_005f-type">NF90_PUT_ATT_ type</a>
</td><td valign="top" width="50%">write an attribute
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fENDDEF">NF90_ENDDEF</a>
</td><td valign="top" width="50%">end define mode
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fPUT_005fVARA_005f-type">NF90_PUT_VARA_ type</a>
</td><td valign="top" width="50%">write arrays of data
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fCLOSE">NF90_CLOSE</a>
</td><td valign="top" width="50%">close the netCDF file
<br></td></tr></table>
<div class="node">
<a name="Creation-in-C++"></a>
<a name="Creation-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Creation-in-F90">Creation in F90</a>,
Up: <a rel="up" accesskey="u" href="#Creation">Creation</a>
</div>
<h4 class="subsection">3.1.4 Creating a NetCDF File in C++</h4>
<p><a name="index-creating-files-in-C_002b_002b-58"></a>
Create an instance of the NcFile class to create a netCDF file. Use
its add_dim and add_var methods to add dimensions and variables. The
add_att method is available for both NcFile and NcVar.
<p>Use the NcError class to specify error handling behavior.
<p>For an example creating a simple file see <a href="#simple_005fxy_005fwr_002ecpp">simple_xy_wr.cpp</a>. For
a more complex example see <a href="#pres_005ftemp_005f4D_005fwr_002ecpp">pres_temp_4D_wr.cpp</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-cxx.html#Class-NcFile">Class NcFile</a>
</td><td valign="top" width="50%">a C++ class to manipulate netCDF files
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-cxx.html#Class-NcDim">Class NcDim</a>
</td><td valign="top" width="50%">a C++ class to manipulate netCDF dimensions
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-cxx.html#Class-NcVar">Class NcVar</a>
</td><td valign="top" width="50%">a C++ class to manipulate netCDF variables
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-cxx.html#Class-NcAtt">Class NcAtt</a>
</td><td valign="top" width="50%">a C++ class to manipulate netCDF attributes
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-cxx.html#Class-NcError">Class NcError</a>
</td><td valign="top" width="50%">a C++ class to control netCDF error handling
<br></td></tr></table>
<div class="node">
<a name="Reading"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Inquiry-Functions">Inquiry Functions</a>,
Previous: <a rel="previous" accesskey="p" href="#Creation">Creation</a>,
Up: <a rel="up" accesskey="u" href="#Useful-Functions">Useful Functions</a>
</div>
<h3 class="section">3.2 Reading NetCDF Files of Known Structure</h3>
<p><a name="index-reading-netCDF-files-of-known-structure-59"></a><a name="index-inquiry-functions-60"></a>
To read a netCDF file of known structure, you need to:
<dl>
<dt><code>open the file</code><dd>Specify the file name and whether you want read-write or read-only
access.
<br><dt><code>read variable or attribute data</code><dd>Read the data or attributes of interest.
<br><dt><code>close the file</code><dd>Release all resources associated with this file.
</dl>
<p>Use ncdump to learn the structure of a file (use the -h option). For
more information about ncdump see <a href="netcdf.html#NetCDF-Utilities">NetCDF Utilities</a>.
<h4 class="subsection">3.2.1 Numbering of NetCDF IDs</h4>
<p>In C, Fortran 77, and Fortran 90, netCDF objects are identified by an
integer: the ID. NetCDF functions use this ID to identify the
object. It's helpful for the programmer to understand these IDs.
<p>Open data files, dimensions, variables, and attributes are each
numbered independently, and are always numbered in the order in which
they were defined. (They also appear in this order in ncdump output.)
Numbering starts with 0 in C, and 1 in Fortran 77/90.
<p>For example, the first variable defined in a file will have an ID of 0
in C programs, and 1 in Fortran programs, and functions that apply to
a variable will need to know the ID of the variable you mean.
<p>(The numbering of files is an exception: file IDs are assigned by the
operating system when a file is opened, and are not permanently
associated with the file. IDs for netCDF dimensions and variables are
persistent, but deleting an attribute changes subsequent attribute
numbers.)
<p>Although netCDF refers to everything by an integer id (varid, dimid,
attnum), there are inquiry functions which, given a name, will return
an ID. For example, in the C API, nc_inq_varid will take a character
string (the name), and give back the ID of the variable of that
name. The variable ID is then used in subsequent calls (to read the data,
for example).
<p>Other inquiry functions exist to further describe the
file. (see <a href="#Inquiry-Functions">Inquiry Functions</a>).
<ul class="menu">
<li><a accesskey="1" href="#Reading-in-C">Reading in C</a>
<li><a accesskey="2" href="#Reading-in-F77">Reading in F77</a>
<li><a accesskey="3" href="#Reading-in-F90">Reading in F90</a>
<li><a accesskey="4" href="#Reading-in-C_002b_002b">Reading in C++</a>
</ul>
<div class="node">
<a name="Reading-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Reading-in-F77">Reading in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#Reading">Reading</a>,
Up: <a rel="up" accesskey="u" href="#Reading">Reading</a>
</div>
<h4 class="subsection">3.2.2 Reading a Known NetCDF File in C</h4>
<p><a name="index-reading-netCDF-files-with-C-61"></a><a name="index-nc_005fopen-62"></a><a name="index-nc_005fget_005fatt-63"></a><a name="index-nc_005fget_005fvara-64"></a>
For a typical sequence of calls to these C functions see <a href="netcdf-c.html#Reading-Known">Reading a NetCDF Dataset with Known Names</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005fopen">nc_open</a>
</td><td valign="top" width="32%">open a netCDF file
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005fget_005fatt">nc_get_att</a>
</td><td valign="top" width="32%">read an attribute
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005fget_005fvara_005f-type">nc_get_vara_ type</a>
</td><td valign="top" width="32%">read arrays of data
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005fclose">nc_close</a>
</td><td valign="top" width="32%">close the file
<br></td></tr></table>
<div class="node">
<a name="Reading-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Reading-in-F90">Reading in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#Reading-in-C">Reading in C</a>,
Up: <a rel="up" accesskey="u" href="#Reading">Reading</a>
</div>
<h4 class="subsection">3.2.3 Reading a Known NetCDF File in Fortran 77</h4>
<p><a name="index-reading-netCDF-files-with-Fortran-77-65"></a><a name="index-NF_005fOPEN-66"></a><a name="index-NF_005fGET_005fATT-67"></a><a name="index-NF_005fGET_005fVARA-68"></a>
For a typical sequence of calls to these functions see <a href="netcdf-f77.html#Reading-Known">Reading a NetCDF Dataset with Known Names</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fOPEN">NF_OPEN</a>
</td><td valign="top" width="50%">open a netCDF file
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fGET_005fATT">NF_GET_ATT</a>
</td><td valign="top" width="50%">read an attribute
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fGET_005fVARA_005f-type">NF_GET_VARA_ type</a>
</td><td valign="top" width="50%">read arrays of data
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f77.html#NF_005fCLOSE">NF_CLOSE</a>
</td><td valign="top" width="50%">close the file
<br></td></tr></table>
<div class="node">
<a name="Reading-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Reading-in-C_002b_002b">Reading in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#Reading-in-F77">Reading in F77</a>,
Up: <a rel="up" accesskey="u" href="#Reading">Reading</a>
</div>
<h4 class="subsection">3.2.4 Reading a Known NetCDF File in Fortran 90</h4>
<p><a name="index-reading-netCDF-files-with-Fortran-90-69"></a><a name="index-NF90_005fOPEN-70"></a><a name="index-NF90_005fGET_005fATT-71"></a><a name="index-NF90_005fGET_005fVARA-72"></a>
For a typical sequence of calls to these functions see <a href="netcdf-f90.html#Reading-Known">Reading a NetCDF Dataset with Known Names</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fOPEN">NF90_OPEN</a>
</td><td valign="top" width="50%">open a netCDF file
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fGET_005fATT">NF90_GET_ATT</a>
</td><td valign="top" width="50%">read an attribute
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fGET_005fVARA">NF90_GET_VARA</a>
</td><td valign="top" width="50%">read arrays of data
<p><br></td></tr><tr align="left"><td valign="top" width="50%"><a href="netcdf-f90.html#NF90_005fCLOSE">NF90_CLOSE</a>
</td><td valign="top" width="50%">close the file
<br></td></tr></table>
<div class="node">
<a name="Reading-in-C++"></a>
<a name="Reading-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Reading-in-F90">Reading in F90</a>,
Up: <a rel="up" accesskey="u" href="#Reading">Reading</a>
</div>
<h4 class="subsection">3.2.5 Reading a Known NetCDF File in C++</h4>
<p><a name="index-reading-netCDF-files-with-C_002b_002b-73"></a><a name="index-NcFile-74"></a>
<p><table summary="">
<tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcFile">Class NcFile</a>
</td><td valign="top" width="32%">a C++ class to manipulate netCDF files
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcDim">Class NcDim</a>
</td><td valign="top" width="32%">a C++ class to manipulate netCDF dimensions
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcVar">Class NcVar</a>
</td><td valign="top" width="32%">a C++ class to manipulate netCDF variables
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcAtt">Class NcAtt</a>
</td><td valign="top" width="32%">a C++ class to manipulate netCDF attributes
<br></td></tr></table>
<div class="node">
<a name="Inquiry-Functions"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Subsets">Subsets</a>,
Previous: <a rel="previous" accesskey="p" href="#Reading">Reading</a>,
Up: <a rel="up" accesskey="u" href="#Useful-Functions">Useful Functions</a>
</div>
<h3 class="section">3.3 Reading NetCDF Files of Unknown Structure</h3>
<p>Perhaps you would like to write your software to handle more general
cases, so that you don't have to adjust your source every time the
grid size changes, or a variable is added to the file.
<p>There are inquiry functions that let you find out everything you need
to know about a file. These functions contain “inq” or “INQ” in
their names.
<p>Using the inquiry functions, it is possible to write code that will
read and understand any netCDF file, whatever its contents. (For
example, ncdump does just that.)
<ul class="menu">
<li><a accesskey="1" href="#Inquiry-in-C">Inquiry in C</a>
<li><a accesskey="2" href="#Inquiry-in-F77">Inquiry in F77</a>
<li><a accesskey="3" href="#Inquiry-in-F90">Inquiry in F90</a>
<li><a accesskey="4" href="#Inquiry-in-C_002b_002b">Inquiry in C++</a>
</ul>
<div class="node">
<a name="Inquiry-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Inquiry-in-F77">Inquiry in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#Inquiry-Functions">Inquiry Functions</a>,
Up: <a rel="up" accesskey="u" href="#Inquiry-Functions">Inquiry Functions</a>
</div>
<h4 class="subsection">3.3.1 Inquiry in C</h4>
<p>First use nc_inq, which will tell you how many variables and global
attributes there are in the file.
<p>Start with global attribute 0, and proceed to natts - 1, the number of
global attributes minus one. The nc_inq_att function will tell you the
name, type, and length of each global attribute.
<p>Then start with dimid 0, and proceed to dimid ndims - 1, calling
nc_inq_dim. This will tell you the name and length of each dimension,
and whether it is unlimited.
<p>Then start with varid 0, and proceed to varid nvars - 1, calling
nc_inq_var. This will tell you the number of dimensions of this
variable, and their associated IDs. It will also get the name and type
of this variable, and whether there are any attributes attached. If
there are attributes attached, use the nc_inq_att function to get
their names, types, and lengths.
<p>(To read an attribute, use the appropriate nc_get_att_<TYPE> function,
like nc_get_att_int() to get the data from an attribute that is an
array of integers.)
<p>There are also functions that return an item's ID, given its
name. To find IDs from the names, use functions nc_inq_dimid,
nc_inq_attnum, and nc_inq_varid.
<p>For a typical sequence of calls to these functions see <a href="netcdf-c.html#Reading-Unknown">Reading a netCDF Dataset with Unknown Names</a>.
<h5 class="subsubsection">3.3.1.1 NULL Parameters in Inquiry Functions</h5>
<p>With any of the C inquiry functions, a NULL pointer can be used to
ignore a return parameter. Consider the
nc_inq function:
<pre class="example"> EXTERNL int
nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp);
</pre>
<p>If you call this with NULL for the last three parameters, you can
learn the number of dimensions without bothering about the number of
variables, number of global attributes, and the ID of the unlimited
dimension.
<p>For further convenience, we provide functions like nc_inq_ndims, which
only finds the number of dimensions, exactly as if you had called
nc_inq, with NULLs in all parameters except ndimsp. (In fact, this is
just what the nc_inq_ndims functions does).
<p><table summary="">
<tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq">nc_inq</a>
</td><td valign="top" width="17%">Find number of dimensions, variables, and global attributes, and the unlimited dimid.
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005fatt">nc_inq_att</a>
</td><td valign="top" width="17%">Find attribute name, type, and length.
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005fdim-Family">nc_inq_dim Family</a>
</td><td valign="top" width="17%">Find dimension name and length.
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005fvar">nc_inq_var</a>
</td><td valign="top" width="17%">Find variable name, type, num dimensions, dim IDs, and num attributes.
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005fdimid">nc_inq_dimid</a>
</td><td valign="top" width="17%">Find dimension ID from its name.
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005fvarid">nc_inq_varid</a>
</td><td valign="top" width="17%">Find variable ID from its name.
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005fformat">nc_inq_format</a>
</td><td valign="top" width="17%">Find file format: classic or 64-bit offset
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-c.html#nc_005finq_005flibvers">nc_inq_libvers</a>
</td><td valign="top" width="17%">Find the netCDF version. (Currently 4.1.3).
<br></td></tr></table>
<div class="node">
<a name="Inquiry-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Inquiry-in-F90">Inquiry in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#Inquiry-in-C">Inquiry in C</a>,
Up: <a rel="up" accesskey="u" href="#Inquiry-Functions">Inquiry Functions</a>
</div>
<h4 class="subsection">3.3.2 Inquiry in Fortran 77</h4>
<p>First use NF_INQ, which will tell you how many variables and global
attributes there are in the file. Then start with varid 1, and proceed
to varid nvars, calling NF_INQ_VAR.
<p>For a typical sequence of calls to these functions see <a href="netcdf-f77.html#Reading-Unknown">Reading a netCDF Dataset with Unknown Names</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ">NF_INQ</a>.
</td><td valign="top" width="27%">Find number of dimensions, variables, and global attributes, and the unlimited dimid.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fDIM">NF_INQ_DIM</a>.
</td><td valign="top" width="27%">Find dimension name and length.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fVARID">NF_INQ_VARID</a>.
</td><td valign="top" width="27%">Find variable ID from its name.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fVAR">NF_INQ_VAR</a>.
</td><td valign="top" width="27%">Find variable name, type, num dimensions, dim IDs, and num attributes.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fDIMID">NF_INQ_DIMID</a>.
</td><td valign="top" width="27%">Find dimension ID from its name.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fDIM">NF_INQ_DIM</a>.
</td><td valign="top" width="27%">Find dimension name and length.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fATT">NF_INQ_ATT</a>.
</td><td valign="top" width="27%">Find attribute name, type, and length.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fFORMAT">NF_INQ_FORMAT</a>.
</td><td valign="top" width="27%">Find file format: classic or 64-bit offset
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f77.html#NF_005fINQ_005fLIBVERS">NF_INQ_LIBVERS</a>.
</td><td valign="top" width="27%">Find the netCDF version. (Currently 4.1.3).
<br></td></tr></table>
<div class="node">
<a name="Inquiry-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Inquiry-in-C_002b_002b">Inquiry in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#Inquiry-in-F77">Inquiry in F77</a>,
Up: <a rel="up" accesskey="u" href="#Inquiry-Functions">Inquiry Functions</a>
</div>
<h4 class="subsection">3.3.3 Inquiry in Fortran 90</h4>
<p>First use NF90_INQ, which will tell you how many variables and global
attributes there are in the file. Then start with varid 1, and proceed
to varid nvars, calling NF90_INQ_VAR.
<p>For a typical sequence of calls to these functions, see See <a href="netcdf-f90.html#Reading-Unknown">Reading a netCDF Dataset with Unknown Names</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ">NF90_INQ</a>.
</td><td valign="top" width="27%">Find number of dimensions, variables, and global attributes, and the unlimited dimid.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fDIM">NF90_INQ_DIM</a>.
</td><td valign="top" width="27%">Find dimension name and length.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fVARID">NF90_INQ_VARID</a>.
</td><td valign="top" width="27%">Find variable ID from its name.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fVAR">NF90_INQ_VAR</a>.
</td><td valign="top" width="27%">Find variable name, type, num dimensions, dim IDs, and num attributes.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fDIMID">NF90_INQ_DIMID</a>.
</td><td valign="top" width="27%">Find dimension ID from its name.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fDIM">NF90_INQ_DIM</a>.
</td><td valign="top" width="27%">Find dimension name and length.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fATT">NF90_INQ_ATT</a>.
</td><td valign="top" width="27%">Find attribute name, type, and length.
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fFORMAT">NF90_INQ_FORMAT</a>.
</td><td valign="top" width="27%">Find file format: classic or 64-bit offset
<p><br></td></tr><tr align="left"><td valign="top" width="27%"><a href="netcdf-f90.html#NF90_005fINQ_005fLIBVERS">NF90_INQ_LIBVERS</a>.
</td><td valign="top" width="27%">Find the netCDF version. (Currently 4.1.3).
<br></td></tr></table>
<div class="node">
<a name="Inquiry-in-C++"></a>
<a name="Inquiry-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Inquiry-in-F90">Inquiry in F90</a>,
Up: <a rel="up" accesskey="u" href="#Inquiry-Functions">Inquiry Functions</a>
</div>
<h4 class="subsection">3.3.4 Inquiry Functions in the C++ API</h4>
<p><table summary="">
<tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcFile">Class NcFile</a>
</td><td valign="top" width="17%">a C++ class to manipulate netCDF files
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcDim">Class NcDim</a>
</td><td valign="top" width="17%">a C++ class to manipulate netCDF dimensions
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcVar">Class NcVar</a>
</td><td valign="top" width="17%">a C++ class to manipulate netCDF variables
<p><br></td></tr><tr align="left"><td valign="top" width="17%"><a href="netcdf-cxx.html#Class-NcAtt">Class NcAtt</a>
</td><td valign="top" width="17%">a C++ class to manipulate netCDF attributes
<br></td></tr></table>
<div class="node">
<a name="Subsets"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Inquiry-Functions">Inquiry Functions</a>,
Up: <a rel="up" accesskey="u" href="#Useful-Functions">Useful Functions</a>
</div>
<h3 class="section">3.4 Reading and Writing Subsets of Data</h3>
<p>Usually users are interested in reading or writing subsets of
variables in a netCDF data file. The netCDF APIs provide a variety
of functions for this purpose.
<p>In the simplest case, you will use the same type for both file and
in-memory storage, but in some cases you may wish to use different
types. For example, you might have a netCDF file that contains
integer data, and you wish to read it into floating-point storage,
converting the data as it is read. The same sort of type conversion
can be done when writing the data.
<p>To convert to a type while reading data, use the appropriate
nc_get_vara_<TYPE> or NF_GET_VARA_<TYPE> function. For example, the C
function nc_get_vara_float(), and the Fortran function NF_GET_VARA_REAL
will read netCDF data of any numeric type into a floating-point array, automatically
converting each element to the desired type.
<p>To convert from a type while writing data, use the appropriate
nc_put_vara_<TYPE> or NF_PUT_VARA_<TYPE> function. For example, the C
function nc_put_vara_float(), and the Fortran function NC_PUT_VARA_REAL
will write floating-point data into netCDF arrays, automatically
converting each element of the array to the type of the netCDF variable.
<p>The <TYPE> in the function name refers to the type of the in-memory
data, in both cases. They type of the file data is determined when the
netCDF variable is defined.
<ul class="menu">
<li><a accesskey="1" href="#Subsetting-in-C">Subsetting in C</a>
<li><a accesskey="2" href="#Subsetting-in-F77">Subsetting in F77</a>
<li><a accesskey="3" href="#Subsetting-in-F90">Subsetting in F90</a>
<li><a accesskey="4" href="#Subsetting-in-C_002b_002b">Subsetting in C++</a>
</ul>
<div class="node">
<a name="Subsetting-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Subsetting-in-F77">Subsetting in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#Subsets">Subsets</a>,
Up: <a rel="up" accesskey="u" href="#Subsets">Subsets</a>
</div>
<h4 class="subsection">3.4.1 Reading and Writing Subsets of Data in C</h4>
<p><a name="index-nc_005fget_005fvar1-75"></a><a name="index-nc_005fget_005fvars-76"></a><a name="index-nc_005fget_005fvarm-77"></a><a name="index-nc_005fput_005fvar1-78"></a><a name="index-nc_005fput_005fvars-79"></a><a name="index-nc_005fput_005fvarm-80"></a>
The type of the data may be automatically converted on read or
write. For more information about type conversion see <a href="netcdf-c.html#Type-Conversion">Type Conversion</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%">Read the entire variable at once
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fget_005fvar_005f-type">nc_get_var_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write the entire variable at once
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fput_005fvar_005f-type">nc_put_var_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read just one value
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fget_005fvar1_005f-type">nc_get_var1_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write just one value
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fput_005fvar1_005f-type">nc_put_var1_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read an array subset
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fget_005fvara_005f-type">nc_get_vara_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write an array subset
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fput_005fvara_005f-type">nc_put_vara_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read an array with strides
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fget_005fvars_005f-type">nc_get_vars_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write an array with strides
</td><td valign="top" width="25%"><a href="netcdf-c.html#nc_005fput_005fvars_005f-type">nc_put_vars_ type</a>
<!-- @item Read an array with strides and mapping -->
<!-- @tab @ref{nc_get_varm_ type,,, netcdf-c, @value{c-man}} -->
<!-- @item Write an array with strides and mapping -->
<!-- @tab @ref{nc_put_varm_ type,,, netcdf-c, @value{c-man}} -->
<br></td></tr></table>
<div class="node">
<a name="Subsetting-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Subsetting-in-F90">Subsetting in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#Subsetting-in-C">Subsetting in C</a>,
Up: <a rel="up" accesskey="u" href="#Subsets">Subsets</a>
</div>
<h4 class="subsection">3.4.2 Reading and Writing Subsets of Data in Fortran 77</h4>
<p><a name="index-NF_005fGET_005fVAR1-81"></a><a name="index-NF_005fGET_005fVARS-82"></a><a name="index-NF_005fGET_005fVARM-83"></a><a name="index-NF_005fPUT_005fVAR1-84"></a><a name="index-NF_005fPUT_005fVARS-85"></a><a name="index-NF_005fPUT_005fVARM-86"></a>
The type of the data may be automatically converted on read or
write. For more information about type conversion see <a href="netcdf-f77.html#Type-Conversion">Type Conversion</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%">Read the entire variable at once
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fGET_005fVAR_005f-type">NF_GET_VAR_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write the entire variable at once
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fPUT_005fVAR_005f-type">NF_PUT_VAR_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read just one value
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fGET_005fVAR1_005f-type">NF_GET_VAR1_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write just one value
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fPUT_005fVAR1_005f-type">NF_PUT_VAR1_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read an array subset
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fGET_005fVARA_005f-type">NF_GET_VARA_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write an array subset
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fPUT_005fVARA_005f-type">NF_PUT_VARA_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read an array with strides
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fGET_005fVARS_005f-type">NF_GET_VARS_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write an array with strides
</td><td valign="top" width="25%"><a href="netcdf-f77.html#NF_005fPUT_005fVARS_005f-type">NF_PUT_VARS_ type</a>
<!-- @item Read an array with strides and mapping -->
<!-- @tab @ref{NF_GET_VARM_ type,,, netcdf-f77, @value{f77-man}} -->
<!-- @item Write an array with strides and mapping -->
<!-- @tab @ref{NF_PUT_VARM_ type,,, netcdf-f77, @value{f77-man}} -->
<br></td></tr></table>
<div class="node">
<a name="Subsetting-in-F90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Subsetting-in-C_002b_002b">Subsetting in C++</a>,
Previous: <a rel="previous" accesskey="p" href="#Subsetting-in-F77">Subsetting in F77</a>,
Up: <a rel="up" accesskey="u" href="#Subsets">Subsets</a>
</div>
<h4 class="subsection">3.4.3 Reading and Writing Subsets of Data in Fortran 90</h4>
<p><a name="index-NF90_005fGET_005fVAR1-87"></a><a name="index-NF90_005fGET_005fVARS-88"></a><a name="index-NF90_005fGET_005fVARM-89"></a><a name="index-NF90_005fPUT_005fVAR1-90"></a><a name="index-NF90_005fPUT_005fVARS-91"></a><a name="index-NF90_005fPUT_005fVARM-92"></a>
The type of the data may be automatically converted on read or
write. For more information about type conversion see <a href="netcdf-f90.html#Type-Conversion">Type Conversion</a>.
<p><table summary="">
<tr align="left"><td valign="top" width="50%">Read the entire variable at once
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fGET_005fVAR_005f-type">NF90_GET_VAR_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write the entire variable at once
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fPUT_005fVAR_005f-type">NF90_PUT_VAR_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read just one value
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fGET_005fVAR1_005f-type">NF90_GET_VAR1_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write just one value
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fPUT_005fVAR1_005f-type">NF90_PUT_VAR1_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read an array subset
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fGET_005fVARA_005f-type">NF90_GET_VARA_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write an array subset
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fPUT_005fVARA_005f-type">NF90_PUT_VARA_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Read an array with strides
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fGET_005fVARS_005f-type">NF90_GET_VARS_ type</a>
<p><br></td></tr><tr align="left"><td valign="top" width="50%">Write an array with strides
</td><td valign="top" width="25%"><a href="netcdf-f90.html#NF90_005fPUT_005fVARS_005f-type">NF90_PUT_VARS_ type</a>
<!-- @item Read an array with strides and mapping -->
<!-- @tab @ref{NF90_GET_VARM_ type,,, netcdf-f90, @value{f90-man}} -->
<!-- @item Write an array with strides and mapping -->
<!-- @tab @ref{NF90_PUT_VARM_ type,,, netcdf-f90, @value{f90-man}} -->
<br></td></tr></table>
<div class="node">
<a name="Subsetting-in-C++"></a>
<a name="Subsetting-in-C_002b_002b"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Subsetting-in-F90">Subsetting in F90</a>,
Up: <a rel="up" accesskey="u" href="#Subsets">Subsets</a>
</div>
<h4 class="subsection">3.4.4 Reading and Writing Subsets of Data in C++</h4>
<p>To read a record of data at a time, use the set_cur method of the
NcVar class to set the number of the record of interest, and then use
the get method to read the record.
<p><table summary="">
<tr align="left"><td valign="top" width="50%"><a href="netcdf-cxx.html#Class-NcVar">Class NcVar</a>
</td><td valign="top" width="25%">a C++ class to manipulate netCDF variables, use the set_cur and
get methods to read records from a file.
<br></td></tr></table>
<div class="node">
<a name="API-Extensions"></a>
<a name="API_002dExtensions"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>,
Previous: <a rel="previous" accesskey="p" href="#Useful-Functions">Useful Functions</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">4 API Extensions Introduced with NetCDF-4</h2>
<p>NetCDF-4 includes many advanced features. These features are only
available when working with files created in the netCDF format. (That
is, HDF5 files, created by netCDF, or simple-model HDF5 files).
<ul class="menu">
<li><a accesskey="1" href="#Interoperability">Interoperability</a>: Reading and writing HDF5 files.
<li><a accesskey="2" href="#Multiple_002dUnlimited_002dDimensions">Multiple-Unlimited-Dimensions</a>: Use more than one unlimited dimension.
<li><a accesskey="3" href="#Groups">Groups</a>: Organizing data hierarchically.
<li><a accesskey="4" href="#Compound_002dTypes">Compound-Types</a>: Creating data type like C structs.
<li><a accesskey="5" href="#Opaque_002dTypes">Opaque-Types</a>: Creating a data type of known size.
<li><a accesskey="6" href="#VLEN_002dType">VLEN-Type</a>: Variable length arrays.
<li><a accesskey="7" href="#Strings">Strings</a>: Storing strings of data.
<li><a accesskey="8" href="#New_002dinq_002dFunctions">New-inq-Functions</a>: Functions to help explore a file.
<li><a accesskey="9" href="#Parallel">Parallel</a>: How to get parallel I/O.
<li><a href="#Future">Future</a>: What's coming next!
</ul>
<div class="node">
<a name="Interoperability"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Multiple_002dUnlimited_002dDimensions">Multiple-Unlimited-Dimensions</a>,
Previous: <a rel="previous" accesskey="p" href="#API_002dExtensions">API-Extensions</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.1 Interoperability with HDF5</h3>
<p>NetCDF-4 allows some interoperability with HDF5.
<h4 class="subsection">4.1.1 Reading and Editing NetCDF-4 Files with HDF5</h4>
<p>The HDF5 Files produced by netCDF-4 are perfectly respectable HDF5
files, and can be read by any HDF5 application.
<p>NetCDF-4 relies on several new features of HDF5, including dimension
scales. The HDF5 dimension scales feature adds a bunch of attributes
to the HDF5 file to keep track of the dimension information.
<p>It is not just wrong, but wrong-headed, to modify these attributes
except with the HDF5 dimension scale API. If you do so, then you will
deserve what you get, which will be a mess.
<p>Additionally, netCDF stores some extra information for dimensions
without dimension scale information. (That is, a dimension without an
associated coordinate variable). So HDF5 users should not write data
to a netCDF-4 file which extends any unlimited dimension.
<p>Also there are some types allowed in HDF5, but not allowed in
netCDF-4 (for example the time type). Using any such type in a
netCDF-4 file will cause the file to become unreadable to netCDF-4. So
don't do it.
<p>NetCDF-4 ignores all HDF5 references. Can't make head nor tail of
them. Also netCDF-4 assumes a strictly hierarchical group
structure. No looping, you weirdo!
<p>Attributes can be added (they must be one of the netCDF-4 types),
modified, or even deleted, in HDF5.
<h4 class="subsection">4.1.2 Reading and Editing HDF5 Files with NetCDF-4</h4>
<p>Assuming a HDF5 file is written in accordance with the netCDF-4 rules
(i.e. no strange types, no looping groups), and assuming that *every*
dataset has a dimension scale attached to each dimension, the netCDF-4
API can be used to read and edit the file.
<p>In HDF5 (version 1.8.0 and later), dimension scales are (generally) 1D
datasets, that hold dimension data. A multi-dimensional dataset can
then attach a dimension scale to any or all of its dimensions. For
example, a user might have 1D dimension scales for lat and lon, and a
2D dataset which has lat attached to the first dimension, and lon to
the second.
<p>Dimension scales are vital to netCDF-4, which uses shared
dimensions. If you want to read a HDF5 file with netCDF-4, it must
use dimension scales, and one dimension scale must be attached to each
dimension of every dataset in the file.
<div class="node">
<a name="Multiple-Unlimited-Dimensions"></a>
<a name="Multiple_002dUnlimited_002dDimensions"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Groups">Groups</a>,
Previous: <a rel="previous" accesskey="p" href="#Interoperability">Interoperability</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.2 Multiple Unlimited Dimensions</h3>
<p>With classic and 64-bit offset netCDF files, each variable may use at
most one unlimited dimension. With netCDF-4 format files, this
restriction is lifted.
<p>Simply define as many unlimited dimensions as you wish, and use them
in a variable. When data are written to that variable, the dimensions
will be expanded as needed.
<div class="node">
<a name="Groups"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Compound_002dTypes">Compound-Types</a>,
Previous: <a rel="previous" accesskey="p" href="#Multiple_002dUnlimited_002dDimensions">Multiple-Unlimited-Dimensions</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.3 Groups</h3>
<p>NetCDF-4 files can store attributes, variables, and dimensions in
hierarchical groups.
<p>This allows the user to create a structure much like a Unix file
system. In netCDF, each group gets an ncid. Opening or creating a
file returns the ncid for the root group (which is named
“/”). Groups can be added with the nc_def_grp function. Get the
number of groups, and their ncids, with the nc_inq_grps function.
<p>Dimensions are scoped such that they are visible to all child
groups. For example, you can define a dimension in the root group, and
use its dimension id when defining a variable in a sub-group.
<p>Attributes defined as NC_GLOBAL apply to the group, not the entire
file.
<p>The degenerate case, in which only the root group is used, corresponds
exactly with the classic data mode, before groups were introduced.
<div class="node">
<a name="Compound-Types"></a>
<a name="Compound_002dTypes"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Opaque_002dTypes">Opaque-Types</a>,
Previous: <a rel="previous" accesskey="p" href="#Groups">Groups</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.4 Compound Types</h3>
<p>In netCDF-4 files it's possible to create a data type which
corresponds to a C struct. These are known as “compound” types
(following HDF5 nomenclature).
<p>That is, a netCDF compound type is a data structure which contains an
arbitrary collection of other data types, including other compound
types.
<p>To define a new compound type, use nc_def_compound. Then call
nc_insert_compound for each type within the compound type.
<p>Read and write arrays of compound data with the nc_get_vara and
nc_put_vara functions. These functions were actually part of the
netCDF-2 API, brought out of semi-retirement to handle user-defined
types in netCDF-4.
<div class="node">
<a name="Opaque-Types"></a>
<a name="Opaque_002dTypes"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#VLEN_002dType">VLEN-Type</a>,
Previous: <a rel="previous" accesskey="p" href="#Compound_002dTypes">Compound-Types</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.5 Opaque Types</h3>
<p>Store blobs of bits in opaque types. Create an opaque type with
nc_def_opaque. Read and write them with nc_get_vara/nc_put_vara.
<div class="node">
<a name="VLEN-Type"></a>
<a name="VLEN_002dType"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Strings">Strings</a>,
Previous: <a rel="previous" accesskey="p" href="#Opaque_002dTypes">Opaque-Types</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.6 Variable Length Arrays (VLEN)</h3>
<p>Create a VLEN type to store variable length arrays of a known base
type. Use nc_def_vlen to define a VLEN type, read and write them with
nc_get_vara/nc_put_vara.
<div class="node">
<a name="Strings"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#New_002dinq_002dFunctions">New-inq-Functions</a>,
Previous: <a rel="previous" accesskey="p" href="#VLEN_002dType">VLEN-Type</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.7 Strings</h3>
<p>Use the NC_STRING type to store arrays of strings. Read and write them
with nc_get_vara/nc_put_vara.
<div class="node">
<a name="New-inq-Functions"></a>
<a name="New_002dinq_002dFunctions"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Parallel">Parallel</a>,
Previous: <a rel="previous" accesskey="p" href="#Strings">Strings</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.8 New Inquiry Functions</h3>
<p>There are many new inquiry functions to allow a program to navigate a
completely unknown netCDF file.
<p>To find the number To find all the dimensions visible from a group, use nc_inq_dimids.
<div class="node">
<a name="Parallel"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Future">Future</a>,
Previous: <a rel="previous" accesskey="p" href="#New_002dinq_002dFunctions">New-inq-Functions</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.9 Parallel I/O with NetCDF</h3>
<p>Parallel I/O allows many processes to read/write netCDF data at the
same time. Used properly, it allows users to overcome I/O bottlenecks
in high performance computing environments.
<h4 class="subsection">4.9.1 Parallel I/O Choices for NetCDF Users</h4>
<p>Parallel read-only access can be achieved netCDF files using the
netCDF C/Fortran library. Each process can run a copy of the netCDF
library and open and read any subsets of the data in the file. This
sort of “fseek parallelism” will break down dramatically for any
kind of writing.
<p>There are two methods available to users for read/write parallel I/O
netCDF-4 or the parallel netCDF package from
Argonne/Northwestern. Unfortunately the two methods involve different
APIs, and different binary formats.
<p>For parallel read/write access to classic and 64-bit offset data users
must use the parallel-netcdf library from Argonne/Northwestern
University. This is not a Unidata software package, but was developed
using the Unidata netCDF C library as a starting point. For more
information see the parallel netcdf web site: http://www.mcs.anl.gov/parallel-netcdf.
<p>For parallel read/write access to netCDF-4/HDF5 files users must use
the netCDF-4 API. The Argonne/Northwestern parallel netcdf package
cannot read netCDF-4/HDF5 files.
<h4 class="subsection">4.9.2 Parallel I/O with NetCDF-4</h4>
<p>NetCDF-4 provides access to HDF5 parallel I/O features for
netCDF-4/HDF5 files. NetCDF classic and 64-bit offset format may not
be opened or created for use with parallel I/O. (They may be opened
and created, but parallel I/O is not available.)
<p>A few functions have been added to the netCDF C API to handle parallel
I/O. These functions are also available in the Fortran 90 and Fortran
77 APIs.
<h5 class="subsubsection">4.9.2.1 Building NetCDF-4 for Parallel I/O</h5>
<p>You must build netCDF-4 properly to take advantage of parallel
features.
<p>For parallel I/O HDF5 must be built with –enable-parallel. Typically
the CC environment variable is set to mpicc. You must build HDF5 and
netCDF-4 with the same compiler and compiler options.
<p>The netCDF configure script will detect the parallel capability of
HDF5 and build the netCDF-4 parallel I/O features automatically. No
configure options to the netcdf configure are required. If the Fortran
APIs are desired set environmental variable FC to mpif90 (or some local
variant.)
<h5 class="subsubsection">4.9.2.2 Opening/Creating Files for Parallel I/O</h5>
<p>The nc_open_par and nc_create_par functions are used to create/open a
netCDF file with the C API. (Or use nf_open_par/nf_create_par from
Fortran 77).
<p>For Fortran 90 users the nf90_open and nf90_create calls have been
modified to permit parallel I/O files to be opened/created using
optional parameters comm and info.
<p>The parallel access associated with these functions is not a
characteristic of the data file, but the way it was opened.
<h5 class="subsubsection">4.9.2.3 Collective/Independent Access</h5>
<p>Parallel file access is either collective (all processors must
participate) or independent (any processor may access the data without
waiting for others).
<p>All netCDF metadata writing operations are collective. That is, all
creation of groups, types, variables, dimensions, or attributes.
<p>Data reads and writes (ex. calls to nc_put_vara_int and
nc_get_vara_int) may be independent (the default) or collective. To
make writes to a variable collective, call the nc_var_par_access
function (or nf_var_par_access for Fortran 77 users, or
nf90_var_par_access for Fortran 90 users).
<p>The example program below demonstrates simple parallel writing and
reading of a netCDF file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fpar-in-C">simple_xy_par in C</a>
</ul>
<div class="node">
<a name="simple_xy_par-in-C"></a>
<a name="simple_005fxy_005fpar-in-C"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Parallel">Parallel</a>,
Up: <a rel="up" accesskey="u" href="#Parallel">Parallel</a>
</div>
<h4 class="subsection">4.9.3 simple_xy_par_wr.c and simple_xy_par_rd.c</h4>
<p>For this release, only a Fortran 90 language version of this example
is provided. Other APIs will be demonstrated in examples in future
releases.
<p>In the simple_xy_par_wr example program an num_procs x num_procs array
is written to the disk, where num_proc is the number of processors on
which this program is run. Each processor writes one row of length
num_proc.
<p>In the simple_xy_par_rd program the file is read in, and each
processor expects to read in a row with its own MPI rank stored. (The
read program must be run on no more processors than were used to
create the file.)
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fpar_005fwr_002ef90">simple_xy_par_wr.f90</a>
<li><a accesskey="2" href="#simple_005fxy_005fpar_005frd_002ef90">simple_xy_par_rd.f90</a>
</ul>
<div class="node">
<a name="simple_xy_par_wr.f90"></a>
<a name="simple_005fxy_005fpar_005fwr_002ef90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fpar_005frd_002ef90">simple_xy_par_rd.f90</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fpar-in-C">simple_xy_par in C</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fpar-in-C">simple_xy_par in C</a>
</div>
<h5 class="subsubsection">4.9.3.1 simple_xy_par_wr.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is a very simple example which writes a 2D array of sample
! data. To handle this in netCDF we create two shared dimensions,
! "x" and "y", and a netCDF variable, called "data". It uses
! parallel I/O to write the file from all processors at the same
! time.
! This example demonstrates the netCDF Fortran 90 API. This is part
! of the netCDF tutorial, which can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_par_wr.f90,v 1.3 2010/06/01 15:34:49 ed Exp $
program simple_xy_par_wr
use netcdf
implicit none
include 'mpif.h'
! This is the name of the data file we will create.
character (len = *), parameter :: FILE_NAME = "simple_xy_par.nc"
! We are writing 2D data.
integer, parameter :: NDIMS = 2
! When we create netCDF files, variables and dimensions, we get back
! an ID for each one.
integer :: ncid, varid, dimids(NDIMS)
integer :: x_dimid, y_dimid
! These will tell where in the data file this processor should
! write.
integer :: start(NDIMS), count(NDIMS)
! This is the data array we will write. It will just be filled with
! the rank of this processor.
integer, allocatable :: data_out(:)
! MPI stuff: number of processors, rank of this processor, and error
! code.
integer :: p, my_rank, ierr
! Loop indexes, and error handling.
integer :: x, stat
! Initialize MPI, learn local rank and total number of processors.
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, my_rank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, p, ierr)
! Create some pretend data. We just need one row.
allocate(data_out(p), stat = stat)
if (stat .ne. 0) stop 3
do x = 1, p
data_out(x) = my_rank
end do
! Create the netCDF file. The NF90_NETCDF4 flag causes a
! HDF5/netCDF-4 file to be created. The comm and info parameters
! cause parallel I/O to be enabled. Use either NF90_MPIIO or
! NF90_MPIPOSIX to select between MPI/IO and MPI/POSIX.
call check(nf90_create(FILE_NAME, IOR(NF90_NETCDF4, NF90_MPIIO), ncid, &
comm = MPI_COMM_WORLD, info = MPI_INFO_NULL))
! Define the dimensions. NetCDF will hand back an ID for
! each. Metadata operations must take place on all processors.
call check(nf90_def_dim(ncid, "x", p, x_dimid))
call check(nf90_def_dim(ncid, "y", p, y_dimid))
! The dimids array is used to pass the IDs of the dimensions of
! the variables. Note that in fortran arrays are stored in
! column-major format.
dimids = (/ y_dimid, x_dimid /)
! Define the variable. The type of the variable in this case is
! NF90_INT (4-byte integer).
call check(nf90_def_var(ncid, "data", NF90_INT, dimids, varid))
! End define mode. This tells netCDF we are done defining
! metadata. This operation is collective and all processors will
! write their metadata to disk.
call check(nf90_enddef(ncid))
! Write the pretend data to the file. Each processor writes one row.
start = (/ 1, my_rank + 1/)
count = (/ p, 1 /)
call check(nf90_put_var(ncid, varid, data_out, start = start, &
count = count))
! Close the file. This frees up any internal netCDF resources
! associated with the file, and flushes any buffers.
call check( nf90_close(ncid) )
! Free my local memory.
deallocate(data_out)
! MPI library must be shut down.
call MPI_Finalize(ierr)
if (my_rank .eq. 0) print *, "*** SUCCESS writing example file ", FILE_NAME, "! "
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program simple_xy_par_wr
</pre>
<div class="node">
<a name="simple_xy_par_rd.f90"></a>
<a name="simple_005fxy_005fpar_005frd_002ef90"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fpar_005fwr_002ef90">simple_xy_par_wr.f90</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fpar-in-C">simple_xy_par in C</a>
</div>
<h5 class="subsubsection">4.9.3.2 simple_xy_par_rd.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2008 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is a simple example which reads a small dummy array, from a
! netCDF data file created by the companion program
! simple_xy_par_wr.f90. The data are read using parallel I/O.
! This is intended to illustrate the use of the netCDF fortran 90
! API. This example program is part of the netCDF tutorial, which can
! be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_par_rd.f90,v 1.2 2009/03/12 18:30:41 ed Exp $
program simple_xy_par_rd
use netcdf
implicit none
include 'mpif.h'
! This is the name of the data file we will read.
character (len = *), parameter :: FILE_NAME = "simple_xy_par.nc"
! These will tell where in the data file this processor should
! write.
integer, parameter :: NDIMS = 2
integer :: start(NDIMS), count(NDIMS)
! We will read data into this array.
integer, allocatable :: data_in(:)
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid
! MPI stuff: number of processors, rank of this processor, and error
! code.
integer :: p, my_rank, ierr
! Loop indexes, and error handling.
integer :: x, y, stat
! Initialize MPI, learn local rank and total number of processors.
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, my_rank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, p, ierr)
! Allocate space to read in data.
allocate(data_in(p), stat = stat)
if (stat .ne. 0) stop 3
! Open the file. NF90_NOWRITE tells netCDF we want read-only access to
! the file.
call check( nf90_open(FILE_NAME, IOR(NF90_NOWRITE, NF90_MPIIO), ncid, &
comm = MPI_COMM_WORLD, info = MPI_INFO_NULL) )
! Get the varid of the data variable, based on its name.
call check( nf90_inq_varid(ncid, "data", varid) )
! Read the data.
start = (/ 1, my_rank + 1/)
count = (/ p, 1 /)
call check( nf90_get_var(ncid, varid, data_in, &
start = start, count = count) )
! Check the data.
do x = 1, p
if (data_in(x) .ne. my_rank) then
print *, "data_in(", x, ") = ", data_in(x)
stop "Stopped"
endif
end do
! Close the file, freeing all resources.
call check( nf90_close(ncid) )
! Free my local memory.
deallocate(data_in)
! MPI library must be shut down.
call MPI_Finalize(ierr)
if (my_rank .eq. 0) print *,"*** SUCCESS reading example file ", FILE_NAME, "! "
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program simple_xy_par_rd
</pre>
<div class="node">
<a name="Future"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#Parallel">Parallel</a>,
Up: <a rel="up" accesskey="u" href="#API_002dExtensions">API-Extensions</a>
</div>
<h3 class="section">4.10 The Future of NetCDF</h3>
<p><a name="index-netCDF_002d4-93"></a>
NetCDF continues under active development at Unidata (see
<a href="http://www.unidata.ucar.edu">http://www.unidata.ucar.edu</a>).
<p>The next few releases of netCDF will include:
<ol type=1 start=1>
<li>A new C++ API which has better error handling and handles netCDF-4
advanced features, such as groups and compound types.
<li>Remote access to files stored on a DAP server.
<li>Bundled packaging with udunits and other useful tools.
<li>More documentation, more examples, more tests, and more fun!
</ol>
<div class="node">
<a name="NetCDF-4-Examples"></a>
<a name="NetCDF_002d4-Examples"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#Combined-Index">Combined Index</a>,
Previous: <a rel="previous" accesskey="p" href="#API_002dExtensions">API-Extensions</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">5 NetCDF-4 Examples</h2>
<p>Any existing netCDF applications can be converted to generate
netCDF-4/HDF5 files. Simply change the file creation call to include
the correct mode flag.
<p>For example, in one of the C examples which write a data file, change
the nc_create call so that NC_NETCDF4 is one of the flags set on the
create.
<p>The corresponding read example will work without modification; netCDF
will notice that the file is a NetCDF-4/HDF5 file, and will read it
automatically, just as if it were a netCDF classic format file.
<p>In the example in this section we show some of the advanced features
of netCDF-4. More examples will be added in future releases.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fnc4">simple_nc4</a>
<li><a accesskey="2" href="#simple_005fxy_005fnc4">simple_xy_nc4</a>
</ul>
<div class="node">
<a name="simple_nc4"></a>
<a name="simple_005fnc4"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fnc4">simple_xy_nc4</a>,
Previous: <a rel="previous" accesskey="p" href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>,
Up: <a rel="up" accesskey="u" href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>
</div>
<h3 class="section">5.1 The simple_nc4 Example</h3>
<p>This example, like the simple_xy netCDF-3 example above, is an overly
simplified example which demonstrates how to use groups in a netCDF-4
file.
<p>This example is only available in C for this version of netCDF-4. The
example creates and then reads the file “simple_nc4.nc.”
<p>The simple_xy.nc data file contains two dimensions, “x” and “y”,
two groups, “grp1” and “grp2”, and two data variables, one in each
group, both named: “data.” One data variable is an unsigned 64-bit
integer, the other a user-defined compound type.
<p>The example program simple_nc4_wr.c creates the example data file
simple_nc4.nc. The example program simple_nc4_rd.c reads the data
file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fnc4-in-C">simple_nc4 in C</a>
</ul>
<div class="node">
<a name="simple_nc4-in-C"></a>
<a name="simple_005fnc4-in-C"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fnc4">simple_nc4</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fnc4">simple_nc4</a>
</div>
<h4 class="subsection">5.1.1 simple_nc4_wr.c and simple_nc4_rd.c</h4>
<p>For this release, only a C language version of this example is
provided. Other APIs will be demonstrated in examples in future
releases.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fnc4_005fwr_002ec">simple_nc4_wr.c</a>
<li><a accesskey="2" href="#simple_005fnc4_005frd_002ec">simple_nc4_rd.c</a>
</ul>
<div class="node">
<a name="simple_nc4_wr.c"></a>
<a name="simple_005fnc4_005fwr_002ec"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fnc4_005frd_002ec">simple_nc4_rd.c</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fnc4-in-C">simple_nc4 in C</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fnc4-in-C">simple_nc4 in C</a>
</div>
<h5 class="subsubsection">5.1.1.1 simple_nc4_wr.c</h5>
<pre class="example"> /* This is part of the netCDF-4 package. Copyright 2007 University
Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
for conditions of use.
This is a very simple example which demonstrates some of the
new features of netCDF-4.0.
We create two shared dimensions, "x" and "y", in a parent group,
and some netCDF variables in different subgroups. The variables
will include a compound and an enum type, as well as some of the
new atomic types, like the unsigned 64-bit integer.
This example demonstrates the netCDF-4 C API. This is part of the
netCDF tutorial, which can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial.html
To understand this example program, users should have a good
understanding of the netCDF-3 API. See the example program
simple_xy_wr.c for a netCDF-3 example.
Full documentation of the netCDF-4 C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.html
$Id: simple_nc4_wr.c,v 1.4 2008/06/19 13:29:18 ed Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "simple_nc4.nc"
/* We are writing 2D data, a 6 x 12 grid. */
#define NDIMS 2
#define NX 6
#define NY 12
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
/* When we create netCDF variables, groups, dimensions, or types,
* we get back an ID for each one. */
int ncid, x_dimid, y_dimid, varid1, varid2, grp1id, grp2id, typeid;
int dimids[NDIMS];
/* This is the data array we will write. It will be filled with a
* progression of numbers for this example. */
unsigned long long data_out[NX][NY];
/* Loop indexes, and error handling. */
int x, y, retval;
/* The following struct is written as a compound type. */
struct s1
{
int i1;
int i2;
};
struct s1 compound_data[NX][NY];
/* Create some pretend data. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
{
data_out[x][y] = x * NY + y;
compound_data[x][y].i1 = 42;
compound_data[x][y].i2 = -42;
}
/* Create the file. The NC_NETCDF4 flag tells netCDF to
* create a netCDF-4/HDF5 file.*/
if ((retval = nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)))
ERR(retval);
/* Define the dimensions in the root group. Dimensions are visible
* in all subgroups. */
if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
ERR(retval);
/* The dimids passes the IDs of the dimensions of the variable. */
dimids[0] = x_dimid;
dimids[1] = y_dimid;
/* Define two groups, "grp1" and "grp2." */
if ((retval = nc_def_grp(ncid, "grp1", &grp1id)))
ERR (retval);
if ((retval = nc_def_grp(ncid, "grp2", &grp2id)))
ERR (retval);
/* Define an unsigned 64bit integer variable in grp1, using dimensions
* in the root group. */
if ((retval = nc_def_var(grp1id, "data", NC_UINT64, NDIMS,
dimids, &varid1)))
ERR(retval);
/* Write unsigned long long data to the file. For netCDF-4 files,
* nc_enddef will be called automatically. */
if ((retval = nc_put_var_ulonglong(grp1id, varid1, &data_out[0][0])))
ERR(retval);
/* Create a compound type. This will cause nc_reddef to be called. */
if (nc_def_compound(grp2id, sizeof(struct s1), "sample_compound_type",
&typeid))
ERR(retval);
if (nc_insert_compound(grp2id, typeid, "i1",
offsetof(struct s1, i1), NC_INT))
ERR(retval);
if (nc_insert_compound(grp2id, typeid, "i2",
offsetof(struct s1, i2), NC_INT))
ERR(retval);
/* Define a compound type variable in grp2, using dimensions
* in the root group. */
if ((retval = nc_def_var(grp2id, "data", typeid, NDIMS,
dimids, &varid2)))
ERR(retval);
/* Write the array of struct to the file. This will cause nc_endef
* to be called. */
if ((retval = nc_put_var(grp2id, varid2, &compound_data[0][0])))
ERR(retval);
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file simple_nc4.nc!\n");
return 0;
}
</pre>
<div class="node">
<a name="simple_nc4_rd.c"></a>
<a name="simple_005fnc4_005frd_002ec"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fnc4_005fwr_002ec">simple_nc4_wr.c</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fnc4-in-C">simple_nc4 in C</a>
</div>
<h5 class="subsubsection">5.1.1.2 simple_nc4_rd.c</h5>
<pre class="example"> /* This is part of the netCDF package. Copyright 2006 University
Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
for conditions of use.
This is a very simple example which demonstrates some of the
new features of netCDF-4.0.
This example reads a simple file created by simple_nc4_wr.c. This
is intended to illustrate the use of the netCDF-4 C API.
This program is part of the netCDF tutorial:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial.html
Full documentation of the netCDF C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c.html
$Id: simple_nc4_rd.c,v 1.3 2008/06/19 13:29:18 ed Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will read. */
#define FILE_NAME "simple_nc4.nc"
/* We are reading 2D data, a 6 x 12 grid. */
#define NX 6
#define NY 12
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
/* There will be netCDF IDs for the file, each group, and each
* variable. */
int ncid, varid1, varid2, grp1id, grp2id;
unsigned long long data_in[NX][NY];
/* Loop indexes, and error handling. */
int x, y, retval;
/* The following struct is written as a compound type. */
struct s1
{
int i1;
int i2;
};
struct s1 compound_data[NX][NY];
/* Open the file. NC_NOWRITE tells netCDF we want read-only access
* to the file.*/
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);
/* Get the group ids of our two groups. */
if ((retval = nc_inq_ncid(ncid, "grp1", &grp1id)))
ERR(retval);
if ((retval = nc_inq_ncid(ncid, "grp2", &grp2id)))
ERR(retval);
/* Get the varid of the uint64 data variable, based on its name, in
* grp1. */
if ((retval = nc_inq_varid(grp1id, "data", &varid1)))
ERR(retval);
/* Read the data. */
if ((retval = nc_get_var_ulonglong(grp1id, varid1, &data_in[0][0])))
ERR(retval);
/* Get the varid of the compound data variable, based on its name,
* in grp2. */
if ((retval = nc_inq_varid(grp2id, "data", &varid2)))
ERR(retval);
/* Read the data. */
if ((retval = nc_get_var(grp2id, varid2, &compound_data[0][0])))
ERR(retval);
/* Check the data. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
{
if (data_in[x][y] != x * NY + y ||
compound_data[x][y].i1 != 42 ||
compound_data[x][y].i2 != -42)
return ERRCODE;
}
/* Close the file, freeing all resources. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
return 0;
}
</pre>
<div class="node">
<a name="simple_xy_nc4"></a>
<a name="simple_005fxy_005fnc4"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fnc4">simple_nc4</a>,
Up: <a rel="up" accesskey="u" href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>
</div>
<h3 class="section">5.2 The simple_xy_nc4 Example</h3>
<p>This example, like the simple_xy netCDF-3 example above, is an overly
simplified example. It is based on the simple_xy example, but used
data chunking, compression, and the fletcher32 filter.
<p>(These are all HDF5 features. For more information see http://hdfgroup.org/HDF5/).
<p>This example is not yet available in C++. We hope to have the C++
example in a future release of netCDF.
<p>The example creates and then reads the file “simple_xy_nc4.nc.”
<p>The example program simple_xy_nc4_wr.c creates the example data file
simple_xy_nc4.nc. The example program simple_xy_nc4_rd.c reads the data
file.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fnc4-in-C">simple_xy_nc4 in C</a>
<li><a accesskey="2" href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>
<li><a accesskey="3" href="#simple_005fxy_005fnc4-in-F90">simple_xy_nc4 in F90</a>
</ul>
<div class="node">
<a name="simple_xy_nc4-in-C"></a>
<a name="simple_005fxy_005fnc4-in-C"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4">simple_xy_nc4</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4">simple_xy_nc4</a>
</div>
<h4 class="subsection">5.2.1 simple_xy_nc4_wr.c and simple_xy_nc4_rd.c</h4>
<p>This is just like the simple_xy example, but with chunking and
variable compression.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fnc4_005fwr_002ec">simple_xy_nc4_wr.c</a>
<li><a accesskey="2" href="#simple_005fxy_005fnc4_005frd_002ec">simple_xy_nc4_rd.c</a>
</ul>
<div class="node">
<a name="simple_xy_nc4_wr.c"></a>
<a name="simple_005fxy_005fnc4_005fwr_002ec"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fnc4_005frd_002ec">simple_xy_nc4_rd.c</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4-in-C">simple_xy_nc4 in C</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4-in-C">simple_xy_nc4 in C</a>
</div>
<h5 class="subsubsection">5.2.1.1 simple_xy_nc4_wr.c</h5>
<pre class="example"> /* This is part of the netCDF package. Copyright 2007 University
Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
for conditions of use.
This is a very simple example which is based on the simple_xy
example, but whch uses netCDF-4 features, such as
compression. Please see the simple_xy example to learn more about
the netCDF-3 API.
Like simple_xy_wr.c, this program writes a 2D netCDF variable
(called "data") and fills it with sample data. It has two
dimensions, "x" and "y".
This example demonstrates the netCDF C API, and netCDF-4
extensions. This is part of the netCDF-4 tutorial, which can be
found at:
http://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-tutorial
Full documentation of the netCDF C API, including netCDF-4
extensions, can be found at:
http://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-c
$Id: simple_xy_nc4_wr.c,v 1.6 2008/12/11 16:40:05 russ Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "simple_xy_nc4.nc"
/* We are writing 2D data, a 6 x 12 grid. */
#define NDIMS 2
#define NX 60
#define NY 120
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
int ncid, x_dimid, y_dimid, varid;
int dimids[NDIMS];
size_t chunks[NDIMS];
int shuffle, deflate, deflate_level;
int data_out[NX][NY];
int x, y, retval;
/* Set chunking, shuffle, and deflate. */
shuffle = NC_SHUFFLE;
deflate = 1;
deflate_level = 1;
/* Create some pretend data. If this wasn't an example program, we
* would have some real data to write, for example, model output. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
data_out[x][y] = x * NY + y;
/* Create the file. The NC_NETCDF4 parameter tells netCDF to create
* a file in netCDF-4/HDF5 standard. */
if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
ERR(retval);
/* Define the dimensions. */
if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
ERR(retval);
/* Set up variabe data. */
dimids[0] = x_dimid;
dimids[1] = y_dimid;
chunks[0] = NX/4;
chunks[1] = NY/4;
/* Define the variable. */
if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
dimids, &varid)))
ERR(retval);
if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
ERR(retval);
if ((retval = nc_def_var_deflate(ncid, varid, shuffle, deflate,
deflate_level)))
ERR(retval);
/* No need to explicitly end define mode for netCDF-4 files. Write
* the pretend data to the file. */
if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
ERR(retval);
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file simple_xy_nc4.nc!\n");
return 0;
}
</pre>
<div class="node">
<a name="simple_xy_nc4_rd.c"></a>
<a name="simple_005fxy_005fnc4_005frd_002ec"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4_005fwr_002ec">simple_xy_nc4_wr.c</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4-in-C">simple_xy_nc4 in C</a>
</div>
<h5 class="subsubsection">5.2.1.2 simple_xy_nc4_rd.c</h5>
<pre class="example"> /* This is part of the netCDF package. Copyright 2007 University
Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
for conditions of use.
This is a very simple example which is based on the simple_xy
example, but whch uses netCDF-4 features, such as
compression. Please see the simple_xy example to learn more about
the netCDF-3 API.
Like simple_xy_rd.c, this example reads a small dummy array, which
was written by simple_xy_wr.c, and is compressed. This is intended
to illustrate the use of the netCDF C API.
This program is part of the netCDF-4 tutorial:
http://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-tutorial
Full documentation of the netCDF-4 C API can be found at:
http://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-c
$Id: simple_xy_nc4_rd.c,v 1.2 2007/06/05 21:47:50 ed Exp $
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will read. */
#define FILE_NAME "simple_xy_nc4.nc"
/* We are reading 2D data, a 6 x 12 grid. */
#define NX 60
#define NY 120
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
/* This will be the netCDF ID for the file and data variable. */
int ncid, varid;
int data_in[NX][NY];
/* Loop indexes, and error handling. */
int x, y, retval;
/* Open the file. NC_NOWRITE tells netCDF we want read-only access
* to the file.*/
if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
ERR(retval);
/* Get the varid of the data variable, based on its name. */
if ((retval = nc_inq_varid(ncid, "data", &varid)))
ERR(retval);
/* Read the data. */
if ((retval = nc_get_var_int(ncid, varid, &data_in[0][0])))
ERR(retval);
/* Check the data. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
if (data_in[x][y] != x * NY + y)
return ERRCODE;
/* Close the file, freeing all resources. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS reading example file %s!\n", FILE_NAME);
return 0;
}
</pre>
<div class="node">
<a name="simple_xy_nc4-in-F77"></a>
<a name="simple_005fxy_005fnc4-in-F77"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fnc4-in-F90">simple_xy_nc4 in F90</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4-in-C">simple_xy_nc4 in C</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4">simple_xy_nc4</a>
</div>
<h4 class="subsection">5.2.2 simple_xy_nc4_wr.f and simple_xy_nc4_rd.f</h4>
<p>This is just like the simple_xy example, but with chunking and
variable compression.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fnc4_005fwr_002ef">simple_xy_nc4_wr.f</a>
<li><a accesskey="2" href="#simple_005fxy_005fnc4_005frd_002ef">simple_xy_nc4_rd.f</a>
</ul>
<div class="node">
<a name="simple_xy_nc4_wr.f"></a>
<a name="simple_005fxy_005fnc4_005fwr_002ef"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fnc4_005frd_002ef">simple_xy_nc4_rd.f</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>
</div>
<h5 class="subsubsection">5.2.2.1 simple_xy_nc4_wr.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is a very simple example which writes a 2D array of
C sample data. To handle this in netCDF we create two shared
C dimensions, "x" and "y", and a netCDF variable, called "data".
C This example demonstrates the netCDF Fortran 77 API. This is part
C of the netCDF tutorial, which can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: simple_xy_nc4_wr.f,v 1.5 2008/02/19 22:01:49 ed Exp $
program simple_xy_wr
implicit none
include 'netcdf.inc'
character*(*) FILE_NAME
parameter (FILE_NAME='simple_xy_nc4.nc')
integer NDIMS
parameter (NDIMS = 2)
integer NX, NY
parameter (NX = 60, NY = 120)
integer ncid, varid, dimids(NDIMS)
integer x_dimid, y_dimid
integer data_out(NY, NX)
integer x, y, retval
integer chunks(2)
integer contiguous, shuffle, deflate, deflate_level
C Create some pretend data. If this wasn't an example program, we
C would have some real data to write, for example, model output.
do x = 1, NX
do y = 1, NY
data_out(y, x) = (x - 1) * NY + (y - 1)
end do
end do
C Create the netCDF file. The nf_netcdf4 tells netCDF to
C create a netCDF-4/HDF5 file.
retval = nf_create(FILE_NAME, NF_NETCDF4, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Define the dimensions.
retval = nf_def_dim(ncid, "x", NX, x_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid, "y", NY, y_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
C The dimids array is used to pass the IDs of the dimensions of
C the variables. Note that in fortran arrays are stored in
C column-major format.
dimids(2) = x_dimid
dimids(1) = y_dimid
C Define the variable.
retval = nf_def_var(ncid, "data", NF_INT, NDIMS, dimids, varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Set up chunking and compression.
contiguous = 0
chunks(1) = NY
chunks(2) = NX
shuffle = 1
deflate = 1
deflate_level = 4
retval = nf_def_var_chunking(ncid, varid, contiguous, chunks)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var_deflate(ncid, varid, shuffle, deflate,
& deflate_level)
if (retval .ne. nf_noerr) call handle_err(retval)
C Write the pretend data to the file.
retval = nf_put_var_int(ncid, varid, data_out)
if (retval .ne. nf_noerr) call handle_err(retval)
C Close the file.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
print *,'*** SUCCESS writing example file simple_xy_nc4.nc!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="simple_xy_nc4_rd.f"></a>
<a name="simple_005fxy_005fnc4_005frd_002ef"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4_005fwr_002ef">simple_xy_nc4_wr.f</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>
</div>
<h5 class="subsubsection">5.2.2.2 simple_xy_nc4_rd.f</h5>
<pre class="example"> C This is part of the netCDF package.
C Copyright 2006 University Corporation for Atmospheric Research/Unidata.
C See COPYRIGHT file for conditions of use.
C This is a simple example which reads a small dummy array, from a
C netCDF data file created by the companion program simple_xy_wr.f.
C This is intended to illustrate the use of the netCDF fortran 77
C API. This example program is part of the netCDF tutorial, which can
C be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
C Full documentation of the netCDF Fortran 77 API can be found at:
C http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
C $Id: simple_xy_nc4_rd.f,v 1.1 2007/05/04 13:33:20 ed Exp $
program simple_xy_rd
implicit none
include 'netcdf.inc'
C This is the name of the data file we will read.
character*(*) FILE_NAME
parameter (FILE_NAME='simple_xy_nc4.nc')
C We are reading 2D data, a 12 x 6 grid.
integer NX, NY
parameter (NX = 60, NY = 120)
integer data_in(NY, NX)
C This will be the netCDF ID for the file and data variable.
integer ncid, varid
C Loop indexes, and error handling.
integer x, y, retval
C Open the file. NF_NOWRITE tells netCDF we want read-only access to
C the file.
retval = nf_open(FILE_NAME, NF_NOWRITE, ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Get the varid of the data variable, based on its name.
retval = nf_inq_varid(ncid, 'data', varid)
if (retval .ne. nf_noerr) call handle_err(retval)
C Read the data.
retval = nf_get_var_int(ncid, varid, data_in)
if (retval .ne. nf_noerr) call handle_err(retval)
C Check the data.
do x = 1, NX
do y = 1, NY
if (data_in(y, x) .ne. (x - 1) * NY + (y - 1)) then
print *, 'data_in(', y, ', ', x, ') = ', data_in(y, x)
stop 2
end if
end do
end do
C Close the file, freeing all resources.
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
print *,'*** SUCCESS reading example file ', FILE_NAME, '!'
end
subroutine handle_err(errcode)
implicit none
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end
</pre>
<div class="node">
<a name="simple_xy_nc4-in-F90"></a>
<a name="simple_005fxy_005fnc4-in-F90"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4-in-F77">simple_xy_nc4 in F77</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4">simple_xy_nc4</a>
</div>
<h4 class="subsection">5.2.3 simple_xy_nc4_wr.f90 and simple_xy_nc4_rd.f90</h4>
<p>This is just like the simple_xy example, but with chunking and
variable compression.
<ul class="menu">
<li><a accesskey="1" href="#simple_005fxy_005fnc4_005fwr_002ef90">simple_xy_nc4_wr.f90</a>
<li><a accesskey="2" href="#simple_005fxy_005fnc4_005frd_002ef90">simple_xy_nc4_rd.f90</a>
</ul>
<div class="node">
<a name="simple_xy_nc4_wr.f90"></a>
<a name="simple_005fxy_005fnc4_005fwr_002ef90"></a>
<p><hr>
Next: <a rel="next" accesskey="n" href="#simple_005fxy_005fnc4_005frd_002ef90">simple_xy_nc4_rd.f90</a>,
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4-in-F90">simple_xy_nc4 in F90</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4-in-F90">simple_xy_nc4 in F90</a>
</div>
<h5 class="subsubsection">5.2.3.1 simple_xy_nc4_wr.f90</h5>
<pre class="example"> ! This is part of the netCDF package. Copyright 2006 University
! Corporation for Atmospheric Research/Unidata. See COPYRIGHT
! file for conditions of use.
! This is a very simple example which writes a 2D array of sample
! data. To handle this in netCDF we create two shared dimensions,
! "x" and "y", and a netCDF variable, called "data".
! This example demonstrates the netCDF Fortran 90 API. This is
! part of the netCDF tutorial, which can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_nc4_wr.f90,v 1.6 2010/04/06 19:32:09 ed Exp $
program simple_xy_wr
use netcdf
implicit none
character (len = *), parameter :: FILE_NAME = "simple_xy_nc4.nc"
integer, parameter :: NDIMS = 2
integer, parameter :: NX = 6, NY = 12
integer :: ncid, varid, dimids(NDIMS)
integer :: x_dimid, y_dimid
integer :: data_out(NY, NX)
integer :: chunks(2)
integer :: deflate_level
integer :: x, y
! Create some pretend data. If this wasn't an example program, we
! would have some real data to write, for example, model output.
do x = 1, NX
do y = 1, NY
data_out(y, x) = (x - 1) * NY + (y - 1)
end do
end do
! Always check the return code of every netCDF function call. In
! this example program, wrapping netCDF calls with "call check()"
! makes sure that any return which is not equal to nf90_noerr (0)
! will print a netCDF error message and exit.
! Create the netCDF file. The nf90_clobber parameter tells netCDF to
! overwrite this file, if it already exists.
call check( nf90_create(FILE_NAME, nf90_hdf5, ncid) )
! Define the dimensions. NetCDF will hand back an ID for each.
call check( nf90_def_dim(ncid, "x", NX, x_dimid) )
call check( nf90_def_dim(ncid, "y", NY, y_dimid) )
! The dimids array is used to pass the IDs of the dimensions of
! the variables. Note that in fortran arrays are stored in
! column-major format.
dimids = (/ y_dimid, x_dimid /)
! Define the variable. The type of the variable in this case is
! NF90_INT (4-byte integer). Optional parameters chunking, shuffle,
! and deflate_level are used.
chunks(1) = NY
chunks(2) = NX
deflate_level = 1
call check( nf90_def_var(ncid, "data", NF90_INT, dimids, varid, &
chunksizes = chunks, shuffle = .TRUE., deflate_level = deflate_level) )
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
! Write the pretend data to the file. Although netCDF supports
! reading and writing subsets of data, in this case we write all the
! data in one operation.
call check( nf90_put_var(ncid, varid, data_out) )
! Close the file. This frees up any internal netCDF resources
! associated with the file, and flushes any buffers.
call check( nf90_close(ncid) )
print *, '*** SUCCESS writing example file ', FILE_NAME, '!'
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program simple_xy_wr
</pre>
<div class="node">
<a name="simple_xy_nc4_rd.f90"></a>
<a name="simple_005fxy_005fnc4_005frd_002ef90"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#simple_005fxy_005fnc4_005fwr_002ef90">simple_xy_nc4_wr.f90</a>,
Up: <a rel="up" accesskey="u" href="#simple_005fxy_005fnc4-in-F90">simple_xy_nc4 in F90</a>
</div>
<h5 class="subsubsection">5.2.3.2 simple_xy_nc4_rd.f90</h5>
<pre class="example"> ! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is a simple example which reads a small dummy array, from a
! netCDF data file created by the companion program simple_xy_wr.f90.
! This is intended to illustrate the use of the netCDF fortran 77
! API. This example program is part of the netCDF tutorial, which can
! be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_nc4_rd.f90,v 1.3 2009/02/25 12:23:45 ed Exp $
program simple_xy_rd
use netcdf
implicit none
! This is the name of the data file and variable.
character (len = *), parameter :: FILE_NAME = "simple_xy_nc4.nc"
character (len = *), parameter :: VAR_NAME = "data"
! We are reading 2D data, a 12 x 6 grid.
integer, parameter :: NX = 6, NY = 12, MAX_DIMS = 2
integer :: data_in(NY, NX)
! This will be the netCDF ID for the file and data variable.
integer :: ncid, varid
! Information we will read about the variable.
character (len = nf90_max_name) :: name
integer :: xtype, ndims, dimids(MAX_DIMS), natts
integer :: chunksizes(MAX_DIMS), deflate_level, endianness
logical :: contiguous, shuffle, fletcher32
! Loop indexes, and error handling.
integer :: x, y
! Open the file. NF90_NOWRITE tells netCDF we want read-only access
! to the file.
call check( nf90_open(FILE_NAME, NF90_NOWRITE, ncid) )
! Get the varid of the data variable, based on its name.
call check( nf90_inq_varid(ncid, VAR_NAME, varid) )
! Learn about the variable. This uses all optional parameters.
call check( nf90_inquire_variable(ncid, varid, name, xtype, ndims, &
dimids, natts, contiguous = contiguous, chunksizes = chunksizes, &
deflate_level = deflate_level, shuffle = shuffle, &
fletcher32 = fletcher32, endianness = endianness) )
! Make sure we got the correct answers. These depend on what was set
! when creating the file in simple_xy_nc4_wr.f90. Endianness will be
! whatever is native for the machine that's building this example.
if (name .ne. VAR_NAME .or. xtype .ne. NF90_INT .or. ndims .ne. 2 .or. &
natts .ne. 0 .or. contiguous .or. .not. shuffle .or. &
deflate_level .ne. 1 .or. fletcher32) stop 3
! Read the data.
call check( nf90_get_var(ncid, varid, data_in) )
! Check the data.
do x = 1, NX
do y = 1, NY
if (data_in(y, x) /= (x - 1) * NY + (y - 1)) then
print *, "data_in(", y, ", ", x, ") = ", data_in(y, x)
stop "Stopped"
end if
end do
end do
! Close the file, freeing all resources.
call check( nf90_close(ncid) )
print *,"*** SUCCESS reading example file ", FILE_NAME, "! "
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop 2
end if
end subroutine check
end program simple_xy_rd
</pre>
<div class="node">
<a name="Combined-Index"></a>
<p><hr>
Previous: <a rel="previous" accesskey="p" href="#NetCDF_002d4-Examples">NetCDF-4 Examples</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="unnumbered">Index</h2>
<ul class="index-cp" compact>
<li><a href="#index-g_t64_002dbit-offset-format-28">64-bit offset format</a>: <a href="#Versions">Versions</a></li>
<li><a href="#index-attribute-7">attribute</a>: <a href="#Data-Model">Data Model</a></li>
<li><a href="#index-C_002b_002b_002c-netCDF-API-22">C++, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-C_002c-netCDF-API-23">C, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-classic-format-27">classic format</a>: <a href="#Versions">Versions</a></li>
<li><a href="#index-common-data-model-8">common data model</a>: <a href="#Common-Data-Model">Common Data Model</a></li>
<li><a href="#index-compound-types-10">compound types</a>: <a href="#Common-Data-Model">Common Data Model</a></li>
<li><a href="#index-creating-files-in-C-34">creating files in C</a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-creating-files-in-C_002b_002b-58">creating files in C++</a>: <a href="#Creation-in-C_002b_002b">Creation in C++</a></li>
<li><a href="#index-creating-files-in-Fortran-50">creating files in Fortran</a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-creating-files-in-Fortran-42">creating files in Fortran</a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-creating-netCDF-files-33">creating netCDF files</a>: <a href="#Creation">Creation</a></li>
<li><a href="#index-data-model-4">data model</a>: <a href="#Data-Model">Data Model</a></li>
<li><a href="#index-dimension-6">dimension</a>: <a href="#Data-Model">Data Model</a></li>
<li><a href="#index-example-programs-29">example programs</a>: <a href="#Examples">Examples</a></li>
<li><a href="#index-Fortran-77_002c-netCDF-API-24">Fortran 77, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-Fortran-90_002c-netCDF-API-25">Fortran 90, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-groups-9">groups</a>: <a href="#Common-Data-Model">Common Data Model</a></li>
<li><a href="#index-inquiry-functions-60">inquiry functions</a>: <a href="#Reading">Reading</a></li>
<li><a href="#index-language-APIs-for-netCDF-18">language APIs for netCDF</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-nc_005fclose-41"><code>nc_close</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fcreate-35"><code>nc_create</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fdef_005fdim-36"><code>nc_def_dim</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fdef_005fvar-37"><code>nc_def_var</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fenddef-39"><code>nc_enddef</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fget_005fatt-63"><code>nc_get_att</code></a>: <a href="#Reading-in-C">Reading in C</a></li>
<li><a href="#index-nc_005fget_005fvar1-75"><code>nc_get_var1</code></a>: <a href="#Subsetting-in-C">Subsetting in C</a></li>
<li><a href="#index-nc_005fget_005fvara-64"><code>nc_get_vara</code></a>: <a href="#Reading-in-C">Reading in C</a></li>
<li><a href="#index-nc_005fget_005fvarm-77"><code>nc_get_varm</code></a>: <a href="#Subsetting-in-C">Subsetting in C</a></li>
<li><a href="#index-nc_005fget_005fvars-76"><code>nc_get_vars</code></a>: <a href="#Subsetting-in-C">Subsetting in C</a></li>
<li><a href="#index-nc_005fopen-62"><code>nc_open</code></a>: <a href="#Reading-in-C">Reading in C</a></li>
<li><a href="#index-nc_005fput_005fatt-38"><code>nc_put_att</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fput_005fvar1-78"><code>nc_put_var1</code></a>: <a href="#Subsetting-in-C">Subsetting in C</a></li>
<li><a href="#index-nc_005fput_005fvara-40"><code>nc_put_vara</code></a>: <a href="#Creation-in-C">Creation in C</a></li>
<li><a href="#index-nc_005fput_005fvarm-80"><code>nc_put_varm</code></a>: <a href="#Subsetting-in-C">Subsetting in C</a></li>
<li><a href="#index-nc_005fput_005fvars-79"><code>nc_put_vars</code></a>: <a href="#Subsetting-in-C">Subsetting in C</a></li>
<li><a href="#index-ncdump-13">ncdump</a>: <a href="#Tools">Tools</a></li>
<li><a href="#index-NcFile-74"><code>NcFile</code></a>: <a href="#Reading-in-C_002b_002b">Reading in C++</a></li>
<li><a href="#index-ncgen-14">ncgen</a>: <a href="#Tools">Tools</a></li>
<li><a href="#index-netCDF_002c-definition-1">netCDF, definition</a>: <a href="#Intro">Intro</a></li>
<li><a href="#index-netCDF_002d4-93">netCDF-4</a>: <a href="#Future">Future</a></li>
<li><a href="#index-netCDF_002d4-model-extensions-12">netCDF-4 model extensions</a>: <a href="#Common-Data-Model">Common Data Model</a></li>
<li><a href="#index-NF90_005fCLOSE-57"><code>NF90_CLOSE</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fCREATE-51"><code>NF90_CREATE</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fDEF_005fDIM-52"><code>NF90_DEF_DIM</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fDEF_005fVAR-53"><code>NF90_DEF_VAR</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fENDDEF-55"><code>NF90_ENDDEF</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fGET_005fATT-71"><code>NF90_GET_ATT</code></a>: <a href="#Reading-in-F90">Reading in F90</a></li>
<li><a href="#index-NF90_005fGET_005fVAR1-87"><code>NF90_GET_VAR1</code></a>: <a href="#Subsetting-in-F90">Subsetting in F90</a></li>
<li><a href="#index-NF90_005fGET_005fVARA-72"><code>NF90_GET_VARA</code></a>: <a href="#Reading-in-F90">Reading in F90</a></li>
<li><a href="#index-NF90_005fGET_005fVARM-89"><code>NF90_GET_VARM</code></a>: <a href="#Subsetting-in-F90">Subsetting in F90</a></li>
<li><a href="#index-NF90_005fGET_005fVARS-88"><code>NF90_GET_VARS</code></a>: <a href="#Subsetting-in-F90">Subsetting in F90</a></li>
<li><a href="#index-NF90_005fOPEN-70"><code>NF90_OPEN</code></a>: <a href="#Reading-in-F90">Reading in F90</a></li>
<li><a href="#index-NF90_005fPUT_005fATT_005f-type-54"><code>NF90_PUT_ATT_ type</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fPUT_005fVAR1-90"><code>NF90_PUT_VAR1</code></a>: <a href="#Subsetting-in-F90">Subsetting in F90</a></li>
<li><a href="#index-NF90_005fPUT_005fVARA-56"><code>NF90_PUT_VARA</code></a>: <a href="#Creation-in-F90">Creation in F90</a></li>
<li><a href="#index-NF90_005fPUT_005fVARM-92"><code>NF90_PUT_VARM</code></a>: <a href="#Subsetting-in-F90">Subsetting in F90</a></li>
<li><a href="#index-NF90_005fPUT_005fVARS-91"><code>NF90_PUT_VARS</code></a>: <a href="#Subsetting-in-F90">Subsetting in F90</a></li>
<li><a href="#index-NF_005fCLOSE-49"><code>NF_CLOSE</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fCREATE-43"><code>NF_CREATE</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fDEF_005fDIM-44"><code>NF_DEF_DIM</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fDEF_005fVAR-45"><code>NF_DEF_VAR</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fENDDEF-47"><code>NF_ENDDEF</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fGET_005fATT-67"><code>NF_GET_ATT</code></a>: <a href="#Reading-in-F77">Reading in F77</a></li>
<li><a href="#index-NF_005fGET_005fVAR1-81"><code>NF_GET_VAR1</code></a>: <a href="#Subsetting-in-F77">Subsetting in F77</a></li>
<li><a href="#index-NF_005fGET_005fVARA-68"><code>NF_GET_VARA</code></a>: <a href="#Reading-in-F77">Reading in F77</a></li>
<li><a href="#index-NF_005fGET_005fVARM-83"><code>NF_GET_VARM</code></a>: <a href="#Subsetting-in-F77">Subsetting in F77</a></li>
<li><a href="#index-NF_005fGET_005fVARS-82"><code>NF_GET_VARS</code></a>: <a href="#Subsetting-in-F77">Subsetting in F77</a></li>
<li><a href="#index-NF_005fOPEN-66"><code>NF_OPEN</code></a>: <a href="#Reading-in-F77">Reading in F77</a></li>
<li><a href="#index-NF_005fPUT_005fATT_005f-type-46"><code>NF_PUT_ATT_ type</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fPUT_005fVAR1-84"><code>NF_PUT_VAR1</code></a>: <a href="#Subsetting-in-F77">Subsetting in F77</a></li>
<li><a href="#index-NF_005fPUT_005fVARA-48"><code>NF_PUT_VARA</code></a>: <a href="#Creation-in-F77">Creation in F77</a></li>
<li><a href="#index-NF_005fPUT_005fVARM-86"><code>NF_PUT_VARM</code></a>: <a href="#Subsetting-in-F77">Subsetting in F77</a></li>
<li><a href="#index-NF_005fPUT_005fVARS-85"><code>NF_PUT_VARS</code></a>: <a href="#Subsetting-in-F77">Subsetting in F77</a></li>
<li><a href="#index-perl_002c-netCDF-API-19">perl, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-pres_005ftemp_005f4D-example-32">pres_temp_4D example</a>: <a href="#Examples">Examples</a></li>
<li><a href="#index-python_002c-netCDF-API-21">python, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-reading-netCDF-files-of-known-structure-59">reading netCDF files of known structure</a>: <a href="#Reading">Reading</a></li>
<li><a href="#index-reading-netCDF-files-with-C-61">reading netCDF files with C</a>: <a href="#Reading-in-C">Reading in C</a></li>
<li><a href="#index-reading-netCDF-files-with-C_002b_002b-73">reading netCDF files with C++</a>: <a href="#Reading-in-C_002b_002b">Reading in C++</a></li>
<li><a href="#index-reading-netCDF-files-with-Fortran-77-65">reading netCDF files with Fortran 77</a>: <a href="#Reading-in-F77">Reading in F77</a></li>
<li><a href="#index-reading-netCDF-files-with-Fortran-90-69">reading netCDF files with Fortran 90</a>: <a href="#Reading-in-F90">Reading in F90</a></li>
<li><a href="#index-ruby_002c-netCDF-API-20">ruby, netCDF API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-sfc_005fpres_005ftemp-example-31">sfc_pres_temp example</a>: <a href="#Examples">Examples</a></li>
<li><a href="#index-simple_005fxy-example-30">simple_xy example</a>: <a href="#Examples">Examples</a></li>
<li><a href="#index-software-for-netCDF-16">software for netCDF</a>: <a href="#Tools">Tools</a></li>
<li><a href="#index-third_002dparty-tools-15">third-party tools</a>: <a href="#Tools">Tools</a></li>
<li><a href="#index-tools-for-manipulating-netCDF-17">tools for manipulating netCDF</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-UCAR-3">UCAR</a>: <a href="#Intro">Intro</a></li>
<li><a href="#index-Unidata-2">Unidata</a>: <a href="#Intro">Intro</a></li>
<li><a href="#index-user_002ddefined-types-11">user-defined types</a>: <a href="#Common-Data-Model">Common Data Model</a></li>
<li><a href="#index-V2-API-26">V2 API</a>: <a href="#APIs">APIs</a></li>
<li><a href="#index-variable-5">variable</a>: <a href="#Data-Model">Data Model</a></li>
</ul></body></html>
|