@@ -9963,7 +9963,6 @@ postprocess_entire_dataset <- function(site_data,
99639963 prepare_for_edi(where = edi_dir,
99649964 dataset_version = dataset_version)
99659965
9966- remove_more_neon_stuff_temporarily()
99679966 manually_edit_eml()
99689967
99699968 log_with_indent(glue('Uploading dataset v{vv} to EDI',
@@ -9984,27 +9983,46 @@ remove_more_neon_stuff_temporarily <- function(){
99849983
99859984manually_edit_eml <- function(){
99869985
9987- if(.Platform$OS.type == 'windows') stop('this will not work on windows')
9986+ if(.Platform$OS.type == 'windows') stop('this will not work on windows (but can be adapted quickly) ')
99889987
99899988 att <- read_tsv('eml/eml_templates/attributes_ws_attr_summaries.txt')
99909989
99919990 most_recent_eml <- system('ls -t eml/eml_out | head -n 1', intern = TRUE)
99929991 eml <- read_lines(file.path('eml/eml_out', most_recent_eml))
99939992
9993+ new_eml_chunk <- c('\t<methods>', '\t\t<methodStep>', '\t\t\t<description>',
9994+ NA, '\t\t\t</description>', '\t\t</methodStep>', '\t</methods>')
9995+
99949996 mvclines <- grep('</missingValueCode>', eml)
99959997 attlines <- grep('</attribute>', eml)
9998+ atnlines <- grep('<attributeName>', eml)
99969999
999710000 for(i in rev(seq_along(mvclines))){
999810001
999910002 mvcl <- mvclines[i]
1000010003
1000110004 if((mvcl + 1) %in% attlines){
10002- #HERE: figure out which variable this is, look up its details in att.
10003- #if no details, next. otherwise, insert a methods-methodStep-description block
1000410005
1000510006 attl <- mvcl + 1
10006- eml <- c(eml[1:mvcl], neweml, eml[attl:length(eml)])
10007+ attdif <- atnlines - attl
10008+ attdif <- attdif[attdif < 0]
10009+ atnl <- atnlines[which.max(attdif)]
10010+ varn <- str_match(eml[atnl], '\\<attributeName\\>([^\\<]+)\\<\\/attributeName\\>$')[, 2]
10011+
10012+ if(length(varn) != 1 || is.na(varn)) stop('problem with varn')
10013+
10014+ if(! varn %in% att$attributeName) next
10015+
10016+ attdeets <- pull(att[att$attributeName == varn, 'details'])
10017+
10018+ if(is.na(attdeets)) next
1000710019
10020+ new_eml_chunk[4] <- attdeets
10021+ eml <- c(eml[1:mvcl], new_eml_chunk, eml[attl:length(eml)])
10022+ }
10023+ }
10024+
10025+ write_lines(eml, file.path('eml/eml_out', most_recent_eml))
1000810026}
1000910027
1001010028make_figshare_docs_skeleton <- function(where){
@@ -10340,7 +10358,7 @@ prepare_for_figshare <- function(where, dataset_version){
1034010358 #prepare documentation and metadata
1034110359 make_figshare_docs_skeleton(where = where)
1034210360 prepare_site_metadata_for_figshare(outfile = file.path(where, 'macrosheds_documentation/04_site_documentation/04a_site_metadata.csv'))
10343- prepare_variable_metadata_for_figshare (outfile = file.path(where, '/macrosheds_documentation/variable_metadata.csv'),
10361+ prepare_variable_meta`data_for_figshare (outfile = file.path(where, '/macrosheds_documentation/variable_metadata.csv'),
1034410362 fs_format = 'new')
1034510363 assemble_misc_docs_figshare(where = where)
1034610364
@@ -10415,7 +10433,7 @@ combine_ts_csvs <- function(where){
1041510433
1041610434 domain_combined %>%
1041710435 arrange(site_code, var_category, var) %>%
10418- write_csv(file.path(network_dir, paste0(d, '.csv')))
10436+ write_csv(file.path(network_dir, paste0('timeseries_', d, '.csv')))
1041910437 }
1042010438}
1042110439
@@ -10502,11 +10520,75 @@ prepare_for_edi <- function(where, dataset_version){
1050210520 logger = logger_module)
1050310521 combine_daymet_csvs(file.path(where, '4_CAMELS-compliant_Daymet_forcings'))
1050410522
10523+ log_with_indent('Combining ws attrs (separately for ms and camels-compliant)',
10524+ indent = 2,
10525+ logger = logger_module)
10526+ combine_ws_attrs(where)
10527+
1050510528 log_with_indent('Combining spatial objects by domain',
1050610529 indent = 2,
1050710530 logger = logger_module)
1050810531 combine_and_move_spatial_objects(from = file.path(where, '2_timeseries_data'),
1050910532 to = file.path(where, '5_shapefiles'))
10533+
10534+ #TEMPORARY
10535+ remove_more_neon_stuff_temporarily()
10536+
10537+ eml_misc(where)
10538+ }
10539+
10540+ combine_ws_attrs <- function(){
10541+
10542+ #ms-standard watershed attributes
10543+ ws_attrs <- list.files(glue('{where}/1_watershed_attribute_data/ws_attr_timeseries'),
10544+ full.names = TRUE)
10545+
10546+ map_dfr(ws_attrs, read_csv) %>%
10547+ write_csv(glue('{where}/1_watershed_attribute_data/ws_attr_timeseries.csv'))
10548+
10549+ file.remove(ws_attrs)
10550+ file.remove(glue('{where}/1_watershed_attribute_data/ws_attr_timeseries'))
10551+
10552+ #camels-compliant watershed attributes
10553+ ws_attrs <- list.files(glue('{where}/3_CAMELS-compliant_watershed_attributes'),
10554+ full.names = TRUE)
10555+
10556+ d <- read_csv(ws_attrs[1])
10557+ for(i in 2:length(ws_attrs)){
10558+ d <- full_join(d, read_csv(ws_attrs[i]), by = 'site_code')
10559+ }
10560+
10561+ write_csv(d, glue('{where}/3_CAMELS-compliant_watershed_attributes/CAMELS_compliant_ws_attr.csv'))
10562+
10563+ file.remove(ws_attrs)
10564+ }
10565+
10566+ eml_misc <- function(where){
10567+
10568+ ## rename some files to clarify what they are in the absence of dir structure
10569+
10570+ # fs <- list.files(glue('{where}/1_watershed_attribute_data/ws_attr_timeseries'),
10571+ # full.names = TRUE)
10572+ # file.rename(fs, sub('ws_attr_timeseries/', 'ws_attr_timeseries/ws_attr_ts_', fs))
10573+ #
10574+ # fs <- list.files(glue('{where}/3_CAMELS-compliant_watershed_attributes'),
10575+ # full.names = TRUE)
10576+ # file.rename(fs, sub('watershed_attributes/', 'watershed_attributes/CAMELS-compliant_ws_attr_ts_', fs))
10577+
10578+ file.rename(glue('{where}/4_CAMELS-compliant_Daymet_forcings/CAMELS-compliant_Daymet_forcings.csv'),
10579+ glue('{where}/4_CAMELS-compliant_Daymet_forcings/CAMELS_compliant_Daymet_forcings.csv'))
10580+
10581+ ## link shapefiles to eml loading dock and zip them together
10582+
10583+ dir.create('eml/data_links/shapefiles', showWarnings = FALSE)
10584+
10585+ sfs <- list.files(glue('{where}/5_shapefiles'), full.names = TRUE)
10586+ sfs_basenames <- basename(sfs)
10587+ file.link(sfs, file.path('eml/data_links/shapefiles', sfs_basenames))
10588+
10589+ zip(zipfile = 'eml/data_links/shapefiles.zip',
10590+ files = 'eml/data_links/shapefiles',
10591+ flags = '-r9Xq')
1051010592}
1051110593
1051210594prepare_for_figshare_packageformat <- function(where, dataset_version){
0 commit comments