Import Data Prep
Reshapes an external data file into a CSV that the Import Links dialog reads. The CSV follows the time grid of the model. A gap report names every grid time step that the source file did not cover.
The app bundles this script as Import_Data_Prep.R and Import_Data_Prep.py. Type the bare file name as a Script’s File, or pick it with Bundled scripts… on a Script row. A copy beside the Experiment file runs in place of the bundled one. See Run your own R or Python. Back to the Scripts.
| File | Needs |
|---|---|
Import_Data_Prep.R | Rscript. Base R only, no package |
Import_Data_Prep.py | python3 and pandas. Install it with pip install pandas |
Agreement is exact on the written files. The test suite checks this on every fixture. The script copies every source cell as text and never parses it to a number, so the two languages write byte-identical output.
Requirements
| Language | Needs |
|---|---|
| Python | Python 3.11 or later, with pandas. Install it with pip install pandas. |
| R | R 4.3 or later. No package. |
The test suite runs on these versions in GitHub Actions. Older versions are not tested.
Where It Runs
This script runs before a batch, not after one. It prepares a file for the import of Stella. It does not read the results of a batch. Its first argument is the source data file. Every other script in this library takes an output folder first.
Rscript Import_Data_Prep.R source.csv --time-grid grid.csv
Rscript Import_Data_Prep.R source.csv /path/to/Outputs/MyBatchThe time grid comes from one of two places.
--time-grid FILEnames a one-column CSV directly. The file has a header that names the time column and one row per grid step.- The
Output.csvof a finished batch supplies the grid instead. Point at it with a second argument or withEXPERIMENTER_OUTPUT_DIR. The script finds the time column with the elimination rule ofScript_Template, against the distinct time values that the batch ran.
--time-grid wins when you give both.
Shapes It Reads
The first column of the source file is always the time column, whatever its name. The rest of the header decides the shape:
| Header | Shape |
|---|---|
Exactly <time column>, Variable, Value (case-insensitive) | Long: one row per (time, variable) pair |
| Anything else, with at least one more column | Wide: one row per time step, one column per variable |
Both shapes read a sparse or irregularly spaced time column the same way. This script needs no even spacing. It needs only a time value in the source that matches one in the grid, to 1e-9.
What It Writes
<source stem>_Import.csv (for source.csv, source_Import.csv) sits
beside the source file. It has one row per grid time step. The time column
of the grid comes first, then one column per variable in the order of the
source.
- The script writes a grid step that the source did not cover as an empty cell. Import files of Stella already use this shape for a missing value.
- A source value cell that does not parse as a number, for example the text “NA”, also becomes an empty cell and counts as a gap. The script never copies it as a literal string, because the Import Links dialog would read it as a bogus value.
<source stem>_Import_Gaps.csv (for source.csv, source_Import_Gaps.csv)
sits beside the source file. It has one row per (Variable, time) pair that
the import file left blank. A file with a header only means full coverage.
Alignment is exact match only. A source time step within 1e-9 of a grid step fills that row. The script interpolates nothing. The Import Links dialog owns interpolation between the rows that this script writes (Continuous, Discrete or Raw, set for each import there). The script does not write a source time step that matches no grid step. It counts that step in the one summary line that it prints. A mismatched or off-grid source file stays visible and the script does not drop it silently.
What It Prints
One summary line. It holds the variable count, the grid time-step count, the gap count and the count of source values at a time not on the grid.
Each of these stops the script with a message that names the problem:
- no source file argument
- a source file that does not exist
- no time grid available
- a non-numeric or repeated time value
- a ragged source row
- on the
Output.csvpath, a column refusal of the loader, for example a file withoutsourceorsource_kind
In a Chain
The script reads the source file that you name on the command line. It reads
Output.csv only to learn the time grid. It writes <source stem>_Import.csv
and <source stem>_Import_Gaps.csv beside the source file. This is the one
script in the library that writes outside the folder it reads. No other
script reads these files. The Import Links dialog reads the first one. See
the chaining rules.
Version Policy
The library README sets the format policy.
On the path that reads an Output.csv, the header of the script names the
Experimenter version that it targets. The script checks every column before
it reads a time grid from the file. An unknown column stops the script with a
message that names it. A version difference prints one line and does not stop
the run. The --time-grid path touches no Experimenter file and has no
version check.