Skip to content

Commit

Permalink
MIES_JSONWaveNotes.ipf: Add read/write support for wave reference waves
Browse files Browse the repository at this point in the history
  • Loading branch information
t-b committed Jan 23, 2024
1 parent cafc1ff commit 7ea6447
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
56 changes: 54 additions & 2 deletions Packages/MIES/MIES_JSONWaveNotes.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ threadsafe Function/WAVE JWN_GetTextWaveFromWaveNote(WAVE wv, string jsonPath)
return textWave
End

/// @brief Return the wave reference wave at jsonPath found in the wave note
///
/// @param wv wave reference where the WaveNote is taken from
/// @param jsonPath path to array
///
/// @returns the wave on success. null wave is returned if it could not be found
threadsafe Function/WAVE JWN_GetWaveRefWaveFromWaveNote(WAVE wv, string jsonPath)

variable jsonID, numRows

ASSERT_TS(WaveExists(wv), "Missing wave")

jsonID = JWN_GetWaveNoteAsJSON(wv)
ASSERT_TS(JSON_GetType(jsonID, jsonPath, ignoreErr = 1) == JSON_ARRAY, "Expected array at jsonPath")
WAVE/Z maxArraySizes = JSON_GetMaxArraySize(jsonID, jsonPath, ignoreErr = 1)
ASSERT_TS(WaveExists(maxArraySizes) && DimSize(maxArraySizes, COLS) <= 1, "Expected only a 1D array at jsonPath")

numRows = maxArraySizes[ROWS]
if(numRows == 0)
return $""
endif

Make/FREE/WAVE/N=(numRows) container = JSON_GetWave(jsonID, jsonPath + "/" + num2str(p), ignoreErr = 1)
JSON_Release(jsonID)

return container
End

/// @brief Return the string value at jsonPath found in the wave note
///
/// @param wv wave reference where the WaveNote is taken from
Expand Down Expand Up @@ -194,13 +222,37 @@ End
/// @param noteWave new wave value
threadsafe Function JWN_SetWaveInWaveNote(WAVE wv, string jsonPath, WAVE noteWave)

variable jsonID
variable jsonID, idx
string jsonPathArray

ASSERT_TS(WaveExists(wv), "Missing wave")
ASSERT_TS(WaveExists(noteWave), "Missing noteWave")

ASSERT_TS(!IsEmpty(jsonPath), "Empty jsonPath")
ASSERT_TS(IsNumericWave(noteWave) || IsTextWave(noteWave), "Only numeric and text waves are supported as JSON wave note entry.")

ASSERT_TS(IsWaveRefWave(noteWave) || IsNumericWave(noteWave) || IsTextWave(noteWave), \
"Only wave references waves, numeric and text waves are supported as JSON wave note entry.")

if(IsWaveRefWave(noteWave))
ASSERT_TS(DimSize(noteWave, COLS) <= 1, "Expected only a 1D wave reference wave")

// create an array at jsonPath with noteWave ROWS entries
jsonID = JWN_GetWaveNoteAsJSON(wv)
Make/FREE/N=(DimSize(noteWave, ROWS)) junk
JSON_SetWave(jsonID, jsonPath, junk)
JWN_WriteWaveNote(wv, JWN_GetWaveNoteHeader(wv), jsonID)

WAVE/WAVE waveRef = noteWave

for(WAVE/Z elem : waveRef)
// and now write at each array index the contained wave
jsonPathArray = jsonPath + "/" + num2str(idx)
JWN_SetWaveInWaveNote(wv, jsonPathArray, elem)
idx += 1
endfor

return NaN
endif

jsonID = JWN_GetWaveNoteAsJSON(wv)
JSON_SetWave(jsonID, jsonPath, noteWave)
Expand Down
26 changes: 26 additions & 0 deletions Packages/tests/Basic/UTF_JSONWaveNotes.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,32 @@ static Function TestSetWaveInJSONWaveNote()
JWN_SetWaveInWaveNote(wv, "waveText", wvDataText)
WAVE/Z data = JWN_GetNumericWaveFromWaveNote(wv, "waveText")
CHECK_WAVE(data, NULL_WAVE)

Make/FREE wvData2 = {1, 2, 3, 4}
Make/FREE/WAVE wvRef = {wvData, wvData2}
// start with a fresh wave note
Note/K wv
JWN_SetWaveInWaveNote(wv, "refWave", wvRef)

WAVE/Z data = JWN_GetNumericWaveFromWaveNote(wv, "refWave/0")
CHECK_EQUAL_WAVES(data, wvData, mode = WAVE_DATA)

WAVE/Z data = JWN_GetNumericWaveFromWaveNote(wv, "refWave/1")
CHECK_EQUAL_WAVES(data, wvData2, mode = WAVE_DATA)

WAVE/WAVE/Z container = JWN_GetWaveRefWaveFromWaveNote(wv, "refWave")
CHECK_EQUAL_VAR(DimSize(container, ROWS), 2)
CHECK_EQUAL_VAR(DimSize(container, COLS), 0)

CHECK_EQUAL_WAVES(wvRef[0], container[0], mode = WAVE_DATA)
CHECK_EQUAL_WAVES(wvRef[1], container[1], mode = WAVE_DATA)

// empty wave ref wave
Note/K wv
Make/FREE/WAVE/N=0 wvRef
JWN_SetWaveInWaveNote(wv, "refWave", wvRef)
WAVE/Z container = JWN_GetWaveRefWaveFromWaveNote(wv, "refWave")
CHECK_WAVE(container, NULL_WAVE)
End

static Function TestJSONWaveNoteCombinations()
Expand Down

0 comments on commit 7ea6447

Please sign in to comment.