library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(fuzzyjoin)
library(FluxDataKit)

# install.packages('sapfluxnetr')
library(sapfluxnetr)
# get sapfluxnet data
dir <- "~/data_2/sapfluxnet/"

# # Data is publicly available on Zenodo to download.
# download.file(
#   url = "https://zenodo.org/record/3971689/files/0.1.5.zip?download=1",
#   destfile = "~/data_2/sapfluxnet/0.1.5.zip"
# )
#
# # unzip the data
# # BE SURE YOU HAVE AT LEAST 24GB OF DISK SPACE
# unzip(paste0(dir, "0.1.5.zip"))
#
# # check if files are present
# list.files(file.path(paste0(dir, "0.1.5"), 'RData', 'plant'))
# list.files(file.path(paste0(dir, "0.1.5"), 'csv', 'plant'))
# collect meta data from all sites
sfn_metadata <- read_sfn_metadata(
  folder = paste0(dir, "0.1.5/RData/plant/"),
  .write_cache = TRUE
  )
## [1] "processing site ARG_MAZ (1 of 194)"
## [1] "processing site ARG_TRE (2 of 194)"
## [1] "processing site AUS_BRI_BRI (3 of 194)"
## [1] "processing site AUS_CAN_ST1_EUC (4 of 194)"
## [1] "processing site AUS_CAN_ST2_MIX (5 of 194)"
## [1] "processing site AUS_CAN_ST3_ACA (6 of 194)"
## [1] "processing site AUS_CAR_THI_00F (7 of 194)"
## [1] "processing site AUS_CAR_THI_0P0 (8 of 194)"
## [1] "processing site AUS_CAR_THI_0PF (9 of 194)"
## [1] "processing site AUS_CAR_THI_CON (10 of 194)"
## [1] "processing site AUS_CAR_THI_T00 (11 of 194)"
## [1] "processing site AUS_CAR_THI_T0F (12 of 194)"
## [1] "processing site AUS_CAR_THI_TP0 (13 of 194)"
## [1] "processing site AUS_CAR_THI_TPF (14 of 194)"
## [1] "processing site AUS_ELL_HB_HIG (15 of 194)"
## [1] "processing site AUS_ELL_MB_MOD (16 of 194)"
## [1] "processing site AUS_ELL_UNB (17 of 194)"
## [1] "processing site AUS_KAR (18 of 194)"
## [1] "processing site AUS_MAR_HSD_HIG (19 of 194)"
## [1] "processing site AUS_MAR_HSW_HIG (20 of 194)"
## [1] "processing site AUS_MAR_MSD_MOD (21 of 194)"
## [1] "processing site AUS_MAR_MSW_MOD (22 of 194)"
## [1] "processing site AUS_MAR_UBD (23 of 194)"
## [1] "processing site AUS_MAR_UBW (24 of 194)"
## [1] "processing site AUS_RIC_EUC_ELE (25 of 194)"
## [1] "processing site AUS_WOM (26 of 194)"
## [1] "processing site AUT_PAT_FOR (27 of 194)"
## [1] "processing site AUT_PAT_KRU (28 of 194)"
## [1] "processing site AUT_PAT_TRE (29 of 194)"
## [1] "processing site AUT_TSC (30 of 194)"
## [1] "processing site BRA_CAM (31 of 194)"
## [1] "processing site BRA_CAX_CON (32 of 194)"
## [1] "processing site BRA_SAN (33 of 194)"
## [1] "processing site CAN_TUR_P39_POS (34 of 194)"
## [1] "processing site CAN_TUR_P39_PRE (35 of 194)"
## [1] "processing site CAN_TUR_P74 (36 of 194)"
## [1] "processing site CHE_DAV_SEE (37 of 194)"
## [1] "processing site CHE_LOT_NOR (38 of 194)"
## [1] "processing site CHE_PFY_CON (39 of 194)"
## [1] "processing site CHE_PFY_IRR (40 of 194)"
## [1] "processing site CHN_ARG_GWD (41 of 194)"
## [1] "processing site CHN_ARG_GWS (42 of 194)"
## [1] "processing site CHN_HOR_AFF (43 of 194)"
## [1] "processing site CHN_YIN_ST1 (44 of 194)"
## [1] "processing site CHN_YIN_ST2_DRO (45 of 194)"
## [1] "processing site CHN_YIN_ST3_DRO (46 of 194)"
## [1] "processing site CHN_YUN_YUN (47 of 194)"
## [1] "processing site COL_MAC_SAF_RAD (48 of 194)"
## [1] "processing site CRI_TAM_TOW (49 of 194)"
## [1] "processing site CZE_BIK (50 of 194)"
## [1] "processing site CZE_BIL_BIL (51 of 194)"
## [1] "processing site CZE_KRT_KRT (52 of 194)"
## [1] "processing site CZE_LAN (53 of 194)"
## [1] "processing site CZE_LIZ_LES (54 of 194)"
## [1] "processing site CZE_RAJ_RAJ (55 of 194)"
## [1] "processing site CZE_SOB_SOB (56 of 194)"
## [1] "processing site CZE_STI (57 of 194)"
## [1] "processing site CZE_UTE_BEE (58 of 194)"
## [1] "processing site CZE_UTE_BNA (59 of 194)"
## [1] "processing site CZE_UTE_BPO (60 of 194)"
## [1] "processing site CZE_UTE_SPR (61 of 194)"
## [1] "processing site DEU_HIN_OAK (62 of 194)"
## [1] "processing site DEU_HIN_TER (63 of 194)"
## [1] "processing site DEU_MER_BEE_NON (64 of 194)"
## [1] "processing site DEU_MER_BEE_THI (65 of 194)"
## [1] "processing site DEU_MER_DOU_NON (66 of 194)"
## [1] "processing site DEU_MER_DOU_THI (67 of 194)"
## [1] "processing site DEU_MER_MIX_NON (68 of 194)"
## [1] "processing site DEU_MER_MIX_THI (69 of 194)"
## [1] "processing site DEU_STE_2P3 (70 of 194)"
## [1] "processing site DEU_STE_4P5 (71 of 194)"
## [1] "processing site ESP_ALT_ARM (72 of 194)"
## [1] "processing site ESP_ALT_HUE (73 of 194)"
## [1] "processing site ESP_ALT_TRI (74 of 194)"
## [1] "processing site ESP_CAN (75 of 194)"
## [1] "processing site ESP_GUA_VAL (76 of 194)"
## [1] "processing site ESP_LAH_COM (77 of 194)"
## [1] "processing site ESP_LAS (78 of 194)"
## [1] "processing site ESP_MAJ_MAI (79 of 194)"
## [1] "processing site ESP_MAJ_NOR_LM1 (80 of 194)"
## [1] "processing site ESP_MON_SIE_NAT (81 of 194)"
## [1] "processing site ESP_RIN (82 of 194)"
## [1] "processing site ESP_RON_PIL (83 of 194)"
## [1] "processing site ESP_SAN_A_45I (84 of 194)"
## [1] "processing site ESP_SAN_A2_45I (85 of 194)"
## [1] "processing site ESP_SAN_B_100 (86 of 194)"
## [1] "processing site ESP_SAN_B2_100 (87 of 194)"
## [1] "processing site ESP_TIL_MIX (88 of 194)"
## [1] "processing site ESP_TIL_OAK (89 of 194)"
## [1] "processing site ESP_TIL_PIN (90 of 194)"
## [1] "processing site ESP_VAL_BAR (91 of 194)"
## [1] "processing site ESP_VAL_SOR (92 of 194)"
## [1] "processing site ESP_YUN_C1 (93 of 194)"
## [1] "processing site ESP_YUN_C2 (94 of 194)"
## [1] "processing site ESP_YUN_T1_THI (95 of 194)"
## [1] "processing site ESP_YUN_T3_THI (96 of 194)"
## [1] "processing site FIN_HYY_SME (97 of 194)"
## [1] "processing site FIN_PET (98 of 194)"
## [1] "processing site FRA_FON (99 of 194)"
## [1] "processing site FRA_HES_HE1_NON (100 of 194)"
## [1] "processing site FRA_HES_HE2_NON (101 of 194)"
## [1] "processing site FRA_PUE (102 of 194)"
## [1] "processing site GBR_ABE_PLO (103 of 194)"
## [1] "processing site GBR_DEV_CON (104 of 194)"
## [1] "processing site GBR_DEV_DRO (105 of 194)"
## [1] "processing site GBR_GUI_ST1 (106 of 194)"
## [1] "processing site GBR_GUI_ST2 (107 of 194)"
## [1] "processing site GBR_GUI_ST3 (108 of 194)"
## [1] "processing site GUF_GUY_GUY (109 of 194)"
## [1] "processing site GUF_GUY_ST2 (110 of 194)"
## [1] "processing site IDN_JAM_OIL (111 of 194)"
## [1] "processing site IDN_JAM_RUB (112 of 194)"
## [1] "processing site IDN_PON_STE (113 of 194)"
## [1] "processing site ISR_YAT_YAT (114 of 194)"
## [1] "processing site ITA_FEI_S17 (115 of 194)"
## [1] "processing site ITA_KAE_S20 (116 of 194)"
## [1] "processing site ITA_MAT_S21 (117 of 194)"
## [1] "processing site ITA_MUN (118 of 194)"
## [1] "processing site ITA_REN (119 of 194)"
## [1] "processing site ITA_RUN_N20 (120 of 194)"
## [1] "processing site ITA_TOR (121 of 194)"
## [1] "processing site JPN_EBE_HYB (122 of 194)"
## [1] "processing site JPN_EBE_SUG (123 of 194)"
## [1] "processing site MDG_SEM_TAL (124 of 194)"
## [1] "processing site MDG_YOU_SHO (125 of 194)"
## [1] "processing site MEX_COR_YP (126 of 194)"
## [1] "processing site MEX_VER_BSJ (127 of 194)"
## [1] "processing site MEX_VER_BSM (128 of 194)"
## [1] "processing site NLD_LOO (129 of 194)"
## [1] "processing site NLD_SPE_DOU (130 of 194)"
## [1] "processing site NZL_HUA_HUA (131 of 194)"
## [1] "processing site PRT_LEZ_ARN (132 of 194)"
## [1] "processing site PRT_MIT (133 of 194)"
## [1] "processing site PRT_PIN (134 of 194)"
## [1] "processing site RUS_CHE_LOW (135 of 194)"
## [1] "processing site RUS_CHE_Y4 (136 of 194)"
## [1] "processing site RUS_FYO (137 of 194)"
## [1] "processing site RUS_POG_VAR (138 of 194)"
## [1] "processing site SEN_SOU_IRR (139 of 194)"
## [1] "processing site SEN_SOU_POS (140 of 194)"
## [1] "processing site SEN_SOU_PRE (141 of 194)"
## [1] "processing site SWE_NOR_ST1_AF1 (142 of 194)"
## [1] "processing site SWE_NOR_ST1_AF2 (143 of 194)"
## [1] "processing site SWE_NOR_ST1_BEF (144 of 194)"
## [1] "processing site SWE_NOR_ST2 (145 of 194)"
## [1] "processing site SWE_NOR_ST3 (146 of 194)"
## [1] "processing site SWE_NOR_ST4_AFT (147 of 194)"
## [1] "processing site SWE_NOR_ST4_BEF (148 of 194)"
## [1] "processing site SWE_NOR_ST5_REF (149 of 194)"
## [1] "processing site SWE_SKO_MIN (150 of 194)"
## [1] "processing site SWE_SKY_38Y (151 of 194)"
## [1] "processing site SWE_SKY_68Y (152 of 194)"
## [1] "processing site SWE_SVA_MIX_NON (153 of 194)"
## [1] "processing site THA_KHU (154 of 194)"
## [1] "processing site USA_CHE_ASP (155 of 194)"
## [1] "processing site USA_CHE_MAP (156 of 194)"
## [1] "processing site USA_DUK_HAR (157 of 194)"
## [1] "processing site USA_HIL_HF1_POS (158 of 194)"
## [1] "processing site USA_HIL_HF1_PRE (159 of 194)"
## [1] "processing site USA_HIL_HF2 (160 of 194)"
## [1] "processing site USA_HUY_LIN_NON (161 of 194)"
## [1] "processing site USA_INM (162 of 194)"
## [1] "processing site USA_MOR_SF (163 of 194)"
## [1] "processing site USA_NWH (164 of 194)"
## [1] "processing site USA_ORN_ST1_AMB (165 of 194)"
## [1] "processing site USA_ORN_ST2_AMB (166 of 194)"
## [1] "processing site USA_ORN_ST3_ELE (167 of 194)"
## [1] "processing site USA_ORN_ST4_ELE (168 of 194)"
## [1] "processing site USA_PAR_FER (169 of 194)"
## [1] "processing site USA_PJS_P04_AMB (170 of 194)"
## [1] "processing site USA_PJS_P08_AMB (171 of 194)"
## [1] "processing site USA_PJS_P12_AMB (172 of 194)"
## [1] "processing site USA_SIL_OAK_1PR (173 of 194)"
## [1] "processing site USA_SIL_OAK_2PR (174 of 194)"
## [1] "processing site USA_SIL_OAK_POS (175 of 194)"
## [1] "processing site USA_SMI_SCB (176 of 194)"
## [1] "processing site USA_SMI_SER (177 of 194)"
## [1] "processing site USA_SWH (178 of 194)"
## [1] "processing site USA_SYL_HL2 (179 of 194)"
## [1] "processing site USA_TNB (180 of 194)"
## [1] "processing site USA_TNO (181 of 194)"
## [1] "processing site USA_TNP (182 of 194)"
## [1] "processing site USA_TNY (183 of 194)"
## [1] "processing site USA_UMB_CON (184 of 194)"
## [1] "processing site USA_UMB_GIR (185 of 194)"
## [1] "processing site USA_WIL_WC1 (186 of 194)"
## [1] "processing site USA_WIL_WC2 (187 of 194)"
## [1] "processing site USA_WVF (188 of 194)"
## [1] "processing site UZB_YAN_DIS (189 of 194)"
## [1] "processing site ZAF_FRA_FRA (190 of 194)"
## [1] "processing site ZAF_NOO_E3_IRR (191 of 194)"
## [1] "processing site ZAF_RAD (192 of 194)"
## [1] "processing site ZAF_SOU_SOU (193 of 194)"
## [1] "processing site ZAF_WEL_SOR (194 of 194)"
# for scaleup table: temperate and boreal forest sites
sites_sfn <- sfn_metadata$site_md |>
  dplyr::filter(si_biome %in% c("Temperate forest", "Boreal forest"), si_flux_network) |>
  dplyr::select(si_code, si_long, si_lat, si_igbp, si_biome)

