'Upload a Large Batch (70+) of MODIS netCDF Files into R to Convert into Raster Files for QGIS

Issue:

I have a large batch of 70+ Aqua Modis files from Ocean Colour, NASA in .nc format containing coordinates for monthly data specifically for one variable called sea surface temperature (SST) and I need to convert them into Raster files.

My data frame has approximately 650 rows that represent the GPS location (latitude and longitude coordinates) of dolphin IDs recorded between 2016-and 2021.

Aim:

My ultimate aim is to convert the Modis files into raster files, stack the Modis files using stack::raster(), interpolate and extract the SST values per data point (dolphin IDs) for our research dates using extract() function, calculate the average SST across all raster 70+ files between 2016-2022, convert the final averaged map into a .tiff file, and write the values into a .csv file.

I am a complete novice with GIS methods and I have been dabbling but I can't find an accurate solution regarding how to perform these actions. I've been uploading and converting the MODIS files individually, and extracting the values using the 'Points to Path' function in QGIS; however, it's very time consuming and I won't have enough time to finish before the deadline using this method.

I have been thinking that there must be a quicker and easier way to analyse my data by using R to convert large batches (70+) of files (e.g. convert netCDF files to raster files and raster files to .tiff files) at the same time between the different file types for further analysis.

So far, I have managed to open one 'Aqua Modis' file using this code:

##Open Packages**
library(ncdf4)
library(terra)
library('RNetCDF')

 #Open the netCDF files
 ncin<-nc_open("AQUA_MODIS.20160901_20160930.L3m.MO.SST.sst.4km.nc")

 ##Check the information on the netCDF file
print(ncin)

 ##Get the variables from the netCDF file
 SST_Variables<-ncvar_get(ncin, "sst")
 lon <- ncvar_get(ncin, 'lon')
 lat <- ncvar_get(ncin, 'lat')

##Get information about each variables dimensions
dim(SST_Variables)           *8640 4320
dim(lon)                     *8640
dim(lat)                     *4320

However, I want to open 70+ Aqua Modis files all at once contained in the same folder.

 ##Open all netCDF files in the folder

filenames = list.files('~/Documents/Ocean_ColorSST_2016_2021',pattern='*.nc',full.names=TRUE)

# Loop over files
for(i in seq_along(filenames)) {
  nc = open.nc(filenames[i])
  
  # Read the whole nc file and read the length of the varying dimension 
  lw = var.get.nc(nc,'sst')
  x=dim(lw)
  
  # Vary the time dimension for each file as required
  lw = var.get.nc(nc,'sst')
  
  # Add the values from each file to a single data.frame
  rbind(df, data.frame(lw))-> SST_Files
}

Error Message

Error in rep(xi, length.out = nvar) : 
  attempt to replicate an object of type 'closure'
> SST_Files
Error: object 'SST_Files' not found

Extract the values from each netCDF file for the variable "sst"

ncin_SST <- raster::stack(filenames,varname = "sst")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source