API download log file and convert | Python
-
Moku:Go
Moku:Go Arbitrary Waveform Generator Moku:Go Data Logger Moku:Go Frequency Response Analyzer Moku:Go Logic Analyzer & Pattern Generator Moku:Go Oscilloscope & Voltmeter Moku:Go PID Controller Moku:Go Spectrum Analyzer Moku:Go Waveform Generator Moku:Go Power Supplies Moku:Go Digital Filter Box Moku:Go FIR Filter Builder Moku:Go Lock-in Amplifier Moku:Go General Moku:Go Logic Analyzer/Pattern Generator Moku:Go Time & Frequency Analyzer Moku:Go Laser Lock Box Moku:Go Phasemeter
-
Moku:Lab
Moku:Lab General Moku:Lab Arbitrary Waveform Generator Moku:Lab Data Logger Moku:Lab Digital Filter Box Moku:Lab FIR Filter Builder Moku:Lab Frequency Response Analyzer Moku:Lab Laser Lock Box Moku:Lab Lock-in Amplifier Moku:Lab Oscilloscope Moku:Lab Phasemeter Moku:Lab PID Controller Moku:Lab Spectrum Analyzer Moku:Lab Waveform Generator Moku:Lab Time & Frequency Analyzer Moku:Lab Logic Analyzer/Pattern Generator
-
Moku:Pro
Moku:Pro Arbitrary Waveform Generator Moku:Pro Data Logger Moku:Pro Frequency Response Analyzer Moku:Pro Oscilloscope Moku:Pro PID Controller Moku:Pro Spectrum Analyzer Moku:Pro Waveform Generator Moku:Pro Lock-in Amplifier Moku:Pro Digital Filter Box Moku:Pro FIR Filter Builder Moku:Pro Phasemeter Moku:Pro Multi-instrument Mode Moku:Pro General Moku:Pro Logic Analyzer/Pattern Generator Moku:Pro Time & Frequency Analyzer
- Python API
- MATLAB API
- Arbitrary Waveform Generator
- Data Logger
- Digital Filter Box
- FIR Filter Builder
- Frequency Response Analyzer
- Laser Lock Box
- Lock-in Amplifier
- Oscilloscope
- Phasemeter
- PID Controller
- Spectrum Analyzer
- Time & Frequency Analyzer
- Waveform Generator
- Logic Analyzer & Pattern Generator
- Multi Instrument Mode
- Moku Cloud Compile
- Moku general
- LabVIEW
Log and download .li file
To stream to a file (log and convert within a single script) instead of logging to a file, please refer to this article:
API streaming and stream_to_file | Python
First, start a logging session and download your file. For more detailed information on the API's please visit our API Reference:
Datalogger | API Reference download_file Datalogger Examples for Python
# moku example: Basic Datalogger
# (c) 2025 Liquid Instruments Pty. Ltd.
import os
import time
from moku.instruments import Datalogger
# Connect to your Moku
i = Datalogger('192.168.###.###', force_connect=False)
try:
# Set-up the Datalogger
i.set_samplerate(100)
i.set_acquisition_mode(mode='Precision')
i.generate_waveform(channel=1, type='Sine', amplitude=1, frequency=10e3)
# Start a logging session
logFile = i.start_logging(duration=10)
# Track progress percentage of the data logging session
complete = False
while complete is False:
time.sleep(0.5)
progress = i.logging_progress()
complete = progress['complete']
if 'time_remaining' in progress:
print(f"Remaining time {progress['time_remaining']} seconds")
# Download the .li log from Moku
i.download(logFile['location'], logFile['file_name'],
os.path.join(os.getcwd(), logFile['file_name']))
print(f"Downloaded log file to local directory. {logFile['file_name']}")
except Exception as e:
print(f'Exception occurred: {e}')
finally:
# Close the connection to the Moku device
i.relinquish_ownership()
Example output:
{'sample_rate': 100.0}
{'mode': 'Precision'}
{'amplitude': 1.0, 'frequency': 10000.0, 'offset': 0.0, 'phase': 0.0, 'type': 'Sine'}
...
Remaining time 0 seconds
Downloaded log file to local directory. MokuDataLoggerData_20250116_102402.li
Mokucli method
Ensure you have mokucli installed, you can download it from the Utilities page.
From your command line, you can convert to your desired file type; csv, npy, mat, or hdf5. We'll be converting to csv for this example. Change directories to where the file is located or use the full file path:
C:\Users\user\Downloads>mokucli convert MokuDataLoggerData_20250116_102402.li --format=csv
[===========================================================================]
Done.
Another method would be to call mokucli from within the script, ideally in this use case, streaming
and stream_to_file
would be used, see this article:
API streaming and stream_to_file | Python
Calling mokucli from within the script is not best practice, but it is available as an option:
import subprocess
sp = subprocess.run(f'mokucli convert --format=csv {logFile['file_name']}', capture_output=True, text=True)
print(sp.stdout)
file_name = logFile['file_name'][:-2] + 'csv'
LI File Converter method
Alternatively, you can convert the file using the LI File Converter GUI, this can be accessed from the Moku: App or downloaded from the Utilities page

