1 For FIgshare to R

Code to download data stored in the Figshare repository “A Database of Global Coastal Conditions” from R. The code below needs just a few user input, each line has been documented and will direct you to what need to be change or selected.

References: http://lcolladotor.github.io/rfigshare_test/download.html

Load packages rfigshare into the R environment as follows. If package is not yet installed, it would install it for you.

if(!require("rfigshare")) install.packages("rfigshare")
## Loading required package: rfigshare

Below the article items ids. Within the project, there are 4 different dataset, each with an unique ID. In the next step you’ll be ask to select one of them to download an specific dataset within the project.

1. Sea Surface Temperature Monthly Statistics: **"13670395"**
2. Sea Surface Temperature Yearly Statistics: **"13681375"**
3. Chlorophyll-a Monthly Statistics: **"13670494"**
4. Chlorophyll-a Yearly Statistics: **"13678429"**

1.1 Accessing Data

Select an article ID form the list above and change the value of the variable article_id below.

article_id <- "change_to_article_id"

The next few lines will get all the necessary data about the files for download. You might need to create a figshare account to download.

# Get the details for all the files contained in the article ID
metadata <- fs_details(article_id, mine = FALSE) 

# Get the urls for download of each of the files
file_urls <- fs_download(article_id)

# Get the names of each of the files
file_names <- sapply(metadata$files, '[[', 'name')

# Combine URLs with files names to create a data frame
files_data <- data.frame(name = file_names,
                         url = file_urls)

1.2 Subsetting

If you wish to subset by a certain month/year or statistic you can do so by following one of the two options below.

To subset by only one, change the value of the variable pattern to your desire month, year or statistic, and run.

pattern <- "2019" 
files_data_subset <- files_data[grep(pattern, files_data$name), ]

To subset by multiple, change the value of the variable patterns to your desire months, years or statistics, and run.

patterns <- c("01", "02", "03")
files_data_subset <- files_data[grep(paste(patterns, collapse = "|"), files_data$name), ]

1.3 Downloading

Create a new directory to store the files to be downloaded. Assign the path or the name (if folder is within working directory) of the desire folder destination for the files

dir_name <- "change_to_directory_name"
dir.create(dir_name)

Run the following lines to download the data and unzip folders. NOTE: if you have subset you will need to change the variable files_data to files_data_subset in all the instances mention below (3 total)

sapply(1:nrow(files_data), FUN = function(i){
  download.file(url = files_data[i, 2], 
                destfile = paste0(dir_name, "/", files_data[i, 1]),
                mode = "wb")
  
  unzip(zipfile = paste0(dir_name, "/", files_data[i, 1]), exdir = dir_name) 
})

2 Figures form Manuscript

Below the description of how the data can be cropped to an specific latitude and longitude. The following code contain the exact commands used to create Figures 3 and 4 for the manuscript. Note that all three used the same commands, the only difference was the latitude and longitude selected. Hence, the example below is only for one of them.

For the plots you would need to load or install the raster and rasterVis packages.

if(!require("raster")) install.packages("raster")
## Loading required package: raster
## Loading required package: sp
if(!require("rasterVis")) install.packages("rasterVis")
## Loading required package: rasterVis
## Loading required package: terra
## terra version 1.2.10
## Loading required package: lattice
## Loading required package: latticeExtra

Lets say we want to plot the data for the mean monthly composites of sea surface temperature (SST).

sst_tif <- list.files(path = "../../Data/SST monthly/",
                      pattern = "(mean).*tif$", # read only the mean 
                      recursive = TRUE, # recursive checks all inside folders
                      full.names = TRUE)

stack_sst <- stack(sst_tif)

names(stack_sst) <- month.abb 

This will set the names of our layers in accordance to the month they represent numerically

2.1 Cropping

Now we can determine coordinates we would like to look at

crop_1 <- extent(-77.47199754307310116, -71.70063605775197857,
                 34.67908943905803199, 40.54291599490499465)

raster_1 <- crop(stack_sst, crop_1)

2.2 Plotting

To plot our cropped data we use the function levelplot from the rasterVis package

lp_1 <- levelplot(raster_1)

lp_1

2.3 Saving

If you want to save your plot as a high resolution picture you can save it as a tiff.

#Create a new directory to store your figures
dir.create("Figures")

tiff(filename = "./Figures/Temp_Zone_SST.tiff", 
     width = 7, height = 5, units = "in", 
     compression = "lzw", res = 300)
lp_1
dev.off()

The same way we have manipulated the SST data we can manipulate the SST yearly, as well as, the CHLO-a monthly and yearly data.

Reference: https://oscarperpinan.github.io/rastervis/