Retrieve data from GYGA

  • João Vasco Silva, CIMMYT-Zimbabwe

  • Marloes van Loon, WUR


Introduction

This notebook complements an earlier notebook describing the methodology for yield gap decomposition. That earlier notebook makes use of water-limited yield data to decompose yield gaps. Such data were derived using the scripts documented in this notebook. The reader is referred to that earlier notebook for further information about the concepts and definitions considered in yield gap analysis. To make the approach fully reproducible, it is explained here how to retrieve the water-limited yield data from the Global Yield Gap Atlas (GYGA) using available APIs for acquiring such data. Also here an example is provided for for wheat in Ethiopia.

Load required R packages

First, the R packages needed to run this workflow are loaded.

# package names
packages <- c("dplyr", "tidyr", "httr", "jsonlite", "sf", "reshape2")
#
# install packages
installed_packages <- packages %in% rownames(installed.packages())
if(any(installed_packages == FALSE)){
  install.packages(packages[!installed_packages], repos="http://cran.us.r-project.org", quiet=T)}
#
# load packages
invisible(lapply(packages, function(x) suppressMessages(require(x, character.only=T, quietly=T, warn.conflicts=F))))

Access to GYGA data

An example on how data can be accessed from GYGA, and linked to farmer field data, using an API specifically set for wheat water-limited yields in Ethiopia. The chunk of code below illustrates how to access from the GYGA API the following data for a given country: (a) crop yield data for all weather stations, (b) weather station coordinates and information and, (c) climate zones. Please note an internet connection is needed to access the API.

# retrieve data for all weather stations
request <- httr::GET(
  "https://www.yieldgap.org/apigyga/json/cropcountrystationyear?accesstoken=anonymous&par1=30&par2=5",
  httr::add_headers())
wheat_data <- httr::content(request, as = "text")
wheat_data <- jsonlite::fromJSON(wheat_data)[["items"]]
wheat_data$harvestyear <- paste0('Yw_', wheat_data$harvestyear)
wheat_data <- dcast(wheat_data, country + country_id + climatezone +
                                station + station_id + crop + crop_id ~ harvestyear,
                    value.var='yw')
#
# get coordinates of weather stations
request <- httr::GET("https://www.yieldgap.org/apigyga/metadata/stations",
                     httr::add_headers())
json_data <- httr::content(request, as="text")
gyga_stations <- jsonlite::fromJSON(json_data)[["items"]]
gyga_stations <- gyga_stations[gyga_stations$station_id %in% unique(wheat_data$station_id),]
#
# retrieve climate zones gyga
request <- httr::GET("https://www.yieldgap.org/apigyga/metadata/climatezones",
                     httr::add_headers())
json_data <- httr::content(request, as="text")
cz_data <- jsonlite::fromJSON(json_data)[["items"]]
cz_data <- cz_data[cz_data$country == unique(wheat_data$country),]

The next step entails merging the yield data from GYGA with the respective weather station coordinates and climate zone information. Once data are merged, the crop yield data is made spatially explicit using the st_as_sf() function. Please refer to the chunk of code below.

# merge wheat data with gyga stations
wheat_data <- merge(wheat_data[,c("country", "country_id", "station", "station_id",
                                  "climatezone", "crop", "crop_id",
                                  "Yw_1998", "Yw_1999", "Yw_2000", "Yw_2001",
                                  "Yw_2002", "Yw_2003", "Yw_2004", "Yw_2005",
                                  "Yw_2006", "Yw_2007", "Yw_2008", "Yw_2009",
                                  "Yw_2010", "Yw_2011", "Yw_2012", "Yw_2013",
                                  "Yw_2014", "Yw_2015", "Yw_2016", "Yw_2017")],
                    gyga_stations[,c("station_id", "latitude", "longitude")],
                    by="station_id", all.x=TRUE)
#
# merge wheat data with climate zones
wheat_data <- merge(wheat_data,
                    cz_data[,c("climatezone", "climatezone_id")],
                    by="climatezone", all.x=TRUE)
#
# convert to spatial dataframe
wheat_data <- st_as_sf(wheat_data, coords=c("longitude", "latitude")) %>%
  st_set_crs(4326)


Export file with GYGA data

The last step in this workflow is to bring back the different subsets of fields into a single data frame. Recall: the different subsets of fields are as follows: (a) fields with a weather station within the respective climate zone, (b) fields without a climate zone for which crop model simulations were done but with a weather station closer than 30km and, (c) fields without a climate zone for which crop model simulations were done and with no weather station closer than 30km. This is implemented in the chunk of code below, where this final data frame is saved to disk as a csv file.

# final data frame
data_rfwh_final <- rbind(data_rfwh_with_data[,c("hhid", "country", "crop",
                                                "data_from", "geometry",
                                                "Yw_1998", "Yw_1999", "Yw_2000", "Yw_2001",
                                                "Yw_2002", "Yw_2003", "Yw_2004", "Yw_2005",
                                                "Yw_2006", "Yw_2007", "Yw_2008", "Yw_2009",
                                                "Yw_2010", "Yw_2011", "Yw_2012", "Yw_2013",
                                                "Yw_2014", "Yw_2015", "Yw_2016", "Yw_2017")],
                         data_rfwh_no_data_30[,c("hhid", "country", "crop",
                                                 "data_from", "geometry",
                                                 "Yw_1998", "Yw_1999", "Yw_2000", "Yw_2001",
                                                 "Yw_2002", "Yw_2003", "Yw_2004", "Yw_2005",
                                                 "Yw_2006", "Yw_2007", "Yw_2008", "Yw_2009",
                                                 "Yw_2010", "Yw_2011", "Yw_2012", "Yw_2013",
                                                 "Yw_2014", "Yw_2015", "Yw_2016", "Yw_2017")],
                         data_rfwh_no_data_m30[,c("hhid", "country", "crop",
                                                  "data_from", "geometry",
                                                  "Yw_1998", "Yw_1999", "Yw_2000", "Yw_2001",
                                                  "Yw_2002", "Yw_2003", "Yw_2004", "Yw_2005",
                                                  "Yw_2006", "Yw_2007", "Yw_2008", "Yw_2009",
                                                  "Yw_2010", "Yw_2011", "Yw_2012", "Yw_2013",
                                                  "Yw_2014", "Yw_2015", "Yw_2016", "Yw_2017")])
#
# get cimate zones for each field
data_rfwh_final <- st_join(data_rfwh_final, climate_zone, join=st_intersects, left=FALSE)
#
# remove geometry
data_rfwh_final <- st_drop_geometry(data_rfwh_final)
#
# calculate average Yw
data_rfwh_final$Yw_average <- rowMeans(data_rfwh_final[c(5:24)], na.rm=TRUE)
#
# save csv file
write.csv(data_rfwh_final, 'data-gps-coordinates-final.csv')

Final remarks

Please note this script does not add data to fields without GPS coordinates reported. The national average could also be considered for those fields, but this is not implemented in this script.

Data from GYGA can be made available to the OneCGIAR initiative on Excellence in Agronomy on a demand basis. Requests should be made to prof. Martin van Ittersum (martin.vanittersum@wur.nl). The script documented here is reproducible and can be used to retrieved GYGA data for other crop x country combinations. Any questions or suggestions for improving the scripts presented in this document should be addressed to j.silva@cgiar.org or marloes.vanloon@wur.nl.