-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_productivity.R
66 lines (46 loc) · 2.09 KB
/
add_productivity.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
library(raster)
library(sp)
library(readr)
library(dplyr)
# This code reprojects the productivity data to 50m resolution
# Then adds it to dive route csvs
# Read the TIFF file
ocean_prod <- raster("/Users/user/Desktop/Mean_productivity.tif")
desired_res_deg <- 0.00045 # 50 meters in degrees
template_raster <- raster(extent(ocean_prod), res=desired_res_deg)
projection(template_raster) <- projection(ocean_prod)
# Resample using bilinear interpolation
high_res_raster <- resample(ocean_prod, template_raster, method='bilinear')
# Save the resampled productivity raster
writeRaster(high_res_raster, "/Users/user/Desktop/high_res_raster.tif", format="GTiff", overwrite=TRUE)
high_res_raster <- raster("/Users/user/Desktop/high_res_raster.tif")
# Define the main and output folder paths
main_folder_path <- "/Users/user/Desktop/coral_biigle_feb/5.coral_biigle_duration"
output_folder_path <- "/Users/user/Desktop/coral_biigle_feb/6.coral_biigle_productivity"
# Function to add productivity data to a CSV file
add_productivity_data <- function(csv_file, raster_file, output_folder) {
print(paste("Processing file:", csv_file))
data <- read_csv(csv_file)
# Check for NA values before conversion
if (any(is.na(data$lon)) || any(is.na(data$lat))) {
print("Warning: NA values found in longitude or latitude")
return()
}
# Ensure lat and long are numeric
data$lon <- as.numeric(data$lon)
data$lat <- as.numeric(data$lat)
coordinates <- data.frame(lon = data$lon, lat = data$lat)
coordinates_sp <- SpatialPoints(coordinates, proj4string = crs(raster_file))
# Extract productivity values
values <- raster::extract(raster_file, coordinates_sp)
data$productivity_mean <- values
# Save the updated CSV file
output_file_path <- file.path(output_folder, basename(csv_file))
write_csv(data, output_file_path)
print(paste("File written:", output_file_path))
}
# List and process all CSV files
csv_files <- list.files(main_folder_path, pattern = "\\.csv$", full.names = TRUE)
for (csv_file in csv_files) {
add_productivity_data(csv_file, high_res_raster, output_folder_path)
}