Skip to content

Commit

Permalink
#18 modified aggregation section in R: added an example of mean over …
Browse files Browse the repository at this point in the history
…all time steps
  • Loading branch information
shospital committed May 12, 2024
1 parent 3b801f1 commit 817f24c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
.RData
.Ruserdata
*.Rproj

**/.DS_Store
/.quarto/
/_site/
/_freeze/
/python-notebooks/demonstrated data/
.DS_Store
.DS_Store
*.nc
27 changes: 23 additions & 4 deletions tutorials/r/3-extract-satellite-data-within-boundary.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ We will construct a data cube to compute monthly average for sea surface tempera
To minimize data loading times, the first 10 results, which correspond to approximately two months
of data, will be used for this exercise.

Select the SST results for end of January and beginning of February.
Select the first 10 SST results (end of January and beginning of February).
```{r}
ras_all <- terra::rast(results[c(25:35)], vsi = TRUE)
```
Expand All @@ -166,23 +166,42 @@ Select SST data.
rc_sst <- rc_all["analysed_sst", ]
```

Calculate monthly SST means.
Calculate mean SST over the entire time series and map it
```{r get_means}
# Compute mean over 12 time layers
raster_mean <- terra::mean(rc_sst, na.rm=TRUE)
# Map mean SST
plot(raster_mean)
# Map only the GFST area
plot(terra::crop(raster_mean, GFST))
```


Calculate monthly mean SST means across raster (lat, lon).
```{r get_means}
# Function to convert times to year-month format
year_month <- function(x) {
format(as.Date(time(x), format="%Y-%m-%d"), "%Y-%m")
}
# Convert time to Year-month format for aggregation
# Format time to Year-month for monthly aggregation
ym <- year_month(rc_sst)
# Compute raster mean grouped by Year-month
monthly_mean_rast <- terra::tapp(rc_sst, ym, fun = mean)
# Compute mean across raster grouped by Year-month
monthly_means <- global(monthly_mean_rast, fun = mean, na.rm=TRUE)
monthly_mean <- global(monthly_mean_rast, fun = mean, na.rm=TRUE)
```


## Convert raster into data frame

```{r ras_to_df}
Expand Down

0 comments on commit 817f24c

Please sign in to comment.