Step 1: Go to Google Colab
Open Google Colab
Step 2: Connect to Google Drive
from google.colab import drive
drive.mount('/content/drive')
Step 3: Install the Library
pip install imdlib
Step 4 (a): Download and Convert to .csv (Single coordinate)
This code will save the year-wise .csv file separately after conversion
import imdlib as imd
import os
path = "/content/drive/MyDrive/Colab Notebooks"
start_yr = 1951
end_yr = 2021
variable = 'tmin' # other options are ('tmin'/ 'tmax')
imd.get_data(variable, start_yr, end_yr, fn_format='yearwise', file_dir=path)
for year in range(start_yr, end_yr+1):
data = imd.open_data(variable, year, year, 'yearwise', path)
ds = data.get_xarray()
print(ds)
lat = 27.25 #lattitude of point
lon = 78.0 #longitude of point
filename = f'{path}/{variable}_{year}.csv'
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
data.to_csv(filename, lat, lon)
print(f'{year} {variable} data saved to {filename}')
Step 4(b): Download and Convert to .csv (Multiple coordinates saved in separate .csv files year-wise)
import imdlib as imd
import xarray as xr
import os
path = "/content/drive/MyDrive/Colab Notebooks"
start_yr = 1959
end_yr = 1962
variable = 'tmin' # other options are ('tmin'/ 'tmax')
imd.get_data(variable, start_yr, end_yr, fn_format='yearwise', file_dir=path)
for year in range(start_yr, end_yr+1):
data = imd.open_data(variable, year, year, 'yearwise', path)
ds = data.get_xarray()
print(ds)
latLong = [[27.25,78.00],[27.0,78.0],[27.0,78.25],[27.0,77.75],[27.5,78.5]]
for points in latLong:
lat = points[0]
lon = points[1]
df = ds.sel(lat=lat, lon=lon, method='nearest').to_dataframe()
filename = f'{path}/{variable}_{year}_{lat}_{lon}.csv'
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
df.to_csv(filename)
print(f'{year} {variable} data saved to {filename}')
If you want to save multiple years to the same .csv file then use the below codes
Step 5 (a): Download and Convert to .csv (Single coordinate, Single File)
import imdlib as imd
path = "/content/drive/MyDrive/Colab Notebooks"
start_yr = 1951
end_yr = 2020
variable = 'rain' # other options are ('tmin'/ 'tmax')
imd.get_data(variable, start_yr, end_yr, fn_format='yearwise', file_dir=path)
data = imd.open_data(variable, start_yr, end_yr,'yearwise', path)
ds = data.get_xarray()
print(ds)
lat = 20.03 #lattitude of point
lon = 77.23 #longitude of point
data.to_csv('data.csv', lat, lon, path)
Step 4(b): Download and Convert to .csv (Multiple coordinates, Multiple .csv Files)
import imdlib as imd
path = "/content/drive/MyDrive/Colab Notebooks"
start_yr = 1951
end_yr = 2021
variable = 'tmax' # other options are ('tmin'/ 'tmax')
imd.get_data(variable, start_yr, end_yr, fn_format='yearwise', file_dir=path)
data = imd.open_data(variable, start_yr, end_yr,'yearwise', path)
ds = data.get_xarray()
print(ds)
latLong = [[27.25,78.00],[27.0,78.0],[27.0,78.25],[27.0,77.75],[27.5,78.5]]
for points in latLong:
lat = points[0]
lon = points[1]
filename = f'{path}/{variable}_{lat}_{lon}.csv'
data.to_csv(filename, lat, lon)
print (f"data saved for {points} to {filename}")
How to Download and Convert IMD Gridded Binary Weather Data Using Python/QGIS