r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
677 stars 146 forks source link

Question - how to get ee_get_date_ic to work after compositing and setting 'system:time_start'? #216

Closed zackarno closed 2 years ago

zackarno commented 2 years ago

Hello,

I was wondering if there is a way/work-around to get the ee_get_date_ic function to work again after creating a composite image collection in which the dates were reset in a map function?

You can see below I started to write a custom function to composite yearly & monthly rasters/ImageCollections (mainly borrowed from Javascript I saw stackoverflow). I believe it is working correctly. However, when I run the ee_get_date_ic on the resulting object I get an error. I thought it would be possible to use this function on the result because the "system:time_start" is re - set in the function. Is there a something I can do to allow ee_get_date_ic to work on the output?

This package is awesome!!! thanks a ton

ex below:

library(tidyverse)
library(rgee)
ee_Initialize()
#> -- rgee 1.1.2 --------------------------------------- earthengine-api 0.1.293 -- 
#>  v user: not_defined
#>  v Initializing Google Earth Engine: v Initializing Google Earth Engine:  DONE!
#>  v Earth Engine account: users/zackarno 
#> --------------------------------------------------------------------------------
chirps_src <- "UCSB-CHG/CHIRPS/DAILY"
chirps <- ee$ImageCollection(chirps_src)

# // Map filtering and reducing across year-month combinations and convert to ImageCollection
yr_mo_composite_stat_ic <- function(dat,month_range, year_range, stat="mean"){

  month_list <- ee$List$sequence(month_range[1], month_range[2])
  year_list <- ee$List$sequence(year_range[1],year_range[2])

  collapse_ic_fun<- switch(
    stat,
    "mean" = function(x)x$mean(),
    "max" = function(x)x$max(),
    "min" = function(x)x$min(),
    "median"= function(x)x$median(),
    "sum"= function(x)x$sum(),
    "sd" = x$reduce(ee$Reducer$stdDev()),
    NULL
  )

  composites <- ee$ImageCollection$fromImages(
    year_list$map(
      ee_utils_pyfunc(function (y) {
        month_list$map(
          ee_utils_pyfunc(function (m) {
            # dat_pre_filt <- 
            dat_filtered_yr_mo<- dat$
              filter(ee$Filter$calendarRange(y, y, 'year'))$
              filter(ee$Filter$calendarRange(m, m, 'month'))

            collapse_ic_fun(dat_filtered_yr_mo)$
              set('year',y)$
              set('month',m)$
              set('system:time_start',ee$Date$fromYMD(y,m,1))

          })
        )
      }))$flatten())
  return(composites)
}

chirps_monthly_sum <- yr_mo_composite_stat_ic(dat = chirps,month_range = c(1,12),year_range = c(1981,2021),stat = "sum")
chirps_monthly_sum |> ee_print()
#> Registered S3 method overwritten by 'geojsonsf':
#>   method        from   
#>   print.geojson geojson
#> ------------------------------------------------ Earth Engine ImageCollection --
#> ImageCollection Metadata:
#>  - Class                      : ee$ImageCollection
#>  - Number of Images           : 492
#>  - Number of Properties       : 0
#>  - Number of Pixels*          : 31881600
#>  - Approximate size*          : 97.29 MB
#> Image Metadata (img_index = 0):
#>  - ID                         : no_id
#>  - system:time_start          : 1981-01-01
#>  - Number of Bands            : 1
#>  - Bands names                : precipitation
#>  - Number of Properties       : 4
#>  - Number of Pixels*          : 64800
#>  - Approximate size*          : 202.50 KB
#> Band Metadata (img_band = 'precipitation'):
#>  - EPSG (SRID)                : WGS 84 (EPSG:4326)
#>  - proj4string                : +proj=longlat +datum=WGS84 +no_defs
#>  - Geotransform               : 1 0 0 0 1 0
#>  - Nominal scale (meters)     : 111319.5
#>  - Dimensions                 : 360 180
#>  - Number of Pixels           : 64800
#>  - Data type                  : DOUBLE
#>  - Approximate size           : 202.50 KB
#>  --------------------------------------------------------------------------------
#>  NOTE: (*) Properties calculated considering a constant  geotransform and data type.
chirps_monthly_sum |> 
  ee_get_date_ic()
#> Warning in ee_utils_py_to_r(ee_utils$eedate_to_rdate_ic(x,
#> "system:time_start")): restarting interrupted promise evaluation
#> Error in py_call_impl(callable, dots$args, dots$keywords): TypeError: float() argument must be a string or a number, not 'dict'
#> 
#> Detailed traceback:
#>   File "C:\Users\ssieb\OneDrive\Documents\R\win-library\4.1\rgee\python\ee_utils.py", line 26, in eedate_to_rdate_ic
#>     ic_dates = [float(img) for img in ic.aggregate_array(var).getInfo()]
#>   File "C:\Users\ssieb\OneDrive\Documents\R\win-library\4.1\rgee\python\ee_utils.py", line 26, in <listcomp>
#>     ic_dates = [float(img) for img in ic.aggregate_array(var).getInfo()]

Created on 2022-01-14 by the reprex package (v2.0.1)

csaybar commented 2 years ago

Hi, @zackarno nice first issue :)

Sorry, it was a bug in the rgee python side. It should work now.

remotes::install_github("r-spatial/rgee")

Let us know if the problem persists.

zackarno commented 2 years ago

Thanks so much @csaybar! It is working now after the reinstall