Skip to content

Commit c758f2d

Browse files
authored
NetCDF4 and CWatM support (#27)
Implements proper NetCDF support for MET Nordic downloads and includes the capability to convert them to CWatM compatible files. Also: - reduced import dependencies, moved most non-critical to "Suggests" - removed `ncdf` and `continue` parameter specific code from `get_metno_reanalysis3()` - moved SWAT+ specific functions to own .R file - moved `get_metno_reanalysis3()` support functions to bottom of .R file - updated documentation - updated vignette for MET Nordic usage.
1 parent 5ca5f5c commit c758f2d

32 files changed

+2637
-1677
lines changed

DESCRIPTION

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Package
22
Package: miljotools
33
Title: An R package hosting a variety of useful functions for
44
environmental data handling and anaylsis
5-
Version: 0.5.0
5+
Version: 0.6.0
66
Author: Moritz Shore
77
Maintainer: Moritz Shore <[email protected]>
88
Description: A few useful functions for environemental data processing and
@@ -16,32 +16,34 @@ Imports:
1616
abind,
1717
crayon,
1818
dplyr,
19-
fs,
20-
ggplot2,
21-
ggrepel,
22-
ggtern,
2319
lubridate,
2420
mapview,
2521
ncdf4,
26-
plotly,
2722
purrr,
28-
RColorBrewer,
2923
readr,
30-
readxl,
3124
sf,
3225
stringr,
26+
terra,
3327
tibble,
34-
utils,
35-
vroom,
36-
writexl
28+
vroom
3729
Suggests:
3830
devtools,
31+
doParallel,
3932
euptf2,
33+
fs,
34+
ggplot2,
35+
ggrepel,
36+
ggtern,
4037
knitr,
38+
lattice,
39+
plotly,
40+
RColorBrewer,
4141
Rdpack,
42+
readxl,
4243
rmarkdown,
4344
roxygen2,
44-
SWATprepR
45+
SWATprepR,
46+
writexl
4547
VignetteBuilder:
4648
knitr
4749
Remotes:

NAMESPACE

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(classify_soil)
4-
export(convert_to_cwatm)
5-
export(cwatm_hourly_to_daily_ncdf4)
4+
export(cwatm_convert_nc)
65
export(get_metno_reanalysis3)
6+
export(metnordic_aggregate)
7+
export(metnordic_buildquery)
8+
export(metnordic_coordwindow)
9+
export(metnordic_download)
10+
export(metnordic_merge)
11+
export(metnordic_reproject)
12+
export(mt_print)
713
export(reanalysis3_daily)
814
export(reanalysis3_swatinput)
915
export(swat_weather_input_chain)
1016
export(thermopluviogram)
11-
importFrom(RColorBrewer,brewer.pal)
17+
import(ncdf4)
1218
importFrom(abind,abind)
1319
importFrom(crayon,bgBlue)
1420
importFrom(crayon,bgCyan)
@@ -21,15 +27,14 @@ importFrom(crayon,bold)
2127
importFrom(crayon,cyan)
2228
importFrom(crayon,green)
2329
importFrom(crayon,italic)
24-
importFrom(crayon,magenta)
25-
importFrom(crayon,red)
2630
importFrom(crayon,underline)
2731
importFrom(crayon,white)
2832
importFrom(crayon,yellow)
2933
importFrom(dplyr,"%>%")
3034
importFrom(dplyr,across)
3135
importFrom(dplyr,all_of)
3236
importFrom(dplyr,filter)
37+
importFrom(dplyr,first)
3338
importFrom(dplyr,group_by)
3439
importFrom(dplyr,if_else)
3540
importFrom(dplyr,last)
@@ -39,11 +44,6 @@ importFrom(dplyr,rename)
3944
importFrom(dplyr,select)
4045
importFrom(dplyr,summarise)
4146
importFrom(dplyr,tibble)
42-
importFrom(fs,dir_ls)
43-
importFrom(ggplot2,aes)
44-
importFrom(ggrepel,geom_label_repel)
45-
importFrom(ggtern,ggtern)
46-
importFrom(grDevices,colorRampPalette)
4747
importFrom(lubridate,as_datetime)
4848
importFrom(lubridate,date)
4949
importFrom(lubridate,day)
@@ -52,21 +52,12 @@ importFrom(lubridate,month)
5252
importFrom(lubridate,year)
5353
importFrom(mapview,mapview)
5454
importFrom(ncdf4,nc_close)
55-
importFrom(ncdf4,nc_create)
5655
importFrom(ncdf4,nc_open)
5756
importFrom(ncdf4,ncatt_get)
58-
importFrom(ncdf4,ncdim_def)
59-
importFrom(ncdf4,ncvar_add)
60-
importFrom(ncdf4,ncvar_def)
6157
importFrom(ncdf4,ncvar_get)
62-
importFrom(ncdf4,ncvar_put)
63-
importFrom(plotly,layout)
64-
importFrom(plotly,plot_ly)
6558
importFrom(purrr,map)
66-
importFrom(purrr,map2)
6759
importFrom(readr,read_csv)
6860
importFrom(readr,write_csv)
69-
importFrom(readxl,read_excel)
7061
importFrom(sf,read_sf)
7162
importFrom(sf,st_as_sf)
7263
importFrom(sf,st_bbox)
@@ -76,15 +67,12 @@ importFrom(sf,st_crs)
7667
importFrom(sf,st_intersects)
7768
importFrom(sf,st_transform)
7869
importFrom(sf,st_zm)
79-
importFrom(stats,reorder)
8070
importFrom(stringr,str_pad)
8171
importFrom(stringr,str_remove)
82-
importFrom(stringr,str_remove_all)
72+
importFrom(stringr,str_replace)
8373
importFrom(stringr,str_replace_all)
8474
importFrom(stringr,str_split)
85-
importFrom(tibble,tibble)
86-
importFrom(utils,packageVersion)
87-
importFrom(utils,read.table)
88-
importFrom(utils,write.csv)
89-
importFrom(vroom,vroom)
90-
importFrom(writexl,write_xlsx)
75+
importFrom(terra,crs)
76+
importFrom(terra,project)
77+
importFrom(terra,rast)
78+
importFrom(terra,writeCDF)

R/cwatm_convert_nc.R

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#' Convert MET Nordic Files to CWatM Meteo Input
2+
#'
3+
#' This function converts a projected MET Nordic file (from
4+
#' `metnordic_reproject()`) to be compatible with CWatm. This function mainly
5+
#' just changes the dimension names from easting and northing to x and y and
6+
#' swapes variable order but has a crap load of boiler plate because... well..
7+
#' ncdf4.
8+
#'
9+
#' Code largely adapted from this handy guide:
10+
#' ([link](https://pjbartlein.github.io/REarthSysSci/netCDF.html#create-and-write-a-projected-netcdf-file))
11+
#' from Pat Bartlein, [email protected]
12+
#'
13+
#' @seealso [metnordic_reproject()]
14+
#'
15+
#' @author Moritz Shore
16+
#'
17+
#' @param infile String: path to projected MET Nordic .nc file as created by `metnordic_reproject()`
18+
#' @param outfile String: path and file name to desired output file
19+
#'
20+
#' @returns Path to outfile
21+
#' @export
22+
#'
23+
#' @examples
24+
#' #TODO
25+
#' @import ncdf4
26+
cwatm_convert_nc <- function(infile, outfile){
27+
### Re-reading it and changing attributes to x and y (This is only needed for CWATM.. TODO: seperate function?)
28+
to_reform <- nc_open(infile)
29+
main_var <- to_reform$var %>% names() %>% nth(1)
30+
31+
## getting data
32+
33+
met_var <- ncvar_get(to_reform, main_var)
34+
met_var_att <- ncatt_get(to_reform, main_var)
35+
easting <- ncvar_get(to_reform,"easting")
36+
easting_att <- ncatt_get(to_reform, "easting")
37+
northing <- ncvar_get(to_reform,"northing")
38+
northing_att <- ncatt_get(to_reform, "northing")
39+
time <- ncvar_get(to_reform, "time")
40+
time_att <- ncatt_get(to_reform, "time")
41+
crs <- ncvar_get(to_reform, "crs")
42+
crs_att <- ncatt_get(to_reform, "crs")
43+
44+
## Dim Definitions
45+
xdef <- ncdf4::ncdim_def(name = "x", units = "meters", vals = easting, longname = "x", )
46+
ydef <- ncdf4::ncdim_def(name = "y", units = "meters", vals = northing, longname = "y")
47+
timedef <- ncdf4::ncdim_def(name = "time", units = time_att$units, vals = time, calendar = time_att$calendar, longname = time_att$long_name)
48+
49+
## Var Definitions
50+
fillvalue <- 1e32
51+
main_var_def <- ncvar_def(name = main_var, units = met_var_att$units, dim = list(xdef,ydef,timedef), missval = met_var_att$`_FillValue`, prec = "float")
52+
crs_def <- ncvar_def(name = "crs", units = "", dim = list(), prec = "integer")
53+
54+
# Creating the new ncfile
55+
proj_nc <- nc_create(filename = outfile, vars = list(crs_def, main_var_def), force_v4 = T) # the order of this might be important
56+
57+
# Adding data
58+
ncvar_put(nc = proj_nc, varid = main_var, vals = met_var)
59+
60+
# Adding Attributes
61+
ncatt_put(proj_nc, varid = main_var, attname = "grid_mapping", "crs")
62+
63+
ncatt_put(proj_nc, varid = "crs", attname = "crs_wkt", crs_att$crs_wkt)
64+
ncatt_put(proj_nc, varid = "crs", attname = "spatial_ref", crs_att$spatial_ref)
65+
ncatt_put(proj_nc, varid = "crs", attname = "proj4", crs_att$proj4)
66+
ncatt_put(proj_nc, varid = "crs", attname = "geotransform", crs_att$geotransform)
67+
68+
ncatt_put(nc = proj_nc, varid = 0 ,attname = "Conventions", attval = ncatt_get(to_reform, 0, "Conventions")[[2]])
69+
ncatt_put(nc = proj_nc, varid = 0 ,attname = "created_by", attval = ncatt_get(to_reform, 0, "created_by")[[2]])
70+
ncatt_put(nc = proj_nc, varid = 0 ,attname = "date", attval = ncatt_get(to_reform, 0, "date")[[2]])
71+
ncatt_put(nc = proj_nc, varid = 0 ,attname = "institution", attval = ncatt_get(to_reform, 0, "institution")[[2]])
72+
ncatt_put(nc = proj_nc, varid = 0 ,attname = "history", attval = ncatt_get(to_reform, 0, "history")[[2]])
73+
74+
# Saving
75+
nc_close(to_reform)
76+
nc_close(proj_nc)
77+
78+
return(outfile)
79+
}

R/globals.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# bandaid
1+
# bandaid (metnorenal3.R "metnorenal3.R --> rename(max_temp = air_temperature_2m)"
22
utils::globalVariables(c("daily", "air_temperature_2m"))

0 commit comments

Comments
 (0)