1

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.

Example of file structure

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

Link on example.csv

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?

4
  • It would be useful to provide a link to data, though likely could be googled.
    – Chris
    Commented Oct 17, 2022 at 4:00
  • I added link on files which should be used
    – Bowe22
    Commented Oct 17, 2022 at 20:17
  • For the first part list.files(full.names = TRUE)[endsWith(list.files(), suffix = 'L3m.DAY.SST.x_sst.nc')] gets you the files you want.
    – Chris
    Commented Oct 18, 2022 at 4:23
  • reading files with specific name is not the main problem. the main problem is to extract values from cdf for each point according to date. but thank you
    – Bowe22
    Commented Oct 19, 2022 at 2:07

1 Answer 1

0

With sst.zip unzipped into working directory and referring to extract:

files <- unzip('~/Downloads/sst.zip')
> files
 [1] "./TERRA_MODIS.20191205.L3b.DAY.SST.NRT.x.nc"    
 [2] "./TERRA_MODIS.20191205.L3b.DAY.SST.x.nc"        
 [3] "./TERRA_MODIS.20191205.L3m.DAY.SST.NRT.x_sst.nc"
 [4] "./TERRA_MODIS.20191205.L3m.DAY.SST.x_sst.nc"    
 [5] "./TERRA_MODIS.20191201.L3b.DAY.SST.NRT.x.nc"    
 [6] "./TERRA_MODIS.20191201.L3b.DAY.SST.x.nc"        
 [7] "./TERRA_MODIS.20191201.L3m.DAY.SST.NRT.x_sst.nc"
 [8] "./TERRA_MODIS.20191201.L3m.DAY.SST.x_sst.nc"    
 [9] "./TERRA_MODIS.20191202.L3b.DAY.SST.NRT.x.nc"    
[10] "./TERRA_MODIS.20191202.L3b.DAY.SST.x.nc"        
[11] "./TERRA_MODIS.20191202.L3m.DAY.SST.NRT.x_sst.nc"
[12] "./TERRA_MODIS.20191202.L3m.DAY.SST.x_sst.nc"    
[13] "./TERRA_MODIS.20191203.L3b.DAY.SST.NRT.x.nc"    
[14] "./TERRA_MODIS.20191203.L3b.DAY.SST.x.nc"        
[15] "./TERRA_MODIS.20191203.L3m.DAY.SST.NRT.x_sst.nc"
[16] "./TERRA_MODIS.20191203.L3m.DAY.SST.x_sst.nc"    
[17] "./TERRA_MODIS.20191204.L3b.DAY.SST.NRT.x.nc"    
[18] "./TERRA_MODIS.20191204.L3b.DAY.SST.x.nc"        
[19] "./TERRA_MODIS.20191204.L3m.DAY.SST.NRT.x_sst.nc"
[20] "./TERRA_MODIS.20191204.L3m.DAY.SST.x_sst.nc"
# just showing order of x_sst.nc 
files[endsWith(files, suffix = 'L3m.DAY.SST.x_sst.nc')]
[1] "./TERRA_MODIS.20191205.L3m.DAY.SST.x_sst.nc"
[2] "./TERRA_MODIS.20191201.L3m.DAY.SST.x_sst.nc"
[3] "./TERRA_MODIS.20191202.L3m.DAY.SST.x_sst.nc"
[4] "./TERRA_MODIS.20191203.L3m.DAY.SST.x_sst.nc"
[5] "./TERRA_MODIS.20191204.L3m.DAY.SST.x_sst.nc"
bouys <- read.csv('~/Downloads/example.csv', header = TRUE, colClasses=c('integer', 'Date', 'numeric', 'numeric'))
bouys
   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
bouys_v <- vect(bouys, geom = c('Longitude', 'Latitude'))
SSTs <- rast(files[endsWith(files, suffix = 'L3m.DAY.SST.x_sst.nc')], 'sst')
sst_extr <- extract(SSTs, bouys_v)
sst_extr
   ID sst    sst sst    sst sst
1   1  NA     NA  NA     NA  NA
2   2  NA     NA  NA     NA  NA
3   3  NA     NA  NA     NA  NA
4   4  NA     NA  NA     NA  NA
5   5  NA     NA  NA     NA  NA
6   6  NA     NA  NA -0.320  NA
7   7  NA     NA  NA -0.380  NA
8   8  NA     NA  NA -0.455  NA
9   9  NA     NA  NA -0.380  NA
10 10  NA     NA  NA -0.130  NA
11 11  NA     NA  NA     NA  NA
12 12  NA     NA  NA     NA  NA
13 13  NA     NA  NA     NA  NA
14 14  NA     NA  NA 11.790  NA
15 15  NA 11.835  NA     NA  NA
sst_idx <- which(is.na(sst_extr) == FALSE, arr.ind = TRUE)
sst_extr[sst_idx]
 [1]  1.000  2.000  3.000  4.000  5.000  6.000  7.000  8.000  9.000 10.000
[11] 11.000 12.000 13.000 14.000 15.000 11.835 -0.320 -0.380 -0.455 -0.380
[21] -0.130 11.790

bouys$sst <- NA
bouys$sst[sst_idx[16:22,][,1]] <- sst_extr[sst_idx][16:22]
bouys
   Buoy       Date Longitude Latitude    sst
1     1 2019-12-01  50.29614 43.92681     NA
2     1 2019-12-02  50.23525 43.89244     NA
3     1 2019-12-03  50.19717 43.88295     NA
4     1 2019-12-04  50.20559 43.88417     NA
5     1 2019-12-05  50.26016 43.86125     NA
6     2 2019-12-01  51.73309 46.53087 -0.320
7     2 2019-12-02  51.79530 46.56380 -0.380
8     2 2019-12-03  51.79190 46.53550 -0.455
9     2 2019-12-04  51.79958 46.56178 -0.380
10    2 2019-12-05  51.85411 46.33031 -0.130
11    3 2019-12-01  51.54246 41.28999     NA
12    3 2019-12-02  50.76324 41.60532     NA
13    3 2019-12-03  51.39782 41.79459     NA
14    3 2019-12-04  49.52380 42.33821 11.790
15    3 2019-12-05  49.48472 42.62323 11.835

Though there must be a better way to get to the final result. which with arr.ind gives (in this case) row/col, [sst_idx[16:22,][,1]] says what rows we want to update, sst_extr[sst_idx][16:22] with what values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.