diff --git a/ExternData/Examples/package.mo b/ExternData/Examples/package.mo index 24d87fbd..da05bbed 100644 --- a/ExternData/Examples/package.mo +++ b/ExternData/Examples/package.mo @@ -31,6 +31,7 @@ package Examples "Test examples" model CSVTest "CSV file read test" extends Modelica.Icons.Example; inner parameter CSVFile csvfile(fileName=Modelica.Utilities.Files.loadResource("modelica://ExternData/Resources/Examples/test.csv")) annotation(Placement(transformation(extent={{-80,60},{-60,80}}))); + parameter Integer m = csvfile.getArrayRows2D() "Number of rows in 2D array"; Modelica.Blocks.Sources.TimeTable timeTable(table=csvfile.getRealArray2D(3, 2)) annotation(Placement(transformation(extent={{-50,60},{-30,80}}))); annotation(experiment(StopTime=1), Documentation(info="
This example model reads the table parameter from the CSV file test.csv. The table parameter is read as Real array of dimension 3x2 by function ExternData.CSVFile.getRealArray2D. The read parameter is assigned by a parameter binding to the appropriate model parameter.
")); diff --git a/ExternData/Resources/BuildProjects/VisualStudio2010/ED_CSVFile.def b/ExternData/Resources/BuildProjects/VisualStudio2010/ED_CSVFile.def index 32149f28..32cb5903 100644 --- a/ExternData/Resources/BuildProjects/VisualStudio2010/ED_CSVFile.def +++ b/ExternData/Resources/BuildProjects/VisualStudio2010/ED_CSVFile.def @@ -2,3 +2,4 @@ EXPORTS ED_createCSV ED_destroyCSV ED_getDoubleArray2DFromCSV + ED_getArray2DDimensionsFromCSV diff --git a/ExternData/Resources/C-Sources/ED_CSVFile.c b/ExternData/Resources/C-Sources/ED_CSVFile.c index b0a1278a..56071bfc 100644 --- a/ExternData/Resources/C-Sources/ED_CSVFile.c +++ b/ExternData/Resources/C-Sources/ED_CSVFile.c @@ -267,3 +267,24 @@ void ED_getDoubleArray2DFromCSV(void* _csv, int* field, double* a, size_t m, siz } } } + +void ED_getArray2DDimensionsFromCSV(void* _csv, int* m, int* n) +{ + CSVFile* csv = (CSVFile*)_csv; + *m = 0; + *n = 0; + ED_PTR_CHECK(csv); + if (NULL != csv) { + char *lineCopy = strdup(utstring_body((Line*)cpo_array_get_at(csv->lines, 0))); + if (NULL != lineCopy) { + char* nextToken = NULL; + char* token = zstring_strtok_dquotes(lineCopy, csv->sep, csv->quote, &nextToken); + *m = (int)csv->lines->num; + while (NULL != token) { + (*n)++; + token = zstring_strtok_dquotes(NULL, csv->sep, csv->quote, &nextToken); + } + free(lineCopy); + } + } +} diff --git a/ExternData/Resources/Include/ED_CSVFile.h b/ExternData/Resources/Include/ED_CSVFile.h index 987447d6..08212253 100644 --- a/ExternData/Resources/Include/ED_CSVFile.h +++ b/ExternData/Resources/Include/ED_CSVFile.h @@ -33,5 +33,6 @@ void* ED_createCSV(const char* fileName, const char* sep, const char* quote, int verbose); void ED_destroyCSV(void* _csv); void ED_getDoubleArray2DFromCSV(void* _csv, int* field, double* a, size_t m, size_t n); +void ED_getArray2DDimensionsFromCSV(void* _csv, int* m, int* n); #endif diff --git a/ExternData/package.mo b/ExternData/package.mo index 914f3961..35bf2e4b 100644 --- a/ExternData/package.mo +++ b/ExternData/package.mo @@ -56,6 +56,9 @@ package ExternData "Library for data I/O of CSV, INI, JSON, MATLAB MAT, TIR, Exc parameter Boolean verboseRead=true "= true, if info message that file is loading is to be printed"; final parameter Types.ExternCSVFile csv=Types.ExternCSVFile(fileName, delimiter, quotation, verboseRead) "External INI file object"; final function getRealArray2D = Functions.CSV.getRealArray2D(final csv=csv) "Get 2D Real values from CSV file" annotation(Documentation(info="")); + final function getArraySize2D = Functions.CSV.getArraySize2D(final csv=csv) "Get the size of a 2D array in a CSV file" annotation(Documentation(info="")); + final function getArrayRows2D = Functions.CSV.getArrayRows2D(final csv=csv) "Get first dimension of 2D array in CSV file" annotation(Documentation(info="")); + final function getArrayColumns2D = Functions.CSV.getArrayColumns2D(final csv=csv) "Get second dimension of 2D array in CSV file" annotation(Documentation(info="")); annotation( Documentation(info="Record that wraps the external object ExternCSVFile and the CSV read function for data access of CSV files.
See Examples.CSVTest for an example.
"), defaultComponentName="csvfile", @@ -307,6 +310,44 @@ package ExternData "Library for data I/O of CSV, INI, JSON, MATLAB MAT, TIR, Exc Include = "#include \"ED_CSVFile.h\"", Library = {"ED_CSVFile", "bsxml-json"}); end getRealArray2D; + + function getArraySize2D "Get dimensions of 2D array in CSV file" + extends Modelica.Icons.Function; + input Types.ExternCSVFile csv "External CSV file object"; + output Integer m "Number of rows in array"; + output Integer n "Number of columns in array"; + external "C" ED_getArray2DDimensionsFromCSV(csv, m, n) annotation( + __iti_dll = "ITI_ED_CSVFile.dll", + __iti_dllNoExport = true, + Include = "#include \"ED_CSVFile.h\"", + Library = {"ED_CSVFile", "bsxml-json"}); + end getArraySize2D; + + function getArrayRows2D "Get first dimension of 2D array in CSV file" + extends Modelica.Icons.Function; + input Types.ExternCSVFile csv "External CSV file object"; + output Integer m "Number of rows in array"; + protected + Integer n[1] "Number of columns in array"; + external "C" ED_getArray2DDimensionsFromCSV(csv, m, n) annotation( + __iti_dll = "ITI_ED_CSVFile.dll", + __iti_dllNoExport = true, + Include = "#include \"ED_CSVFile.h\"", + Library = {"ED_CSVFile", "bsxml-json"}); + end getArrayRows2D; + + function getArrayColumns2D "Get second dimension of 2D array in CSV file" + extends Modelica.Icons.Function; + input Types.ExternCSVFile csv "External CSV file object"; + output Integer n "Number of columns in array"; + protected + Integer m[1] "Number of rows in array"; + external "C" ED_getArray2DDimensionsFromCSV(csv, m, n) annotation( + __iti_dll = "ITI_ED_CSVFile.dll", + __iti_dllNoExport = true, + Include = "#include \"ED_CSVFile.h\"", + Library = {"ED_CSVFile", "bsxml-json"}); + end getArrayColumns2D; annotation(Icon(coordinateSystem(preserveAspectRatio=false, extent={{-100,-100},{100,100}}), graphics={Text(lineColor={128,128,128},extent={{-90,-90},{90,90}},textString="f")})); end CSV;