0

I want to use the EPlusWth.dll provided by EnergyPlus, a building energy simulation engine, within Python (EnergyPlus download here, file is located in the preprocess folder). The goal is to convert a multitude of weather files to the EnergyPlus Weather File format. The functions of the DLL are described here.

For the setup of the code, I followed paxdiablo's answer to this question.

My code looks like this:

import ctypes
from ctypes import byref, c_bool, c_char_p, c_long

conversionDLL = ctypes.WinDLL('C:/[...]/EPlusWth.dll')
conversionPrototype = ctypes.WINFUNCTYPE(
    c_bool,                    # Output type.
    c_char_p, c_long,          # Input file type and string length.
    c_char_p, c_long,          # Output file type and string length.
    c_char_p, c_long,          # Input file name and string length.
    c_char_p, c_long,          # Output file name and string length.
    c_bool,                    # Output variable ErrorFlag to use. Positive, if an error occurred.
)

conversionApiParams = (
    (1, 'strInType ', 'CUSTOM'), (1, 'InTypeLen', 0),
    (1, 'strOutType', 'EPW'), (1, 'OutTypeLen', 3),
    (1, 'strInFileName', ''), (1, 'InFileNameLen', 0),
    (1, 'strOutFileName', ''), (1, 'OutFileNameLen', 0),
    (1, 'ErrorFlag ', False),
)

conversionApi = conversionPrototype(('ProcessWeather', conversionDLL), conversionApiParams)

# value = 'yes'
# EPlusWth.SetFixOutOfRangeData(value, len(value))

inputFileType =  b'CUSTOM'
outputFileType = b'EPW'
inputFileName =  b'C:/[...]/BAS_2035_RCP85_DRY.csv'
outputFileName = b'C:/[...]/BAS_2035_RCP85_DRY.epw'
errorFlag = c_bool(False)

strInType =         c_char_p(inputFileType)
strOutType =        c_char_p(outputFileType)
strInFileName =     c_char_p(inputFileName)
strOutFileName =    c_char_p(outputFileName)
InTypeLen =         c_long(len(inputFileType))
OutTypeLen =        c_long(len(outputFileType))
InFileNameLen =     c_long(len(inputFileName))
OutFileNameLen =    c_long(len(outputFileName))

conversionApi(
    byref(strInType),      byref(InTypeLen),
    byref(strOutType),     byref(OutTypeLen),
    byref(strInFileName),  byref(InFileNameLen),
    byref(strOutFileName), byref(OutFileNameLen),
    # byref(errorFlag),
    errorFlag,
)

It works (within Python 3.10 32 bit) up to the actual function call. I get this error:

Traceback (most recent call last):
  File "C:\[...]\WeatherFilesToEPW.py", line 42, in <module>
    conversionApi(
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

Without byref(), the error is:

OSError: exception: access violation writing 0x00000000

Without knowing the difference, I've used c_wchar_p before instead of c_char_p. Didn't work, either.

Thanks for your help.

I tried using a DLL to convert custom weather files to the EnergyPlus EPW format. I was expecting a converted output file and an error flag.

Degux
  • 33
  • 5
  • [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/minimal-reproducible-example). Add the exception traceback. – CristiFati Mar 15 '23 at 09:48
  • Also, check [\[SO\]: Discover missing module using command-line ("DLL load failed" error) (@CristiFati's answer)](https://stackoverflow.com/a/74883130/4788546). Using one of the tools there, check the *.dll* for exported functions. If *ProcessWeather* is not present (I suspect that would be the case as the *.dll* is written in *.NET*), this approach is wrong. Anyway, is that *.dll* available somewhere? Or any official documentation (as the one from the *URL* in the question is kind of lame)? – CristiFati Mar 16 '23 at 09:08
  • Thanks. I added the traceback in the question. The DLL doesn't seem to be available as separate download, but is part of the free [EnergyPlus installation](https://energyplus.net/downloads). I downloaded _Dependencies_ and had a look at the DLL: `ProcessWeather` is the second of a total of six exports of the module. The others being `EndListProcessing`, `SetDefaultChgLimit`, `SetFixOutOfRangeData`, `SetupPWInternalDataPath` and `StartListProcessing`. – Degux Mar 20 '23 at 06:35
  • Can I send you the DLL somehow, @CristiFati? – Degux Mar 23 '23 at 06:34
  • Send it (although it may depend on others), I'm not sure what channel to use. Aren't there some header files (*.h*, *.hpp*, ...) containing the *C* function declarations? – CristiFati Mar 23 '23 at 06:49
  • You can download the entire WeatherConverter folder via wetransfer with the code "t-NHJhP1oIcq" (active until 18th April 2023). Append the code to "https://we.tl/" - I couldn't send it as one link as it get's detected as URL shortener. There's an EXE that also uses the DLL, but that's for the conversion of single files. I don't see any .h or .hpp files. – Degux Apr 11 '23 at 05:59

0 Answers0