1

I have a GrADS formatted (Raw Binary) file with a control file as follows:

dset data.bin
undef  -999.
options template
title Example data
xdef 720 linear    0.25    0.50
ydef 360 linear  -89.75    0.50
tdef   1 linear 00Z01JAN1990 1yr
zdef 1 linear 1 1
vars 1
dat 0 99 Estimated data
ENDVARS

Does anyone have an idea of how to convert or reformat it into a common 2D raster file format using R or Python?

The expected output is a 2D map (which is usually as a raster file or 2D array) something like this map: Example of output

vempi
  • 11
  • 3

2 Answers2

1

You can use CDO (Climate Data Operator) to do this by one line of code like

cdo -f nc import_binary Temp.ctl try.nc

But CDO installation in Windows is bit tricky. You can install Ubuntu subsystem within Windows and then install CDO within Ubuntu. After that install CDO in Ubuntu subsystem following the steps provided here https://zoomadmin.com/HowToInstall/UbuntuPackage/cdo. Then open the terminal in the directory where the .ctl file is there, then execute the above code.

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Thanks for your solution @bappa. But, I still want to do it in my windows (using R and python), as I faced difficulties when installing OpenGrADS and CDO as you said. – vempi Jan 28 '20 at 07:08
  • Now in Windows 10 it is very easy to install Ubuntu and CDO. You can follow the steps provided in this link https://learn.microsoft.com/en-us/windows/wsl/install-win10 – UseR10085 Jan 28 '20 at 07:10
1

You may want to try this python package xgrads, which will parse the ctl file and load the raw binary data into a widely-used data struct xarray in earth science:

from xgrads import open_CtlDataset

# load the data into xarray.Dataset
dset = open_CtlDataset('data.ctl')

Then it is straightforward to have the data plotted as:

dset['dat'].plot()

Note that if the ctl and binary files are in the same folder, then the first line of ctl should be dset ^data.bin. If they are not in the same folder, you should specify the absolute path of the .bin file.

Hope this helps.

MiniUFO
  • 171
  • 1
  • 12
  • How to install `xgrads` for Python or Anaconda and how can I handle multiple ctl file at a time? – UseR10085 May 02 '20 at 06:58
  • You can download it using `git` command `git clone https://github.com/miniufo/xgrads.git` and then install it using `python setup.py install`. Multiple binary files are usually described using [`template`](http://cola.gmu.edu/grads/gadoc/templates.html) keyword in a single .ctl file. – MiniUFO May 02 '20 at 14:10