How to Read .nc NetCDF Files in Python: Two Practical Methods
This article explains what .nc (netCDF) files are, why they are useful for storing scientific array data, and provides two complete Python examples—one using conventional netCDF4 inspection and another using direct value extraction—to help readers read and process .nc files efficiently.
Introduction
The author, a Python enthusiast, answers a fan's question about reading .nc files, which are based on the netCDF (Network Common Data Form) format used to store multi‑dimensional scientific data.
1. Basics of .nc Files
.nc files can store a series of arrays, making them ideal for long‑term scientific observations such as meteorology, hydrology, or temperature data. Using a single .nc file avoids the need to manage many CSV or TXT files.
2. Solution
Method 1: Conventional Reading
First install the required library: pip install netCDF4 If the download is slow, use a mirror source:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple netCDF4Then read the file with the following script (replace the file path with your own):
# -*- coding: utf-8 -*-
import netCDF4
from netCDF4 import Dataset
nc_obj = Dataset('D:\\tem_e0025_2.nc')
# Inspect variables
print(nc_obj)
print('---------------------------------------')
print(nc_obj.variables.keys())
# Access variable data
lat = nc_obj.variables['lat'][:]
lon = nc_obj.variables['lon'][:]
lev = nc_obj.variables['lev'][:]
time = nc_obj.variables['time'][:]
tem = nc_obj.variables['tem'][:]
# Write selected data to a text file
with open('ppm_lat.txt', 'a') as file:
file.write('lat,lon,time,tem
')
for i in range(len(lat)):
file.write(f"{lat[i]},{lon[i]},{lev[i]},{time[i]},{tem[i]}
")Method 2: Direct Value Extraction
An alternative approach provided by the fan uses direct indexing:
# -*- coding: utf-8 -*-
import netCDF4
from netCDF4 import Dataset
A_temp = Dataset(r'E:\PythonCrawler\有趣的代码\数据分析\最小二乘法数据分析代码和数据\Temperature_20211031.nc')
lat = A_temp['latitude'][:].data
lon = A_temp['longitude'][:].data
temp = A_temp['T'][:].data
depth = A_temp['depth'][:].data
print(len(lat))
print(len(lon))Both methods demonstrate how to list variables, read their attributes, and extract the actual data values.
Conclusion
The article successfully answered the fan's question by presenting two workable ways to read .nc files in Python, enabling further analysis such as regression or clustering. Readers are encouraged to adapt the code to their own datasets.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
