---
title: "India foodcrop data preparation with R"
author: "Anastasis Giannousakis"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{India foodcrop data preparation with R}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```

The mrfable R package gives you quick, easy, and handy access to a large amount of foodrcrop data (area, production, and yield) for the federal States of India in the period 1967-2018. With mrfbale you can automatically download the data from https://eands.dacnet.nic.in/, and correct, clean, and aggregate them. This document provides a quick guide for using the package. 

## Setup

mrfable is based on the MADRaT engine (for more information run `vignettes("madrat")`. To load mrfable simply type (the typical output is shown under some commands as comment):

```{r, echo = TRUE, eval=TRUE}
# load package
library(mrfable)

# set sub-national mapping
setConfig(extramappings = "mappingIndiaAPY.csv")
```

The package is ready to use. 

## Data for all India

To compute the total foodcrop data for all India simply type (for e.g. area data):
```{r, echo = TRUE, eval=FALSE}
myData <- calcOutput("IndiaFoodcrop", subtype = "Area")
```
for yield/production data use:
```{r, echo = TRUE, eval=FALSE}
myDataY <- calcOutput("IndiaFoodcrop", subtype = "Yield")
myDataP <- calcOutput("IndiaFoodcrop", subtype = "Production")
```
This will download, clean, sort, and aggregate the data automatically (access to the individual steps will be shown further down).
The object `mydata` will contain your data. It is an object of the `magpie` class, i.e. an array:
```{r, echo = TRUE, eval=FALSE}
str(myData)
# Formal class 'magpie' [package "magclass"] with 1 slot
#   ..@ .Data: num [1, 1:53, 1:40] 12240 12807 12052 12493 12913 ...
#   .. ..- attr(*, "dimnames")=List of 3
#   .. .. ..$ state                    : chr "IND"
#   .. .. ..$ year                     : chr [1:53] "y1966" "y1967" "y1968" "y1969" ...
#   .. .. ..$ crop.variable.unit.season: chr [1:40] "Bajra.Area.kHectares.total" "Barley.Area.kHectares.total" "Gram.Area.kHectares.total" "Jowar.Area.kHectares.total" ...
```
The dimensions of the object are the spatial, temporal and data dimension. To access any of the dimensions follow this scheme: `myData["spatial","temporal","data"]`, e.g.:
```{r, echo = TRUE, eval=FALSE}
myData["IND", 2018, "Rice.Area.kHectares.total"]
# An object of class "magpie"
# , , crop.variable.unit.season = Rice.Area.kHectares.total
#
#     year
# state    y2018
#  IND 44156.45
# ...
```
This is not so spectacular, however. Let's see how we can see more.
(Note that you don't have to type the unit each time, and you can choose more than one seasons/years):
```{r, echo = TRUE, eval=FALSE}
myData["IND", 2000:2018, "Rice"][, , c("kharif", "rabi")]
# An object of class "magpie"
# , , crop.variable.unit.season = Rice.Area.kHectares.kharif
#
#      year
# state   y2000   y2001 y2002   y2003 y2004   y2005   y2006 y2007   y2008    y2009    y2010    y2011   y2012    y2013 y2014    y2015    y2016    y2017    y2018
#   IND 22672.8 22255.4 20522 21699.7 21187 21790.9 22199.7 21978 22938.9 20979.07 22724.91 23113.69 22488.8 23029.17 39829 39656.45 39845.77 39349.27 39964.35
#
# , , crop.variable.unit.season = Rice.Area.kHectares.rabi
#
#      year
# state  y2000  y2001 y2002 y2003 y2004  y2005  y2006  y2007  y2008  y2009    y2010  y2011    y2012    y2013   y2014    y2015    y2016    y2017    y2018
#   IND 1336.8 1502.1 812.2 975.7 982.8 1569.4 1468.5 1524.7 1688.9 1478.1 1939.762 1321.6 1352.174 2037.124 4281.56 3842.727 4147.582 4424.799 4192.091
```

## Data for individual states

To compute the total foodcrop data for all India states simply type (for e.g. area data):
```{r, echo = TRUE, eval=FALSE}
myData <- calcOutput("IndiaFoodcrop", subtype = "Area", aggregate = "state")
```
Again, to access the data you can filter them like this (a blank field means "show all data"):
```{r, echo = TRUE, eval=FALSE}
myData[, 2000:2018, "Rice"][, , c("kharif", "rabi")]
# An object of class "magpie"
# , , crop.variable.unit.season = Rice.Area.kHectares.kharif
#
#                                           year
# state                                       y2000  y2001  y2002  y2003  y2004  y2005  y2006  y2007  y2008    y2009    y2010    y2011    y2012    y2013   y2014     y2015
#   Andaman and Nicobar Islands                10.9    9.8   10.9   10.6   10.7   10.6   10.6    7.3    7.9    8.140    8.390    8.100    7.850    8.005    6.43    6.1000
#   Andhra Pradesh                           3004.0 2426.1 2109.3 2109.0 2215.0 2526.0 2641.0 2578.0 2803.0 2063.000 2922.000 2874.000 2487.000 2651.000 1635.00 1399.0000
#   Arunachal Pradesh                         118.6  119.5  124.6  119.2  121.6  122.3  122.3  124.0  126.8  121.468  121.570  123.500  126.085  131.990  127.20  128.3000
#   Assam                                       0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.000    0.000    0.000    0.000    0.000 2078.94 2080.0050
#   Bihar                                       0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.000    0.000    0.000    0.000    0.000 3181.11 3151.0120
#   Chhattisgarh                             3769.7 3810.1 3777.7 3829.0 3746.7 3747.2 3723.6 3752.4 3734.0 3670.700 3702.500 3773.800 3784.800 3802.100 3808.50 3816.0000
#   Dadra and Nagar Haveli and Daman and Diu   15.6   15.3   15.3   15.3   15.2   15.4   15.4   15.6   15.4   14.545   12.800   12.320   15.850   15.260   15.38   17.5333
#   Delhi                                       6.1    6.4    6.1    6.5    6.0    7.5    7.4    7.4    7.4    6.821    7.045    6.850    6.700    6.040    6.04    6.0350
#   Goa                                         0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.000    0.000    0.000    0.000    0.000   27.84   27.7300
#   Gujarat                                   583.6  667.9  468.9  651.4  661.7  666.0  693.0  726.0  722.0  658.000  728.000  752.000  672.000  757.000  755.00  741.0000
#   Haryana                                  1054.0 1028.0  906.0 1015.0 1028.0 1052.0 1041.0 1075.0 1210.0 1205.000 1245.000 1235.000 1215.000 1228.000 1287.00 1354.0000
#   Himachal Pradesh                           81.5   80.6   83.3   81.3   81.0   79.4   79.2   78.6   77.7   76.696   77.064   77.230   76.900   74.360   72.47   73.6850
#   Jammu & Kashmir                           244.1  249.8  236.2  259.8  250.0  259.0  252.5  263.2  257.6  259.892  261.347  262.169  261.660  271.490  276.42  304.5000
#   Jharkhand                                   0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.000    0.000    0.000    0.000    0.000 1502.20 1588.9790
#   Karnataka                                1120.5 1088.0 1030.5  944.3 1056.0 1076.0 1066.0 1051.0 1130.0 1102.000 1130.000 1118.000 1048.000 1035.000 1000.00  977.0000
#   Kerala                                      0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.000    0.000    0.000    0.000    0.000  152.97  150.7663
#   Madhya Pradesh                           1707.6 1776.4 1681.3 1718.8 1622.5 1657.7 1661.3 1558.9 1682.3 1445.700 1602.900 1662.000 1882.600 1930.000 2153.00 2024.0000
#   Maharashtra                              1486.4 1487.2 1497.0 1500.0 1488.0 1474.0 1491.0 1535.0 1500.0 1450.000 1486.000 1516.000 1528.000 1568.000 1508.00 1471.0000
# ...
```

## Data for India Zonal Councils

To compute the total foodcrop data for all Zonal Councils of India states type (for e.g. area data):
```{r, echo = TRUE, eval=FALSE}
myData <- calcOutput("IndiaFoodcrop", subtype = "Area", aggregate = "Zonal.Council")
```
And access the data same as above.

## Output to file(s)

By use of typical functions (see e.g. `magclass::as.data.frame`) you can transform the cleaned data to other data types (e.g. data frames etc.) and/or write them in files:
```{r,echo=TRUE,eval=FALSE}
write.magpie(myData, file_name = "mydata.csv")
```
(see `magclass::write.magpie` for more data types).

You can also extract the whole cleaned dataset for use in projects (e.g. for use as input for a model) like this:
```{r,echo=TRUE,eval=FALSE}
retrieveData("DATAINDIA")
```
The location of the data on your machine will be reported in the command output as `outputfolder`
