From cde06001ac09048ff28df783eb90a0cb59baf67a Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 5 May 2021 09:47:58 +0200 Subject: [PATCH 1/2] Correct the results. The data were wrong after the UTC time zone fix. --- inst/doc/usecase/ex1_7_split_merge.R | 53 +++++++++++++++++----------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/inst/doc/usecase/ex1_7_split_merge.R b/inst/doc/usecase/ex1_7_split_merge.R index 539a24d..d041e4e 100644 --- a/inst/doc/usecase/ex1_7_split_merge.R +++ b/inst/doc/usecase/ex1_7_split_merge.R @@ -49,9 +49,9 @@ dim(dates) #----------------------------------------------------------------------- # If you need to reorder the dimensions of the 'time' selector, you can use -# s2dv::Reorder function. These two lines are not used in the following example. -library(s2dv) -dates <- Reorder(dates, c('syear', 'sdate', 'time')) +# s2dv::Reorder function. This line is not used in the following example. +# +# dates <- s2dv::Reorder(dates, c('syear', 'sdate', 'time')) #----------------------------------------------------------------------- #----------------------------------------------------------------------- @@ -84,30 +84,43 @@ obs <- Start(dat = path.obs, time = 'file_date'), retrieve = T) -# check obs data +#----------- Check data ---------------- +attr(hcst, 'Dimensions') +# dat var sdate syear time latitude longitude ensemble +# 1 1 2 3 12 10 10 11 dim(obs) # dat var latitude longitude sdate syear time # 1 1 10 10 2 3 12 -obs[1, 1, 1, 1, 2, 1:2, ] -# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] -#[1,] 4.402223 2.657466 7.296539 10.263944 6.367464 5.433421 3.021327 7.498292 -#[2,] 6.802123 7.110264 7.584915 4.255134 2.047740 3.619044 5.648496 8.322672 -# [,9] [,10] [,11] [,12] -#[1,] 15.321060 1.131008 6.326981 5.301850 -#[2,] 7.942419 7.594263 6.189313 7.627579 - -# check with ncdf4 + +# The dimension structure is the same as experimental one. + +#------------ Check if the data are placed correctly----------------- +# For example, [sdate = 2, syear = 1, time = 1:12] +dates[2, 1, ] +# [1] "1996-12-22 UTC" "1996-12-23 UTC" "1996-12-24 UTC" "1996-12-25 UTC" +# [5] "1996-12-26 UTC" "1996-12-27 UTC" "1996-12-28 UTC" "1996-12-29 UTC" +# [9] "1996-12-30 UTC" "1996-12-31 UTC" "1997-01-01 UTC" "1997-01-02 UTC" + +# obs at [sdate = 2, syear = 1, time = 1:12] should have data for the corresponding times. +# Use ncdf4 package to read the netCDF files and compare. + +obs[1, 1, 2, 3, 2, 1, ] +# [1] 4.565837 3.254213 5.710205 10.255745 5.809094 5.477635 3.184075 +# [8] 7.230077 14.662762 2.256792 6.470966 5.574388 + library(ncdf4) file199612 <- nc_open('/esarchive/recon/ecmwf/era5/1hourly/sfcWind/sfcWind_199612.nc') wind199612 <- ncvar_get(file199612, 'sfcWind') file199701 <- nc_open('/esarchive/recon/ecmwf/era5/1hourly/sfcWind/sfcWind_199701.nc') wind199701 <- ncvar_get(file199701, 'sfcWind') -data_wanted_199612 <- seq(506, 722, 24) -wind199612[1, 1, data_wanted_199612] -# [1] 4.402223 2.657466 7.296539 10.263944 6.367464 5.433421 3.021327 -# [8] 7.498292 15.321060 1.131008 -data_wanted_199701 <- seq(2, 26, 24) -wind199701[1, 1, data_wanted_199701] -#[1] 6.326981 5.301850 +# The file has 1hr frequency and the dimensions are [lon, lat, time] +# 505 is 1996-12-22 0; 721 is 1996-12-31 0; etc. +wind199612[3, 2, seq(505, 721, by = 24)] +# [1] 4.565837 3.254213 5.710205 10.255745 5.809094 5.477635 3.184075 +# [8] 7.230077 14.662762 2.256792 + +wind199701[3, 2, c(1, 25)] +#[1] 6.470966 5.574388 +# The data of retrieved obs and netCDF are identical. -- GitLab From ec1d8bf3406f41e677406c5ac48b1b9fc1eb08a1 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 5 May 2021 10:54:09 +0200 Subject: [PATCH 2/2] Add easyNCDF check --- inst/doc/usecase/ex1_3_attr_loadin.R | 110 ++++++++++++++++++--------- 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/inst/doc/usecase/ex1_3_attr_loadin.R b/inst/doc/usecase/ex1_3_attr_loadin.R index d514c30..1968159 100644 --- a/inst/doc/usecase/ex1_3_attr_loadin.R +++ b/inst/doc/usecase/ex1_3_attr_loadin.R @@ -25,17 +25,12 @@ longitude = NULL, time = c('sdate'))) -#-------- Check exp data ----------- - attr(system4, 'Dimensions') -# dat var sdate time ensemble latitude longitude -# 1 1 8 31 51 10 10 -#----------------------------------- -# ------- retrieve the attributes for obs load-in ---------- - dates <- attr(system4, 'Variables')$common$time -# dim(dates) -#sdate time -# 8 31 +# retrieve the attributes for obs load-in +dates <- attr(system4, 'Variables')$common$time +dim(dates) +# sdate time +# 8 31 # NOTE: Even though June, September, and November only have 30 days, it reads # 31 days for each month. Therefore, the last day of these months is the @@ -46,6 +41,7 @@ substr, 1, 7)))) # dates_file #[1] "199405" "199406" "199407" "199408" "199409" "199410" "199411" "199412" + # ----------------------------------------------------------- # observational data @@ -79,36 +75,23 @@ # If the NAs are not removed, unwanted NAs will exist and make the # values misplaced in the array. See 'bonus' below for more explanation. -#------- Check erai ----------- + +#-------------------------------------------------------- +# Check data +#-------------------------------------------------------- + +# (0) + attr(system4, 'Dimensions') +# dat var sdate time ensemble latitude longitude +# 1 1 8 31 51 10 10 dim(erai) # dat var sdate time latitude longitude # 1 1 8 31 10 10 -erai[1, 1, 1, , 1, 1] -# [1] 255.0120 256.8095 254.3654 254.6059 257.0551 255.5087 256.8167 257.9717 -# [9] 258.7491 259.2942 259.6682 260.7215 260.0988 261.2605 263.3590 265.6683 -#[17] 262.4813 262.6136 263.0591 262.8377 261.7276 263.9910 264.7755 266.0213 -#[25] 268.5927 267.8699 268.9210 269.4702 267.6735 267.9255 268.2216 - -erai[1, 1, , 1, 2, 2] -#[1] 254.5793 269.6221 274.5021 274.0269 269.5855 253.7458 243.9750 244.2415 +# --> The experimental and observational data are comparable with same structure. -# NOTE: You will see that the observation array is the same as experiment one -# that the last day in 30-day months are the first day of the following -# month. -erai[1, 1, 3, 1, 1, 1] # 1st March -#[1] 274.6019 -erai[1, 1, 2, 31, 1, 1] # 1st March also, since June only has 30 days -#[1] 274.6019 -#------------------------------ - -# The experimental and observational data are comparable with same structure. - -#---------Check time attributes-------- -dim(attr(erai, 'Variables')$common$time) -#file_date time -# 8 31 -attr(erai, 'Variables')$common$time[1, ] +# (1) +dates[1, ] # [1] "1994-05-01 UTC" "1994-05-02 UTC" "1994-05-03 UTC" "1994-05-04 UTC" # [5] "1994-05-05 UTC" "1994-05-06 UTC" "1994-05-07 UTC" "1994-05-08 UTC" # [9] "1994-05-09 UTC" "1994-05-10 UTC" "1994-05-11 UTC" "1994-05-12 UTC" @@ -117,7 +100,29 @@ attr(erai, 'Variables')$common$time[1, ] #[21] "1994-05-21 UTC" "1994-05-22 UTC" "1994-05-23 UTC" "1994-05-24 UTC" #[25] "1994-05-25 UTC" "1994-05-26 UTC" "1994-05-27 UTC" "1994-05-28 UTC" #[29] "1994-05-29 UTC" "1994-05-30 UTC" "1994-05-31 UTC" -attr(erai, 'Variables')$common$time[2, ] + +# The following values should belong to the above times. +erai[1, 1, 1, , 1, 1] +# [1] 255.0120 256.8095 254.3654 254.6059 257.0551 255.5087 256.8167 257.9717 +# [9] 258.7491 259.2942 259.6682 260.7215 260.0988 261.2605 263.3590 265.6683 +#[17] 262.4813 262.6136 263.0591 262.8377 261.7276 263.9910 264.7755 266.0213 +#[25] 268.5927 267.8699 268.9210 269.4702 267.6735 267.9255 268.2216 + +# Use easyNCDF to read netCDF files and compare to erai. +file199405 <- NcOpen('/esarchive/recon/ecmwf/erainterim/6hourly/tas/tas_199405.nc') +obs199405 <- NcToArray(file199405, vars_to_read = 'tas', + dim_indices = list(longitude = 1, latitude = 1, time = seq(1, 4*31, 4))) +NcClose(file199405) +obs199405[1, 1, 1, ] +# [1] 255.0120 256.8095 254.3654 254.6059 257.0551 255.5087 256.8167 257.9717 +# [9] 258.7491 259.2942 259.6682 260.7215 260.0988 261.2605 263.3590 265.6683 +#[17] 262.4813 262.6136 263.0591 262.8377 261.7276 263.9910 264.7755 266.0213 +#[25] 268.5927 267.8699 268.9210 269.4702 267.6735 267.9255 268.2216 + +# --> CORRECT. + +# (2) +dates[2, ] # [1] "1994-06-01 UTC" "1994-06-02 UTC" "1994-06-03 UTC" "1994-06-04 UTC" # [5] "1994-06-05 UTC" "1994-06-06 UTC" "1994-06-07 UTC" "1994-06-08 UTC" # [9] "1994-06-09 UTC" "1994-06-10 UTC" "1994-06-11 UTC" "1994-06-12 UTC" @@ -125,11 +130,40 @@ attr(erai, 'Variables')$common$time[2, ] #[17] "1994-06-17 UTC" "1994-06-18 UTC" "1994-06-19 UTC" "1994-06-20 UTC" #[21] "1994-06-21 UTC" "1994-06-22 UTC" "1994-06-23 UTC" "1994-06-24 UTC" #[25] "1994-06-25 UTC" "1994-06-26 UTC" "1994-06-27 UTC" "1994-06-28 UTC" -#[29] "1994-06-29 UTC" "1994-06-30 UTC" NA +#[29] "1994-06-29 UTC" "1994-06-30 UTC" "1994-07-01 UTC" +# The following values should belong to the above times. +erai[1, 1, 2, , 1, 1] +# [1] 269.9410 269.6855 268.7380 268.5008 270.3236 271.5151 270.5046 270.1686 +# [9] 270.5395 272.0379 272.5489 271.1494 270.7764 270.5678 272.0331 273.7856 +#[17] 273.9849 274.5904 273.4369 273.8404 274.4068 274.2292 274.7375 275.5104 +#[25] 275.4324 274.9408 274.8679 276.5602 275.0995 274.6409 274.6019 + +# Use easyNCDF to read netCDF files and compare to erai. +file199406 <- NcOpen('/esarchive/recon/ecmwf/erainterim/6hourly/tas/tas_199406.nc') +obs199406 <- NcToArray(file199406, vars_to_read = 'tas', + dim_indices = list(longitude = 1, latitude = 1, time = seq(1, 4*31, 4))) +NcClose(file199405) +obs199406[1, 1, 1, ] +# [1] 269.9410 269.6855 268.7380 268.5008 270.3236 271.5151 270.5046 270.1686 +# [9] 270.5395 272.0379 272.5489 271.1494 270.7764 270.5678 272.0331 273.7856 +#[17] 273.9849 274.5904 273.4369 273.8404 274.4068 274.2292 274.7375 275.5104 +#[25] 275.4324 274.9408 274.8679 276.5602 275.0995 274.6409 + +# --> CORRECT. + +# (3) +# NOTE that the observation array is the same as the experiment one that the +# last day in those 30-day months is the first day of the following month. +erai[1, 1, 3, 1, 1, 1] # 1st July +#[1] 274.6019 +erai[1, 1, 2, 31, 1, 1] # same as 1st July, since June only has 30 days +#[1] 274.6019 -# //////////////////"BONUS"////////////////////// +#-------------------------------------------------------- +# BONUS +#-------------------------------------------------------- # Here is something more to show the usage of parameter 'merge_across_dims_narm'. # If the last day of 30-day months is NA instead of the first day of the following month, # NAs are needed to exist in the array. In this case, 'merge_across_dims_narm' -- GitLab