Handle NetCDF format data in Python

environment

Ubuntu18.04LTS Python3

Environment

A NetCDF4 module is required to use NetCDF format files in Python3. This module was easy to install with pip3.

NetCDF file I / O in Python

The following is a program that opens a NetCDF file that stores the three-dimensional variables T2 of latitude, longitude, and time, and creates a new file called test_ncout.nc.

#coding: utf-8
# This is a sample program to read and write s netCDF file with Python3
from netCDF4 import Dataset
import numpy as np

#*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
#read netCDF file
#*~*~*~*~*~*~*~*~*~*~*~*~*~*~*

#open a netCDF file to read
ifile = "test.nc"
ncin = Dataset(ifile, 'r')

#check
#print(ncin.file_format)

#get variables
#print(ncin.variables.keys())

#get axis data
tin = ncin.variables['time']
latin = ncin.variables['lat']
lonin = ncin.variables['lon']

#get length of axis data
ntime = len(tin)
nlat = len(latin)
nlon = len(lonin)

#check axis
#print(tim[:])
#print(latin[:])
#print(lonin[:])

#read data
vin = ncin.variables['T2']

#check data
#print(vin[:,:,:,:])

#*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
#write netCDF file
#*~*~*~*~*~*~*~*~*~*~*~*~*~*~*

#open a netCDF file to write
ncout = Dataset('test_ncout.nc', 'w', format="NETCDF4")

#check file format
#print(ncout.file_format)

#define axix size
ncout.createDimension('time',ntime)
ncout.createDimension('lat', nlat)
ncout.createDimension('lon', nlon)

#create time axis
time = ncout.createVariable('time', np.dtype('f4').char, ('time'))
time.long_name = 'time'
time.units = 'months since 1850-1-1'
time.axis = 'T'

#create lat axis
lat = ncout.createVariable('lat', np.dtype('f4').char, ('lat'))
lat.long_name = 'latitude'
lat.units ='degrees_north'
lat.axis = 'Y'

#create lon axis
lon = ncout.createVariable('lon', np.dtype('f8').char, ('lon'))
lon.long_name = 'longitude'
lon.units = 'degrees_east'
lon.axis = 'X'

#create variable arry
vout = ncout.createVariable('T2', np.dtype('f4').char, ('time', 'lat', 'lon'))
vout.long_name = '2m temperature'
vout.units = 'K'

#copy axis from original data
time[:] = tin[:]
lon[:] = lonin[:]
lat[:] = latin[:]
vout[:] = vin[:]

ncin.close()
ncout.close()

reference

http://unidata.github.io/netcdf4-python/ https://github.com/Unidata/netcdf4-python

Recommended Posts

Handle NetCDF format data in Python
Handle GDS II format in Python
format in python
Image format in Python
Handle markdown in python
Handle Parquet in Python
[Introduction to Python] How to handle JSON format data
Easily format JSON in Python
Display UTM-30LX data in Python
Handle environment variables in Python
Write data in HDF format
Handle complex numbers in Python
Get Leap Motion data in Python.
Export DB data in json format
Read Protocol Buffers data in Python3
Get data from Quandl in Python
Python Application: Data Handling Part 3: Data Format
Automatically format Python code in Vim
Handle posix message queues in python
Hashing data in R and Python
How to handle Japanese in Python
Handle multiple python versions in one jupyter
Get additional data in LDAP with python
Data input / output in Python (CSV, JSON)
Ant book in python: Sec. 2-4, data structures
Try working with binary data in Python
Get Google Fit API data in Python
Python: Preprocessing in machine learning: Data acquisition
Get Youtube data in Python using Youtube Data API
Easily graph data in shell and Python
Python: Preprocessing in machine learning: Data conversion
Download files in any format using Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Python string format
SendKeys in Python
Meta-analysis in Python
Unittest in python
Data analysis python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python