Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for start rains dialog #9380

Open
lilyclements opened this issue Jan 21, 2025 · 0 comments
Open

Fixes for start rains dialog #9380

lilyclements opened this issue Jan 21, 2025 · 0 comments

Comments

@lilyclements
Copy link
Contributor

lilyclements commented Jan 21, 2025

Now we've done the work to the end of rains dialog, there's a few bits we want to do to the start of rains dialog :)

I'll put this into three parts:

  1. Before our instat_calculation code
  2. Our instat_calculation code
  3. After our instat_calculation code

1. Before our instat_calculation code

year_type <- data_book$get_column_data_types(data_name="data", columns="s_year1")
data_book$convert_column_to_type(data_name="data", col_names="s_year1", to_type="factor")
station_type <- data_book$get_column_data_types(data_name="data", columns="station")
data_book$convert_column_to_type(data_name="data", col_names="station", to_type="factor")
data_book$convert_linked_variable(from_data_frame="data", link_cols=c("s_year1", "station"))

(We currently run lines 1-2 of this)

If there is no year, then we can ignore lines 3-4, and change line 5 to just contain "s_year1"

2. Our instat_calculation code

No changes

3. After our instat_calculation code

linked_data_name <- data_book$get_linked_to_data_name("data", link_cols=c("s_year1", "station"))

# Create the list dynamically using setNames
calculated_from_list <- c(
  setNames("start_rain_status", linked_data_name),
  setNames("start_rain", linked_data_name)
)

# Pass the dynamically created list into instat_calculation
start_rain_status_2 <- instat_calculation$new(
  type = "calculation",
  function_exp = "ifelse(!is.na(start_rain), TRUE, start_rain_status)",
  calculated_from = calculated_from_list,
  result_name = "start_rain_status",
  save = 2
)

start_rain_combined_status_2 <- instat_calculation$new(type="combination", sub_calculations=list(start_rain_status_2))
data_book$run_instat_calculation(display=FALSE, param_list=list(drop=drop_val), calc=start_rain_combined_status_2)

data_book$convert_column_to_type(data_name="data", col_names="s_year1", to_type=year_type)
data_book$convert_column_to_type(data_name="data", col_names="station", to_type=station_type)
data_book$convert_linked_variable(from_data_frame="data", link_cols=c("s_year1", "station"))
data_book$remove_unused_station_year_combinations(data_name="data", year="s_year1", station="station")

rm(list=c("start_of_rains_combined", "grouping_by_station", "conditions_filter", "roll_sum_rain", "grouping_by_year", "doy_filter", "start_of_rains_doy", "start_rain_date", "start_of_rains_status", "year_type", "linked_data_name"))

If there is no station we:
i) do not run the remove_unused_station_year_combinations line,
ii) ignore convert_column_to_type for station_type
iii) we amend the linked_data_name to contain only s_year1:

# Create the list dynamically using setNames
calculated_from_list <- c(
  setNames("start_rain_status", linked_data_name),
  setNames("start_rain", linked_data_name)
)

# Pass the dynamically created list into instat_calculation
start_rain_status_2 <- instat_calculation$new(
  type = "calculation",
  function_exp = "ifelse(!is.na(start_rain), TRUE, start_rain_status)",
  calculated_from = calculated_from_list,
  result_name = "start_rain_status",
  save = 2
)

start_rain_combined_status_2 <- instat_calculation$new(type="combination", sub_calculations=list(start_rain_status_2))
data_book$run_instat_calculation(display=FALSE, param_list=list(drop=drop_val), calc=start_rain_combined_status_2)

data_book$convert_column_to_type(data_name="data", col_names="s_year1", to_type=year_type)
data_book$convert_linked_variable(from_data_frame="data", link_cols=c("s_year1"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant