Getting started with Python and IMDLIB

Getting started with Python and IMDLIB

Pratiman Patel, 07 October 2020

2 min read.

This is intended for those who have never used python or just starting to work in python. Here are the steps which will help them with getting started with IMDLIB and Python.

Setting up the Python in Windows

There are multiple options you can choose for Python distribution. I like the use of miniconda which is light weight and does not come with unnecessary libraries.

Steps to install miniconda

  1. Download the installer from here.
  2. Install the miniconda. Install miniconda Install miniconda
  3. Once you have installed the check by clicking in the start button. Check miniconda

Install an IDE (Spyder) and library packages

  1. Open Anaconda Prompt (miniconda3) Open miniconda prompt
  2. Write the following code in the anaconda prompt window
conda install -c conda-forge numpy scipy rioxarray xarray netcdf4 proplot matplotlib cartopy spyder
pip install imdlib

Install packages

Output:

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
...
...
...
Proceed ([y]/n)? y
...
...
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

You might get extra warnings. You can ignore them.

  1. Check the installation. Check packages

Running your Python code

  1. Open Spyder. Open Spyder

  2. Test your first imdlib code. Copy the following code to the to editor window of the spyder and run.

import imdlib as imd
import rioxarray as rio

# Downloading 1 years of rainfall data for India
start_yr = 2018
end_yr = 2018
variable = 'rain' # other options are ('tmin'/ 'tmax')
#file_dir = (r'C:\Users\imdlib\Desktop\') #Path to save the files
imd.get_data(variable, start_yr, end_yr, fn_format='yearwise')

# Opeining the downloaded dataset
data = imd.open_data(variable, start_yr, end_yr,'yearwise')
ds = data.get_xarray()

#Plot the mean
ds.mean('time').plot()

#Set the CRS:
pr = ds.rio.set_crs("epsg:4326")

# Transpose according to GeoTIFF conventions:
pr = pr.transpose('time', 'lat', 'lon')

#Define lat/long 
pr = pr.rio.set_spatial_dims('lon', 'lat')

#Saving the file
pr.rio.to_raster(r"IMD_Rain_2010_2018.tif")

Run Code

Output:

Output Plot

Output Plot

Welcome to the world of Python!

If you would like to provide any suggestions to this post. Kindly email me at pratiman_patel@hotmail.com

⇠ India Meteorological Department (IMD) grided data to GeoTIFF

Extracting Regions from IMD datasets using Python ⇢