-
Notifications
You must be signed in to change notification settings - Fork 3
/
set_param_txt.R
873 lines (793 loc) · 26.2 KB
/
set_param_txt.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#' Set (replace) STICS input file parameters
#'
#' @description Replace or set an input parameter from a pre-existing
#' STICS input file.
#'
#' @param workspace Path of the workspace containing the STICS (txt)
#' input files.
#' @param param Vector of parameter names.
#' @param value New parameter value
#' @param append Boolean. Append input to existing file
#' @param plant_id The plant identifier (main crop: 1 ; associated crop: 2).
#' Only used for plant, technical or initialisation parameters (default = 1).
#' @param variety The plant variety to set the parameter value,
#' either the variety name (`codevar` in the plant file) or
#' the index (`variete` in the technical file).
#' @param value_id The soil layers id or technical interventions id
#' @param stics_version An optional version name as listed in
#' get_stics_versions_compat() return
#' @param dirpath `r lifecycle::badge("deprecated")` `dirpath` is no
#' longer supported, use `workspace` instead.
#' @param add `r lifecycle::badge("deprecated")` `add` is no
#' longer supported, use `append` instead.
#' @param plant `r lifecycle::badge("deprecated")` `plant` is no
#' longer supported, use `plant_id` instead.
#' @param layer `r lifecycle::badge("deprecated")` `layer` is no
#' longer supported, use `value_id` instead.
#'
#' @param file Path (including name) of the file to modify
#' @param filepath `r lifecycle::badge("deprecated")` `filepath` is no
#' longer supported, use `file` instead.
#'
#'
#' @details The \code{plant} parameter can be either equal to \code{1},
#' \code{2} for the associated plant in the case of intercrop, or
#' \code{c(1,2)} for both Principal and associated plants.
#' \code{\link{get_var_info}} is a helper function that returns
#' all possible output variables.
#' If the \code{variety} is not given and if \code{param}
#' is a varietal parameter, the function will modify the value of
#' \code{param} for the simulated variety, as given
#' in the technical file.
#'
#' @note \code{gen_varmod} is not used by \code{set_param_txt}.
#' To replace the output variables required from STICS,
#' please directly call \code{gen_varmod}.
#'
#' @return None
#'
#' @export
#'
#' @examples
#' # Getting example data path
#' path <- get_examples_path(file_type = "txt")
#'
#' # Change the value of durvieF for the current variety:
#' set_param_txt(workspace = path, param = "durvieF", value = 245)
#'
#' # Change the value of durvieF for another variety:
#' set_param_txt(workspace = path, param = "durvieF",
#' variety = "Nefer", value = 178)
#' # Change the value of soil parameter "cailloux" for all layers
#' # or a specific one
#' set_param_txt(workspace = path, param = "cailloux", value = 1)
#' set_param_txt(workspace = path, param = "cailloux", value_id = 2, value = 2)
#'
#' # Change the value of parameter "amount" for all water supply interventions
#' # or a specific one
#' set_param_txt(workspace = path, param = "amount", value = 50)
#' set_param_txt(workspace = path, param = "amount", value_id = 2, value = 40)
#'
#'
#'
set_param_txt <- function(workspace,
param,
value,
append = FALSE,
plant_id = 1,
variety = NULL,
value_id = NULL,
stics_version = "latest",
dirpath = lifecycle::deprecated(),
add = lifecycle::deprecated(),
plant = lifecycle::deprecated(),
layer = lifecycle::deprecated()) {
# dirpath
if (lifecycle::is_present(dirpath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_param_txt(dirpath)",
"set_param_txt(workspace)"
)
} else {
dirpath <- workspace # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_param_txt(add)",
"set_param_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
# plant
if (lifecycle::is_present(plant)) {
lifecycle::deprecate_warn(
"1.0.0", "set_param_txt(plant)",
"set_param_txt(plant_id)"
)
} else {
plant <- plant_id # to remove when we update inside the function
}
# layer
if (lifecycle::is_present(layer)) {
lifecycle::deprecate_warn(
"1.4.0", "set_param_txt(plant)",
"set_param_txt(plant_id)"
)
value_id <- layer
}
stics_version <- check_version_compat(stics_version = stics_version)
param <- gsub("P_", "", param)
param_val <- get_param_txt(
workspace = dirpath,
param = param,
exact = TRUE,
stics_version = stics_version
)
if (length(param_val) == 0)
stop("Unknown parameter: ", param, "\n",
"Check case sensitivity or ",
"use get_param_info for searching the exact name")
file_param_list <- lapply(
strsplit(names(param_val), "\\$"), function(x) {
x[1] }
)
file_type <- file_param_list %>%
unlist() %>%
unique()
if (length(file_type) > 1) {
stop(
"Parameter found in several files:", paste(file_type, collapse = ", "),
"\nPlease use the set_* functions directly to set the parameter value."
)
}
switch(file_type,
ini = {
set_ini_txt(
file = file.path(dirpath, "ficini.txt"),
param = param, value = value, append = add,
plant_id = plant, layer = value_id,
stics_version = stics_version
)
},
general = {
set_general_txt(
file = file.path(dirpath, "tempopar.sti"),
param = param, value = value, append = add
)
},
tmp = {
set_tmp_txt(
file = file.path(dirpath, "tempoparV6.sti"),
param = param, value = value, append = add
)
},
soil = {
set_soil_txt(
file = file.path(dirpath, "param.sol"),
param = param,
value = value,
layer = value_id,
stics_version = stics_version
)
},
usm = {
set_usm_txt(
file = file.path(dirpath, "new_travail.usm"),
param = param, value = value
)
},
station = {
set_station_txt(
file = file.path(dirpath, "station.txt"),
param = param, value = value, append = add
)
},
tec = {
lapply(plant, function(x) {
set_tec_txt(
file = file.path(dirpath, paste0("fictec", x, ".txt")),
param = param, value = value, append = add,
value_id = value_id
)
})
},
plant = {
lapply(plant, function(x) {
if (is.null(variety)) {
variety <-
unlist(get_param_txt(workspace = dirpath,
param = "variete",
exact = TRUE,
stics_version = stics_version))[plant]
} else {
if (is.character(variety)) {
varieties <-
get_plant_txt(file = file.path(dirpath,
paste0("ficplt",
x,
".txt")))$codevar
variety <- match(variety, varieties)
if (is.na(variety)) {
cli::cli_alert_danger(
paste0("Variety not found in plant",
"file. Possible varieties are: ",
"{.val {varieties}}")
)
return()
}
}
}
set_plant_txt(
file = file.path(dirpath, paste0("ficplt", x, ".txt")),
param = param, value = value, append = add, variety = variety
)
})
},
stop("Parameter not found")
)
invisible()
}
#' @rdname set_param_txt
#' @export
set_usm_txt <- function(file = "new_travail.usm",
param,
value,
append = FALSE,
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_usm_txt(filepath)",
"set_usm_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_usm_txt(add)",
"set_usm_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(filepath, param, value, add)
}
#' @rdname set_param_txt
#' @export
set_station_txt <- function(file = "station.txt",
param,
value,
append = FALSE,
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_station_txt(filepath)",
"set_station_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_station_txt(add)",
"set_station_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(filepath, param, value, add)
}
#' @rdname set_param_txt
#' @export
set_ini_txt <- function(file = "ficini.txt",
param,
value,
append = FALSE,
plant_id = 1,
layer = NULL,
stics_version = "latest",
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_ini_txt(filepath)",
"set_ini_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_ini_txt(add)",
"set_ini_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(filepath, param, value, add,
plant_id = plant_id,
value_id = layer,
stics_version = stics_version
)
}
#' @rdname set_param_txt
#' @export
set_general_txt <- function(file = "tempopar.sti",
param,
value,
append = FALSE,
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_general_txt(filepath)",
"set_general_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_general_txt(add)",
"set_general_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(filepath, param, value, add)
}
#' @rdname set_param_txt
#' @export
set_tmp_txt <- function(file = "tempoparv6.sti",
param,
value,
append = FALSE,
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_tmp_txt(filepath)",
"set_tmp_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_tmp_txt(add)",
"set_tmp_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(filepath, param, value, add)
}
#' @rdname set_param_txt
#' @export
set_plant_txt <- function(file = "ficplt1.txt",
param,
value,
append = FALSE,
variety = NULL,
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_plant_txt(filepath)",
"set_plant_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_plant_txt(add)",
"set_plant_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(filepath, param, value, add, variety = variety)
}
#' @rdname set_param_txt
#' @export
set_tec_txt <- function(file = "fictec1.txt",
param,
value,
append = FALSE,
value_id = NULL,
filepath = lifecycle::deprecated(),
add = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_tec_txt(filepath)",
"set_tec_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
# add
if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0", "set_tec_txt(add)",
"set_tec_txt(append)"
)
} else {
add <- append # to remove when we update inside the function
}
set_file_txt(file = filepath,
param = param,
value = value,
append = add,
value_id = value_id)
}
#' @rdname set_param_txt
#' @export
set_soil_txt <- function(file = "param.sol",
param,
value,
layer = NULL,
stics_version = "latest",
filepath = lifecycle::deprecated()) {
# filepath
if (lifecycle::is_present(filepath)) {
lifecycle::deprecate_warn(
"1.0.0", "set_soil_txt(filepath)",
"set_soil_txt(file)"
)
} else {
filepath <- file # to remove when we update inside the function
}
param <- gsub("P_", "", param)
ref <- get_soil_txt(filepath, stics_version = stics_version)
param <- paste0("^", param, "$")
if (!is.null(layer)) {
check_param_dim(param = param,
file_value = ref[[grep(param, names(ref))]],
value_id = layer,
value = value)
ref[[grep(param, names(ref))]][layer] <- format(value, scientific = FALSE)
} else {
if(length(value) > 1) {
check_param_dim(param = param,
file_value = ref[[grep(param, names(ref))]],
value = value)
}
ref[[grep(param, names(ref))]][] <- format(value, scientific = FALSE)
}
if (get_version_num(stics_version = stics_version) < 10) {
line <- paste(
" ", " ", " ", ref$numsol[1], " ", " ", " ", ref$typsol,
ref$argi, ref$Norg, ref$profhum, ref$calc,
ref$pH, ref$concseuil, ref$albedo, ref$q0,
ref$ruisolnu, ref$obstarac, ref$pluiebat,
ref$mulchbat, ref$zesx, ref$cfes,
ref$z0solnu, ref$CsurNsol, ref$penterui
)
} else {
line <- paste(
" ", " ", " ", ref$numsol[1], " ", " ", " ", ref$typsol,
ref$argi, ref$Norg, ref$profhum, ref$calc,
ref$pH, ref$concseuil, ref$albedo, ref$q0,
ref$ruisolnu, ref$obstarac, ref$pluiebat,
ref$mulchbat, ref$zesx, ref$cfes,
ref$z0solnu, ref$CsurNsol, ref$finert, ref$penterui
)
}
writeLines(line, filepath)
write(paste(
" ", " ", " ", ref$numsol[1], " ", " ", " ",
ref$codecailloux, ref$codemacropor,
ref$codefente, ref$codrainage, ref$coderemontcap,
ref$codenitrif, ref$codedenit
),
filepath,
append = TRUE
)
write(paste(
" ", " ", " ", ref$numsol[1], " ", " ", " ", ref$profimper,
ref$ecartdrain, ref$ksol,
ref$profdrain, ref$capiljour, ref$humcapil,
ref$profdenit, ref$vpotdenit
),
filepath,
append = TRUE
)
for (icou in 1:5) {
write(paste(
" ", " ", " ", ref$numsol[1], " ", " ", " ",
ref$epc[icou], ref$hccf[icou],
ref$hminf[icou], ref$DAF[icou], ref$cailloux[icou],
ref$typecailloux[icou], ref$infil[icou],
ref$epd[icou]
),
filepath,
append = TRUE
)
}
}
#' Internal function to set some STICS input file parameters
#'
#' @description Replace or set an input parameter from a pre-existing
#' STICS input file. This function is called by some of the
#' generic \code{set_*} functions under the hood.
#'
#' @param file Path to the parameter file
#' @param param Parameter name
#' @param value New parameter value
#' @param append Boolean. Append input to existing file
#' @param plant_id The plant identifier (main crop: 1 ; associated crop: 2).
#' @param variety The plant variety to set the parameter value,
#' either the variety
#' name (`codevar` in the plant file) or the index
#' (`variete` in the technical file).
#' @param value_id index of technical interventions to be used to
#' set parameter values, or layer index for soil parameters
#' @param stics_version An optional version name as listed in
#' get_stics_versions_compat() return
#'
#' @details The function uses `base::sys.call()` to know from which function
#' of the \code{set_*} family it is called, so it won't work properly
#' if called by the user directly. This is why this function
#' is internal.
#'
#' @note This function is not used for \code{\link{set_soil_txt}}.
#'
#' @seealso \code{\link{set_param_txt}}.
#'
#' @keywords internal
#'
#' @noRd
#'
set_file_txt <- function(file,
param,
value,
append,
plant_id = NULL,
variety = NULL,
value_id = NULL,
stics_version = "latest") {
param <- gsub("P_", "", param)
stics_version <- check_version_compat(stics_version = stics_version)
# access the function name from which set_file_txt was called
type <- strsplit(deparse(sys.call(-1)), split = "\\(")[[1]][1]
params <- readLines(file)
param_ <- paste0("^:{0,1}", param, "$")
switch(type,
set_usm_txt = {
ref <- get_usm_txt(file)
if (grep(param_, names(ref)) < grep("fplt", names(ref))) {
ref_index <- grep(param_, names(ref)) * 2
} else {
ref_index <- grep(param_, params) + 1
}
},
set_station_txt = {
ref <- get_station_txt(file)
ref_index <- grep(param_, names(ref)) * 2
},
set_ini_txt = {
ref <- get_ini_txt(file, stics_version = stics_version)
# fix plant id if param is attached to a plant
if (is.null(plant_id) &&
(param %in% names(ref$plant$plant1))) {
plant_id <- 1
}
# changing param value in ref
if (is.null(plant_id)) {
if (is.null(value_id)) {
if (length(value) > 1){
check_param_dim(param = param,
file_value = ref[[param]],
value = value)
}
# all values take the same now
ref[[param]][] <- value
} else {
# check layers idx
# and values number
check_param_dim(param = param,
file_value = ref[[param]],
value_id = value_id,
value = value)
ref[[param]][[value_id]] <- value
}
} else {
plt_tag <- paste0("plant", plant_id)
if (is.null(value_id)) {
if (length(value) > 1) {
check_param_dim(param = param,
file_value = ref$plant[[plt_tag]][[param]],
value = value)
}
# all values take the same now
ref$plant[[plt_tag]][[param]][] <- value
} else {
check_param_dim(param = param,
file_value = ref$plant[[plt_tag]][[param]],
value_id = value_id,
value = value)
ref$plant[[plt_tag]][[param]][value_id] <- value
}
}
value <- list_to_character_vector(ref)
# rows index according to version
ref_index <- get_ini_val_idx(stics_version)
},
set_plant_txt = {
ref_index <- grep(param_, params) + 1
if (!is.null(variety) & length(ref_index) > 1) {
if (length(ref_index) >= variety) {
ref_index <- ref_index[variety]
} else {
stop("Variety number set in the tec file is superior",
"to the number of varieties defined in the plant file.")
}
}
},
set_tec_txt = {
ref <- get_tec_txt(file, stics_version = stics_version)
# add treatment for getting lines
# add index on the line for the parameters when several
# (interventions)
# question: replacing existing individual values and
# modifying interventions plan (i.e. reduce irrigations nb )
# getting sublist from ref, change values and
# transform to text and replace using lines index !
idx_lines <- grep(param, params)
# Getting par names on one line
line_param <- unlist(strsplit(params[idx_lines[1]], split = " "))
lines_values <- ref[line_param]
# replacing values
# all values with a single
if(is.null(value_id)) {
if (length(value) > 1) {
check_param_dim(param = param,
file_value = lines_values[[param]],
value = value)
}
lines_values[[param]][] <- value
} else {
# several values for specific ids
check_param_dim(param = param,
file_value = lines_values[[param]],
value_id = value_id,
value = value)
lines_values[[param]][value_id] <- value
}
df_lines_values <- as.data.frame(
lapply(lines_values, as.character), stringsAsFactors = FALSE)
# Values of parameters to replace in params at idx_lines + 1
value <- apply(df_lines_values,
1,
function(x) paste(x, collapse = " "))
ref_index <- idx_lines + 1
},
# Default here
{
ref_index <- grep(param_, params) + 1
}
)
if (!length(ref_index) > 0) {
if (append) {
value <- paste(value, collapse = " ")
params <- c(params, param, value)
ref_index <- grep(param_, params) + 1
} else {
stop(paste(param, "parameter not found in:\n", file))
}
} else {
if (append) {
stop(paste(
"Parameter", param, "already present in the file,",
"try to replace its value",
"instead of adding the parameter"
))
}
}
if (length(ref_index) != length(value)) {
stop(paste(
"Length of input value different from parameter value length.\n",
"Original values:\n", paste(params[ref_index], collapse = ", "),
"\ninput:\n", paste(value, collapse = ", ")
))
}
params[ref_index] <- format(value, scientific = FALSE)
writeLines(params, file)
}
get_ini_val_idx <- function(stics_version) {
if (get_version_num(stics_version = stics_version) < 10) {
idx <- c(
2,
4:10,
12,
14:20,
22,
24,
26,
28
)
} else {
idx <- c(
2,
4:7,
9:16,
18,
20:23,
25:32,
34,
36,
38,
40,
43,
45,
47,
49
)
}
idx
}
#' Check consistency of 2 vectors lengths
#'
#' @param param parameter name
#' @param file_value vector of param values
#' @param value_id vector of values id used for replacement
#' @param value vector of new values for `param` (its length is
#' either equal to file_value or value_id length)
#'
#' @return None
#' @keywords internal
#'
#' @noRd
#'
check_param_dim <- function(param,
file_value,
value_id = NULL,
value = NULL) {
file_val_nb <- length(file_value)
if (is.null(value_id)) {
max_id <- file_val_nb
} else {
value_id <- unique(value_id)
max_id <- max(value_id)
}
if (max_id > file_val_nb)
stop("for ", param, " parameter values replacement\n",
"the maximum number of values to be replaced in the file (",
file_val_nb,
") ",
"exceeds with the maximum of given id (",
max_id,
")")
# no more checks
if (is.null(value)) return(invisible())
# checking replacing value
replace_val_nb <- length(value)
if (file_val_nb == replace_val_nb) return(invisible())
if (!is.null(value_id)) {
replace_val_id_nb <- length(value_id)
if (replace_val_id_nb == replace_val_nb)
return(invisible())
}
stop("for ", param, " parameter values replacement\n",
"the number of values to be replaced in the file (", replace_val_nb, ") ",
"is not consistent with the given values' ids (", replace_val_id_nb,
")")
}