### Generating a list of granule files to process from the GEDI downloads folder
setwd(download_dir)
list <- list.files(setwd(download_dir), pattern = "*.h5")
list
### Define the desired beams you want to retain - (these are full power beams)
target_beams <- c("BEAM0101", "BEAM0110", "BEAM1000", "BEAM1011")
### For loop to read in and process the GEDI data
for (i in seq(list)){
### Get the first object in the list
desired_granule <- list[[i]]
desired_granule
### Get an output name
out_name <- tools::file_path_sans_ext(desired_granule)
out_name_shp <- paste0(out_name, ".shp")
out_name_csv <- paste0(out_name, ".csv")
### Read in the .h5 file, convert to dataframe object
setwd(download_dir)
file = readLevel2A(desired_granule)
file = getLevel2AM(file)
file = as.data.frame(file)
### Filter the GEDI dataframe to contain desired information
file = subset(file, select = c(shot_number, beam, degrade_flag, quality_flag,
sensitivity, solar_elevation, lat_lowestmode,
lon_lowestmode, elev_lowestmode, rh0, rh5,
rh10, rh20, rh30, rh40,rh50, rh60, rh65,
rh70, rh75, rh80, rh85, rh90, rh95, rh96,
rh97, rh98, rh99, rh100))
file = filter(file, ((solar_elevation < 0) &
(degrade_flag == 0) &
(quality_flag == 1) &
(beam %in% target_beams) &
(sensitivity >= 0.95) ) )
### Convert the gedi dataframe to a spatial points object, reproject to EPSG 5070
file_spdf = SpatialPointsDataFrame(cbind(file$lon_lowestmode,file$lat_lowestmode), data=file)
proj4string(file_spdf) = CRS("+init=epsg:4326")
file_spdf <- spTransform(file_spdf, CRS("+init=epsg:5070"))
proj4string(file_spdf) = CRS("+init=epsg:5070")
### Write out a shapefile object
out_shp <- st_as_sf(file_spdf)
setwd(processed_data_dir)
st_write(out_shp, out_name_shp, append=FALSE)
### Convert back to dataframe, write out .csv file
file <- as.data.frame(file_spdf)
setwd(processed_data_dir)
fwrite(file, file = out_name_csv, sep = ",")
### Clean up your working environment to save memory
rm(file, out_name, desired_granule)
gc()
}