From 944edbecf9762c37fbe9e2162c3a2b136199861c Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 11 Mar 2020 13:32:11 +0100 Subject: [PATCH 1/4] New FAQ for loading single gridpoint data --- inst/doc/usecase.md | 4 ++ inst/doc/usecase/ex1_5_gridpoint_data.R | 79 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 inst/doc/usecase/ex1_5_gridpoint_data.R diff --git a/inst/doc/usecase.md b/inst/doc/usecase.md index fcaa782..43bc3ec 100644 --- a/inst/doc/usecase.md +++ b/inst/doc/usecase.md @@ -22,6 +22,10 @@ In this document, you can link to the example scripts for various demands. For t 4. [Checking impact of start date order in the number of members](inst/doc/usecase/ex1_4_variable_nmember.R) Mixing start dates of different months can lead to load different number of members, check the code provided and the [FAQ 10](/inst/doc/faq.md). + 5. [Loading gridpoint data](inst/doc/usecase/ex1_5_gridpoint_data.R) + + **Start** can be used to load single point data by providing a vector of longitudes and latitudes. This use case also ilustrates how to reformat it to get a 'gridpoint' dimension. + 2. **Execute computation (use `Compute()`)** 1. [Function working on time dimension](inst/doc/usecase/ex2_1_timedim.R) diff --git a/inst/doc/usecase/ex1_5_gridpoint_data.R b/inst/doc/usecase/ex1_5_gridpoint_data.R new file mode 100644 index 0000000..2cdb6e6 --- /dev/null +++ b/inst/doc/usecase/ex1_5_gridpoint_data.R @@ -0,0 +1,79 @@ +# LOADING GRID POINT DATA +# This code shows how to load grid point data +# given lat and lon values Start will load the nearest grid point +# and how to create a grid point dimension. +# See also FAQ 11 [How to load grid point data](/inst/doc/faq.md) + +library(startR) + +lats <- c(51.30760, 51.97100, 52.10000, 54.01486, 55.00690, 55.19500, 51.94355, + 53.51917, 46.95580, 53.12750, 50.92847, 52.16630, 53.40360, 55.35277, + 55.11070, 62.90566, 67.36186) +lons <- c(4.519800, 4.926000, -0.416700, 6.587639, 13.154200, 7.158000, 1.922200, + 10.102861, 16.652200, 0.436100, 6.219629, 14.122600, 6.352800, -7.332018, + 36.597700, 27.653497, 26.637833) +data <- Start(dat = '/esarchive/exp/ecmwf/system5c3s/monthly_mean/$var$_f6h/$var$_$sdate$.nc', + var = 'sfcWind', + sdate = '19931101', + ensemble = indices(1), + time = 'all', + latitude = values(lats), + longitude = values(lons), + pattern_dims = 'dat', + synonims = list(latitude = c('lat', 'latitude'), + longitude = c('lon', 'longitude')), + return_vars = list(time = NULL, + latitude = 'dat', + longitude = 'dat'), + retrieve = TRUE) + +# The warning is reporting: +#5: ! Warning: Numeric selectors have been provided for a dimension defined along a +#! numeric variable, but no exact match found for all the selectors. +#! Taking the index of the nearest values. + +# The attributes shows the values loaded, +# that has lower length since there are repeated values: +lon <- as.vector(attributes(data)$Variables$dat1$lon) +#[1] 5 0 7 13 2 10 17 6 14 37 28 27 +lat <- as.vector(attributes(data)$Variables$dat1$lat) +#[1] 51 52 54 55 47 53 63 67 + +# while the data loaded has the same length of the requested values: +dim(data) +# dat var sdate ensemble time latitude longitude +# 1 1 1 1 8 17 17 + + +# The real retrieved values can be calculated: +lats_retrieved <- unlist(lapply(lats, function(x) {lat[which(abs(lat - x) == min(abs(lat - x)))]})) +#[1] 51 52 52 54 55 55 52 54 47 53 51 52 53 55 55 63 67 +lons_retrieved <- unlist(lapply(lons, function(x) {lon[which(abs(lon - x) == min(abs(lon - x)))]})) +# [1] 5 5 0 7 13 7 2 10 17 0 6 14 6 0 37 28 27 + +# The desired information is the time series of the grid points in the list: +gridpoint <- lapply(1:length(lats), function(x) {c(lats_retrieved[x], lons_retrieved[x])}) + +# The latitude and longitude dimension in data can be merged to the corresponding gridpoint dimension: +data_gridpoint <- unlist(lapply(1:17, function(x) {data[,,,,,x,x]})) +dim(data_gridpoint) <- c(dim(data)[1:5], gridpoint = 17) + +plot(1:8, data_gridpoint[1,1,1,1,,4], main = paste("lat", gridpoint[[4]][1], ", lon" , gridpoint[[4]][2]), + bty = "n", type = "l", ylab = 'sfcWind (m/s)', + xlab = 'forecast time (month)') +lines(1:8, data[1,1,1,1,,4,4], type = 'p', col = 'green') + +# Comparison against independet loading tool: +library(ncdf4) +f <- nc_open('/esarchive/exp/ecmwf/system5c3s/monthly_mean/sfcWind_f6h/sfcWind_19931101.nc') +ncvar_get(f, varid = 'latitude') +ncvar_get(f, varid = 'longitude') +dat <- ncvar_get(f, varid = 'sfcWind', start = c(8, 37, 1, 1), count = c(1, 1, 1, 8)) +nc_close(f) +lines(1:8, dat, col = 'blue', lty = 3, lwd = 2) + +all(dat == data_gridpoint[1,1,1,1,,4]) + +dat +#[1] 8.080409 9.172594 11.310596 11.145370 9.234114 6.885310 5.471250 +#[8] 5.467621 -- GitLab From d4b38ca7185e56b827516a37e8b706936dd275ca Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 11 Mar 2020 14:57:18 +0100 Subject: [PATCH 2/4] FAQ for loading gird point data --- inst/doc/faq.md | 9 +++++++++ inst/doc/usecase.md | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/inst/doc/faq.md b/inst/doc/faq.md index 572bff8..d9f652f 100644 --- a/inst/doc/faq.md +++ b/inst/doc/faq.md @@ -15,6 +15,8 @@ This document intends to be the first reference for any doubts that you may have 9. [Use CDORemap() in function](#9-use-cdoremap-in-function) 10. [The number of members depends on the start date](#10-the-number-of-members-depends-on-the-start-date) + 11. [Load specific grid points data](#11-load-specific-grid-points-data) + 2. **Something goes wrong...** 1. [No space left on device](#1-no-space-left-on-device) @@ -401,6 +403,13 @@ When trying to load both start dates at once using Start(), the order in which t The code to reproduce this behaviour could be found in the Use Cases section, [example 1.4](/inst/doc/usecase/ex1_4_variable_nmember.R). +### 11. Load specific grid points data + +A single or list of grid points, defined by pairs of latitude and logitud values, can be loaded using **Start**. If the values does not match the defined spatial point in the files, **Start** will load the nearest gridpoint. + +An example of how to load several gridpoints and how to transform the data could be found in the Use Cases section [example 1.5](/inst/doc/usecase/ex1_5_gridpoint_data.R). + + ## Something goes wrong... diff --git a/inst/doc/usecase.md b/inst/doc/usecase.md index 43bc3ec..d43e336 100644 --- a/inst/doc/usecase.md +++ b/inst/doc/usecase.md @@ -23,7 +23,6 @@ In this document, you can link to the example scripts for various demands. For t Mixing start dates of different months can lead to load different number of members, check the code provided and the [FAQ 10](/inst/doc/faq.md). 5. [Loading gridpoint data](inst/doc/usecase/ex1_5_gridpoint_data.R) - **Start** can be used to load single point data by providing a vector of longitudes and latitudes. This use case also ilustrates how to reformat it to get a 'gridpoint' dimension. -- GitLab From fb1d74f19f84dfe3e72d5508e013b2080a56cf8a Mon Sep 17 00:00:00 2001 From: nperez Date: Wed, 11 Mar 2020 15:01:36 +0100 Subject: [PATCH 3/4] the user can consider the regrid option --- inst/doc/faq.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/inst/doc/faq.md b/inst/doc/faq.md index d9f652f..5554394 100644 --- a/inst/doc/faq.md +++ b/inst/doc/faq.md @@ -14,8 +14,8 @@ This document intends to be the first reference for any doubts that you may have 8. [Define a path with multiple dependencies](#8-define-a-path-with-multiple-dependencies) 9. [Use CDORemap() in function](#9-use-cdoremap-in-function) 10. [The number of members depends on the start date](#10-the-number-of-members-depends-on-the-start-date) - 11. [Load specific grid points data](#11-load-specific-grid-points-data) + 2. **Something goes wrong...** @@ -405,7 +405,8 @@ The code to reproduce this behaviour could be found in the Use Cases section, [e ### 11. Load specific grid points data -A single or list of grid points, defined by pairs of latitude and logitud values, can be loaded using **Start**. If the values does not match the defined spatial point in the files, **Start** will load the nearest gridpoint. +A single or list of grid points, defined by pairs of latitude and logitud values, can be loaded using **Start**. +If the values does not match the defined spatial point in the files, **Start** will load the nearest gridpoint (The user can also consider the regriding option to fulfill his/her expectations). An example of how to load several gridpoints and how to transform the data could be found in the Use Cases section [example 1.5](/inst/doc/usecase/ex1_5_gridpoint_data.R). -- GitLab From f2b010a83118c7c06fad71d56709088a757a461d Mon Sep 17 00:00:00 2001 From: aho Date: Tue, 24 Mar 2020 14:17:21 +0100 Subject: [PATCH 4/4] fix hyperlink for how-to-13 --- inst/doc/faq.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/doc/faq.md b/inst/doc/faq.md index cbbf72a..e7e6bac 100644 --- a/inst/doc/faq.md +++ b/inst/doc/faq.md @@ -16,7 +16,7 @@ This document intends to be the first reference for any doubts that you may have 10. [The number of members depends on the start date](#10-the-number-of-members-depends-on-the-start-date) 11. [Select the longitude/latitude region](#11-select-the-longitudelatitude-region) 12. [What will happen if reorder function is not used](#12-what-will-happen-if-reorder-function-is-not-used) - 13. [Load specific grid points data](#11-load-specific-grid-points-data) + 13. [Load specific grid points data](#13-load-specific-grid-points-data) -- GitLab