NetCDF reading and writing examples
NetCDF reading and writing examples
A self-contained example for EarthInversion’s NetCDF tutorials. All temperature values are synthetic teaching data, not observations or a physical model.
Run
Use Python 3.10 or newer. Create and activate a virtual environment, then run:
python -m pip install netCDF4 numpy
python write_netcdf.py
python read_netcdf.py example.nc
If you downloaded example.nc already, skip the writing command or choose a different output filename. The writer refuses to overwrite existing files.
Optional plotting and appending:
python -m pip install matplotlib
python plot_netcdf.py example.nc
python append_netcdf.py example.nc example-appended.nc
python read_netcdf.py example-appended.nc
Appending creates a copy, leaving the source unchanged. This teaching script is not an atomic transaction system for production acquisition.
Run python test_examples.py for round-trip checks. Install xarray to include its optional independent readback check. Exact tested versions are in requirements-tested.txt.
The sample has temperature(time, lat, lon), shape (3, 5, 6), units K, three six-hourly times starting January 1, 2026 at 00:00 UTC, latitudes 35 to 39 degrees north, and longitudes -124 to -119 degrees east. Cell [0, 0, 0] is missing. The deterministic formula is in the writing script. No external data is required or redistributed.
The scripts use CF-style coordinate metadata without claiming formal compliance with a particular version of CF.
Documentation: netCDF4-Python and CF conventions.
Disclaimer of liability
The information provided by the Earth Inversion is made available for educational purposes only.
Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.
UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.