Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read .nc (netcdf) files using python

Tags:

python

I am trying to learn how to read .nc (netcdf) files using Python in the most easiest/fastest way. I heard that it can be done with 3 lines of code but I really don't know how.

I am running the MITgcm numerical model. 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.

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.

Specially, I don't understand plt.imshow(nc['Temp'][1,:,0,:]) thhat [1,:,0,:] I tried to change it and does not compile; but I don't understand what is it doing and why this numbers.

like image 743
Alejandro Jiménez Rico Avatar asked Apr 01 '16 15:04

Alejandro Jiménez Rico


People also ask

How do you read .NC data?

You can use programming languages or windows software. If you want just to view nc file and display it, Panoply is the best and you can use free version of Netcdf Extractor for viewing. But, if you want to extract time series from one file or many files, you can use Netcdf Extractor and do it easily.

How do I import netCDF4 into Python?

To install with anaconda (conda) simply type conda install netCDF4 . Alternatively, you can install with pip . To be sure your netCDF4 module is properly installed start an interactive session in the terminal (type python and press 'Enter'). Then import netCDF4 as nc .


2 Answers

I use the MITgcm too. Say you have your state.nc output. 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:

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)

like image 51
Alessandro Avatar answered Oct 25 '22 17:10

Alessandro


For netCDF4 files (with python 3), use:

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:

import os
cwd = os.getcwd()

I am using Windows, so file directory will be different than for Mac or 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':

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/

like image 28
scififlamingo Avatar answered Oct 25 '22 16:10

scififlamingo