I am new to R and am now looking for a solution to my problem. I have a directory with files in .nc (netCDF) format that contain daily data on sea surface temperature. Each day during the period from December 1, 2019 to August 1, 2021 corresponds to a file containing data on the water temperature for the day. The date to which the file belongs is contained in the middle of the file name in the yyyymmdd format (for example – 'TERRA_MODIS.20191201.L3m.DAY.SST.x_sst.nc ').
Link on example of MODIS files. In this work only files with name endings "L3m.DAY.SST.x_sst.nc" should be used.
A simple visualization of sea surface temperature data looks like this:
library('ncdf4')
library('lattice')
library('RColorBrewer')
nc <- nc_open('TERRA_MODIS.20191201.L3m.DAY.SST.x_sst.nc')
lon <- ncvar_get(nc, 'lon')
lat <- ncvar_get(nc, 'lat', verbose = F)
SST <- ncvar_get(nc, 'sst')
grid <- expand.grid(lon=lon, lat=lat)
cutpts <- c(0,5,10,15,20,25,30,35,40,45)
levelplot(SST ~ lon * lat, data=grid, at=cutpts, cuts=11, pretty=T, col.regions=(rev(brewer.pal(10,'RdBu'))))
I also have a dataframe containing the No. of the buoy, the date of the location and the geographic coordinates of the location of the buoy on that day
example <- read.csv('example.csv', sep=';',dec='.')
example
Buoy Date Longitude Latitude
1 1 2019-12-01 50.29614 43.92681
2 1 2019-12-02 50.23525 43.89244
3 1 2019-12-03 50.19717 43.88295
4 1 2019-12-04 50.20559 43.88417
5 1 2019-12-05 50.26016 43.86125
6 2 2019-12-01 51.73309 46.53087
7 2 2019-12-02 51.79530 46.56380
8 2 2019-12-03 51.79190 46.53550
9 2 2019-12-04 51.79958 46.56178
10 2 2019-12-05 51.85411 46.33031
11 3 2019-12-01 51.54246 41.28999
12 3 2019-12-02 50.76324 41.60532
13 3 2019-12-03 51.39782 41.79459
14 3 2019-12-04 49.52380 42.33821
15 3 2019-12-05 49.48472 42.62323
I need to extract the sea surface temperature value to column 'SST' for each location depending on the date for which the location was obtained. The result should look something like this:
> example
Buoy Date Longitude Latitude SST
1 1 2019-12-01 50.29614 43.92681 13
2 1 2019-12-02 50.23525 43.89244 16
3 1 2019-12-03 50.19717 43.88295 2
4 1 2019-12-04 50.20559 43.88417 10
5 1 2019-12-05 50.26016 43.86125 8
6 2 2019-12-01 51.73309 46.53087 18
7 2 2019-12-02 51.79530 46.56380 4
8 2 2019-12-03 51.79190 46.53550 17
9 2 2019-12-04 51.79958 46.56178 20
10 2 2019-12-05 51.85411 46.33031 13
11 3 2019-12-01 51.54246 41.28999 14
12 3 2019-12-02 50.76324 41.60532 18
13 3 2019-12-03 51.39782 41.79459 8
14 3 2019-12-04 49.52380 42.33821 7
15 3 2019-12-05 49.48472 42.62323 2
Could you tell me how this can be implemented in R?
list.files(full.names = TRUE)[endsWith(list.files(), suffix = 'L3m.DAY.SST.x_sst.nc')]
gets you the files you want.