Skip to content

Commit 3e718f4

Browse files
committed
add text type
1 parent 0727021 commit 3e718f4

5 files changed

Lines changed: 89 additions & 1 deletion

File tree

src/hecdss/download_hecdss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def download_and_unzip(url, zip_file, destination_dir):
4545
logger.error(error_msg)
4646

4747
base_url = "https://www.hec.usace.army.mil/nexus/repository/maven-public/mil/army/usace/hec/hecdss/"
48-
version = "7-IU-16"
48+
version = "7-IW-3"
4949

5050
destination_dir = Path(__file__).parent.joinpath("lib")
5151
zip_url = f"{base_url}{version}-win-x86_64/hecdss-{version}-win-x86_64.zip"

src/hecdss/hecdss.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import hecdss.record_type
99
from hecdss.array_container import ArrayContainer
1010
from hecdss.location_info import LocationInfo
11+
from hecdss.text import Text
1112
from hecdss.paired_data import PairedData
1213
from hecdss.native import _Native
1314
from hecdss.dateconverter import DateConverter
@@ -179,8 +180,32 @@ def get(self, pathname: str, startdatetime=None, enddatetime=None, trim=False):
179180
return self._get_array(pathname)
180181
elif type == RecordType.LocationInfo:
181182
return self._get_location_info(pathname)
183+
elif type == RecordType.Text:
184+
return self._get_text(pathname)
182185
return None
183186

187+
def _get_text(self, pathname: str):
188+
textLength = 1024
189+
190+
191+
BUFFER_TOO_SMALL = -1
192+
textArray = []
193+
status = self._native.hec_dss_textRetrieve(pathname, textArray, textLength)
194+
while status == BUFFER_TOO_SMALL:
195+
textLength *= 2
196+
status = self._native.hec_dss_textRetrieve(pathname, textArray, textLength)
197+
if textLength > 2*1048576: # 2 MB
198+
logger.error("Text record too large to read from '%s'", pathname)
199+
return None
200+
201+
if status != 0:
202+
logger.error("Error reading text from '%s'", pathname)
203+
return None
204+
text = Text()
205+
text.id = pathname
206+
text.text = textArray[0]
207+
return text
208+
184209
def _get_array(self, pathname: str):
185210
intValuesCount = [0]
186211
floatValuesCount = [0]
@@ -682,6 +707,10 @@ def put(self, container) -> int:
682707
elif type(container) is LocationInfo:
683708
status = self._native.hec_dss_locationStore(container,1)
684709
self._catalog = None
710+
elif type(container) is Text:
711+
text = container
712+
status = self._native.hec_dss_textStore(text.id, text.text, len(text.text))
713+
self._catalog = None
685714
else:
686715
raise NotImplementedError(f"unsupported record_type: {type(container)}. Expected types are: {RecordType.SUPPORTED_RECORD_TYPES.value}")
687716

@@ -812,6 +841,8 @@ def set_debug_level(self, level) -> int:
812841
int: _description_
813842
"""
814843
return self._native.hec_dss_set_debug_level(level)
844+
845+
def
815846

816847

817848
if __name__ == "__main__":

src/hecdss/native.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,3 +1223,51 @@ def hec_dss_delete(self, pathname: str) -> int:
12231223
logger.error("Function call failed with result: %s", result)
12241224

12251225
return result
1226+
1227+
def hec_dss_textStore(self, pathname, text, length=None):
1228+
"""
1229+
Store text data in a DSS file.
1230+
Args:
1231+
pathname (str): The DSS pathname where the text will be stored.
1232+
text (str): The text data to store.
1233+
length (int, optional): The length of the text. If None, it will be set to the length of the text.
1234+
"""
1235+
f = self.dll.hec_dss_textStore
1236+
f.argtypes = [
1237+
c_void_p, # dss
1238+
c_char_p, # pathname
1239+
c_char_p, # text
1240+
c_int # length
1241+
]
1242+
f.restype = c_int
1243+
1244+
result = f(self.handle, pathname.encode("utf-8"),
1245+
text.encode("utf-8"),
1246+
length if length is not None else len(text))
1247+
1248+
return result
1249+
1250+
def hec_dss_textRetrieve(self, pathname, buffer :List[str], buff_size: int) -> int:
1251+
"""
1252+
Store text data in a DSS file.
1253+
Args:
1254+
pathname (str): The DSS pathname where the text will be stored.
1255+
text (str): The text data to store.
1256+
length (int, optional): The length of the text. If None, it will be set to the length of the text.
1257+
"""
1258+
f = self.dll.hec_dss_textRetrieve
1259+
f.argtypes = [
1260+
c_void_p, # dss
1261+
c_char_p, # pathname
1262+
c_char_p, # buffer
1263+
c_int # buff_size
1264+
]
1265+
f.restype = c_int
1266+
1267+
c_buffer = create_string_buffer(buff_size)
1268+
result = f(self.handle, pathname.encode("utf-8"),
1269+
c_buffer,
1270+
buff_size)
1271+
1272+
buffer[0] = c_buffer.value.decode("utf-8")
1273+
return result

src/hecdss/record_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class RecordType(Enum):
2323
"PairedData",
2424
"GriddedData",
2525
"ArrayContainer",
26+
"Text"
2627
]
2728

2829
@staticmethod

src/hecdss/text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class Text:
3+
def __init__(self):
4+
"""
5+
Initialize a Text object with default values.
6+
"""
7+
self.id = None
8+
self.text = ""

0 commit comments

Comments
 (0)