Skip to main content

Parse and read ctl file commonly used by GrADS.

Project description

xgrads

DOI GitHub Documentation Status PyPI version Publish to PyPI pytest Build Status Codacy Badge

3D plot

1. Introduction

The Grid Analysis and Display System (GrADS or OpenGrADS) is a widely used software for easy access, manipulation, and visualization of earth science data. It uses a descriptor (or control) file with a suffix .ctl to describe a raw binary 4D dataset. The ctl file is similar to the header information of a NetCDF file, containing all the information about dimensions, attributes, and variables except for the variable data.

This python package xgrads is designed for parse and read the .ctl file commonly used by GrADS. Right now it can parse various kinds of .ctl files. However, only the commonly used raw binary 4D datasets can be read using dask and return as a xarray.Dataset Other types of binary data, like dtype is station orgrib, may be supported in the future.


2. How to install

Requirements xgrads is developed under the environment with xarray (=version 0.15.0), dask (=version 2.11.0), numpy (=version 1.15.4), cartopy (=version 0.17.0), and pyproj (=version 1.9.6). Older versions of these packages are not well tested.

Install via pip

pip install xgrads

Install via conda

conda install -c conda-forge xgrads

Install from github

git clone https://github.com/miniufo/xgrads.git
cd xgrads
python setup.py install

3. Examples

3.1 Parse a .ctl file

Parsing a .ctl file is pretty simple using the following code:

from xgrads import CtlDescriptor

ctl = CtlDescriptor(file='test.ctl')

# print all the info in ctl file
print(ctl)

If you have already load the ASCII content in the .ctl file, you can do it as:

content = \
    "dset ^binary.dat\n" \
    "* this is a comment line\n" \
    "title 10-deg resolution model\n" \
    "undef -9.99e8\n" \
    "xdef 36 linear   0 10\n" \
    "ydef 19 linear -90 10\n" \
    "zdef  1 linear   0  1\n" \
    "tdef  1 linear 00z01Jan2000 1dy\n" \
    "vars  1\n" \
    "test  1 99 test variable\n" \
    "endvars\n"

ctl = CtlDescriptor(content=content)

# print all the info
print(ctl)

3.2 Read binary data into a xarray.Dataset

Reading a .ctl related binary data file is also pretty simple using the following code:

from xgrads import open_CtlDataset

dset = open_CtlDataset('test.ctl')

# print all the info in ctl file
print(dset)

Then you have the dset as a xarray.Dataset. This is similar to xarray.open_dataset that use dask to chunk (buffer) parts of the whole dataset in physical memory if the whole dataset is too large to fit in.

If there are many .ctl files in a folder, we can also open all of them in a single call of open_mfdataset as:

from xgrads import open_mfDataset

dset = open_mfDataset('./folder/*.ctl')

# print all the info in ctl file
print(dset)

assuming that every .ctl file has similar data structure except the time step is different. This is similar to xarray.open_mfdataset.


3.3 Convert a GrADS dataset to a NetCDF dataset

With the above functionality, it is easy to convert a .ctl (GrADS) dataset to a .nc (NetCDF) dataset:

from xgrads import open_CtlDataset

open_CtlDataset('input.ctl').to_netcdf('output.nc')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xgrads-0.2.7.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xgrads-0.2.7-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file xgrads-0.2.7.tar.gz.

File metadata

  • Download URL: xgrads-0.2.7.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for xgrads-0.2.7.tar.gz
Algorithm Hash digest
SHA256 430aecd995ad6804d2e9121c30b2dbc2d68a515f22d30f5e56a95e501ee49501
MD5 e8d648ba1c6302acc11557a603bbe748
BLAKE2b-256 38e405cc24ac503653d2ae9746baaa39fabb3e877e3580bee889a589cb1471eb

See more details on using hashes here.

File details

Details for the file xgrads-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: xgrads-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for xgrads-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 54f932a8e9b079df35c5576011634ad53c7ac0938e84fd3b0a14d1196c389ed1
MD5 3a7298ee573de86280ae639af0a8fcc5
BLAKE2b-256 d0f9e96d79de2d809870a678fa04d880f74d5f8956a9dbf82d5afad25e3d07d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page