VicenteYago / OPTRAM

Satellite and sensor based model to predict soil moisture at the surface level.
7 stars 0 forks source link

Obtain sensing datetime for all the satellite images #3

Closed VicenteYago closed 2 years ago

VicenteYago commented 2 years ago

It can be obtained throught sen2r lib in R:

files <- list.files(folder.BOA, full.names = T)

# obtain sensig datetime
time_window <- as.Date(c(files %>% basename() %>% head(1),
                         files %>% basename() %>% tail(1)))

results <- geojsonsf::geojson_sfc(parcela.json) %>% 
  sen2r::s2_list(time_interval = time_window)

sensing_datetime <- sen2r::safe_getMetadata(results, "sensing_datetime") %>
VicenteYago commented 2 years ago

Solving :

File: sat_sensing_dt.R

args <- commandArgs(trailingOnly = TRUE)
start_dt = args[1] # yyyy-mm-dd
end_dt   = args[2] # yyyy-mm-dd
geojson  = args[3]

time_window <- as.Date(c(start_dt, end_dt))
gj.sf = geojsonsf::geojson_sfc(geojson)
results = sen2r::s2_list(gj.sf, time_interval = time_window)

sensing_dt <- sen2r::safe_getMetadata(results, "sensing_datetime")
sensing_dt

Run :

# Rscript sat_sensing_dt.R "2021-01-01" "2021-01-10" Walnut-Gulch.geojson 2> /dev/null
['S2A_MSIL2A_20210101T175741_N0214_R141_T12SWA_20210101T203251.SAFE ',
 '                                        "2021-01-01 17:57:41 UTC" ',
 'S2A_MSIL2A_20210104T180741_N0214_R041_T12SWA_20210104T203145.SAFE ',
 '                                        "2021-01-04 18:07:41 UTC" ',
 'S2B_MSIL2A_20210106T175729_N0214_R141_T12SWA_20210106T203319.SAFE ',
 '                                        "2021-01-06 17:57:29 UTC" ',
 'S2B_MSIL2A_20210109T180729_N0214_R041_T12SWA_20210109T204254.SAFE ',
 '                                        "2021-01-09 18:07:29 UTC" ']

Thus the R script can be invoked from python as a bash command:

days_offset = 1
start_dt = S2_getDate(boa_fp) - datetime.timedelta(days=days_offset)
end_dt   = S2_getDate(boa_fp) + datetime.timedelta(days=days_offset)

start_dt = start_dt.strftime('%Y-%m-%d')
end_dt   = end_dt.strftime('%Y-%m-%d')

print('Requesting image metadata between %s - %s' % (start_dt, end_dt))

# bash callback: 
dts = ! Rscript ./sen2r/sat_sensing_dt.R $start_dt $end_dt ./sen2r/Walnut-Gulch.geojson 2> /dev/null
dts