Package: psr-graf
PyGrafUtility module to read Sddp hdr/bin result file pairs. Someexamplesto convert it to
popular formats are available.InstallingInstall the latest version fromPyPIpip install psr-grafOr download this repository contents.UsageStart by importingpsr.grafmodule. It's possible to read data directly usingopen_binandopen_csvfunctions
orload_as_dataframefunction ifpandaspackage is available.The example below shows how to load data directly into apandas.DataFrameand prints the first 5 lines of data.importpsr.grafdf=psr.graf.load_as_dataframe("sample_data/gerter.hdr")print(df.head())The output is:Thermal 1  Thermal 2  Thermal 3
stage scenario block                                 
1     1        1       7.440000      0.744   0.368069
               2       6.437624      0.000   0.000000
               3       7.440000      0.744   0.576140
               4       7.440000      0.744   2.994997
               5       7.440000      0.744   0.916644Alternatively,open_binandopen_csvfunctions can be used for direct data access as shown in the example next.importpsr.grafwithpsr.graf.open_bin("sample_data/gerter.hdr")asgraf_file:print("Stages:",graf_file.stages)print("Scenarios:",graf_file.scenarios)print("Agents:",graf_file.agents)print(f"Initial date:{graf_file.initial_year:04d}/{graf_file.initial_stage:02d}")print("Units:",graf_file.units)stage=2print(f"Number of blocks at stage{stage}:{graf_file.blocks(stage)}")scenario=10block=1print(f"Data at stage{stage}, scenario{scenario}, block{block}:",graf_file.read(stage,scenario,block))The output is:Stages: 12
Scenarios: 50
Agents: ('Thermal 1', 'Thermal 2', 'Thermal 3')
Initial date: 2013/01
Units: GWh
Number of blocks at stage 2: 1
Data at stage 2, scenario 10, block 1: (7.440000057220459, 0.7440000176429749, 0.3680693209171295)RequirementsPython 2.7, 3.3 or newer.(Optional)pandaspackage to useload_as_dataframefunction.File FormatsFile ExtensionDescription.hdr or .binBinary .hdr and .bin pair.datSingle-binary file.csvCSV file with specific structureload_as_dataframesupports all of them and will determine which reader will be used based on the file extension.open_binsupports only .hdr/bin pairs or single-binary files.open_csvsupports only CSV.Bothopen_bin,open_csv, andload_as_dataframefunctions acceptencodingparameter to specify the encoding of the strings in file. The default isutf-8.DataFrame optionsMultiIndex or single indexload_as_dataframeaccepts an optional keyword argumentmulti_index(defaultTrue) to specify if the
returnedpandas.DataFrameshould usepandas.MultiIndexor not. IfFalse, the returnedpandas.DataFramewill have a
single automatic index and the columns 'stage', 'scenario', 'block' will appear before the agents' data.Example:importpsr.grafdf=psr.graf.load_as_dataframe("sample_data/gerter.hdr",multi_index=False)print(df.head())print("Column names:",df.columns.values)The output is:stage  scenario  block  Thermal 1  Thermal 2  Thermal 3
0      1         1      1   7.440000      0.744   0.368069
1      1         2      1   6.437624      0.000   0.000000
2      1         3      1   7.440000      0.744   0.576140
3      1         4      1   7.440000      0.744   2.994997
4      1         5      1   7.440000      0.744   0.916644
Column names: ['stage' 'scenario' 'block' 'Thermal 1' 'Thermal 2' 'Thermal 3']On the other hand,importpsr.grafdf=psr.graf.load_as_dataframe("sample_data/gerter.hdr",multi_index=True)print(df.head())print("Column names:",df.columns.values)Will produce the following output:Thermal 1  Thermal 2  Thermal 3
stage scenario block                                 
1     1        1       7.440000      0.744   0.368069
      2        1       6.437624      0.000   0.000000
      3        1       7.440000      0.744   0.576140
      4        1       7.440000      0.744   2.994997
      5        1       7.440000      0.744   0.916644
Column names: ['Thermal 1' 'Thermal 2' 'Thermal 3']Index formatsTheindex_formatspecifies the format of index columns of the returnedpandas.Dataframe. It accepts the following
values:Index FormatColumnsdefaultstage, scenario, block or hourperiodyear, month or week, scenario, block or hourdefaultcreates apandas.DataFramewith the columns as they are stored in the original file.periodconvertsstageintoyear,monthorweekdepending on the stage type of the file and the
initial year and stage.Filter by stage, scenario, block and/or agentTheload_as_dataframefunction accepts the following optional keyword arguments to filter data:ArgumentDescriptionfilter_stagesList of stages to be includedfilter_blocksList of blocks to be includedfilter_scenariosList of scenarios to be includedfilter_agentsList of agents to be included. The name is case insensitive.An empty list or unspecified means all stages, blocks, scenarios or agents will be included.Example:importpsr.grafdf=psr.graf.load_as_dataframe("sample_data/gerter.hdr",filter_stages=[8,],filter_scenarios=[10,],filter_agents=["Thermal 3","Thermal 2"])print(df.head())Outputs:Thermal 3  Thermal 2
stage scenario block                      
10    8        1       9.127362        0.0Usage Samplesdataframes_sample.pyShows how to read data intopandas.DataFrames.Requirespandaspackage installed.matplotlib_sample.pyShows how to read data and plot data from hdr/bin file pairs.Requiresmatplotlibpackage installed.csv_sample.pyShows how to convert from hdr/bin file pairs to csv usingpsr.grafmodule.This script can also be called from command line:python csv_sample.py input_file.hdr output_file.csvWhereoutput_file.csvis optional.parquet_sample.pyShows how to convert from hdr/bin file pairs to Apache Parquet format.Requirespyarrowpackage installed.This script can also be called from command line:python parquet_sample.py input_file.hdr output_file.parquetWhereoutput_file.parquetis optional.Issues and SupportCheck PyGraf'sGitHub repositoryandissues pagefor support.