Consistent time mappings and allow adding new ones
JohannesEmm opened this issue · 8 comments
Now regional mappings are OK to added. SO would make sense to also allow custom time mappings.
In the pre-Package version there was the function
load_time_mapping = function(file){ tab = fread(file,colClasses="character") return(tab) }
However now I see some issues
- There is the new column "refyear" which is NOT in the raw csv files neither in WITCH nor witchtools
- the csv files are on yearly basis not tstep, so the function woudl be more complex now, but let's discuss how to have consisteny here too.
refyear
isyear
in the csv file.- I think it is already possible to have custom time mappings, in a similar way that it is working for regions.
Can you provide some examples (use cases) with a clear description of the input and output you are expected? so I can see exactly how I can improve the code (and add some validation tests).
thanks
hmm I did not find any function in the package.
Before, as above, the function was there (load_time_mapping ) but is basically just loading the csv file.
So using the same logic as with regions now collating them in a list does not work however since the csv input files and the actually used list elements are not compatible.
Here is how I do it in RICE+ as a work around using dplyr just temporary. Maybe just the same steps but in data.table/baseR and then as witch_time_mapping function would be enough. This way woul dbe 100% in parallel to the region function (see example in the last two lines).
`#add time mapping t58 for RICE50+
#for now workaround until witchtools updated
t58 <- data.table::fread("input/time/t58.csv", colClasses="character")
require(tidyverse)
t58 <- t58 %>% select(-year) %>% full_join(data.frame(year=seq(2013,2302)) %>% mutate(refyear=round(year / 5) * 5) %>% mutate(year=as.character(year), refyear=as.character(refyear))) %>% select(t,year,refyear,pred,tperiod,begyear,endyear) %>% mutate(year=as.numeric(year)) %>% mutate(pred=ifelse(is.na(pred)|pred=="NA", "", pred))
time_mappings <- c(time_mappings, list(t58 = t58))
#add ed57 mapping (same as enerdata56 but actually they ARE 57 regions)
ed57 <- witch_region_mapping("input/regions/ed57.inc")
region_mappings <- c(region_mappings,list(ed57 = ed57))`
The CSV should not be used as an input or as a reference. This is a convenient way to save the time definition in the package but it should not be used otherwise.
The correct format of time definition is a data.table with columns t, year, refyear, pred, tperiod, begyear, endyear. As you can find in the list witchtools::time_mappings . If you want to add a new time definition, you need to provide a data.table whatever is way you build it or it is stored.
But perhaps, I don't get exactly what you would like to have? I agree to improve the documentation to help also.
Ok, perhaps I get it. You might follow what this script is doing to translate the csv:
https://github.com/witch-team/witchtools/blob/master/data-raw/time_mappings.R
What you would like is to have this function load_timescale_mapping
available in the package? (now just to prepare the data of the package)
right that would be it!
I htink its useful to have thi sfunction as well. Cause else there is hard wired inconsistency between the df's and csv files, so this way they can be also used and edited/added by the user.