@@ -307,49 +307,73 @@ createClusterRES <- function(area,
307307
308308 # API block
309309 if (is_api_study(opts )) {
310-
311- if (cluster_type == " thermal" ) {
312- cmd <- api_command_generate(
313- action = " create_cluster" ,
314- area_id = area ,
315- cluster_name = cluster_name ,
316- prepro = prepro_data ,
317- modulation = prepro_modulation ,
318- parameters = params_cluster
319- )
320- } else {
321- cmd <- api_command_generate(
322- action = " create_renewables_cluster" ,
323- area_id = area ,
324- cluster_name = cluster_name ,
325- parameters = params_cluster
326- )
327- }
328-
329- api_command_register(cmd , opts = opts )
330- `if`(
331- should_command_be_executed(opts ),
332- api_command_execute(cmd , opts = opts , text_alert = " {.emph create_cluster}: {msg_api}" ),
333- cli_command_registered(" create_cluster" )
334- )
335-
336- if (is.null(time_series )) {
337- time_series <- matrix (0 ,8760 ) # Default
310+
311+ if (! is_api_mocked(opts )) {
312+ thermal_type <- identical(cluster_type , " thermal" )
313+ renewables_type <- identical(cluster_type , " renewables" )
314+
315+ if (thermal_type ) {
316+ suffix_endpoint <- " thermal"
317+ } else if (renewables_type ) {
318+ suffix_endpoint <- " renewable"
319+ }
320+
321+ body <- transform_list_to_json_for_createCluster(cluster_parameters = params_cluster , cluster_type = cluster_type )
322+ result <- api_post(opts = opts ,
323+ endpoint = file.path(opts [[" study_id" ]], " areas" , area , " clusters" , suffix_endpoint ),
324+ body = body ,
325+ encode = " raw" )
326+ cli :: cli_alert_success(" Endpoint Create {.emph {.strong {suffix_endpoint}}} (properties) {.emph {.strong {cluster_name}}} success"
327+ )
328+
329+ cmd <- NULL
330+ if (renewables_type ) {
331+ if (! is.null(time_series )) {
332+ cmd <- api_command_generate(
333+ action = " replace_matrix" ,
334+ target = sprintf(" input/renewables/series/%s/%s/series" , area , lower_cluster_name ),
335+ matrix = time_series
336+ )
337+ }
338+ }
339+
340+ if (thermal_type ) {
341+ thermal_time_series <- list (" prepro_data" = list (" path" = " input/thermal/prepro/%s/%s/data" ,
342+ " matrix" = prepro_data
343+ ),
344+ " prepro_modulation" = list (" path" = " input/thermal/prepro/%s/%s/modulation" ,
345+ " matrix" = prepro_modulation
346+ ),
347+ " thermal_availabilities" = list (" path" = " input/thermal/series/%s/%s/series" ,
348+ " matrix" = time_series )
349+ )
350+ not_null_matrix <- sapply(thermal_time_series , FUN = function (l ) {! is.null(l [[" matrix" ]])})
351+ thermal_time_series <- thermal_time_series [not_null_matrix ]
352+
353+ if (length(thermal_time_series ) > 0 ) {
354+ actions <- lapply(
355+ X = seq_along(thermal_time_series ),
356+ FUN = function (i ) {
357+ list (
358+ target = sprintf(thermal_time_series [[i ]][[" path" ]], area , lower_cluster_name ),
359+ matrix = thermal_time_series [[i ]][[" matrix" ]]
360+ )
361+ }
362+ )
363+ actions <- setNames(actions , rep(" replace_matrix" , length(actions )))
364+ cmd <- do.call(api_commands_generate , actions )
365+ }
366+ }
367+
368+ if (! is.null(cmd )) {
369+ api_command_register(cmd , opts = opts )
370+ `if`(
371+ should_command_be_executed(opts ),
372+ api_command_execute(cmd , opts = opts , text_alert = " Writing cluster's series: {msg_api}" ),
373+ cli_command_registered(" replace_matrix" )
374+ )
375+ }
338376 }
339-
340- currPath <- ifelse(identical(cluster_type , " renewables" ), " input/renewables/series/%s/%s/series" , " input/thermal/series/%s/%s/series" )
341- cmd <- api_command_generate(
342- action = " replace_matrix" ,
343- target = sprintf(currPath , area , lower_cluster_name ),
344- matrix = time_series
345- )
346- api_command_register(cmd , opts = opts )
347- `if`(
348- should_command_be_executed(opts ),
349- api_command_execute(cmd , opts = opts , text_alert = " Writing cluster's series: {msg_api}" ),
350- cli_command_registered(" replace_matrix" )
351- )
352-
353377 return (invisible (opts ))
354378 }
355379
@@ -488,3 +512,72 @@ list_pollutants_values <- function(multi_values = NULL) {
488512 " op5" = multi_values ,
489513 " co2" = multi_values )
490514}
515+
516+
517+ # ' Transform a user list to a json object to use in the endpoint of cluster creation
518+ # '
519+ # ' @importFrom jsonlite toJSON
520+ # ' @importFrom assertthat assert_that
521+ # '
522+ # ' @param cluster_parameters a list containing the metadata of the cluster you want to create.
523+ # ' @param cluster_type the type of cluster you want to create. Each type has specific values.
524+ # '
525+ # ' @return a json object
526+ # ' @noRd
527+ transform_list_to_json_for_createCluster <- function (cluster_parameters , cluster_type ) {
528+
529+ assert_that(cluster_type %in% c(" thermal" , " renewables" ))
530+ assert_that(inherits(x = cluster_parameters , what = " list" ))
531+
532+ if (cluster_type == " thermal" ) {
533+ cluster_parameters <- list (" name" = cluster_parameters [[" name" ]],
534+ " group" = cluster_parameters [[" group" ]],
535+ " enabled" = cluster_parameters [[" enabled" ]],
536+ " mustRun" = cluster_parameters [[" must-run" ]],
537+ " unitCount" = cluster_parameters [[" unitcount" ]],
538+ " nominalCapacity" = cluster_parameters [[" nominalcapacity" ]],
539+ " minStablePower" = cluster_parameters [[" min-stable-power" ]],
540+ " spinning" = cluster_parameters [[" spinning" ]],
541+ " minUpTime" = cluster_parameters [[" min-up-time" ]],
542+ " minDownTime" = cluster_parameters [[" min-down-time" ]],
543+ " costGeneration" = cluster_parameters [[" costgeneration" ]],
544+ " marginalCost" = cluster_parameters [[" marginal-cost" ]],
545+ " spreadCost" = cluster_parameters [[" spread-cost" ]],
546+ " fixedCost" = cluster_parameters [[" fixed-cost" ]],
547+ " startupCost" = cluster_parameters [[" startup-cost" ]],
548+ " marketBidCost" = cluster_parameters [[" market-bid-cost" ]],
549+ " genTs" = cluster_parameters [[" gen-ts" ]],
550+ " volatilityForced" = cluster_parameters [[" volatility.forced" ]],
551+ " volatilityPlanned" = cluster_parameters [[" volatility.planned" ]],
552+ " lawForced" = cluster_parameters [[" law.forced" ]],
553+ " lawPlanned" = cluster_parameters [[" law.planned" ]],
554+ " co2" = cluster_parameters [[" co2" ]],
555+ " nh3" = cluster_parameters [[" nh3" ]],
556+ " so2" = cluster_parameters [[" so2" ]],
557+ " nox" = cluster_parameters [[" nox" ]],
558+ " pm25" = cluster_parameters [[" pm2_5" ]],
559+ " pm5" = cluster_parameters [[" pm5" ]],
560+ " pm10" = cluster_parameters [[" pm10" ]],
561+ " nmvoc" = cluster_parameters [[" nmvoc" ]],
562+ " op1" = cluster_parameters [[" op1" ]],
563+ " op2" = cluster_parameters [[" op2" ]],
564+ " op3" = cluster_parameters [[" op3" ]],
565+ " op4" = cluster_parameters [[" op4" ]],
566+ " op5" = cluster_parameters [[" op5" ]],
567+ " efficiency" = cluster_parameters [[" efficiency" ]],
568+ " variableOMCost" = cluster_parameters [[" variableomcost" ]]
569+ )
570+ } else if (cluster_type == " renewables" ) {
571+ cluster_parameters <- list (" name" = cluster_parameters [[" name" ]],
572+ " group" = cluster_parameters [[" group" ]],
573+ " enabled" = cluster_parameters [[" enabled" ]],
574+ " tsInterpretation" = cluster_parameters [[" ts-interpretation" ]],
575+ " unitCount" = cluster_parameters [[" unitcount" ]],
576+ " nominalCapacity" = cluster_parameters [[" nominalcapacity" ]]
577+ )
578+ }
579+
580+ cluster_parameters <- dropNulls(cluster_parameters )
581+
582+ return (toJSON(cluster_parameters , auto_unbox = TRUE ))
583+ }
0 commit comments