Downloading

IMDLIB is capable of downloading gridded rainfall and temperature (minimum and maximum) data. Here is an example of downloading rainfall data from 2010 to 2018:

import imdlib as imd

start_yr = 2010
end_yr = 2018
variable = 'rain' # other options are ('tmin'/ 'tmax')
data = imd.get_data(variable, start_yr, end_yr, fn_format='yearwise')

Output

Downloading: rain for year 2010
Downloading: rain for year 2011
Downloading: rain for year 2012
Downloading: rain for year 2013
Downloading: rain for year 2014
Downloading: rain for year 2015
Downloading: rain for year 2016
Downloading: rain for year 2017
Downloading: rain for year 2018
Download Successful !!!

The output is saved in the current working directory. If you want to save the files to a different directory, then you can use the following code:

import imdlib as imd

start_yr = 2010
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', file_dir=file_dir)

Reading IMD datasets

One of the major purposes of IMDLIB is to process IMD’s gridded datasets. The original data is available in grd file format. IMDLIB can read grd file in xarray and will create an IMD class object.

import imdlib as imd

start_yr = 2010
end_yr = 2018
variable = 'rain' # other options are ('tmin'/ 'tmax')
file_dir = (r'C:\Users\imdlib\Desktop\\') #Path to save the files
data = imd.open_data(variable, start_yr, end_yr,'yearwise', file_dir)
data
*

This step is for reading IMD datasets after they are downloaded. If you have the data already downloaded and stored locally, you can directly use this step to read the datasets.

Output

<imdlib.core.IMD at 0x13e5b3753c8>

  • file_dir should refer to top-level directory. It should contain 3 sub-directories rain, tmin, and tmax.

  • If file_dir exists without any subdirectory, IMDLIB will look for the files in file_dir. But be careful if you are using file_format = ‘yearwise’, as it will not differentiate between the datasets, 2018.grd for rainfall and 2018.grd for tmin.

  • If file_dir is not given, it will look for the adatasets from the current directory and its subdirectories.

Processing

Getting the xarray object for further processing:

ds = data.get_xarray()
print(ds)
<xarray.Dataset>
Dimensions:  (lat: 129, lon: 135, time: 3287)
Coordinates:
* lat      (lat) float64 6.5 6.75 7.0 7.25 7.5 ... 37.5 37.75 38.0 38.25 38.5
* lon      (lon) float64 66.5 66.75 67.0 67.25 67.5 ... 99.25 99.5 99.75 100.0
* time     (time) datetime64[ns] 2010-01-01 2010-01-02 ... 2018-12-31
Data variables:
    rain     (time, lat, lon) float64 -999.0 -999.0 -999.0 ... -999.0 -999.0
Attributes:
    Conventions:  CF-1.7
    title:        IMD gridded data
    source:       https://imdpune.gov.in/
    history:      2021-02-27 08:10:43.519783 Python
    references:
    comment:
    crs:          epsg:4326

Plotting

Plotting can be done by:

ds = ds.where(ds['rain'] != -999.) #Remove NaN values
ds['rain'].mean('time').plot()
_images/fig1.png

Saving

Get data for a given location, convert, and save into csv file:

lat = 20.03
lon = 77.23
data.to_csv('test.csv', lat, lon, file_dir)

Save data in netCDF format:

data.to_netcdf('test.nc', file_dir)

Save data in GeoTIFF format (if you have rioxarray library):

data.to_geotiff('test.tif', file_dir)