Created
June 11, 2024 22:43
-
-
Save aolinto/3828be642d06857788500d51f114eb49 to your computer and use it in GitHub Desktop.
script to process Copernicus Multi Observation Global Ocean 3D Temperature Salinity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ======================================================================================================================= | |
# Antonio Olinto Avila-da-Silva, Instituto de Pesca, Brasil | |
# version 2024/06/04 | |
# https://gist.github.com/aolinto/3828be642d06857788500d51f114eb49 | |
# script to process Multi Observation Global Ocean 3D Temperature Salinity Height Geostrophic Current and MLD | |
# product MULTIOBS_GLO_PHY_TSUV_3D_MYNRT_015_012 | |
# https://doi.org/10.48670/moi-00052 | |
# nc file downloaded from | |
# https://data.marine.copernicus.eu/product/MULTIOBS_GLO_PHY_TSUV_3D_MYNRT_015_012/services | |
# dataset-armor-3d-rep-monthly | |
# using the phyton script copernicus_mod_glo_phy.py | |
# https://gist.github.com/aolinto/d14d72711a648a34ab5b31a8c26d6360 | |
# the script will open nc file, organize data in a data matrix and write it into a single csv file | |
# ======================================================================================================================= | |
# load libraries | |
# -------------- | |
# install.packages(c("ncdf4")) | |
# ncdf4 needs libnetcdf-dev netcdf-bin in Linux | |
library(ncdf4) | |
# set workspace | |
# -------------- | |
# list and remove objects from workspace | |
ls() | |
rm(list = ls()) | |
options("width"=226) # 100,226 | |
# set working directory | |
setwd("...") # indicate the path to the nc file | |
# import nc file | |
# -------------- | |
list.files(pattern = "^dataset-armor.*\\.nc$") | |
nc.file <- "dataset-armor-3d-rep-monthly_mlotst-so-to_....nc" | |
nc.data <- nc_open(nc.file) | |
nc.data | |
# check ncfile information | |
# ------------------------ | |
# list of the nc variable names | |
var.names <- attributes(nc.data$var)$names | |
var.names | |
# variables info | |
ncatt_get(nc.data,var.names[1]) | |
ncatt_get(nc.data,var.names[2]) | |
ncatt_get(nc.data,var.names[3]) | |
# list of the nc dimensions names | |
attributes(nc.data$dim) | |
nc.data$dim$longitude$len | |
nc.data$dim$latitude$len | |
nc.data$dim$depth$len | |
nc.data$dim$time$len | |
# dimensions info | |
ncatt_get(nc.data,"longitude") | |
ncvar_get(nc.data,"longitude") | |
ncatt_get(nc.data,"latitude") | |
ncvar_get(nc.data,"latitude") | |
ncatt_get(nc.data,"depth") | |
ncvar_get(nc.data,"depth") | |
ncatt_get(nc.data,"time") | |
ncvar_get(nc.data,"time") | |
as.Date(ncvar_get(nc.data,"time")/24, origin="1950-01-01") | |
# dimentions levels (depth, latitude, longitude, time) | |
longitude <- ncvar_get(nc.data,"longitude") | |
latitude <- ncvar_get(nc.data,"latitude") | |
depth <- ncvar_get(nc.data,"depth") | |
time <- ncvar_get(nc.data,"time") | |
date <- as.Date(time/24, origin="1950-01-01") | |
# dataframe per variable | |
#. mixed layer depth | |
arr.mlotst <- ncvar_get(nc.data,"mlotst",raw_datavals=F) | |
dim(arr.mlotst) | |
dimnames(arr.mlotst) <- list(lon = longitude, lat = latitude, time = time) | |
indices.mlotst <- expand.grid(lon = longitude, lat = latitude, time = time) | |
val.mlotst <- c(arr.mlotst) | |
length(val.mlotst) | |
dm.mlotst <- cbind(indices.mlotst, value = val.mlotst) | |
dim(dm.mlotst) | |
dm.mlotst$depth <- NA_integer_ | |
dm.mlotst$cod_variable <- 1 # mlotst | |
dm.mlotst$cod_source <- 1 | |
dm.mlotst$date <- as.Date(dm.mlotst$time/24, origin="1950-01-01") | |
dm.mlotst_final <- dm.mlotst[,c("cod_source","date","lon","lat","depth","cod_variable","value")] | |
dm.mlotst_final <- dm.mlotst_final[order(dm.mlotst_final$date,dm.mlotst_final$lon,dm.mlotst_final$lat),] | |
summary(dm.mlotst_final) | |
dm.mlotst_final <- dm.mlotst_final[!is.na(dm.mlotst_final$value ),] | |
head(dm.mlotst_final) | |
tail(dm.mlotst_final) | |
#. salinity | |
arr.so <- ncvar_get(nc.data,"so",raw_datavals=F) | |
dim(arr.so) | |
dimnames(arr.so) <- list(lon = longitude, lat = latitude, depth = depth, time = time) | |
indices.so <- expand.grid(lon = longitude, lat = latitude, depth = depth, time = time) | |
val.so <- c(arr.so) | |
length(val.so) | |
dm.so <- cbind(indices.so, value = val.so) | |
dim(dm.so) | |
dm.so$cod_variable <- 2 # so | |
dm.so$cod_source <- 1 | |
dm.so$date <- as.Date(dm.so$time/24, origin="1950-01-01") | |
dm.so_final <- dm.so[,c("cod_source","date","lon","lat","depth","cod_variable","value")] | |
dm.so_final <- dm.so_final[order(dm.so_final$date,dm.so_final$lon,dm.so_final$lat),] | |
summary(dm.so_final) | |
dm.so_final <- dm.so_final[!is.na(dm.so_final$value ),] | |
head(dm.so_final) | |
tail(dm.so_final) | |
#. temperature | |
arr.to <- ncvar_get(nc.data,"to",raw_datavals=F) | |
dim(arr.to) | |
dimnames(arr.to) <- list(lon = longitude, lat = latitude, depth = depth, time = time) | |
indices.to <- expand.grid(lon = longitude, lat = latitude, depth = depth, time = time) | |
val.to <- c(arr.to) | |
length(val.to) | |
dm.to <- cbind(indices.to, value = val.to) | |
dim(dm.to) | |
dm.to$cod_variable <- 3 # to | |
dm.to$cod_source <- 1 | |
dm.to$date <- as.Date(dm.to$time/24, origin="1950-01-01") | |
dm.to_final <- dm.to[,c("cod_source","date","lon","lat","depth","cod_variable","value")] | |
dm.to_final <- dm.to_final[order(dm.to_final$date,dm.to_final$lon,dm.to_final$lat),] | |
summary(dm.to_final) | |
dm.to_final <- dm.to_final[!is.na(dm.to_final$value ),] | |
head(dm.to_final) | |
tail(dm.to_final) | |
# merge data | |
dm.armor_3d_rep_monthly <- rbind(dm.mlotst_final,dm.so_final,dm.to_final) | |
summary(dm.armor_3d_rep_monthly) | |
dim(dm.armor_3d_rep_monthly) | |
# save csv | |
write.csv(dm.armor_3d_rep_monthly,file=paste0(substr(nc.file,1,nchar(nc.file)-3),".csv"),row.names = FALSE) | |
# close | |
nc_close(nc.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment