# Keysight PNA

Control the Keysight PNA network analyzer from Python with the `KeysightPNA` driver.

**Driver:** [pymeasure](https://pymeasure.readthedocs.io)  
**Type:** [Network analyzers](https://dev.nominal.io/instro/instruments/type/vna.md)  
**Manufacturer:** [Keysight](https://dev.nominal.io/instro/instruments/manufacturer/keysight.md)

A class representing a Keysight PNA vector network analyzer.

## Publishing through instro

instro publishes the commands you send to a KeysightPNA and the values you read from it, through any [publisher](https://dev.nominal.io/instro/library/publishers.md). It takes two forms, both described in the [pymeasure guide](https://dev.nominal.io/instro/library/pymeasure.md). Install with `uv add "instro[pymeasure]"`.

### With a PymeasureInstrument

[`PymeasureInstrument`](https://dev.nominal.io/instro/library/pymeasure.md#write-new-instro-code) wraps the driver in an instro instrument, with tags and background polling. The instrument is created inside the `with` block, and `.device` is the pymeasure instrument.

```python
from pymeasure.instruments.keysight import KeysightPNA
from instro.pymeasure import PymeasureInstrument

with PymeasureInstrument(
    name="pna",
    instrument=KeysightPNA('GPIB0::16::INSTR', timeout=5000),
    dataset_rid="<dataset rid>",
) as pna:

    pna.device.load_state("D:/States/MyState.csa")
    pna.device.adapter.connection.timeout = 20000  # set a new timeout to 20 sec
    pna.device.ch_1.single()  # execute a single trigger of channel 1
    pna.device.complete  # wait till the sweep has finished
    x_data = pna.device.ch_1.tr_1.x_data  # get the X data of trace 1 in channel 1
    y_data = pna.device.ch_2.tr_5.y_data  # get the Y data of trace 5 in channel 2
    y_complex = pna.device.ch_2.tr_5.y_data_complex  # get the complex Y data of trace 5 in channel 2
    pna.device.ch_1.tr_1.mkr_1.enabled = True  # Activate marker 1 on trace 1 in channel 1
    pna.device.ch_1.tr_1.mkr_1.x = 123e6  # Set marker 1 to 123 MHz
    mkr_y = pna.device.ch_1.tr_1.mkr_1.y  # Read the marker 1 y data
```

### With existing pymeasure code (`attach()`)

[`attach()`](https://dev.nominal.io/instro/library/pymeasure.md#publish-from-existing-code) adds publishing to pymeasure code you already have. The imports, the `attach()` call after the instrument is created, and `recorder.close()` are all that instro adds.

```python
from pymeasure.instruments.keysight import KeysightPNA
from instro.lib import NominalCorePublisher
from instro.pymeasure import attach

pna = KeysightPNA("GPIB0::16::INSTR",
                  timeout=5000,
                  )
recorder = attach(pna, "pna", [NominalCorePublisher(dataset_rid="<dataset rid>")])

pna.load_state("D:/States/MyState.csa")
pna.adapter.connection.timeout = 20000  # set a new timeout to 20 sec
pna.ch_1.single()  # execute a single trigger of channel 1
pna.complete  # wait till the sweep has finished
x_data = pna.ch_1.tr_1.x_data  # get the X data of trace 1 in channel 1
y_data = pna.ch_2.tr_5.y_data  # get the Y data of trace 5 in channel 2
y_complex = pna.ch_2.tr_5.y_data_complex  # get the complex Y data of trace 5 in channel 2
pna.ch_1.tr_1.mkr_1.enabled = True  # Activate marker 1 on trace 1 in channel 1
pna.ch_1.tr_1.mkr_1.x = 123e6  # Set marker 1 to 123 MHz
mkr_y = pna.ch_1.tr_1.mkr_1.y  # Read the marker 1 y data

recorder.close()  # flush and close the publishers
```

The examples are adapted from the [pymeasure documentation for `KeysightPNA`](https://pymeasure.readthedocs.io/en/latest/api/instruments/keysight/keysightPNA.html#pymeasure.instruments.keysight.keysightPNA.KeysightPNA), which lists every property and method.
