ropensci / chirps

API Client for CHIRPS
https://docs.ropensci.org/chirps/
MIT License
30 stars 12 forks source link

Using get_chirps over a SFC, possible? #37

Open MariusHees opened 2 years ago

MariusHees commented 2 years ago

Hello everyone and thank you very much for all of your amazing work,

I am trying to pull the chirps data for Ethiopia and I am struggling to advance.

This is my first time using CHIRPS, but I am really interested in working myself into the subject. My goal is to get CHIRPS data for ETHIOPIA, I will want to finally de-trend the precipitation averages (either daily or monthly) in order to find abnormalities, in order to identify regional droughts as the basis for me research. Hence, I would ideally have both the precipitation on a pixel by pixel basis and will also be able to connect the pixel to the respective districts.

I am using: https://geodata.lib.berkeley.edu/catalog/stanford-kz352sx7513 : as base input of Ethiopian districts over which I want get the chirps data.

Problem

I am reading in the ETHIPIA District Shapefile above as a SFC_Multipolygon, with the intention of pulling the CHIRPS data for each district over a given period of time. Am I correct in the assumption that the "get_chirps" command is currently only able to pull data for geographic points or is it somehow able to pull the CHIRPS data for entire districts?

I have been trying around a lot but can't figure which approach would work, if you guys have any insights or inputs I would be incredibly grateful. Even if the answer is an affirmation that what I want to do is not possible with the get_chirps command. (Would have to manually load in all the CHIRPS data images.)

The code (ETHIOPIA = The data input):

ETHIOPIA_shp <- st_read(ETHIOPIA)

ETHIOPIAREP_shp <-st_transform(ETHIOPIA_shp, CRS(OGP))

st_write( ETHIOPIAREP_shp, ETHIOPIAREP, format="GTiff", append=FALSE)

SPDF <- readOGR(dsn=ETHIOPIAREPP,layer="ETHIOPIAREP")

dates <- c("2017-12-15", "2017-12-31")

get_chirps(SPDF, dates, operation = 5)

yields the error :

Fehler in [.data.frame(lonlat, , 2) : undefined columns selected

and if I use:

get_chirps(ETHIOPIAREP_shp, dates, operation = 5)

in the last line, it runs forever without end. This is where I think it might not be possible to run it for multipolygons.

I hope this question / issue is not to far away from the intended use of get_chirps and that I am not wasting your time.

Thank you in advance for any help or input!

All the best, Marius

data(1).zip

MariusHees commented 2 years ago

For clarity, this is the set up:

ETHIOPIA <- paste0(sourcepath, "/EthiopiaSHAPE")

ETHIOPIAREP <- paste0(datapath, "/IN_PROGRESS/ETHIOPIA/ETHIOPIAREP.shp")

ETHIOPIAREPP <- paste0(datapath, "/IN_PROGRESS/ETHIOPIA")

OGP <- "+proj=longlat +datum=WGS84 +no_defs"

samyadelara commented 2 years ago

Hello dear team, hello @MariusHees ....

I am facing similar issue (how to retrieve data using get_chirps with a polygon as object input). Is there any update on this issue? @MariusHees, did you managed to work with your district polygons somehow?

Thank you! Kind regards Sllap

kauedesousa commented 2 years ago

Hi @samyadelara and @MariusHees sorry I missed this issue, but this is something that I asked to the ClimateSERV team with @billyz313 in this issue https://github.com/SERVIR/ClimateSERV/issues/3

@billyz313 do you know if this is already available? I tried to find at the API document but I didn't.

As an alternative option we could request the data from the other server CHC as COG files and handle it internally using terra and return either a raster or a sfc polygon. This might be a bit slow but good enough for reproducible analysis and workflows. What do you think @adamhsparks ?

kauedesousa commented 2 years ago

I worked in an alternative solution using CHC server. Remember that it may take a while to fetch the data if you request for many years data.

If you install chirps v0.1.4.001 from Github using devtools you will be able to run this example.

library(terra)
library(sf)
library(chirps)

data("tapajos")

area <- st_bbox(tapajos)

area <- as.vector(area)

area <- ext(area)

datas <- c("2010-01-01", "2010-01-15")

rain <- get_chirps(area, datas, server = "CHC")

Is this useful for you?

adamhsparks commented 2 years ago

@kauedesousa, I agree handling it internally with terra / sf with just regular rectangular data would be a fine compromise for reproducibility.

samyadelara commented 2 years ago

Dear @kauedesousa and @adamhsparks , thank you so much for your prompt feedback on this and suggestions.

Indeed, working with the bbox, extented using terra, worked perfectly for me here. Thank you so much!

Cheers all!

albirayi commented 2 years ago

extnt <- as(extent(c(xmin= 31, xmax= 49, ymin= 2, ymax= 20)), 'SpatialPolygons') crs(extnt) <- "+proj=longlat +datum=WGS84 +no_defs" area <- ext(extnt) dates <- c("2017-12-15","2017-12-31") get_chirps(area, dates, server = "CHC", as.raster = TRUE)

this worked well for me to extract data for the given extent. However, I could not able to extract sample points from the domain.

43

spt <- st_sample(area, 30) spt <- st_as_sf(spt) dft <- get_chirps(spt, dates = c("1990-01-01","2018-12-31")) p_ind <- precip_indices(dft, timeseries = TRUE, intervals = 30)

I hope you found a way to solve this as well. Thank you in advance for any help or input!

All the best,

Markos