# CLimd: Generating Rainfall Rasters from IMD NetCDF Data

Authors - Nirmal Kumar, Nobin Chandra Paul and G.P. Obi Reddy

## Welcome to the CLimd vignette

This vignette will guide users through the features and functionalities of the CLimd package, which allows users to convert the IMD NetCDF rainfall data into raster maps for various temporal scales,facilitating spatial-temporal trend analysis.

## Introduction

****
The developed function is a comprehensive tool for the analysis of India Meteorological Department (IMD) NetCDF rainfall data. Specifically designed to process high-resolution daily gridded rainfall datasets. It provides four key functions to process IMD NetCDF rainfall data and create rasters for various temporal scales, including annual, seasonal, monthly, and weekly rainfall.It supports different aggregation methods, such as sum, min, max, mean, and standard deviation. These functions are designed for spatial-temporal analysis of rainfall patterns, trend analysis, geo-statistical modeling of rainfall variability, identifying rainfall anomalies and extreme events and can be an input for hydrological and agricultural models.

****

    ### Installation and loading the library of CLimd R package
    # You can install the CLimd package from CRAN using the following command:
    # install.packages("CLimd")
    # Once installed, you can load the package using
    library(CLimd)

    ###  Generating Monthly Rainfall Rasters from IMD NetCDF Data
    # The "MonthRF_raster" function generates the monthly rainfall rasters.

    # Example:
    nc_data <- system.file("extdata", "imd_RF_2022.nc", package = "CLimd")
    output_dir <- NULL
    fun<-"sum"
    year<-2022
    # nc_data: Path to the IMD NetCDF rainfall file.
    # output_dir: Directory to save the generated rasters. (Optional)
    # fun: Aggregation function ("sum", "min", "max", "mean", "sd").
    # year: Year for which to generate monthly rasters.

    # Calculate monthly rainfall sum for the year 2022
    MonthRF<-MonthRF_raster(nc_data, output_dir=NULL, fun="sum", year)
    MonthRF

    ## class      : RasterBrick
    ## dimensions : 129, 135, 17415, 12  (nrow, ncol, ncell, nlayers)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : r_tmp_2024-01-09_153102.283701_28540_05903.grd
    ## names      :       Jan,       Feb,       Mar,       Apr,       May,       Jun,       Jul,       Aug,       Sep,       Oct,       Nov,       Dec
    ## min values :         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0,         0
    ## max values :  385.2808,  173.4496,  549.1419, 1932.5845, 2555.1173, 5369.6788, 2417.0628, 1659.8029, 1025.7819,  678.4005,  908.8165,  381.6000

    ### This creates a list of 12 rasters, one for each month in 2022. Each raster provides a detailed snapshot of rainfall distribution for that specific month. You can visualize these rasters using the plot function to gain insights into monthly trends and variations in rainfall patterns
    # plot(MonthRF[[1]])  # Plot the first layer (Jan)
    # plot(MonthRF)  # Plot all layers (Jan to Dec) as a multi-panel display

    ###  Generating Weekly Rainfall Rasters from IMD NetCDF Data
    # The "WeeklyRF_raster" function generates weekly rainfall rasters. Example:
    library(CLimd)
    nc_data <- system.file("extdata", "imd_RF_2022.nc", package = "CLimd")
    output_dir <- NULL
    fun<-"sum"
    year<-2022
    WeekRF<-WeeklyRF_raster(nc_data, output_dir=NULL, fun="sum", year)
    WeekRF

    ## class      : RasterBrick
    ## dimensions : 129, 135, 17415, 52  (nrow, ncol, ncell, nlayers)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : r_tmp_2024-01-09_153109.986667_28540_24141.grd
    ## names      :    Week.01,    Week.02,    Week.03,    Week.04,    Week.05,    Week.06,    Week.07,    Week.08,    Week.09,    Week.10,    Week.11,    Week.12,    Week.13,    Week.14,    Week.15, ...
    ## min values :          0,          0,          0,          0,          0,          0,          0,          0,          0,          0,          0,          0,          0,          0,          0, ...
    ## max values :  273.29267,  151.61869,   90.81800,   73.83112,  116.05548,  140.93001,   62.79663,  136.18923,   44.04206,   92.98983,   96.60000,  523.21410,  694.51535,  627.44993,  471.50262, ...

    ### This creates a list of 52 rasters, one for each week in 2022. You can visualize them using the plot function to explore rainfall dynamics at a weekly scale.
    # plot(WeekRF)
    # plot(WeekRF[[45:52]])
    ###  Generating Seasonal Rainfall Rasters from IMD NetCDF Data
    # According to the IMD, four prominent seasons namely (i) Winter (December-February), (ii) Pre-Monsoon (March–May), (iii) Monsoon (June-September), and (iv) Post-Monsoon (October-November) are dominant in India.
    # The "SeasonalRF_raster" function generates seasonal rainfall rasters. Example:
    library(CLimd)
    nc_data <- system.file("extdata", "imd_RF_2022.nc", package = "CLimd")
    output_dir <- NULL
    fun<-"sum"
    year<-2022
    SeasonalRF<-SeasonalRF_raster(nc_data, output_dir=NULL, fun="sum", year)
    SeasonalRF

    ## $Winter
    ## class      : RasterLayer
    ## dimensions : 129, 135, 17415  (nrow, ncol, ncell)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : memory
    ## names      : layer
    ## values     : 0, 522.0706  (min, max)
    ##
    ##
    ## $PreMonsoon
    ## class      : RasterLayer
    ## dimensions : 129, 135, 17415  (nrow, ncol, ncell)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : memory
    ## names      : layer
    ## values     : 0, 5036.844  (min, max)
    ##
    ##
    ## $SWMonsoon
    ## class      : RasterLayer
    ## dimensions : 129, 135, 17415  (nrow, ncol, ncell)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : memory
    ## names      : layer
    ## values     : 0, 7590.691  (min, max)
    ##
    ##
    ## $PostMonsoon
    ## class      : RasterLayer
    ## dimensions : 129, 135, 17415  (nrow, ncol, ncell)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : memory
    ## names      : layer
    ## values     : 0, 1248.255  (min, max)

    ### This creates a set of 4 rasters representing the four seasons (Winter, Pre-Monsoon, Monsoon, and Post-Monsoon) of 2022. Visualize them using the plot function to uncover seasonal rainfall patterns and their impacts
    # plot(SeasonalRF$Winter)
    # plot(SeasonalRF$PreMonsoon)
    # plot(SeasonalRF$SWMonsoon)
    # plot(SeasonalRF$PostMonsoon)

    ###  Generating Annual Rainfall Raster from IMD NetCDF Data
    # The "AnnualRF_raster" function generates annual rainfall raster. Example:
    library(CLimd)
    nc_data <- system.file("extdata", "imd_RF_2022.nc", package = "CLimd")
    output_dir <- NULL
    fun<-"sum"
    year<-2022
    AnnualRF<-AnnualRF_raster(nc_data, output_dir=NULL, fun="sum", year)
    AnnualRF

    ## class      : RasterLayer
    ## dimensions : 129, 135, 17415  (nrow, ncol, ncell)
    ## resolution : 0.25, 0.25  (x, y)
    ## extent     : 66.375, 100.125, 6.375, 38.625  (xmin, xmax, ymin, ymax)
    ## crs        : +proj=longlat +datum=WGS84 +no_defs
    ## source     : r_tmp_2024-01-09_153126.333758_28540_13343.grd
    ## names      : layer
    ## values     : 0, 13273.81  (min, max)

    ### This generates a single raster summarizing the total rainfall for the entire year 2022. Plot this raster to visualize the overall rainfall distribution and identify areas of high and low precipitation.
    # plot(AnnualRF)