# join with FLUXNET site info table, identifying site matches by whether
# longitude and latitude values are within 0.1 degrees.
sites_sfn <- fuzzyjoin::fuzzy_inner_join(
  sites_sfn,
  FluxDataKit::fdk_site_info,
  by = c("si_long" = "lon", "si_lat" = "lat"),
  match_fun = list(
    function(x, y) abs(x - y) <= 0.1,  # Condition for value1a and value2a
    function(x, y) abs(x - y) <= 0.1  # Condition for value1b and value2b
    )
  )

# Complement with species information
sites_sfn <- sites_sfn |>
  dplyr::left_join(
    sfn_metadata$species_md |>
      dplyr::group_by(si_code) |>
      dplyr::summarise(
        species = paste(sp_name, collapse = ", "),
        .groups = "drop"  # Ensures the result is not grouped
      )
    ) |>
  dplyr::select(id_fluxnet = sitename, id_sapfluxnet = si_code, si_biome, si_igbp, species)
## Joining with `by = join_by(si_code)`
knitr::kable(sites_sfn)
id_fluxnet id_sapfluxnet si_biome si_igbp species
CA-TP3 CAN_TUR_P39_POS Temperate forest ENF Pinus strobus
CA-TP3 CAN_TUR_P39_PRE Temperate forest ENF Pinus strobus
CA-TP3 CAN_TUR_P74 Temperate forest ENF Pinus strobus
CH-Dav CHE_DAV_SEE Temperate forest ENF Picea abies
CZ-BK1 CZE_BIK Temperate forest ENF Picea abies
FI-Hyy FIN_HYY_SME Temperate forest ENF Pinus sylvestris, Betula pendula
FR-Hes FRA_HES_HE1_NON Temperate forest DNF Fagus sylvatica
FR-Hes FRA_HES_HE2_NON Temperate forest DNF Fagus sylvatica
UK-Gri GBR_ABE_PLO Temperate forest ENF Picea sitchensis
IT-Ren ITA_REN Temperate forest ENF Picea abies, Pinus cembra, Larix decidua
IT-Tor ITA_TOR Temperate forest DNF Larix decidua
RU-Fy2 RUS_FYO Temperate forest ENF Picea abies, Betula sp., Pinus sylvestris
RU-Fyo RUS_FYO Temperate forest ENF Picea abies, Betula sp., Pinus sylvestris
US-PFa USA_CHE_ASP Temperate forest MF Abies balsamea, Betula papyrifera, Acer rubrum, Acer saccharum, Populus tremuloides, Thuja occidentalis
US-PFa USA_CHE_MAP Temperate forest DBF Acer saccharum, Pinus resinosa
US-MMS USA_INM Temperate forest DBF Acer saccharum, Fraxinus americana, Liriodendron tulipifera, Quercus alba, Sassafras albidum, Ulmus americana
US-MMS USA_MOR_SF Temperate forest DBF Acer saccharum, Liriodendron tulipifera, Quercus rubra, Quercus alba
US-NC1 USA_PAR_FER Temperate forest ENF Pinus taeda
US-Syv USA_SYL_HL2 Temperate forest MF Betula alleghaniensis, Acer saccharum, Tsuga canadensis, Ostrya virginiana
US-UM3 USA_UMB_CON Temperate forest DBF Acer rubrum, Betula papyrifera, Populus grandidentata, Pinus strobus, Quercus rubra
US-UMB USA_UMB_CON Temperate forest DBF Acer rubrum, Betula papyrifera, Populus grandidentata, Pinus strobus, Quercus rubra
US-UMd USA_UMB_CON Temperate forest DBF Acer rubrum, Betula papyrifera, Populus grandidentata, Pinus strobus, Quercus rubra
US-UM3 USA_UMB_GIR Temperate forest DBF Acer rubrum, Pinus strobus, Quercus rubra, Fagus grandifolia
US-UMB USA_UMB_GIR Temperate forest DBF Acer rubrum, Pinus strobus, Quercus rubra, Fagus grandifolia
US-UMd USA_UMB_GIR Temperate forest DBF Acer rubrum, Pinus strobus, Quercus rubra, Fagus grandifolia
US-WCr USA_WIL_WC1 Temperate forest DBF Fraxinus pennsylvanica, Acer saccharum, Tilia americana, Ostrya virginiana, Quercus rubra
US-WCr USA_WIL_WC2 Temperate forest DBF Fraxinus pennsylvanica, Acer saccharum, Tilia americana, Ostrya virginiana