Skip to content

Commit

Permalink
refs #21: Add function to get dimension for CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed Oct 13, 2017
1 parent 98bb2a4 commit 3a9dba1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions ExternData/Examples/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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="<html><p>This example model reads the table parameter from the CSV file <a href=\"modelica://ExternData/Resources/Examples/test.csv\">test.csv</a>. The table parameter is read as Real array of dimension 3x2 by function <a href=\"modelica://ExternData.CSVFile.getRealArray2D\">ExternData.CSVFile.getRealArray2D</a>. The read parameter is assigned by a parameter binding to the appropriate model parameter.</p></html>"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ EXPORTS
ED_createCSV
ED_destroyCSV
ED_getDoubleArray2DFromCSV
ED_getArray2DDimensionsFromCSV
21 changes: 21 additions & 0 deletions ExternData/Resources/C-Sources/ED_CSVFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
1 change: 1 addition & 0 deletions ExternData/Resources/Include/ED_CSVFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 41 additions & 0 deletions ExternData/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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="<html></html>"));
final function getArraySize2D = Functions.CSV.getArraySize2D(final csv=csv) "Get the size of a 2D array in a CSV file" annotation(Documentation(info="<html></html>"));
final function getArrayRows2D = Functions.CSV.getArrayRows2D(final csv=csv) "Get first dimension of 2D array in CSV file" annotation(Documentation(info="<html></html>"));
final function getArrayColumns2D = Functions.CSV.getArrayColumns2D(final csv=csv) "Get second dimension of 2D array in CSV file" annotation(Documentation(info="<html></html>"));
annotation(
Documentation(info="<html><p>Record that wraps the external object <a href=\"modelica://ExternData.Types.ExternCSVFile\">ExternCSVFile</a> and the <a href=\"modelica://ExternData.Functions.CSV\">CSV</a> read function for data access of <a href=\"https://en.wikipedia.org/wiki/Comma-separated_values\">CSV</a> files.</p><p>See <a href=\"modelica://ExternData.Examples.CSVTest\">Examples.CSVTest</a> for an example.</p></html>"),
defaultComponentName="csvfile",
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3a9dba1

Please sign in to comment.