简体   繁体   English

使用 python 读取 .nc (netcdf) 文件

[英]Read .nc (netcdf) files using python

I am trying to learn how to read .nc (netcdf) files using Python in the most easiest/fastest way.我正在尝试学习如何以最简单/最快的方式使用 Python 读取 .nc (netcdf) 文件。 I heard that it can be done with 3 lines of code but I really don't know how.我听说它可以用 3 行代码完成,但我真的不知道如何。

I am running the MITgcm numerical model.我正在运行 MITgcm 数值模型。 I'm trying to get an easy way to visualize the output data in the same way as programs like NCview does but with Python, so I can customise the parameters to read and everything.我试图找到一种简单的方法来可视化输出数据,其方式与 NCview 等程序相同,但使用 Python,因此我可以自定义要读取的参数和所有内容。

I found this:我发现了这个:

from matplotlib import pyplot as plt
import pandas as pd
import netCDF4
fp='uwstemp.nc'
nc = netCDF4.Dataset(fp)
plt.imshow(nc['Temp'][1,:,0,:])
plt.show()

It worked roughly like I want it, but I would like to understand word by word what is it doing.它大致按照我想要的方式工作,但我想逐字逐句地了解它在做什么。 I guess 'Temp' is one of my variables, but I don't know how to figure out what all my variables are.我猜 'Temp' 是我的变量之一,但我不知道如何弄清楚我所有的变量是什么。

Specially, I don't understand plt.imshow(nc['Temp'][1,:,0,:]) thhat [1,:,0,:] I tried to change it and does not compile;特别是,我不明白plt.imshow(nc['Temp'][1,:,0,:])那个 [1,:,0,:] 我试图改变它并且没有编译; but I don't understand what is it doing and why this numbers.但我不明白它在做什么以及为什么这个数字。

I use the MITgcm too.我也使用 MITgcm。 Say you have your state.nc output.假设您有 state.nc 输出。 First of all make sure you import all you need:首先,请确保导入所需的所有内容:

from scipy.io import netcdf
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

The easiest way to read the data is:读取数据的最简单方法是:

file2read = netcdf.NetCDFFile(path+'state.nc','r')
temp = file2read.variables[var] # var can be 'Theta', 'S', 'V', 'U' etc..
data = temp[:]*1
file2read.close()

Then a quick way to plot say layer z at time t is:然后在时间 t 绘制图层 z 的一种快速方法是:

plt.contourf(data[t,z,:,:])

To answer your question I commented the code:为了回答你的问题,我评论了代码:

from matplotlib import pyplot as plt # import libraries
import pandas as pd # import libraries
import netCDF4 # import libraries
fp='uwstemp.nc' # your file name with the eventual path
nc = netCDF4.Dataset(fp) # reading the nc file and creating Dataset
""" in this dataset each component will be 
in the form nt,nz,ny,nx i.e. all the variables will be flipped. """
plt.imshow(nc['Temp'][1,:,0,:]) 
""" imshow is a 2D plot function
according to what I have said before this will plot the second
iteration of the vertical slize with y = 0, one of the vertical
boundaries of your model. """
plt.show() # this shows the plot

If you want to check the various dimensions of your data so you know what you can plot simply do print(nc['Temp'].shape)如果您想检查数据的各个维度,以便了解可以绘制的内容,只需执行print(nc['Temp'].shape)

If you are working in Linux, my package nctoolkit (toolkit.readthedocs.io/en/latest/) offers similar functionality to ncview, but within Python.如果您在 Linux 中工作,我的包 nctoolkit (toolkit.readthedocs.io/en/latest/) 提供了与 ncview 类似的功能,但在 Python 中。 It can autoplot the contents of NetCDF files either within a Jupyter notebook or a web browser.它可以在 Jupyter notebook 或 Web 浏览器中自动绘制 NetCDF 文件的内容。 The following should work for the data given:以下应该适用于给定的数据:

import nctoolkit as nc
fp='uwstemp.nc'
data = nc.open_data(fp)
data.plot()

For netCDF4 files (with python 3), use:对于 netCDF4 文件(使用 python 3),请使用:

import netCDF4
file2read = netCDF4.Dataset(cwd+'\filename.nc','r')
var1 = file2read.variables['var1']  # access a variable in the file

where cwd is my current working directory for getting the file path for the .nc file in order to read it:其中 cwd 是我当前的工作目录,用于获取 .nc 文件的文件路径以读取它:

import os
cwd = os.getcwd()

I am using Windows, so file directory will be different than for Mac or Linux.我使用的是 Windows,所以文件目录将与 Mac 或 Linux 不同。

To look at all of the variable keys:查看所有变量键:

print(file2read.variables.keys())

Which will give an output like this:这将给出如下输出:

dict_keys(['ap', 'ap_bnds', 'b', 'b_bnds', 'bnds', 'ch4', 'lat', 'lat_bnds', 'lev', 'lev_bnds', 'lon', 'lon_bnds', 'time', 'time_bnds'])

Or to look at all of the variables in your netcfd4 file, you can just print 'file2read':或者要查看 netcfd4 文件中的所有变量,只需打印“file2read”:

print(file2read)

And the output will include something like this (look at the end specifically):输出将包括这样的内容(具体看最后):

source_id: GFDL-ESM4
source_type: AOGCM AER CHEM BGC
sub_experiment: none
sub_experiment_id: none
title: NOAA GFDL GFDL-ESM4 model output prepared for CMIP6 update of RCP8.5 based on SSP5
variable_id: ch4
variant_info: N/A
references: see further_info_url attribute
variant_label: r1i1p1f1
dimensions(sizes): lev(49), bnds(2), time(1032), lat(180), lon(288)
variables(dimensions): float64 ap(lev), float64 ap_bnds(lev, bnds), float64 b(lev), float64 b_bnds(lev, bnds), float64 bnds(bnds), float32 ch4(time, lev, lat, lon), float64 lat(lat), float64 lat_bnds(lat, bnds), float64 lev(lev), float64 lev_bnds(lev, bnds), float64 lon(lon), float64 lon_bnds(lon, bnds), float64 time(time), float64 time_bnds(time, bnds)

You can notice that the last part includes the dimensions of the variables along with the type and name of the variables.您可以注意到最后一部分包括变量的维度以及变量的类型和名称。

Check out this website for more info and examples: https://www.earthinversion.com/utilities/reading-NetCDF4-data-in-python/查看此网站以获取更多信息和示例: https : //www.earthinversion.com/utilities/reading-NetCDF4-data-in-python/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM