modelica-3rdparty / ExternData

:page_facing_up: Modelica library for data I/O of CSV, INI, JSON, MATLAB MAT, SSV, TIR, Excel XLS/XLSX and XML files
https://doi.org/10.3384/ecp21181141
BSD 2-Clause "Simplified" License
74 stars 30 forks source link

Error reading array of reals from XML file #3

Closed jnangle closed 9 years ago

jnangle commented 9 years ago

A little background on what I'm attempting to accomplish:

I am using XML and JSON files output from NREL's PVWatts API and other web services to get solar PV data for a given geographic location, and use it in Modelica models. I thought this would be a quicker approach than trying to re-create PVWatts in Modelica. Once I get this code working, I will be using it for wind data as well.

The Modelica code I'm using is:

model PVWatts "XML read test - PVWatts file"
  extends Modelica.Icons.Example;

  // Read the PVWatts XML data file
  ExternData.XMLFile xmlfile(fileName=Modelica.Utilities.Files.loadResource("modelica://ExternData/Resources/Examples/PVWattsv5.xml"));

  // Get the measurement station latitude and longitude from the file.  This is just a test to make sure the file can be read.
  // Real stationLat = xmlfile.getReal("station-info.lat");
  // Real stationLon = xmlfile.getReal("station-info.lon");

  // Create an array to hold 12 monthly AC power values
  Real pvMonthly[12] = xmlfile.getRealArray1D("outputs.ac-monthly",12);

end PVWatts;

The two lines that read the station lat and lon work just fine. I run into trouble when attempting to read an array of 12 values from the XML file into the pvMonthly array. I've looked at the function code for getRealArray1D and I think I'm calling it correctly. Once this is working, I will be loading an array of 8760 values of hourly PV data.

The log file dump from Dymola when I run the above code is:

Log-file of program ./dymosim
(generated: Thu Jul 30 08:24:18 2015)

dymosim started
... "dsin.txt" loading (dymosim input file)
The following error was detected at time: 0
Error in line 33 when reading double value from file "C:/Users/jnangle/Desktop/Dymola/Libraries/ExternDatamaster/ExternData/Resources/Examples/PVWattsv5.xml"

The stack of functions is:
ExternData.Functions.XML.Internal.getRealArray1D
xmlfile.getRealArray1D_Unique'"xmlfile"'
xmlfile.getRealArray1D_Unique'"xmlfile"'(
"outputs.ac-monthly", 
12, 
xmlfile.xml)
Error: Failed to start model.

The XML data I'm using is:

<response>
   <inputs>...</inputs>
   <errors type="array"/>
   <warnings type="array"/>
   <version>1.2.0</version>
   <ssc-info>...</ssc-info>
   <station-info>
      <lat type="float">40.016666412353516</lat>
      <lon type="float">-105.25</lon>
      <elev type="float">1634.0</elev>
      <tz type="float">-7.0</tz>
      <location>94018</location>
      <city>BOULDER</city>
      <state>CO</state>
      <solar-resource-file>94018.tm2</solar-resource-file>
      <distance type="integer">38036</distance>
   </station-info>
   <outputs>
      <ac-monthly type="array">
      <ac-monthly type="float">9707.1943359375</ac-monthly>
      <ac-monthly type="float">10191.212890625</ac-monthly>
      <ac-monthly type="float">14889.302734375</ac-monthly>
      <ac-monthly type="float">15481.29296875</ac-monthly>
      <ac-monthly type="float">16531.017578125</ac-monthly>
      <ac-monthly type="float">16387.6953125</ac-monthly>
      <ac-monthly type="float">16299.7412109375</ac-monthly>
      <ac-monthly type="float">15908.3876953125</ac-monthly>
      <ac-monthly type="float">14523.0478515625</ac-monthly>
      <ac-monthly type="float">12817.548828125</ac-monthly>
      <ac-monthly type="float">9812.5400390625</ac-monthly>
      <ac-monthly type="float">9209.373046875</ac-monthly>
   </ac-monthly>
   </outputs>
</response>

Apologies if this is a simple fix. I'm new to Modelica, but I have experience in Java and VBA.

Thank you in advance for your help.

tbeu commented 9 years ago

The array given in PVWattsv5.xml does not work with the library since the format is not recognized. If you change it to

<response>
   <inputs>...</inputs>
   <errors type="array"/>
   <warnings type="array"/>
   <version>1.2.0</version>
   <ssc-info>...</ssc-info>
   <station-info>
      <lat type="float">40.016666412353516</lat>
      <lon type="float">-105.25</lon>
      <elev type="float">1634.0</elev>
      <tz type="float">-7.0</tz>
      <location>94018</location>
      <city>BOULDER</city>
      <state>CO</state>
      <solar-resource-file>94018.tm2</solar-resource-file>
      <distance type="integer">38036</distance>
   </station-info>
   <outputs>
      <ac-monthly type="float">[9707.1943359375, 10191.212890625, 14889.302734375, 15481.29296875, 16531.017578125, 16387.6953125, 16299.7412109375, 15908.3876953125, 14523.0478515625, 12817.548828125, 9812.5400390625, 9209.373046875]
      </ac-monthly>
   </outputs>
</response>

it works as expected.

tbeu commented 9 years ago

This is now fixed in v1.0.4, i.e. both xmlfile.getRealArray1D("outputs.ac-monthly",12) and xmlfile.getRealArray1D("outputs.ac-monthly.ac-monthly",12) work on your original XML file PVWattsv5.xml.