1/*! \page tutorial.dox The NetCDF-C Tutorial 2 3\brief The NetCDF-C Tutorial 4 5 6\tableofcontents 7 8\section sec_tut Tutorial Documentation 9 10This page contains references to various other NetCDF background and tutorial pages. 11 12\subsection tutorial_pages Tutorial Pages 13 14- \subpage netcdf_working_with_netcdf_files 15- \subpage tutorial_ncids 16- \subpage creating 17- \subpage reading_known 18- \subpage reading_unknown 19- \subpage accessing_subsets 20 21\subsection background_further_reading Background and Further Reading 22 23- \ref what_is_netcdf 24- \subpage netcdf_documentation 25 26\subsection sub_sec_netcdf_data_model NetCDF Data Model: 27- \subpage netcdf_data_model 28- \ref classic_model 29- \ref enhanced_model 30- \subpage unlimited_dims 31- \subpage groups 32- \subpage user_defined_types 33- \subpage string_type 34 35\subsection sub_sec_important_conventions Important Conventions: 36- \subpage fill_values 37 38\subsection sub_sec_tools_for_netcdf Tools for NetCDF: 39- \ref netcdf_utilities 40- \ref external_netcdf_tools 41 42\subsection sub_sec_programming_with_netcdf Programming with NetCDF: 43- \subpage netcdf_apis 44- \subpage error_handling 45- \subpage interoperability_hdf5 46- \subpage parallel_io 47 48\subsection sub_sec_example_programs Example Programs: 49- \subpage examples1 50 51\page netcdf_working_with_netcdf_files Working with NetCDF Files from the command line. 52 53\brief Options for working with netcdf-formatted files from the command line or with an external program. 54 55\tableofcontents 56 57\section netcdf_utilities The NetCDF Utilities 58 59The ncdump command reads a netCDF file and outputs text in a format 60called CDL. The ncgen command reads a text file in CDL format, and 61generates a netCDF data file. The nccopy command copies a netCDF file 62from one binary format to another, optionally changing compression and 63chunksize settings. 64 65\subsection netcdf_utilities_ncdump ncdump 66 67The utility ncdump can be used to show the contents of netCDF 68files. By default, ncdump shows the CDL description of the file. This 69CDL description can be fed into ncgen to create the data file. 70 71See also: \ref ncdump_guide 72 73\subsection netcdf_utilities_ncgen ncgen 74 75The ncgen utility can take an ASCII input file, in CDL format, and 76generate a binary netCDF file. It is the opposite of ncdump. 77 78See also: 79- \ref guide_ncgen 80- \ref guide_ncgen3 81 82\subsection netcdf_utilities_nccopy nccopy 83 84The nccopy utility can copy a netCDF file, changing binary format, 85chunk sizes, compression, and other storage settings. 86 87See also: \ref guide_nccopy 88 89\section external_netcdf_tools Tools for Manipulating NetCDF Files 90 91Many existing 3rd-party software applications can read and manipulate netCDF 92files. Before writing your own program, check to see if any existing 93programs meet your needs. 94 95Three utilities come with the netCDF distribution: ncdump, ncgen, and 96nccopy. (See \ref netcdf_utilities). 97 98The following general-purpose tools have been found to be useful in 99many situations. Some of the tools on this list are developed at 100Unidata. The others are developed elsewhere, and we can make no 101guarantees about their continued availability or success. All of these 102tools are open-source. 103- <a href="http://www.unidata.ucar.edu/software/udunits">UDUNITS</a> - Unidata library to help with scientific units. 104- <a href="http://www.unidata.ucar.edu/software/idv">IDV</a> - Unidata's Integrated Data Viewer, a 3D visualization and analysis package (Java based). 105- <a href="http://www.ncl.ucar.edu">NCL</a> - NCAR Command Language, a graphics and data manipulation package. 106- <a href="http://grads.iges.org/grads/grads.html">GrADS</a> - The Grid Analysis and Display System package. 107- <a href="http://nco.sourceforge.net">NCO</a> - NetCDF Command line Operators, tools to manipulate netCDF files. 108 109A <a href="http://www.unidata.ucar.edu/netcdf/software.html">list of 110netCDF tools</a> that we know about can be found on the website. If 111you know of any that should be added to this list, send email to 112support-netcdf@unidata.ucar.edu. 113 114\page netcdf_apis The NetCDF Programming APIs 115 116Unidata supports netCDF APIs in C, C++, Fortran 77, Fortran 90, and 117Java. 118 119The <a href="http://www.unidata.ucar.edu/software/netcdf-java">netCDF 120Java</a> API is a complete implementation of netCDF in Java. It is 121distributed independently of the other APIs. If you are writing web 122server software, you should certainly be doing so in Java. 123 124The main netCDF distribution contains the C library and the netCDF 125utilities, ncgen/ncdump/nccopy. 126 127The C++, Fortran 77 and Fortran 90 APIs are distributed separately 128from the C library. The C library must be installed before any of 129these APIs may be built. They depend on the C API. 130 131Due to the nature of C++ and Fortran 90, users of those languages can 132also use the C and Fortran 77 APIs (respectively) directly. 133 134Full documentation exists for each API (see \ref 135netcdf_documentation). 136 137In addition, many other language APIs exist, including Perl, Python, 138and Ruby. Most of these APIs were written and supported by netCDF 139users. Some of them are listed on 140the <a href="http://www.unidata.ucar.edu/netcdf/software.html">netCDF 141software page</a>. Since these generally use the C API, they should 142work well with netCDF-4/HDF5 files, but the maintainers of the APIs 143must add support for netCDF-4 advanced features. 144 145In addition to the main netCDF-3 C API, there is an additional (older) 146C API, the netCDF-2 API. This API produces exactly the same files as 147the netCDF-3 API - only the API is different. That is, users can 148create either classic CDF-1 format files, the default, 64-bit offset 149files (CDF-2), 64-bit data files (CDF-5), or netCDF-4/HDF5 files. 150 151The version 2 API was the API before netCDF-3.0 came out. It is still 152fully supported, however. Programs written to the version 2 API will 153continue to work. The version 2 API is built with the netCDF library 154by default. 155 156Users writing new programs should use the netCDF-3 API, which contains 157better type checking, better error handling, and better documentation. 158 159The netCDF-2 API is provided for backward compatibility. Documentation 160for the netCDF-2 API can be found on the netCDF website, see 161http://www.unidata.ucar.edu/netcdf/old_docs/really_old/guide_toc.html. 162 163\page netcdf_documentation NetCDF Documentation 164 165Language specific programming guides are available for netCDF for the 166C, C++, Fortran 77, Fortran 90, and Java APIs: 167 168- C - The NetCDF C Interface Guide. 169- C++ - The NetCDF C++ Interface Guide. 170- Fortran 77 - The NetCDF Fortran 77 Interface Guide. 171- Fortran 90 - The NetCDF Fortran 90 Interface Guide. 172- Java <a href="http://www.unidata.ucar.edu/software/netcdf-java/v2.1/NetcdfJavaUserManual.htm">The netCDF-Java Users Guide</a>. 173 174Man pages for the C, F77, and F90 interfaces, and ncgen and ncdump, 175are available on the documentation page of the netCDF web site 176(http://www.unidata.ucar.edu/netcdf/docs), and are installed with the 177libraries. 178 179The latest version of all netCDF documentation can always be found at 180the <a href="http://www.unidata.ucar.edu/netcdf/docs">netCDF 181documentation page</a>. 182 183\page netcdf_data_model The NetCDF Data Model 184 185\tableofcontents 186 187The netCDF data model is the way that we think about data. The data 188model of dimensions, variables, and attributes, which define the \ref 189classic_model, was extended starting with netCDF-4.0. The new \ref 190enhanced_model supports the classic model in a completely 191backward-compatible way, while allowing access to new features such as 192groups, multiple unlimited dimensions, and new types, including 193user-defined types. 194 195For maximum interoparability with existing code, new data should be 196created with the \ref classic_model. 197 198<p>\image html nc4-model.png "The NetCDF Enhanced Data Model" 199 200 201\section classic_model The Classic Model 202 203The classic netCDF data model consists of variables, dimensions, and 204attributes. This way of thinking about data was introduced with the 205very first netCDF release, and is still the core of all netCDF files. 206 207<p> 208\image html nc-classic-uml.png "NetCDF Classic Data Model" 209 210In version 4.0, the netCDF data model has been expanded. See \ref 211enhanced_model. 212 213<table> 214<tr><td> 215Variables 216</td><td> 217N-dimensional arrays of data. Variables in netCDF files can be one 218of six types (char, byte, short, int, float, double). 219</td></tr> 220 221<tr><td> 222Dimensions 223</td><td> 224describe the axes of the data arrays. A dimension has a name and a 225length. An unlimited dimension has a length that can be expanded at 226any time, as more data are written to it. NetCDF files can contain at 227most one unlimited dimension. 228</td></tr> 229 230<tr><td> 231Attributes 232</td><td> 233annotate variables or files with small notes or supplementary 234metadata. Attributes are always scalar values or 1D arrays, which can 235be associated with either a variable or the file as a whole. Although 236there is no enforced limit, the user is expected to keep attributes 237small. 238</td></tr> 239 240</table> 241 242\section enhanced_model The Enhanced Data Model 243 244With netCDF-4, the netCDF data model has been extended, in a backwards 245compatible way. 246 247The new data model, which is known as the “Common Data Model” is part 248of an effort here at Unidata to find a common engineering language for 249the development of scientific data solutions. It contains the 250variables, dimensions, and attributes of the classic data model, but 251adds: 252 253<ul> 254 255<li>groups - A way of hierarchically organizing data, similar to 256directories in a Unix file system. 257 258<li>user-defined types - The user can now define compound types 259(like C structures), enumeration types, variable length arrays, and 260opaque types. 261 262</ul> 263 264These features may only be used when working with a netCDF-4/HDF5 265file. Files created in classic formats cannot support 266groups or user-defined types (see \ref netcdf_format). 267 268<p>\image html nc4-model.png 269 270With netCDF-4/HDF5 files, the user may define groups, which may 271contain variables, dimensions, and attributes. In this way, a group 272acts as a container for the classic netCDF dataset. But netCDF-4/HDF5 273files can have many groups, organized hierarchically. 274 275Each file begins with at least one group, the root group. The user may 276then add more groups, receiving a new ncid for each group created. 277 278Since each group functions as a complete netCDF classic dataset, it is 279possible to have variables with the same name in two or more different 280groups, within the same netCDF-4/HDF5 data file. 281 282Dimensions have a special scope: they may be seen by all variables in 283their group, and all descendant groups. This allows the user to define 284dimensions in a top-level group, and use them in many sub-groups. 285 286Since it may be necessary to write code which works with all types of 287netCDF data files, we also introduce the ability to create 288netCDF-4/HDF5 files which follow all the rules of the classic netCDF 289model. That is, these files are in HDF5, but will not support multiple 290unlimited dimensions, user-defined types, groups, etc. They act just 291like a classic netCDF file. 292 293\section met_example Meteorological Example 294 295NetCDF can be used to store many kinds of data, but it was originally 296developed for the Earth science community. 297 298NetCDF views the world of scientific data in the same way that an 299atmospheric scientist might: as sets of related arrays. There are 300various physical quantities (such as pressure and temperature) located 301at points at a particular latitude, longitude, vertical level, and 302time. 303 304A scientist might also like to store supporting information, such as 305the units, or some information about how the data were produced. 306 307The axis information (latitude, longitude, level, and time) would be 308stored as netCDF dimensions. Dimensions have a length and a name. 309 310The physical quantities (pressure, temperature) would be stored as 311netCDF variables. Variables are N-dimensional arrays of data, with a 312name and an associated set of netCDF dimensions. 313 314It is also customary to add one variable for each dimension, to hold 315the values along that axis. These variables are called “coordinate 316variables.” The latitude coordinate variable would be a 317one-dimensional variable (with latitude as its dimension), and it 318would hold the latitude values at each point along the axis. 319 320The additional bits of metadata would be stored as netCDF attributes. 321 322Attributes are always single values or one-dimensional arrays. (This 323works out well for a string, which is a one-dimensional array of ASCII 324characters.) 325 326The pres_temp_4D_wr.c/pres_temp_4D_rd.c examples show 327how to write and read a file containing some four-dimensional pressure 328and temperature data, including all the metadata needed. 329 330\page fill_values Fill Values 331 332Sometimes there are missing values in the data, and some value is 333needed to represent them. 334 335For example, what value do you put in a sea-surface temperature 336variable for points over land? 337 338In netCDF, you can create an attribute for the variable (and of the 339same type as the variable) called “_FillValue” that contains a value 340that you have used for missing data. Applications that read the data 341file can use this to know how to represent these values. 342 343Using attributes it is possible to capture metadata that would 344otherwise be separated from the data. Various conventions have been 345established. By using a set of conventions, a data producer is more 346likely to produce files that can be easily shared within the research 347community, and that contain enough details to be useful as a long-term 348archive. Conventions also make it easier to develop software that 349interprets information represented in data, because a convention 350selects one conventional way to represent information when multiple 351equivalent representations are possible. 352 353For more information on _FillValue and other attribute conventions, 354see \ref attribute_conventions. 355 356Climate and meteorological users are urged to follow the Climate and 357Forecast (CF) metadata conventions when producing data files. For more 358information about the CF conventions, see http://cf-pcmdi.llnl.gov. 359 360\page error_handling Error Handling 361 362\addtogroup error 363 364Each netCDF function in the C, Fortran 77, and Fortran 90 APIs returns 3650 on success, in the tradition of C. 366 367When programming with netCDF in these languages, always check return 368values of every netCDF API call. The return code can be looked up in 369netcdf.h (for C programmers) or netcdf.inc (for Fortran programmers), 370or you can use the strerror function to print out an error 371message. (See nc_strerror/NF_STRERROR/NF90_STRERROR). 372 373In general, if a function returns an error code, you can assume it 374didn't do what you hoped it would. The exception is the NC_ERANGE 375error, which is returned by any of the reading or writing functions 376when one or more of the values read or written exceeded the range for 377the type. (For example if you were to try to read 1000 into an 378unsigned byte.) 379 380In the case of NC_ERANGE errors, the netCDF library completes the 381read/write operation, and then returns the error. The type conversion 382is handled like a C type conversion, whether or not it is within 383range. This may yield bad data, but the netCDF library just returns 384NC_ERANGE and leaves it up to the user to handle. (For more 385information about type conversion see Type Conversion). 386 387\page unlimited_dims Unlimited Dimensions 388 389Sometimes you don't know the size of all dimensions when you create a 390file, or you would like to arbitrarily extend the file along one of 391the dimensions. 392 393For example, model output usually has a time dimension. Rather than 394specifying that there will be forty-two output times when creating the 395file, you might like to create it with one time, and then add data for 396additional times, until you wanted to stop. 397 398For this purpose netCDF provides the unlimited dimension. By 399specifying a length of “unlimited” when defining a dimension, you 400indicate to netCDF that the dimension may be extended, and its length 401may increase. 402 403In netCDF classic files, there can only be one unlimited dimension, 404and it must be declared first in the list of dimensions for a 405variable. 406 407For programmers, the unlimited dimension will correspond with the 408slowest-varying dimension. In C this is the first dimension of an 409array, in Fortran, the last. 410 411The third example in this tutorial, pres_temp_4D, demonstrates how to 412write and read data one time step at a time along an unlimited 413dimension in a classic netCDF file. See pres_temp_4D. 414 415In netCDF-4/HDF5 files, any number of unlimited dimensions may be 416used, and there is no restriction as to where they appear in a 417variable's list of dimension IDs. 418 419\page examples1 NetCDF Example Programs 420 421\tableofcontents 422 423The netCDF example programs show how to use netCDF. 424 425In the netCDF distribution, the “examples” directory contains examples 426in C and CDL. The examples create, and then read, example data files 427of increasing complexity. 428 429There are three sets of netCDF classic example programs; corresponding 430examples are included with the netCDF Fortran and C++ APIs. 431- \ref example_simple_xy 432- \ref example_sfc_pres_temp 433- \ref example_pres_temp_4D 434 435Additionally, there is a example program demonstrating 436how to use the filter API. This is C only and only accessible 437when built with automake currently. 438- \ref example_filter 439 440Any existing netCDF applications can be converted to generate 441netCDF-4/HDF5 files. Simply change the file creation call to include 442the correct mode flag. 443 444In one of the netCDF classic examples which write a data file, change 445the nc_create() call so that ::NC_NETCDF4 is one of the flags set on 446the create. 447 448The corresponding read example will work without modification; netCDF 449will notice that the file is a NetCDF-4/HDF5 file, and will read it 450automatically, just as if it were a netCDF classic format file. 451 452In the example in this section we show some of the advanced features 453of netCDF-4. 454- \ref example_simple_nc4 455- \ref example_simple_xy_nc4 456 457The examples are built and run with the “make check” command. (See 458\ref building). 459 460The corresponding examples in each language create identical netCDF 461data files. For example, the C program sfc_pres_temp_wr.c produces the 462same data file as the Fortran 77 program sfc_pres_temp_wr.f. 463 464\section example_simple_xy The simple_xy Example 465 466This example is an unrealistically simple netCDF file, to demonstrate 467the minimum operation of the netCDF APIs. Users should seek to make 468their netCDF files more self-describing than this primitive example. 469- simple_xy_wr.c 470- simple_xy_rd.c 471 472As in all the netCDF tutorial examples, this example file can be 473created by C and by ncgen, which creates it from a CDL script. Both 474ncgen and the C example create identical files, “simple_xy.nc.” 475 476The simple_xy.nc data file contains two dimensions, “x” and “y”, and 477one netCDF variable, “data.” 478 479The CDL for this example is shown below. For more information on 480ncdump and ncgen see NetCDF Utilities. 481 482\code 483 netcdf simple_xy { 484 dimensions: 485 x = 6 ; 486 y = 12 ; 487 variables: 488 int data(x, y) ; 489 data: 490 491 data = 492 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 493 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 494 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 495 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 496 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 497 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71 ; 498 } 499\endcode 500 501\section example_sfc_pres_temp The sfc_pres_temp Example 502 503This example has been constructed for the meteorological mind. 504 505Suppose you have some data you want to write to a netCDF file. For 506example, you have one time step of surface temperature and surface 507pressure, on a 6 x 12 latitude longitude grid. 508 509To store this in netCDF, create a file, add two dimensions (latitude 510and longitude) and two variables (pressure and temperature). 511- sfc_pres_temp_wr.c 512- sfc_pres_temp_rd.c 513 514In this example we add some netCDF attributes, as is typical in 515scientific applications, to further describe the data. In this case we 516add a units attribute to every netCDF variable. 517 518In this example we also add additional netCDF variables to describe 519the coordinate system. These “coordinate variables” allow us to 520specify the latitudes and longitudes that describe the data grid. 521 522The CDL version of the data file, generated by ncdump, is shown below 523(see \ref netcdf_utilities). 524 525\code 526 netcdf sfc_pres_temp { 527 dimensions: 528 latitude = 6 ; 529 longitude = 12 ; 530 variables: 531 float latitude(latitude) ; 532 latitude:units = "degrees_north" ; 533 float longitude(longitude) ; 534 longitude:units = "degrees_east" ; 535 float pressure(latitude, longitude) ; 536 pressure:units = "hPa" ; 537 float temperature(latitude, longitude) ; 538 temperature:units = "celsius" ; 539 data: 540 541 latitude = 25, 30, 35, 40, 45, 50 ; 542 543 longitude = -125, -120, -115, -110, -105, -100, -95, -90, -85, -80, -75, -70 ; 544 545 pressure = 546 900, 906, 912, 918, 924, 930, 936, 942, 948, 954, 960, 966, 547 901, 907, 913, 919, 925, 931, 937, 943, 949, 955, 961, 967, 548 902, 908, 914, 920, 926, 932, 938, 944, 950, 956, 962, 968, 549 903, 909, 915, 921, 927, 933, 939, 945, 951, 957, 963, 969, 550 904, 910, 916, 922, 928, 934, 940, 946, 952, 958, 964, 970, 551 905, 911, 917, 923, 929, 935, 941, 947, 953, 959, 965, 971 ; 552 553 temperature = 554 9, 10.5, 12, 13.5, 15, 16.5, 18, 19.5, 21, 22.5, 24, 25.5, 555 9.25, 10.75, 12.25, 13.75, 15.25, 16.75, 18.25, 19.75, 21.25, 22.75, 24.25, 556 25.75, 557 9.5, 11, 12.5, 14, 15.5, 17, 18.5, 20, 21.5, 23, 24.5, 26, 558 9.75, 11.25, 12.75, 14.25, 15.75, 17.25, 18.75, 20.25, 21.75, 23.25, 24.75, 559 26.25, 560 10, 11.5, 13, 14.5, 16, 17.5, 19, 20.5, 22, 23.5, 25, 26.5, 561 10.25, 11.75, 13.25, 14.75, 16.25, 17.75, 19.25, 20.75, 22.25, 23.75, 562 25.25, 26.75 ; 563 } 564\endcode 565 566\section example_pres_temp_4D The pres_temp_4D Example 567 568This example expands on the previous example by making our 569two-dimensional data into four-dimensional data, adding a vertical 570level axis and an unlimited time step axis. 571- pres_temp_4D_wr.c 572- pres_temp_4D_rd.c 573 574Additionally, in this example the data are written and read one time 575step at a time, as is typical in scientific applications that use the 576unlimited dimension. 577 578The sample data file created by pres_temp_4D_wr can be examined with 579the utility ncdump (see \ref netcdf_utilities). 580 581\code 582 netcdf pres_temp_4D { 583 dimensions: 584 level = 2 ; 585 latitude = 6 ; 586 longitude = 12 ; 587 time = UNLIMITED ; // (2 currently) 588 variables: 589 float latitude(latitude) ; 590 latitude:units = "degrees_north" ; 591 float longitude(longitude) ; 592 longitude:units = "degrees_east" ; 593 float pressure(time, level, latitude, longitude) ; 594 pressure:units = "hPa" ; 595 float temperature(time, level, latitude, longitude) ; 596 temperature:units = "celsius" ; 597 data: 598 599 latitude = 25, 30, 35, 40, 45, 50 ; 600 601 longitude = -125, -120, -115, -110, -105, -100, -95, -90, -85, -80, -75, -70 ; 602 603 pressure = 604 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 605 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 606 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 607 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 608 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 609 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 610 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 611 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 612 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 613 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 614 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 615 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 616 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 617 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 618 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 619 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 620 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 621 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 622 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 623 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 624 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 625 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 626 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 627 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043 ; 628 629 temperature = 630 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 631 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 632 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 633 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 634 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 635 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 636 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 637 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 638 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 639 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 640 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 641 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 642 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 643 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 644 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 645 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 646 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 647 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 648 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 649 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 650 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 651 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 652 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 653 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152 ; 654 } 655\endcode 656 657\section example_simple_nc4 The simple_nc4 Example 658 659This 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. 660 661This example is only available in C for this version of netCDF-4. The example creates and then reads the file “simple_nc4.nc.” 662 663The 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. 664 665The 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. 666- simple_nc4_wr.c 667- simple_nc4_rd.c 668 669\section example_simple_xy_nc4 The simple_xy_nc4 Example 670 671This 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. 672 673(These are all HDF5 features. For more information see http://hdfgroup.org/HDF5/). 674 675This example is not yet available in C++. We hope to have the C++ example in a future release of netCDF. 676 677The example creates and then reads the file “simple_xy_nc4.nc.” 678 679The example program simple_xy_nc4_wr.c creates the example data file 680simple_xy_nc4.nc. The example program simple_xy_nc4_rd.c reads the 681data file. 682- simple_xy_nc4_wr.c 683- simple_xy_nc4_rd.c 684 685\section example_filter The filter example 686 687This example demonstrates how to write and read a variable that is 688compressed using, in this example, bzip2 compression. 689- filter_example.c 690 691\page interoperability_hdf5 Interoperability with HDF5 692 693NetCDF-4 allows some interoperability with HDF5. 694 695\section reading_with_hdf5 Reading and Editing NetCDF-4 Files with HDF5 696 697The HDF5 Files produced by netCDF-4 are perfectly respectable HDF5 698files, and can be read by any HDF5 application. 699 700NetCDF-4 relies on several new features of HDF5, including dimension 701scales. The HDF5 dimension scales feature adds a bunch of attributes 702to the HDF5 file to keep track of the dimension information. 703 704It is not just wrong, but wrong-headed, to modify these attributes 705except with the HDF5 dimension scale API. If you do so, then you will 706deserve what you get, which will be a mess. 707 708Additionally, netCDF stores some extra information for dimensions 709without dimension scale information. (That is, a dimension without an 710associated coordinate variable). So HDF5 users should not write data 711to a netCDF-4 file which extends any unlimited dimension, or change 712any of the extra attributes used by netCDF to track dimension 713information. 714 715Also there are some types allowed in HDF5, but not allowed in netCDF-4 716(for example the time type). Using any such type in a netCDF-4 file 717will cause the file to become unreadable to netCDF-4. So don't do it. 718 719NetCDF-4 ignores all HDF5 references. Can't make head nor tail of 720them. Also netCDF-4 assumes a strictly hierarchical group 721structure. No looping, you weirdo! 722 723Attributes can be added (they must be one of the netCDF-4 types), 724modified, or even deleted, in HDF5. 725 726\section accessing_hdf5 Reading and Editing HDF5 Files with NetCDF-4 727 728Assuming a HDF5 file is written in accordance with the netCDF-4 rules 729(i.e. no strange types, no looping groups), and assuming that *every* 730dataset has a dimension scale attached to each dimension, the netCDF-4 731API can be used to read and edit the file, quite easily. 732 733In HDF5 (version 1.8.0 and later), dimension scales are (generally) 1D 734datasets, that hold dimension data. A multi-dimensional dataset can 735then attach a dimension scale to any or all of its dimensions. For 736example, a user might have 1D dimension scales for lat and lon, and a 7372D dataset which has lat attached to the first dimension, and lon to 738the second. 739 740If dimension scales are not used, then netCDF-4 can still edit the 741file, and will invent anonymous dimensions for each variable 742shape. This is done by iterating through the space of each dataset. As 743each space size is encountered, a phony dimension of that size is 744checked for. It it does not exist, a new phony dimension is created 745for that size. In this way, a HDF5 file with datasets that are using 746shared dimensions can be seen properly in netCDF-4. (There is no 747shared dimension in HDF5, but data users will freqently write many 748datasets with the same shape, and intend these to be shared 749dimensions.) 750 751Starting with version 4.7.3, if a dataset is encountered with uses the 752same size for two or more of its dataspace lengths, then a new phony 753dimension will be created for each. That is, a dataset with size 754[100][100] will result in two phony dimensions, each of size 100. 755 756\page groups Groups 757 758NetCDF-4 files can store attributes, variables, and dimensions in 759hierarchical groups. 760 761This allows the user to create a structure much like a Unix file 762system. In netCDF, each group gets an ncid. Opening or creating a file 763returns the ncid for the root group (which is named “/”). Groups can 764be added with the nc_def_grp() function. Get the number of groups, and 765their ncids, with the nc_inq_grps() function. 766 767Dimensions are scoped such that they are visible to all child 768groups. For example, you can define a dimension in the root group, and 769use its dimension id when defining a variable in a sub-group. 770 771Attributes defined as ::NC_GLOBAL apply to the group, not the entire 772file. 773 774The degenerate case, in which only the root group is used, corresponds 775exactly with the classic data mode, before groups were introduced. 776 777\page user_defined_types User Defined Types 778 779\section compound_types Compound Types 780 781In netCDF-4 files it's possible to create a data type which 782corresponds to a C struct. These are known as “compound” types 783(following HDF5 nomenclature). 784 785That is, a netCDF compound type is a data structure which contains an 786arbitrary collection of other data types, including other compound 787types. 788 789To define a new compound type, use nc_def_compound(). Then call 790nc_insert_compound() for each type within the compound type. 791 792Read and write arrays of compound data with the nc_get_vara() and 793nc_put_vara() functions. These functions were actually part of the 794netCDF-2 API, brought out of semi-retirement to handle user-defined 795types in netCDF-4. 796 797\section opaque_types Opaque Types 798 799Store blobs of bits in opaque types. Create an opaque type with 800nc_def_opaque. Read and write them with nc_get_vara()/nc_put_vara(). 801 802\section vlen_type Variable Length Arrays (VLEN) 803 804Create a VLEN type to store variable length arrays of a known base 805type. Use nc_def_vlen() to define a VLEN type, read and write them with 806nc_get_vara()/nc_put_vara(). 807 808\page string_type Strings 809 810Use the ::NC_STRING type to store arrays of strings. Read and write them 811with nc_get_vara()/nc_put_vara(). 812 813\page parallel_io Parallel I/O with NetCDF-4 814 815NetCDF-4 provides parallel file access to both classic and netCDF-4/HDF5 files. 816The parallel I/O to netCDF-4 files is achieved through the HDF5 library while 817the parallel I/O to classic files is through PnetCDF. A few functions have been 818added to the netCDF C API to handle parallel I/O. You must build netCDF-4 819properly to take advantage of parallel features (see \ref build_parallel). 820 821The nc_open_par() and nc_create_par() functions are used to 822create/open a netCDF file with parallel access. 823 824\note The parallel access associated with these functions is not a characteristic of the data file, but the way it was opened. 825 826\section collective_independent Collective/Independent Access 827 828Parallel file access is either collective (all processors must participate) or 829independent (any processor may access the data without waiting for others). All 830netCDF metadata writing operations are collective. That is, all creation of 831groups, types, variables, dimensions, or attributes. Data reads and writes 832(e.g. calls to nc_put_vara_int() and nc_get_vara_int()) may be independent, the 833default) or collective. To change from collective to independent mode or vis 834versa, call nc_var_par_access() with argument 'access' set to either 835NC_INDEPENDENT or NC_COLLECTIVE. Note when using PnetCDF, the argument 'varid' 836is ignored, as PnetCDF does not support per-variable collective/independent 837mode change. 838 839\page tutorial_ncids Numbering of NetCDF IDs 840 841In C, Fortran 77, and Fortran 90, netCDF objects are identified by an 842integer: the ID. NetCDF functions use this ID to identify the 843object. It's helpful for the programmer to understand these IDs. 844 845Open data files, dimensions, variables, and attributes, and 846used-defined types are each numbered independently, and are always 847numbered in the order in which they were defined. (They also appear in 848this order in ncdump output.) Numbering starts with 0 in C, and 1 in 849Fortran 77/90. 850 851For example, the first variable defined in a file will have an ID of 0 852in C programs, and 1 in Fortran programs, and functions that apply to 853a variable will need to know the ID of the variable you mean. 854 855IDs for netCDF dimensions and variables are persistent, but deleting 856an attribute changes subsequent attribute numbers. 857 858Although netCDF refers to everything by an integer id (varid, dimid, 859attnum), there are inquiry functions which, given a name, will return 860an ID. For example, nc_inq_varid() will take a character 861string (the name), and give back the ID of the variable of that 862name. The variable ID is then used in subsequent calls (to read the 863data, for example). 864 865The ncid used to identify a file in the classic model, or a group 866within that file in the enhanced model (see \ref netcdf_data_model), or a 867user-defined type, are not permanently associated with the file. They 868may change the next time the file is opened. 869 870\page creating Creating New Files and Metadata, an Overview 871 872To construct a netCDF file you need to: 873- create the file - Specify the name, optionally the format: classic 874(the default), 64bit-offset, or 64-bit data. 875- define metadata - Specify the names and types of dimensions, data 876variables, and attributes. 877- write data - Write arrays of data from program variables to the 878netCDF file. Arrays of data may be written all at once, or in subsets. 879- close the file - Close the file to flush all buffers to the disk and 880free all resources allocated for this file. 881 882\page reading_known Reading NetCDF Files of Known Structure 883 884To read a netCDF file of known structure, you need to: 885- open the file - Specify the file name and whether you want 886read-write or read-only access. 887- read variable or attribute data - Read the data or attributes of 888interest. 889- close the file - Release all resources associated with this file. 890 891Use ncdump to learn the structure of a file (use the -h option). 892 893\page reading_unknown Reading NetCDF Files of Unknown Structure 894 895Perhaps you would like to write your software to handle more general 896cases, so that you don't have to adjust your source every time the 897grid size changes, or a variable is added to the file. 898 899There are inquiry functions that let you find out everything you need 900to know about a file. These functions contain “inq” or “INQ” in their 901names. 902 903Using the inquiry functions, it is possible to write code that will 904read and understand any netCDF file, whatever its contents. (For 905example, ncdump does just that.) 906 907First use nc_inq(), which will tell you how many variables and global 908attributes there are in the file. 909 910Start with global attribute 0, and proceed to natts - 1, the number of 911global attributes minus one. The nc_inq_att() function will tell you 912the name, type, and length of each global attribute. 913 914Then start with dimid 0, and proceed to dimid ndims - 1, calling 915nc_inq_dim(). This will tell you the name and length of each 916dimension, and whether it is unlimited. 917 918Then start with varid 0, and proceed to varid nvars - 1, calling 919nc_inq_var(). This will tell you the number of dimensions of this 920variable, and their associated IDs. It will also get the name and type 921of this variable, and whether there are any attributes attached. If 922there are attributes attached, use the nc_inq_att() function to get 923their names, types, and lengths. 924 925(To read an attribute, use the appropriate nc_get_att_<TYPE> function, 926like nc_get_att_int() to get the data from an attribute that is an 927array of integers.) 928 929There are also functions that return an item's ID, given its name. To 930find IDs from the names, use functions nc_inq_dimid(), 931nc_inq_attnum(), and nc_inq_varid(). 932 933The inquiry functions are: 934- nc_inq() Find number of dimensions, variables, and global 935attributes, and the unlimited dimid. 936- nc_inq_att() Find attribute name, type, and length. 937- nc_inq_dim() Find dimension name and length. 938- nc_inq_var() Find variable name, type, num dimensions, dim IDs, and 939num attributes. 940- nc_inq_dimid() Find dimension ID from its name. 941- nc_inq_varid() Find variable ID from its name. 942- nc_inq_format() Find file format: classic CDF-1, 64-bit offset CDF-2, or 64-bit data CDF-5 943- nc_inq_libvers() Find the netCDF library version. 944 945\page accessing_subsets Reading and Writing Subsets of Data 946 947Usually users are interested in reading or writing subsets of 948variables in a netCDF data file. The netCDF APIs provide a variety of 949functions for this purpose. 950 951In the simplest case, you will use the same type for both file and 952in-memory storage, but in some cases you may wish to use different 953types. For example, you might have a netCDF file that contains integer 954data, and you wish to read it into floating-point storage, converting 955the data as it is read. The same sort of type conversion can be done 956when writing the data. 957 958To convert to a type while reading data, use the appropriate 959nc_get_vara_<TYPE> or NF_GET_VARA_<TYPE> function. For example, the C 960function nc_get_vara_float(), and the Fortran function 961NF_GET_VARA_REAL will read netCDF data of any numeric type into a 962floating-point array, automatically converting each element to the 963desired type. 964 965To convert from a type while writing data, use the appropriate 966nc_put_vara_<TYPE> or NF_PUT_VARA_<TYPE> function. For example, the C 967function nc_put_vara_float() will write floating-point data into 968netCDF arrays, automatically converting each element of the array to 969the type of the netCDF variable. 970 971The TYPE in the function name refers to the type of the in-memory 972data, in both cases. They type of the file data is determined when the 973netCDF variable is defined. 974 975The type of the data may be automatically converted on read or 976write. 977 978\example simple_xy_wr.c 979\example simple_xy_rd.c 980\example sfc_pres_temp_wr.c 981\example sfc_pres_temp_rd.c 982\example pres_temp_4D_wr.c 983\example pres_temp_4D_rd.c 984\example simple_nc4_wr.c 985\example simple_nc4_rd.c 986\example simple_xy_nc4_wr.c 987\example simple_xy_nc4_rd.c 988*/ 989