You can choose the file type, then drag and drop the file to the converter, or open the file from the converter using “File | Open file” or "Ctrl/Cmd + O"

Loading data from file
You can then use the converted file to load and analyze your data.
import os
import csv
import numpy as np
import matplotlib.pyplot as plt
file_name = "MokuDataLoggerData_20250116_102402.csv"
channels = 2
print(file_name)
assert os.path.isfile(file_name), "Streaming failed, no file received"
data = {'time':[], 'ch1':[]}
with open(file_name, 'r') as f:
load = csv.reader(f)
file = np.array([[float(row[0])] + [float(row[i]) for i in range(1, channels+1)] for row in load if row[0][0] != '%'])
f.close()
data['time'] = file[:,0]
for i in range(1, channels+1):
data[f'ch{i}'] = file[:,i]
keys = data.keys()
for key in keys:
print(f"'{key}': {str(data[key][:3])[:-1]} ... {data[key][-1]}]")
fig = plt.figure(figsize=(10, 3))
for i in range(1, channels+1):
plt.plot(data['time'], data[f'ch{i}'], label=f'Channel {i}')
plt.title("Logged Data")
plt.grid(True)
plt.xlabel("Time (s)")
plt.ylabel("Voltage (V)")
plt.legend(loc=0)
plt.show()
Example output:
MokuDataLoggerData_20250116_102402.csv
'time': [0. 0.01 0.02 ... 9.99]
'ch1': [0.00063297 0.00077466 0.00071688 ... 0.00067479823461]
'ch2': [0.01406568 0.01389566 0.014152 ... 0.014421103911]

Other file type conversions and reading methods for Python
Here is a cheat-sheet style summary of the file type conversions and how to read them in Python, adjust the file_name
and channels
parameters to match your experiment.
.mat
C:\Users\LItes\Downloads>mokucli convert MokuDataLoggerData_20250116_102402.li --format=mat
[===========================================================================]
Done.
import os
from scipy.io import loadmat
file_name = "MokuDataLoggerData_20250116_102402.mat"
channels = 2
print(file_name)
assert os.path.isfile(file_name), "Convert failed, no file received"
data = {'time':[], 'ch1':[]}
file = loadmat(file_name)
data['time'] = file['moku']['data'][0, 0][:,0]
for i in range(1, channels+1):
data[f'ch{i}'] = file['moku']['data'][0, 0][:,i]
keys = data.keys()
for key in keys:
print(f"'{key}': {str(data[key][:3])[:-1]} ... {data[key][-1]}]")
.npy
C:\Users\LItes\Downloads>mokucli convert MokuDataLoggerData_20250116_102402.li --format=npy
[===========================================================================]
Done.
import os
import numpy as np
file_name = "MokuDataLoggerData_20250116_102402.npy"
channels = 2
print(file_name)
assert os.path.isfile(file_name), "Convert failed, no file received"
data = {'time':[], 'ch1':[]}
load = np.load(file_name)
file = np.array([[load[i][0]] + [load[i][j] for j in range(1, channels+1)] for i in range(len(load))])
data['time'] = file[:,0]
for i in range(1, channels+1):
data[f'ch{i}'] = file[:,i]
keys = data.keys()
for key in keys:
print(f"'{key}': {str(data[key][:3])[:-1]} ... {data[key][-1]}]")
.hdf5
C:\Users\LItes\Downloads>mokucli convert MokuDataLoggerData_20250116_102402.li --format=hdf5
[===========================================================================]
Done.
import os
import numpy as np
import h5py
file_name = "MokuDataLoggerData_20250116_102402.hdf5"
channels = 2
print(file_name)
assert os.path.isfile(file_name), "Convert failed, no file received"
data = {'time':[], 'ch1':[]}
load = h5py.File(file_name, 'r')
file = np.array(list(load['moku']),dtype=np.dtype('float'+',float'*channels))
file.dtype.names=['time'] + [f'ch{i}' for i in range(1, channels+1)]
data['time'] = file['time']
for i in range(1, channels+1):
data[f'ch{i}'] = file[f'ch{i}']
keys = data.keys()
for key in keys:
print(f"'{key}': {str(data[key][:3])[:-1]} ... {data[key][-1]}]")