dfujim / mudpy

MUon Data file IO
GNU General Public License v3.0
4 stars 3 forks source link

Handle varied element sizes for int/real ivar arrays #7

Closed aPeter1 closed 2 years ago

aPeter1 commented 2 years ago

Hello! I was creating my own mudpy package when I found out one was already made 😂 ! Anyways, I saw when testing with this file that the function for getting ivar array data doesn't support formats outside of a regular int (in the case of this file the type was real and the element size was 4). This addition will support that, but let me know if you think it could be improved. Thanks!

Also, I only went up to int32 because looking at MUD_SEC_GEN_HIST_dopack, it appears like that is as far as it goes.

Tested with this:

from mudpy import mud_friendly

file_path = "your file"
fh = mud_friendly.open_readwrite(file_path)
i = 1

# Setting and Getting Int8
mud_friendly.set_ivar_n_data(fh, i, 5)
mud_friendly.set_ivar_element_size(fh, i, 1)
mud_friendly.set_ivar_data_type(fh, i, 1)
mud_friendly.set_ivar_data(fh, i, [0, 2, 3, 5, 255])

print(mud_friendly.get_ivar_data(fh, i))

# Setting and Getting Int16
mud_friendly.set_ivar_n_data(fh, i, 5)
mud_friendly.set_ivar_element_size(fh, i, 2)
mud_friendly.set_ivar_data_type(fh, i, 1)
mud_friendly.set_ivar_data(fh, i, [0, 2, 3, 32768, 32767])

print(mud_friendly.get_ivar_data(fh, i))

# Setting and Getting Int32
mud_friendly.set_ivar_n_data(fh, i, 5)
mud_friendly.set_ivar_element_size(fh, i, 4)
mud_friendly.set_ivar_data_type(fh, i, 1)
mud_friendly.set_ivar_data(fh, i, [0, 5, 90, 2147483648, 2147483647])

print(mud_friendly.get_ivar_data(fh, i))

# Setting and Getting Float32
mud_friendly.set_ivar_n_data(fh, i, 5)
mud_friendly.set_ivar_element_size(fh, i, 4)
mud_friendly.set_ivar_data_type(fh, i, 2)
mud_friendly.set_ivar_data(fh, i, [1.2, 2.8, 3.6, -3.4028235e+38, 3.4028235e+38])

print(mud_friendly.get_ivar_data(fh, i))

# Setting and Getting Float64
mud_friendly.set_ivar_n_data(fh, i, 5)
mud_friendly.set_ivar_element_size(fh, i, 8)
mud_friendly.set_ivar_data_type(fh, i, 2)
mud_friendly.set_ivar_data(fh, i, [1.23, 2.89, 3.456, -1.79769313e+308, 1.79769313e+308])

print(mud_friendly.get_ivar_data(fh, i))

mud_friendly.close_read(fh)
aPeter1 commented 2 years ago

Just saw my IDE made some whitespace changes (got rid of unnecessary tabs/spaces), hate it when it does that automatically when opening a file. I'll get rid of those if you would prefer.

dfujim commented 2 years ago

Seems to work! I'll merge and update pypi.