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
|
import netCDF4
import random
import json
import os
from os import remove
from os.path import isfile
from os.path import expandvars as os_path_expandvars
from os.path import expanduser as os_path_expanduser
from string import hexdigits
from numpy import array as numpy_array
from numpy import bool_ as numpy_bool_
from numpy import dtype as numpy_dtype
from numpy import ndindex as numpy_ndindex
from numpy import integer as numpy_integer
from numpy import intersect1d as numpy_intersect1d
from numpy import floating as numpy_floating
from numpy import size as numpy_size
from numpy.ma import empty as numpy_ma_empty
from numpy.ma import isMA as numpy_ma_isMA
from numpy.ma import masked as numpy_ma_masked
from .. import __Conventions__
from ..cfdatetime import dt2rt
from ..coordinate import Coordinate
from ..coordinatebounds import CoordinateBounds
from ..field import Field, FieldList
from ..functions import equals, flat, relpath, abspath
from ..data.data import Data
from .filearray import NetCDFFileArray
from .functions import _close_netcdf_file, _open_netcdf_file
class NetCDFError(Exception):
'''A runtime netCDF error'''
pass
def write(fields, filename, fmt='NETCDF3_CLASSIC', overwrite=True,
verbose=False, cfa_options=None, mode='w',
least_significant_digit=None, endian='native', compress=0,
fletcher32=False, no_shuffle=False, datatype=None,
single=False, double=False, reference_datetime=None,
variable_attributes=None, HDF_chunks=None, unlimited=None):
'''Write fields to a CF-netCDF or CFA-netCDF file.
NetCDF dimension and variable names will be taken from variables'
`~Variable.ncvar` attributes and the domain attribute
`~Domain.nc_dimensions` if present, otherwise they are inferred from
standard names or set to defaults. NetCDF names may be automatically
given a numerical suffix to avoid duplication.
Output netCDF file global properties are those which occur in the set
of CF global properties and non-standard data variable properties and
which have equal values across all input fields.
Logically identical field components are only written to the file
once, apart from when they need to fulfil both dimension coordinate
and auxiliary coordinate roles for different data variables.
:Parameters:
fields : (arbitrarily nested sequence of) cf.Field or cf.FieldList
The field or fields to write to the file.
filename : str
The output CF-netCDF file. Various type of expansion are
applied to the file names:
==================== ======================================
Expansion Description
==================== ======================================
Tilde An initial component of ``~`` or
``~user`` is replaced by that *user*'s
home directory.
Environment variable Substrings of the form ``$name`` or
``${name}`` are replaced by the value
of environment variable *name*.
==================== ======================================
Where more than one type of expansion is used in the same
string, they are applied in the order given in the above
table.
Example: If the environment variable *MYSELF* has been set
to the "david", then ``'~$MYSELF/out.nc'`` is equivalent to
``'~david/out.nc'``.
fmt : str, optional
The format of the output file. One of:
===================== ================================================
fmt Description
===================== ================================================
``'NETCDF3_CLASSIC'`` Output to a CF-netCDF3 classic format file
``'NETCDF3_64BIT'`` Output to a CF-netCDF3 64-bit offset format file
``'NETCDF4_CLASSIC'`` Output to a CF-netCDF4 classic format file
``'NETCDF4'`` Output to a CF-netCDF4 format file
``'CFA3'`` Output to a CFA-netCDF3 classic format file
``'CFA4'`` Output to a CFA-netCDF4 format file
===================== ================================================
By default the *fmt* is ``'NETCDF3_CLASSIC'``. Note that the
netCDF3 formats may be slower than any of the other options.
overwrite: bool, optional
If False then raise an exception if the output file
pre-exists. By default a pre-existing output file is over
written.
verbose : bool, optional
If True then print one-line summaries of each field written.
cfa_options : dict, optional
A dictionary giving parameters for configuring the output
CFA-netCDF file:
========== ===============================================
Key Value
========== ===============================================
``'base'`` * If ``None`` (the default) then file names
within CFA-netCDF files are stored with
absolute paths.
* If set to an empty string then file names
within CFA-netCDF files are given relative to
the directory or URL base containing the
output CFA-netCDF file.
* If set to a string then file names within
CFA-netCDF files are given relative to the
directory or URL base described by the
value. For example: ``'../archive'``.
========== ===============================================
By default no parameters are specified.
mode : str, optional
Specify the mode of write access for the output file. One of:
======= ==================================================
mode Description
======= ==================================================
``'w'`` Create the file. If it already exists and
*overwrite* is True then the file is deleted prior
to being recreated.
======= ==================================================
By default the file is opened with write access mode ``'w'``.
datatype : dict, optional
Specify data type conversions to be applied prior to writing
data to disk. Arrays with data types which are not specified
remain unchanged. By default, array data types are preserved.
**Example:**
To convert 64 bit floats and integers to their 32 bit
counterparts: ``datatype={numpy.dtype(float):
numpy.dtype('float32'), numpy.dtype(int):
numpy.dtype('int32')}``.
single : bool, optional
Write 64-bit floats as 32-bit floats and 64-bit integers as
32-bit integers. By default, input data types are
preserved. Note that ``single=True`` is equivalent to
``datatype={numpy.dtype('float64'): numpy.dtype('float32'),
numpy.dtype('int64'): numpy.dtype('int32')}``.
double : bool, optional
Write 32-bit floats as 64-bit floats and 32-bit integers as
64-bit integers. By default, input data types are
preserved. Note that ``double=True`` is equivalent to
``datatype={numpy.dtype('float32'): numpy.dtype('float64'),
numpy.dtype('int32'): numpy.dtype('int64')}``.
:Returns:
None
:Raises:
IOError :
If *overwrite* is False and the output file pre-exists.
ValueError :
If a field does not have information required to write certain
aspects of a CF-netCDF file.
:Examples:
>>> f
[<CF Field: air_pressure(30, 24)>,
<CF Field: u_compnt_of_wind(19, 29, 24)>,
<CF Field: v_compnt_of_wind(19, 29, 24)>,
<CF Field: potential_temperature(19, 30, 24)>]
>>> write(f, 'file')
>>> type(f)
<class 'cf.field.FieldList'>
>>> type(g)
<class 'cf.field.Field'>
>>> cf.write([f, g], 'file.nc', verbose=True)
[<CF Field: air_pressure(30, 24)>,
<CF Field: u_compnt_of_wind(19, 29, 24)>,
<CF Field: v_compnt_of_wind(19, 29, 24)>,
<CF Field: potential_temperature(19, 30, 24)>]
'''
compress = int(compress)
zlib = bool(compress)
if fmt not in ('NETCDF3_CLASSIC', 'NETCDF3_64BIT', 'CFA3',
'NETCDF4', 'NETCDF4_CLASSIC', 'CFA4'):
raise ValueError("Unknown output file format: {0}".format(fmt))
if compress and fmt in ('NETCDF3_CLASSIC', 'NETCDF3_64BIT', 'CFA3'):
raise ValueError("Can't compress {0} format file".format(fmt))
if least_significant_digit and fmt in ('CFA3', 'CFA4'):
raise ValueError("Can't truncate data variables in {0} format file".format(fmt))
# ----------------------------------------------------------------
# Set up non-global attributes
# ----------------------------------------------------------------
if variable_attributes:
if isinstance(variable_attributes, basestring):
variable_attributes = set((variable_attributes,))
else:
variable_attributes = set(variable_attributes)
else:
variable_attributes = set()
# ----------------------------------------------------------------
# Set up data type conversions
# ----------------------------------------------------------------
dtype_conversions = {numpy_dtype(bool) : numpy_dtype('int32'),
numpy_dtype(object): numpy_dtype(float)}
if datatype:
if single:
raise ValueError("Can't set datatype and single")
if double:
raise ValueError("Can't set datatype and double")
dtype_conversions.update(datatype)
else:
if single and double:
raise ValueError("Can't set single and double")
if single:
dtype_conversions[numpy_dtype(float)] = numpy_dtype('float32')
dtype_conversions[numpy_dtype(int)] = numpy_dtype('int32')
if double:
dtype_conversions[numpy_dtype('float32')] = numpy_dtype(float)
dtype_conversions[numpy_dtype('int32')] = numpy_dtype(int)
datatype = dtype_conversions
if not unlimited:
unlimited = ()
# ----------------------------------------------------------------
# Initialize dictionary of useful global variables
# ----------------------------------------------------------------
g = {'netcdf' : None, # - netCDF4.Dataset instance
#-----------------------------
'nc' : {}, # - Map netCDF variable names
# to netCDF4.Variable
# instances
'ncdim_to_size' : {}, # - Map netCDF dimension names
# to netCDF dimension sizes
'ncpdim_to_size' : {}, # - Dictionary of PARTITION
# dimension sizes keyed by
# netCDF dimension names.
'seen' : {}, # - Dictionary of netCDF
# variable names and netCDF
# dimensions keyed by items
# of the field (such as a
# coordinate or a coordinate
# reference).
# -----------------------------
'ncvar_names' : set(()), # - Set of all netCDF
# dimension and netCDF
# variable names.
'global_properties' : set(()), # - Set of global or
# non-standard CF properties
# which have identical
# values across all input
# fields.
#-----------------------------
'variable_attributes': variable_attributes,
#-----------------------------
'dimN' : 1, # - Counter
'auxN' : 1, # - Counter
'scalarN' : 1, # - Counter
'cmN' : 1, # - Counter
'dataN' : 1, # - Counter
'gmN' : 1, # - Counter
'bndN' : 1, # - Counter
'bnddimN' : 1, # - Counter
'strlenN' : 1, # - Counter
'partition_arrayN' : 1, # - Counter
'partitionN' : 1, # - Counter
# -----------------------------------------------------------
# CFA parameters
# -----------------------------------------------------------
'cfa' : False, # - flag to use the CFA
# convention, or not.
'cfa_options': {}, # -
'CFA_ncdims' : set(()), # - set of all private CFA
# netCDF dimension names.
'CFAdimN' : 1, # - Counter
# -----------------------------------------------------------
# Compression/endian
# -----------------------------------------------------------
'compression' : {'zlib' : zlib,
'complevel' : compress,
'fletcher32' : fletcher32,
'shuffle' : not no_shuffle},
'endian' : endian,
'least_significant_digit': least_significant_digit,
# -----------------------------------------------------------
# CF properties which need not be set on bounds if they're
# set on the parent coordinate
# -----------------------------------------------------------
'omit_bounds_properties': ('units', 'standard_name', 'axis',
'positive', 'calendar', 'month_lengths',
'leap_year', 'leap_month'),
'least_significant_digit': least_significant_digit,
# ------------------------------------------------------------
# Specify data type conversions to be applied prior to writing
# ------------------------------------------------------------
'datatype': datatype,
# ------------------------------------------------------------
# Specify unit conversions to be applied prior to writing
# ------------------------------------------------------------
'reference_datetime': reference_datetime,
# ------------------------------------------------------------
#
# ------------------------------------------------------------
'unlimited': unlimited,
}
if fmt == 'CFA3':
g['cfa'] = True
fmt = 'NETCDF3_CLASSIC'
if cfa_options:
g['cfa_options'] = cfa_options
elif fmt == 'CFA4':
g['cfa'] = True
fmt = 'NETCDF4'
if cfa_options:
g['cfa_options'] = cfa_options
g['fmt'] = fmt
# ---------------------------------------------------------------
# Flatten the sequence of intput fields
# ---------------------------------------------------------------
fields = FieldList(flat(fields))
# ---------------------------------------------------------------
# Still here? Open the output netCDF file.
# ---------------------------------------------------------------
# if mode != 'w':
# raise ValueError("Can only set mode='w' at the moment")
filename = os_path_expanduser(os_path_expandvars(filename))
if mode == 'w' and isfile(filename):
if not overwrite:
raise IOError(
"Can't write to an existing file unless overwrite=True: {0}".format(
abspath(filename)))
if not os.access(filename, os.W_OK):
raise IOError(
"Can't overwrite an existing file without permission: {0}".format(
abspath(filename)))
_close_netcdf_file(filename)
remove(filename)
#--- End: if
g['netcdf'] = _open_netcdf_file(filename, mode, fmt)
#netCDF4.Dataset(filename, mode, format=fmt)
# ---------------------------------------------------------------
# Set the fill mode for a Dataset open for writing to off. This
# will prevent the data from being pre-filled with fill values,
# which may result in some performance improvements.
# ---------------------------------------------------------------
g['netcdf'].set_fill_off()
# ---------------------------------------------------------------
# Write global properties to the file first. This is important as
# doing it later could slow things down enormously. This function
# also creates the g['global_properties'] set, which is used in
# the _write_a_field function.
# ---------------------------------------------------------------
_create_global_properties(fields, g=g)
# ---------------------------------------------------------------
# ---------------------------------------------------------------
for f in fields:
# Set HDF chunking
org_chunks = f.HDF_chunks(HDF_chunks)
default_chunks = f.HDF_chunks()
chunks = org_chunks.copy()
shape = f.shape
for i, size in org_chunks.iteritems():
if size is None:
size = default_chunks[i]
dim_size = shape[i]
if size is None or size > dim_size:
size = dim_size
chunks[i] = size
#--- End: for
f.HDF_chunks(chunks)
# Write the field
_write_a_field(f, g=g)
# Reset HDF chunking
f.HDF_chunks(org_chunks)
if verbose:
for e in f:
print repr(e)
#-- End: for
# ---------------------------------------------------------------
# Write all of the buffered data to disk
# ---------------------------------------------------------------
g['netcdf'].close()
#--- End: def
def _check_name(base, counter, g=None, dimsize=None, cfa=False):
'''
:Parameters:
base : str
counter : int
g : dict
dimsize : int, optional
cfa : bool, optional
:Returns:
ncvar : str
NetCDF dimension name or netCDF variable name.
counter : int
'''
ncvar_names = g['ncvar_names']
if dimsize is not None:
if not cfa:
if base in ncvar_names and dimsize == g['ncdim_to_size'][base]:
# Return the name of an existing netCDF dimension with
# this size
return base, counter
elif base in g['CFA_ncdims']:
# Return the name of an existing private CFA-netCDF
# dimension with this size
return base, counter
#--- End: if
if base in ncvar_names:
ncvar = '%(base)s_%(counter)d' % locals()
while ncvar in ncvar_names:
counter += 1
ncvar = '%(base)s_%(counter)d' % locals()
else:
ncvar = base
ncvar_names.add(ncvar)
return ncvar, counter
#--- End: def
def _write_attributes(netcdf_var, netcdf_attrs):
'''
:Parameters:
netcdf_var : netCDF4.Variable
netcdf_attrs : dict
:Returns:
None
:Examples:
'''
if hasattr(netcdf_var, 'setncatts'):
# Use the faster setncatts
netcdf_var.setncatts(netcdf_attrs)
else:
# Otherwise use the slower setncattr
for attr, value in netcdf_attrs.iteritems():
netcdf_var.setncattr(attr, value)
#--- End: def
def _character_array(array):
'''
Convert a numpy string array to a numpy character array wih an extra
trailing dimension.
:Parameters:
array : numpy array
:Returns:
out : numpy array
:Examples:
>>> print a, a.shape, a.dtype.itemsize
['fu' 'bar'] (2,) 3
>>> b = _character_array(a)
>>> print b, b.shape, b.dtype.itemsize
[['f' 'u' ' ']
['b' 'a' 'r']] (2, 3) 1
>>> print a, a.shape, a.dtype.itemsize
[-- 'bar'] (2,) 3
>>> b = _character_array(a)
>>> print b, b.shape, b.dtype.itemsize
[[-- -- --]
['b' 'a' 'r']] (2, 3) 1
'''
strlen = array.dtype.itemsize
shape = array.shape
new = numpy_ma_empty(shape + (strlen,), dtype='S1')
for index in numpy_ndindex(shape):
value = array[index]
if value is numpy_ma_masked:
new[index] = numpy_ma_masked
else:
new[index] = tuple(value.ljust(strlen, ' '))
#--- End: for
return new
#--- End: def
def _datatype(variable, g=None):
'''
Return the netCDF4.createVariable datatype corresponding to the
datatype of the array of the input variable
For example, if variable.dtype is 'float32', then 'f4' will be
returned.
Numpy string data types will return 'S1' regardless of the numpy
string length. This means that the required conversion of
multi-character datatype numpy arrays into single-character datatype
numpy arrays (with an extra trailing dimension) is expected to be done
elsewhere (currently in the _create_netcdf_variable function).
If the input variable has no `!dtype` attribute (or it is None) then
'S1' is returned.
:Parameters:
variable :
Any object with a `!dtype` attribute whose value is a
`numpy.dtype` object or None.
g : dict
:Returns:
out : str
The netCDF4.createVariable datatype corresponding to the
datatype of the array of the input variable.
'''
if (not hasattr(variable, 'dtype') or
variable.dtype.char == 'S' or
variable.dtype is None):
return 'S1'
dtype = variable.dtype
convert_dtype = g['datatype']
new_dtype = convert_dtype.get(dtype, None)
if new_dtype is not None:
dtype = new_dtype
return '{0}{1}'.format(dtype.kind, dtype.itemsize)
#--- End: def
def _string_length_dimension(size, g=None):
'''
Create, if necessary, a netCDF dimension for string variables.
:Parameters:
size : int
g : dict
:Returns:
out : str
The netCDF dimension name.
'''
# ----------------------------------------------------------------
# Create a new dimension for the maximum string length
# ----------------------------------------------------------------
ncdim, g['strlenN'] = _check_name('strlen%d' % size, g['strlenN'],
dimsize=size, g=g)
if ncdim not in g['ncdim_to_size']:
# This string length dimension needs creating
g['ncdim_to_size'][ncdim] = size
g['netcdf'].createDimension(ncdim, size)
return ncdim
#--- End: def
def _random_hex_string(size=10):
'''
Return a random hexadecimal string with the given number of
characters.
:Parameters:
size : int, optional
The number of characters in the generated string.
:Returns:
out : str
The hexadecimal string.
:Examples:
>>> _random_hex_string()
'C3eECbBBcf'
>>> _random_hex_string(6)
'7a4acc'
'''
return ''.join(random.choice(hexdigits) for i in xrange(size))
#--- End: def
def _cfa_dimension(size, g=None):
'''
Write a private CFA dimension to the netCDF file, unless one for the
given size already exists. In either case returns the netCDF dimension
name.
.. note:: This function updates ``g['CFA_ncdims']``,
``g['ncvar_names']``, ``g['netcdf']``.
:Parameters:
size : int
The size of the private CFA dimension.
g : dict
:Returns:
out : str
The netCDF dimension name.
:Examples:
>>> _cfa_dimension(10, g=g)
'cfa10'
'''
ncdim, g['CFAdimN'] = _check_name('cfa%d' % size, g['CFAdimN'], g=g,
dimsize=size, cfa=True)
if ncdim not in g['CFA_ncdims']:
g['CFA_ncdims'].add(ncdim)
g['netcdf'].createDimension(ncdim, size)
return ncdim
#--- End: def
def _write_cfa_variable(ncvar, ncdimensions, netcdf_attrs, data, g=None):
'''
Write a CFA variable to the netCDF file.
Any CFA private variables required will be autmatically created and
written to the file.
:Parameters:
ncvar : str
The netCDF name for the variable.
ncdimensions : sequence of str
netcdf_attrs : dict
data : cf.Data
g : dict
:Returns:
None
:Examples:
'''
fill_value = data.fill_value # False, None speed?
g['nc'][ncvar] = g['netcdf'].createVariable(ncvar, _datatype(data, g=g), (),
fill_value=fill_value,
least_significant_digit=None,
endian=g['endian'],
**g['compression'])
netcdf_attrs['cf_role'] = 'cfa_variable'
netcdf_attrs['cfa_dimensions'] = ' '.join(ncdimensions)
# Create a dictionary representation of the data object
data = data.copy()
axis_map = {}
for axis0, axis1 in zip(data._axes, ncdimensions):
axis_map[axis0] = axis1
data._change_axis_names(axis_map)
data._move_flip_to_partitions()
cfa_array = data.dumpd()
# Modify the dictionary so that it is suitable for JSON
# serialization
del cfa_array['_axes']
del cfa_array['shape']
del cfa_array['Units']
del cfa_array['dtype']
cfa_array.pop('_cyclic', None)
cfa_array.pop('_fill', None)
cfa_array.pop('fill_value', None)
pmshape = cfa_array.pop('_pmshape', None)
if pmshape:
cfa_array['pmshape'] = pmshape
pmaxes = cfa_array.pop('_pmaxes', None)
if pmaxes:
cfa_array['pmdimensions'] = pmaxes
pda_args = data.pda_args(revert_to_file=True)
base = g['cfa_options'].get('base', None)
if base is not None:
cfa_array['base'] = base
convert_dtype = g['datatype']
for attrs in cfa_array['Partitions']:
fmt = attrs.get('format', None)
if fmt is None:
# --------------------------------------------------------
# This partition has an internal sub-array. This could be
# a numpy array or a temporary FileArray object.
# --------------------------------------------------------
index = attrs.get('index', ())
if len(index) == 1:
index = index[0]
else:
index = tuple(index)
partition = data.partitions.matrix.item(index)
array = partition.dataarray(**pda_args)
# Convert data type
new_dtype = convert_dtype.get(array.dtype, None)
if new_dtype is not None:
array = array.astype(new_dtype)
shape = array.shape
ncdim_strlen = []
if array.dtype.kind == 'S':
# This is an array of strings
strlen = array.dtype.itemsize
if strlen > 1:
# Convert to an array of characters
array = _character_array(array)
# Get the netCDF dimension for the string length
ncdim_strlen = [_string_length_dimension(strlen, g=None)]
#--- End: if
# Create a name for the netCDF variable to contain the array
p_ncvar = 'cfa_'+_random_hex_string()
while p_ncvar in g['ncvar_names']:
p_ncvar = 'cfa_'+_random_hex_string()
#--- End: while
g['ncvar_names'].add(p_ncvar)
# Get the private CFA netCDF dimensions for the array.
cfa_dimensions = [_cfa_dimension(n, g=g) for n in array.shape]
# Create the private CFA variable and write the array to it
v = g['netcdf'].createVariable(p_ncvar, _datatype(array, g=g),
cfa_dimensions + ncdim_strlen,
fill_value=fill_value,
least_significant_digit=None,
endian=g['endian'],
**g['compression'])
_write_attributes(v, {'cf_role': 'cfa_private'})
v[...] = array
# Update the attrs dictionary.
#
# Note that we don't need to set 'part', 'dtype', 'units',
# 'calendar', 'dimensions' and 'reverse' since the
# partition's in-memory data array always matches up with
# the master data array.
attrs['subarray'] = {'shape' : shape,
'ncvar' : p_ncvar}
else:
# --------------------------------------------------------
# This partition has an external sub-array
# --------------------------------------------------------
# PUNITS, PCALENDAR: Change from Units object to netCDF
# string(s)
units = attrs.pop('Units', None)
if units is not None:
attrs['punits'] = units.units
if hasattr(units, 'calendar'):
attrs['pcalendar'] = units.calendar
# PDIMENSIONS:
p_axes = attrs.pop('axes', None)
if p_axes is not None:
attrs['pdimensions'] = p_axes
# REVERSE
p_flip = attrs.pop('flip', None)
if p_flip:
attrs['reverse'] = p_flip
# DTYPE: Change from numpy.dtype object to netCDF string
dtype = attrs['subarray'].pop('dtype', None)
if dtype is not None:
if dtype.kind != 'S':
attrs['subarray']['dtype'] = _convert_to_netCDF_datatype(dtype)
# FORMAT:
sfmt = attrs.pop('format', None)
if sfmt is not None:
attrs['subarray']['format'] = sfmt
#--- End: if
# LOCATION: Change from python to CFA indexing (i.e. range
# includes the final index)
attrs['location'] = [(x[0], x[1]-1) for x in attrs['location']]
# PART: Change from python to to CFA indexing (i.e. slice
# range includes the final index)
part = attrs.get('part', None)
if part:
p = []
for x, size in zip(part, attrs['subarray']['shape']):
if isinstance(x, slice):
x = x.indices(size)
if x[2] > 0:
p.append([x[0], x[1]-1, x[2]])
elif x[1] == -1:
p.append([x[0], 0, x[2]])
else:
p.append([x[0], x[1]+1, x[2]])
else:
p.append(tuple(x))
#--- End: for
attrs['part'] = str(p)
#--- End: if
if 'base' in cfa_array and 'file' in attrs['subarray']:
# Make the file name relative to base
attrs['subarray']['file'] = relpath(attrs['subarray']['file'],
cfa_array['base'])
#--- End: for
# Add the description (as a JSON string) of the partition array to
# the netcdf attributes.
netcdf_attrs['cfa_array'] = json.dumps(cfa_array,
default=_convert_to_builtin_type)
# Write the netCDF attributes to the file
_write_attributes(g['nc'][ncvar], netcdf_attrs)
#--- End: def
def _convert_to_netCDF_datatype(dtype):
'''
Convert a numpy.dtype object to a netCDF data type string.
:Parameters:
dtype : numpy.dtype
:Returns:
out : str
:Examples:
>>> _convert_to_netCDF_datatype(numpy.dtype('float32'))
'float'
>>> _convert_to_netCDF_datatype(numpy.dtype('float64'))
'double'
>>> _convert_to_netCDF_datatype(numpy.dtype('int8'))
'byte'
'''
if dtype.char is 'f':
return 'float'
if dtype.char is 'd':
return 'double'
if dtype.kind is 'i': # long int??
return 'int'
if dtype.char is 'S':
return 'char'
if dtype.char is 'b':
return 'byte'
if dtype.char is 'h':
return 'short'
raise TypeError("Ho hum de hum")
#--- End: def
def _convert_to_builtin_type(x):
'''
Convert a non-JSON-encodable object to a JSON-encodable built-in type.
Possible conversions are:
============== ============= ======================================
Input object Output object numpy data types covered
============== ============= ======================================
numpy.bool_ bool bool
numpy.integer int int, int8, int16, int32, int64, uint8,
uint16, uint32, uint64
numpy.floating float float, float16, float32, float64
============== ============= ======================================
:Parameters:
x :
:Returns:
out :
:Raises:
TypeError :
If *x* can't be converted to a JSON serializableis type.
:Examples:
>>> type(_convert_to_netCDF_datatype(numpy.bool_(True)))
bool
>>> type(_convert_to_netCDF_datatype(numpy.array([1.0])[0]))
double
>>> type(_convert_to_netCDF_datatype(numpy.array([2])[0]))
int
'''
if isinstance(x, numpy_bool_):
return bool(x)
if isinstance(x, numpy_integer):
return int(x)
if isinstance(x, numpy_floating):
return float(x)
raise TypeError(
"{0!r} object is not JSON serializable: {1!r}".format(type(x), x))
#--- End: def
def _grid_ncdimensions(f, key, axis_to_ncdim, g=None):
'''
Return a tuple of the netCDF dimension names for the axes of a
coordinate or cell measures objects.
:Parameters:
f : cf.Field
key : str
axis_to_ncdim : dict
Mapping of field axis identifiers to netCDF dimension names.
g : dict
:Returns:
out : tuple
A tuple of the netCDF dimension names.
'''
domain = f.domain
if domain.get(key).ndim == 0:
return ()
else:
# return tuple(g['axis_to_ncdim'][axis]
# for axis in domain._axes[key])
return tuple([axis_to_ncdim[axis] for axis in f.item_axes(key)])
#--- End: def
def _variable_ncvar(variable, default, counter, g=None):
'''
:Returns:
variable : cf.Variable
default : str
counter : int
g : dict
'''
ncvar = getattr(variable, 'ncvar', variable.identity(default=default))
return _check_name(ncvar, counter, g=g)
#--- End: def
def _write_dimension(ncdim, f, axis, axis_to_ncdim, unlimited=False, g=None):
'''Write a dimension to the netCDF file.
.. note:: This function updates ``axis_to_ncdim``, ``g['ncdim_to_size']``.
:Parameters:
ncdim: `str`
The netCDF dimension name.
f: `cf.Field`
axis: `str`
The field's axis identifier.
axis_to_ncdim: `dict`
Mapping of field axis identifiers to netCDF dimension names.
unlimited: `bool`, optional
If true then create an unlimited dimension. By default
dimensions are not unlimited.
g: `dict`
:Returns:
`None`
'''
size = f.axis_size(axis)
g['ncdim_to_size'][ncdim] = size
axis_to_ncdim[axis] = ncdim
if unlimited:
# Create an unlimited dimension
try:
g['netcdf'].createDimension(ncdim, None)
except RuntimeError as error:
message = "Can't create unlimited dimension in {} file ({}).".format(
g['netcdf'].file_format, error)
error = str(error)
if error == 'NetCDF: NC_UNLIMITED size already in use':
raise NetCDFError(
message+" Only one unlimited dimension allowed. Consider using a netCDF4 format.")
raise NetCDFError(message)
else:
try:
g['netcdf'].createDimension(ncdim, size)
except RuntimeError as error:
raise NetCDFError(
"Can't create dimension of size {} in {} file ({})".format(
size, g['netcdf'].file_format, error))
#--- End: def
def _change_reference_datetime(coord, g=None):
'''
:Parameters:
coord : cf.Coordinate
g : dict
:Returns:
out : cf.Coordinate
'''
if not coord.Units.isreftime:
return coord
reference_datetime = g['reference_datetime']
if not reference_datetime:
return coord
coord2 = coord.copy()
try:
coord2.reference_datetime = reference_datetime
except ValueError:
raise ValueError(
"Can't override coordinate reference date-time {0!r} with {1!r}".format(
coord.reference_datetime, reference_datetime))
else:
return coord2
#--- End: def
def _write_dimension_coordinate(f, axis, coord, key_to_ncvar, axis_to_ncdim,
g=None):
'''
Write a dimension coordinate and bounds to the netCDF file.
This also writes a new netCDF dimension to the file and, if required,
a new netCDF bounds dimension.
.. note:: This function updates ``axis_to_ndim``, ``g['dimN']``,
``g['seen']``.
:Parameters:
f : cf.Field
axis : str
coord : cf.DimensionCoordinate
key_to_ncvar : dict
Mapping of field item identifiers to netCDF dimension names.
axis_to_ncdim : dict
Mapping of field axis identifiers to netCDF dimension names.
g : dict
:Returns:
out : str
The netCDF name of the dimension coordinate.
'''
seen = g['seen']
coord = _change_reference_datetime(coord, g)
create = False
if not _seen(coord, g=g):
create = True
elif seen[id(coord)]['ncdims'] != ():
if seen[id(coord)]['ncvar'] != seen[id(coord)]['ncdims'][0]:
# Already seen this coordinate but it was an auxiliary
# coordinate, so it needs to be created as a dimension
# coordinate.
create = True
#--- End: if
if create:
ncdim, g['dimN'] = _variable_ncvar(coord, 'dim', g['dimN'], g=g)
# Create a new dimension, if it is not a scalar coordinate
if coord.ndim > 0:
unlimited = _unlimited(f, axis, g=g)
_write_dimension(ncdim, f, axis, axis_to_ncdim,
unlimited=unlimited, g=g)
ncdimensions = _grid_ncdimensions(f, axis, axis_to_ncdim, g=g)
# If this dimension coordinate has bounds then create the
# bounds netCDF variable and add the bounds or climatology
# attribute to the dictionary of extra attributes
extra = _write_coordinate_bounds(coord, ncdimensions, ncdim, g=g)
# Create a new dimension coordinate variable
_create_netcdf_variable(ncdim, ncdimensions, coord,
extra=extra, g=g)
else:
ncdim = seen[id(coord)]['ncvar']
key_to_ncvar[axis] = ncdim
# try: ### ????? why not always do this dch??
axis_to_ncdim[axis] = ncdim
# except KeyError:
# pass
return ncdim
#--- End: def
def _seen(variable, ncdims=None, g=None):
'''
Return True if a variable is logically equal any variable in the
g['seen'] dictionary.
If this is the case then the variable has already been written to the
output netCDF file and so we don't need to do it again.
If 'ncdims' is set then a extra condition for equality is applied,
namely that of 'ncdims' being equal to the netCDF dimensions (names
and order) to that of a variable in the g['seen'] dictionary.
When True is returned, the input variable is added to the g['seen']
dictionary.
.. note:: This function updates ``g['seen']``.
:Parameters:
variable :
ncdims : tuple, optional
g : dict
:Returns:
out : bool
True if the variable has already been written to the file,
False otherwise.
'''
seen = g['seen']
for key, value in seen.iteritems():
if ncdims is not None and ncdims != value['ncdims']:
# The netCDF dimensions (names and order) of the input
# variable are different to those of this variable in
# the 'seen' dictionary
continue
# Still here?
if variable.equals(value['variable']):
seen[id(variable)] = {'variable': variable,
'ncvar' : value['ncvar'],
'ncdims' : value['ncdims']}
return True
#--- End: for
return
#--- End: def
def _write_coordinate_bounds(coord, coord_ncdimensions, coord_ncvar, g=None):
'''
Create a coordinate's bounds netCDF variable, creating a new bounds
netCDF dimension if required. Return the bounds variable's netCDF
variable name.
.. note:: This function updates ``g['bnddimN']``, ``g['bndN']``,
``g['netcdf']``.
:Parameters:
coord : cf.Coordinate
coord_ncdimensions : tuple
The ordered netCDF dimension names of the coordinate's
dimensions (which do not include the bounds dimension).
coord_ncvar : str
The netCDF variable name of the coordinate.
g : dict
:Returns:
out : dict
:Examples:
>>> extra = _write_coordinate_bounds(c, ('dim2',), g=g)
'''
if not (coord._hasbounds and coord.bounds._hasData):
return {}
extra = {}
# Still here? Then this coordinate has a bounds attribute
# which contains data.
bounds = coord.bounds
size = bounds.shape[-1]
ncdim, g['bnddimN'] = _check_name('bounds%(size)d' % locals(),
g['bnddimN'], dimsize=size, g=g)
# Check if this bounds variable has not been previously
# created.
ncdimensions = coord_ncdimensions +(ncdim,)
if _seen(bounds, ncdimensions, g=g):
# This bounds variable has been previously created, so no
# need to do so again.
ncvar = g['seen'][id(bounds)]['ncvar']
else:
# This bounds variable has not been previously created, so
# create it now.
ncdim_to_size = g['ncdim_to_size']
if ncdim not in ncdim_to_size:
ncdim_to_size[ncdim] = size
g['netcdf'].createDimension(ncdim, size) #ncdim_to_size[ncdim])
ncvar = getattr(bounds, 'ncvar', '%s_bounds' % coord_ncvar)
ncvar, g['bndN'] = _check_name(ncvar, g['bndN'], g=g)
# Note that, in a field, bounds always have equal units to
# their parent coordinate
# Select properties to omit
omit = []
for prop in g['omit_bounds_properties']:
if coord.hasprop(prop):
omit.append(prop)
# Create the bounds netCDF variable
_create_netcdf_variable(ncvar, ncdimensions, bounds, omit=omit, g=g)
#--- End: if
if getattr(coord, 'climatology', None):
extra['climatology'] = ncvar
else:
extra['bounds'] = ncvar
return extra
#--- End: def
def _write_scalar_coordinate(f, axis, coord, coordinates,
key_to_ncvar, axis_to_ncscalar, g=None):
'''
Write a scalar coordinate and bounds to the netCDF file.
It is assumed that the input coordinate is has size 1, but this is not
checked.
If an equal scalar coordinate has already been written to the file
then the input coordinate is not written.
.. note:: This function updates ``key_to_ncvar``,
``axis_to_ncscalar``, ``g['scalarN']``.
:Parameters:
f : cf.Field
axis : str
The field's axis identifier for the scalar coordinate.
key_to_ncvar : dict
Mapping of field item identifiers to netCDF dimension names.
axis_to_ncscalar : dict
Mapping of field axis identifiers to netCDF scalar coordinate
variable names.
coordinates : list
g : dict
:Returns:
coordinates : list
The updated list of netCDF auxiliary coordinate names.
'''
coord = _change_reference_datetime(coord, g)
coord = coord.squeeze()
if not _seen(coord, (), g=g):
ncvar, g['scalarN'] = _variable_ncvar(coord, 'scalar', g['scalarN'],
g=g)
# If this scalar coordinate has bounds then create the
# bounds netCDF variable and add the bounds or climatology
# attribute to the dictionary of extra attributes
extra = _write_coordinate_bounds(coord, (), ncvar, g=g)
# Create a new auxiliary coordinate variable
_create_netcdf_variable(ncvar, (), coord, extra=extra, g=g)
else:
# This scalar coordinate has already been written to the
# file
ncvar = g['seen'][id(coord)]['ncvar']
axis_to_ncscalar[axis] = ncvar
key_to_ncvar[axis] = ncvar
coordinates.append(ncvar)
return coordinates
#--- End: def
def _write_auxiliary_coordinate(f, key, coord, coordinates,
key_to_ncvar, axis_to_ncdim, g=None):
'''
Write an auxiliary coordinate and its bounds to the netCDF file.
If an equal auxiliary coordinate has already been written to the file
then the input coordinate is not written.
.. note:: This function updates ``g['auxN']``.
:Parameters:
f : cf.Field
key : str
coord : cf.Coordinate
coordinates : list
key_to_ncvar : dict
Mapping of field item identifiers to netCDF dimension names.
axis_to_ncdim : dict
Mapping of field axis identifiers to netCDF dimension names.
g : dict
:Returns:
coordinates : list
The list of netCDF auxiliary coordinate names updated in
place.
:Examples:
>>> coordinates = _write_auxiliary_coordinate(f, 'aux2', coordinates, g=g)
'''
coord = _change_reference_datetime(coord, g)
ncdimensions = _grid_ncdimensions(f, key, axis_to_ncdim, g=g)
if _seen(coord, ncdimensions, g=g):
ncvar = g['seen'][id(coord)]['ncvar']
else:
ncvar, g['auxN'] = _variable_ncvar(coord, 'aux', g['auxN'], g=g)
# If this auxiliary coordinate has bounds then create the
# bounds netCDF variable and add the bounds or climatology
# attribute to the dictionary of extra attributes
extra = _write_coordinate_bounds(coord, ncdimensions, ncvar, g=g)
# Create a new auxiliary coordinate variable
_create_netcdf_variable(ncvar, ncdimensions, coord, extra=extra, g=g)
#--- End: if
key_to_ncvar[key] = ncvar
coordinates.append(ncvar)
return coordinates
#--- End: def
def _write_cell_measure(f, key, cm, cell_measures,
key_to_ncvar, axis_to_ncdim, g=None):
'''
Write an auxiliary coordinate and bounds to the netCDF file.
If an equal cell measure has already been written to the file then the
input coordinate is not written.
.. note:: This function updates ``g['cmN']``.
:Parameters:
f : cf.Field
The field containing the cell measure.
key : str
The domain identifier of the cell measure (e.g. 'cmo').
cell_measures : list
key_to_ncvar : dict
Mapping of field item identifiers to netCDF dimension names.
axis_to_ncdim : dict
Mapping of field axis identifiers to netCDF dimension names.
g : dict
:Returns:
cell_measures : list
The updated list of netCDF cell_measures 'measure: name'
pairings.
:Examples:
'''
ncdimensions = _grid_ncdimensions(f, key, axis_to_ncdim, g=g)
if _seen(cm, ncdimensions, g=g):
ncvar = g['seen'][id(cm)]['ncvar']
else:
ncvar, g['cmN'] = _variable_ncvar(cm, 'cm', g['cmN'], g=g)
if not hasattr(cm, 'measure'):
raise ValueError(
"Can't create a cell measure variable without a 'measure' attribute")
_create_netcdf_variable(ncvar, ncdimensions, cm, g=g)
#--- End: if
key_to_ncvar[key] = ncvar
# Update the cell_measures list
cell_measures.append('%s: %s' % (cm.measure, ncvar))
return cell_measures
#--- End: def
def _write_grid_mapping(f, coordref, grid_mapping, multiple_grid_mappings,
key_to_ncvar, g=None):
'''
Write a grid mapping georeference to the netCDF file.
.. note:: This function updates ``grid_mapping``, ``g['seen']``.
:Parameters:
f : cf.Field
coordref : cf.CoordinateReference
The grid mapping coordinate reference to write to the file.
grid_mapping : list
The list of netCDF grid_mapping names. This is updated in
place with the netCDF variable name of the input coordinate
reference object.
multiple_grid_mappings : bool
key_to_ncvar : dict
Mapping of field item identifiers to netCDF variable names.
g : dict
:Returns:
None
:Examples:
'''
if _seen(coordref, g=g):
# Use existing grid_mapping
ncvar = g['seen'][id(coordref)]['ncvar']
else:
# Create a new grid mapping
ncvar = getattr(coordref, 'ncvar', coordref.name)
ncvar, g['gmN'] = _check_name(ncvar, g['gmN'], g=g)
g['nc'][ncvar] = g['netcdf'].createVariable(ncvar, 'S1', (),
endian=g['endian'],
**g['compression'])
t = f.domain.canonical_ref(coordref)
# Add properties from key/value pairs
if hasattr(g['nc'][ncvar], 'setncatts'):
# Use the faster setncatts
for term, value in coordref.iteritems():
if value is None:
del t[term]
elif numpy_size(value) == 1:
t[term] = numpy_array(value, copy=False).item()
else:
t[term] = numpy_array(value, copy=False).tolist()
#--- End: for
if t.name is not None and 'grid_mapping_name' not in t:
t['grid_mapping_name'] = t.name
g['nc'][ncvar].setncatts(t)
else:
# Otherwise use the slower setncattr
for term, value in t.iteritems():
if value is None:
continue
if numpy_size(value) == 1:
value = numpy_array(value, copy=False).item()
else:
value = numpy_array(value, copy=False).tolist()
g['nc'][ncvar].setncattr(term, value)
#--- End: for
if t.name is not None:
g['nc'][ncvar].setncattr('grid_mapping_name', t.name)
#--- End: if
# Update the 'seen' dictionary. Note that grid mappings have
# no netCDF dimensions.
g['seen'][id(coordref)] = {'variable': coordref,
'ncvar' : ncvar,
'ncdims' : ()}
#--- End: if
# Update the grid_mapping list in place
if multiple_grid_mappings:
grid_mapping.append(ncvar+':')
coords = sorted([key_to_ncvar[key] for key in coordref.coords])
grid_mapping.extend(coords)
else:
grid_mapping.append(ncvar)
#--- End: def
def _create_netcdf_variable(ncvar, dimensions, cfvar, omit=(),
extra={}, data_variable=False, g=None):
'''
Create a netCDF variable from *cfvar* with name *ncvar* and dimensions
*ncdimensions*. The new netCDF variable's properties are given by
cfvar.properties(), less any given by the *omit* argument. If a new
string-length netCDF dimension is required then it will also be
created. The ``seen`` dictionary is updated for *cfvar*.
.. note:: This function updates ``g['strlenN']``,
``g['ncdim_to_size']``, ``g['netcdf']``, ``g['nc']``,
``g['seen']``.
:Parameters:
ncvar : str
The netCDF name of the variable.
dimensions : tuple
The netCDF dimension names of the variable
cfvar : cf.Variable
The coordinate, cell measure or field object to write to the
file.
omit : sequence of str, optional
extra : dict, optional
g : dict
:Returns:
None
'''
# Set the netCDF4.createVariable datatype
datatype = _datatype(cfvar, g=g)
ncdimensions = dimensions
if not cfvar._hasData:
data = None
else:
data = cfvar.Data
pda_args = data.pda_args()
if datatype == 'S1':
# --------------------------------------------------------
# Convert a string data type numpy array into a character
# data type ('S1') numpy array with an extra trailing
# dimension.
# --------------------------------------------------------
strlen = data.dtype.itemsize
if strlen > 1:
ncdim = _string_length_dimension(strlen, g=g)
ncdimensions = dimensions + (ncdim,)
data = data.copy()
new_axis = data._new_axis_identifier()
for partition in data.partitions.flat:
array = partition.dataarray(**pda_args)
# Convert the partition's string array into a
# character array. Note that it is very important
# to not change the mutable attributes of the
# partition object in-place.
if partition.part:
partition.part = partition.part + [slice(None)]
partition.axes = partition.axes + [new_axis]
partition.shape = partition.shape + [strlen]
partition.location = partition.location + [(0, strlen)]
partition.subarray = _character_array(array)
partition.close()
#--- End: for
data._axes = data._axes + [new_axis]
data._shape += (strlen,)
data._ndim += 1
data.dtype = datatype
pda_args['axes'] = data._axes
pda_args['keep_in_memory'] = True
#--- End: if
# Find the fill value (note that this is set in the call to
# netCDF4.createVariable, rather than with setncattr).
fill_value = cfvar.fill_value()
# Add simple properties (and units and calendar) to the netCDF
# variable
netcdf_attrs = cfvar.properties
for attr in ('units', 'calendar'):
value = getattr(cfvar, attr, None)
if value is not None:
netcdf_attrs[attr] = value
#--- End: for
netcdf_attrs.update(extra)
netcdf_attrs.pop('_FillValue', None)
for attr in omit:
netcdf_attrs.pop(attr, None)
is1d_coord = (isinstance(cfvar, Coordinate) and cfvar.ndim <= 1 or
isinstance(cfvar, CoordinateBounds) and cfvar.ndim <= 2)
if not g['cfa'] or data.in_memory or is1d_coord:
#---------------------------------------------------------
# Write a normal netCDF variable
#---------------------------------------------------------
# ------------------------------------------------------------
# Create a new netCDF variable and set the _FillValue
# ------------------------------------------------------------
if data_variable:
lsd = g['least_significant_digit']
else:
lsd = None
# Set HDF chunk sizes
chunksizes = [size for i, size in sorted(cfvar.HDF_chunks().items())]
if chunksizes == [None] * cfvar.ndim:
chunksizes = None
try:
g['nc'][ncvar] = g['netcdf'].createVariable(
ncvar,
datatype,
ncdimensions,
fill_value=fill_value,
least_significant_digit=lsd,
endian=g['endian'],
chunksizes=chunksizes,
**g['compression']
)
# except (TypeError, RuntimeError):
except RuntimeError as error:
error = str(error)
if error == 'NetCDF: Not a valid data type or _FillValue type mismatch':
raise ValueError(
"Can't write {0} data from {1!r} to a {2} file. Consider using a netCDF4 format or use the 'single' or 'datatype' parameters or change the datatype before writing.".format(
cfvar.dtype.name, cfvar, g['netcdf'].file_format))
message = "Can't create variable in {} file from {} ({})".format(g['netcdf'].file_format, cfvar, error)
if error == 'NetCDF: NC_UNLIMITED in the wrong index':
raise NetCDFError(
message+". Unlimited dimension must be the first (leftmost) dimension of the variable. Consider using a netCDF4 format.")
raise NetCDFError(message)
#--- End: try
_write_attributes(g['nc'][ncvar], netcdf_attrs)
#-------------------------------------------------------------
# Add data to the netCDF variable
#
# Note that we don't need to worry about scale_factor and
# add_offset, since if a partition's data array is *not* a
# numpy array, then it will have its own scale_factor and
# add_offset parameters which will be applied when the array
# is realised, and the python netCDF4 package will deal with
# the case when scale_factor or add_offset are set as
# properties on the variable.
# -------------------------------------------------------------
if data is not None:
# Find the missing data values, if any.
if not fill_value:
missing_data = None
else:
_FillValue = getattr(cfvar, '_FillValue', None)
missing_value = getattr(cfvar, 'missing_value', None)
missing_data = [value for value in (_FillValue, missing_value)
if value is not None]
pda_args['revert_to_file'] = True
# if data._isdt:
# # Convert date-time objects to numeric reference times
# pda_args['func'] = dt2rt
# # Turn off data type checking and partition updating
# pda_args['dtype'] = None
# pda_args['update'] = False
# #--- End: if
convert_dtype = g['datatype']
for partition in data.partitions.flat:
array = partition.dataarray(**pda_args)
# Convert data type
new_dtype = convert_dtype.get(array.dtype, None)
if new_dtype is not None:
array = array.astype(new_dtype)
# Check that the array doesn't contain any elements
# which are equal to any of the missing data values
if missing_data:
if partition.masked:
temp_array = array.compressed()
else:
temp_array = array
if numpy_intersect1d(missing_data, temp_array):
raise ValueError(
"ERROR: Can't write field when array has _FillValue or missing_value at unmasked point: {0!r}".format(
cfvar))
# Copy the array into the netCDF variable
g['nc'][ncvar][partition.indices] = array
partition.close()
#--- End: for
#--- End: if
# Update the 'seen' dictionary
g['seen'][id(cfvar)] = {'variable': cfvar,
'ncvar' : ncvar,
'ncdims' : dimensions}
return
elif data is not None:
#---------------------------------------------------------
# Write a CFA variable
#---------------------------------------------------------
_write_cfa_variable(ncvar, ncdimensions, netcdf_attrs, data, g=g)
return
#--- End: def
def _write_a_field(f, add_to_seen=False, allow_data_expand_dims=True,
remove_crap_axes=False, g=None):
'''
:Parameters:
f : cf.Field
add_to_seen : bool, optional
allow_data_expand_dims : bool, optional
g : dict
:Returns:
None
'''
seen = g['seen']
if add_to_seen:
id_f = id(f)
org_f = f
f = f.copy()
data_axes = f.data_axes()
domain = f.domain
dimensions = domain._axes
# Mapping of field axis identifiers to netCDF dimension names
axis_to_ncdim = {}
# Mapping of field axis identifiers to netCDF scalar coordinate
# variable names
axis_to_ncscalar = {}
# Mapping of field item identifiers to netCDF variable names
key_to_ncvar = {}
# Initialize the list of the field's auxiliary coordinates
coordinates = []
# For each of the field's axes ...
for axis in sorted(f.axes()):
dim_coord = f.dim(axis)
if dim_coord is not None:
# --------------------------------------------------------
# A dimension coordinate exists for this axis
# --------------------------------------------------------
if axis in data_axes:
# The data array spans this axis, so write the
# dimension coordinate to the file as a netCDF 1-d
# coordinate variable.
ncdim = _write_dimension_coordinate(f, axis, dim_coord,
key_to_ncvar, axis_to_ncdim,
g=g)
else:
# The data array does not span this axis (and
# therefore it must have size 1).
if f.items(role=('a', 'm'), axes=axis):
# There ARE auxiliary coordinates/cell measures
# which span this axis, so write the dimension
# coordinate to the file as a netCDF 1-d
# coordinate variable.
ncdim = _write_dimension_coordinate(f, axis, dim_coord,
key_to_ncvar,
axis_to_ncdim, g=g)
# Expand the field's data array to include this
# axis
f.expand_dims(0, axes=axis, i=True)
else:
# There are NO auxiliary coordinates/cell measures
# which span this axis, so write the dimension
# coordinate to the file as a netCDF scalar
# coordinate variable.
coordinates = _write_scalar_coordinate(f, axis, dim_coord,
coordinates,
key_to_ncvar,
axis_to_ncscalar,
g=g)
else:
# --------------------------------------------------------
# There is no dimension coordinate for this axis
# --------------------------------------------------------
if axis not in data_axes and f.items(role=('a', 'm'), axes=axis):
# The data array doesn't span the axis but an
# auxiliary coordinate or cell measure does, so expand
# the data array to include it.
f.expand_dims(0, axes=axis, i=True)
data_axes.append(axis)
# If the data array (now) spans this axis then create a
# netCDF dimension for it
if axis in data_axes:
try:
ncdim = domain.nc_dimensions[axis]
except (AttributeError, KeyError):
ncdim = 'dim'
ncdim, g['dimN'] = _check_name(ncdim, g['dimN'], g=g)
unlimited = _unlimited(f, axis, g=g)
_write_dimension(ncdim, f, axis, axis_to_ncdim,
unlimited=unlimited, g=g)
#--- End: for
# ----------------------------------------------------------------
# Create auxiliary coordinate variables, except those which might
# be completely specified elsewhere by a transformation.
# ----------------------------------------------------------------
# Initialize the list of 'coordinates' attribute variable values
# (each of the form 'name')
for key, aux_coord in sorted(f.auxs().items()):
coordinates = _write_auxiliary_coordinate(f, key, aux_coord,
coordinates, key_to_ncvar,
axis_to_ncdim, g=g)
# ----------------------------------------------------------------
# Create cell measures variables
# ----------------------------------------------------------------
# Initialize the list of 'cell_measures' attribute values (each of
# the form 'measure: name')
cell_measures = []
for key, clm in sorted(f.measures().items()):
cell_measures = _write_cell_measure(f, key, clm, cell_measures,
key_to_ncvar, axis_to_ncdim, g=g)
# ----------------------------------------------------------------
# Create grid mapping and formula terms variables
# ----------------------------------------------------------------
# Find out if there is more than one grid_mapping
multiple_grid_mappings = len([ref for ref in f.refs().values()
if ref.type == 'grid_mapping']) > 1
# Initialize the list of 'grid_mapping' attribute netCDF variable
# names
grid_mapping = []
multiple_grid_mappings = False
for coordref in f.refs().values():
coordref_type = coordref.type
if coordref_type == 'grid_mapping':
# --------------------------------------------------------
# This coordinate reference is a grid mapping
# --------------------------------------------------------
_write_grid_mapping(f, coordref, grid_mapping,
multiple_grid_mappings, key_to_ncvar, g=g)
elif coordref_type == 'formula_terms':
# --------------------------------------------------------
# This coordinate reference is a formula_terms
# --------------------------------------------------------
formula_terms = []
owning_coord = f.coord(coordref.name, exact=True)
if owning_coord is None:
# This formula_terms coordinate reference doesn't
# match up with an existing coordinate
formula_terms = None
# Still here? Then create the formula_terms attribute
# string
for term, value in coordref.iteritems():
if term in coordref.coord_terms:
# The value is a pointer to a coordinate
value = f.coord(value, exact=True)
if value is None:
# Do not create broken formula_terms
formula_terms = None
elif value is None:
# Do not create broken formula_terms
formula_terms = None
else:
# Make sure that value is a field
if not isinstance(value, Field):
data = getattr(value, 'data', None)
if data is None:
data = Data(value)
value = Field(data=data)
#--- End: if
if not _seen(value, g=g):
# The field hasn't yet been written to the
# file
value.ncvar = getattr(value, 'ncvar', term)
_write_a_field(value, add_to_seen=True, g=g)
#--- End: if
# Record the netCDF variable name of the value
# formula_terms string
if formula_terms is not None:
ncvar = seen[id(value)]['ncvar']
formula_terms.append('%(term)s: %(ncvar)s' % locals())
#--- End: for
# Add the formula_terms attribute to the output
# variable
if formula_terms:
ncvar = seen[id(owning_coord)]['ncvar']
g['nc'][ncvar].setncattr('formula_terms',
' '.join(formula_terms))
else:
raise ValueError("Ooops: unknown coordinate reference type: %s" %
coordref_type)
#--- End: for
# ----------------------------------------------------------------
# Create ancillary variables
# ----------------------------------------------------------------
# Initialize the list of 'ancillary_variables' attribute
# values
ancillary_variables = []
if hasattr(f, 'ancillary_variables'):
for av in f.ancillary_variables:
_write_a_field(av, add_to_seen=True, allow_data_expand_dims=False,
remove_crap_axes=1, g=g)
ancillary_variables.append(seen[id(av)]['ncvar'])
# ----------------------------------------------------------------
# Create the data variable
# ----------------------------------------------------------------
ncvar = getattr(f, 'ncvar', f.identity(default='data'))
ncvar, g['dataN'] = _check_name(ncvar, g['dataN'], g=g)
# axis_to_ncdim = g['axis_to_ncdim']
# axis_to_ncscalar = g['axis_to_ncscalar']
ncdimensions = tuple([axis_to_ncdim[axis] for axis in f.data_axes()])
# for dim in domain._axes['data'])
extra = {}
# Cell measures
if cell_measures:
extra['cell_measures'] = ' '.join(cell_measures)
# Auxiliary/scalar coordinates
if coordinates:
extra['coordinates'] = ' '.join(coordinates)
# Grid mapping
if grid_mapping:
extra['grid_mapping'] = ' '.join(grid_mapping)
# Ancillary variables
if ancillary_variables:
extra['ancillary_variables'] = ' '.join(ancillary_variables)
# Flag values
if hasattr(f, 'flag_values'):
extra['flag_values'] = f.flag_values
# Flag masks
if hasattr(f, 'flag_masks'):
extra['flag_masks'] = f.flag_masks
# Flag meanings
if hasattr(f, 'flag_meanings'):
extra['flag_meanings'] = ' '.join(f.flag_meanings)
# Cell methods
cell_methods = getattr(f, 'cell_methods', None)
if cell_methods is not None:
cell_methods = cell_methods.set_axes(f, override=False)
cell_methods = cell_methods.netcdf_names(
axis_to_ncdim, axis_to_ncscalar)
extra['cell_methods'] = str(cell_methods)
# Create a new data variable
_create_netcdf_variable(ncvar, ncdimensions, f,
omit=g['global_properties'],
extra=extra,
data_variable=True,
g=g)
# Update the 'seen' dictionary, if required
if add_to_seen:
g['seen'][id_f] = {'variable': org_f,
'ncvar' : ncvar,
'ncdims' : ncdimensions}
#--- End: def
def _unlimited(f, axis, g=None):
'''
'''
unlimited = f.unlimited().get(axis)
if unlimited is None:
unlimited = False
for u in g['unlimited']:
if f.axis(u) == axis:
unlimited = True
break
return unlimited
#--- End: def
def _create_global_properties(fields, g=None):
'''
Find the netCDF global properties from all of the input fields and
write them to the netCDF4.Dataset.
.. note:: This function updates ``g['global_properties']``.
:Parameters:
fields : cf.FieldList
g : dict
:Returns:
None
'''
# Data variable properties, as defined in Appendix A, without
# those which are not simple.
data_properties = set(('add_offset',
'cell_methods',
'_FillValue',
'flag_masks',
'flag_meanings',
'flag_values',
'long_name',
'missing_value',
'scale_factor',
'standard_error_multiplier',
'standard_name',
'units',
'valid_max',
'valid_min',
'valid_range',
))
# Global properties, as defined in Appendix A
global_properties = set(('comment',
'Conventions',
'history',
'institution',
'references',
'source',
'title',
))
# Put all non-standard CF properties (i.e. those not in the
# data_properties set) into the global_properties set, but
# omitting those which have been requested to be on variables.
for f in fields:
for attr in set(f._simple_properties()) - global_properties - g['variable_attributes']:
if attr not in data_properties:
global_properties.add(attr)
#--- End: for
# Remove properties from the new global_properties set which
# have different values in different fields
f0 = fields[0]
for prop in tuple(global_properties):
if not f0.hasprop(prop):
global_properties.remove(prop)
continue
prop0 = f0.getprop(prop)
if len(fields) > 1:
for f in fields[1:]:
if (not f.hasprop(prop) or
not equals(f.getprop(prop), prop0, traceback=False)):
global_properties.remove(prop)
break
#--- End: for
# Write the global properties to the file
Conventions = __Conventions__
if g['cfa']:
Conventions += ' CFA'
g['netcdf'].setncattr('Conventions', Conventions)
for attr in global_properties - set(('Conventions',)):
g['netcdf'].setncattr(attr, f0.getprop(attr))
g['global_properties'] = global_properties
#--- End: def
|