# Scan a grid around each sample in a grid¶

## Problem¶

Examples are arranged on a substrate. There are two motors, x and y, for moving a detector over the subtrate. Scan a grid of readings around the center position of each sample.

## Approach¶

Specify the samples and their arrangement as a mapping of sample names to (x, y) positions, like `{'A': (1, 1), 'B': (1, 2)}`. Write a custom plan that loops through the samples. For each sample, move to sample’s center position and perform a `bluesky.plans.relative_outer_product_scan()` (i.e., grid scan) around that position. For each sample, one run will be saved. Include the sample name in the metadata.

## Example Solution¶

Out:

```+-----------+------------+------------+------------+-----------------+------------+-----------------+
|   seq_num |       time |       det4 |     motor1 | motor1_setpoint |     motor2 | motor2_setpoint |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|         1 | 21:00:46.7 |      0.527 |      0.800 |           0.800 |      0.800 |           0.800 |
|         2 | 21:00:46.8 |      0.484 |      0.800 |           0.800 |      0.900 |           0.900 |
|         3 | 21:00:46.8 |      0.440 |      0.800 |           0.800 |      1.000 |           1.000 |
|         4 | 21:00:46.8 |      0.397 |      0.800 |           0.800 |      1.100 |           1.100 |
|         5 | 21:00:46.9 |      0.353 |      0.800 |           0.800 |      1.200 |           1.200 |
|         6 | 21:00:46.9 |      0.325 |      0.900 |           0.900 |      1.200 |           1.200 |
|         7 | 21:00:46.9 |      0.364 |      0.900 |           0.900 |      1.100 |           1.100 |
|         8 | 21:00:47.0 |      0.405 |      0.900 |           0.900 |      1.000 |           1.000 |
|         9 | 21:00:47.0 |      0.445 |      0.900 |           0.900 |      0.900 |           0.900 |
|        10 | 21:00:47.0 |      0.484 |      0.900 |           0.900 |      0.800 |           0.800 |
|        11 | 21:00:47.1 |      0.440 |      1.000 |           1.000 |      0.800 |           0.800 |
|        12 | 21:00:47.1 |      0.405 |      1.000 |           1.000 |      0.900 |           0.900 |
|        13 | 21:00:47.1 |      0.368 |      1.000 |           1.000 |      1.000 |           1.000 |
|        14 | 21:00:47.2 |      0.331 |      1.000 |           1.000 |      1.100 |           1.100 |
|        15 | 21:00:47.2 |      0.295 |      1.000 |           1.000 |      1.200 |           1.200 |
|        16 | 21:00:47.2 |      0.266 |      1.100 |           1.100 |      1.200 |           1.200 |
|        17 | 21:00:47.3 |      0.298 |      1.100 |           1.100 |      1.100 |           1.100 |
|        18 | 21:00:47.3 |      0.331 |      1.100 |           1.100 |      1.000 |           1.000 |
|        19 | 21:00:47.4 |      0.364 |      1.100 |           1.100 |      0.900 |           0.900 |
|        20 | 21:00:47.4 |      0.397 |      1.100 |           1.100 |      0.800 |           0.800 |
|        21 | 21:00:47.4 |      0.353 |      1.200 |           1.200 |      0.800 |           0.800 |
|        22 | 21:00:47.5 |      0.325 |      1.200 |           1.200 |      0.900 |           0.900 |
|        23 | 21:00:47.5 |      0.295 |      1.200 |           1.200 |      1.000 |           1.000 |
|        24 | 21:00:47.5 |      0.266 |      1.200 |           1.200 |      1.100 |           1.100 |
|        25 | 21:00:47.6 |      0.237 |      1.200 |           1.200 |      1.200 |           1.200 |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
generator rel_grid_scan ['ed3e7375'] (scan num: 1)
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|   seq_num |       time |       det4 |     motor1 | motor1_setpoint |     motor2 | motor2_setpoint |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|         1 | 21:00:47.7 |      0.144 |      0.800 |           0.800 |      1.800 |           1.800 |
|         2 | 21:00:47.8 |      0.119 |      0.800 |           0.800 |      1.900 |           1.900 |
|         3 | 21:00:47.8 |      0.098 |      0.800 |           0.800 |      2.000 |           2.000 |
|         4 | 21:00:47.9 |      0.080 |      0.800 |           0.800 |      2.100 |           2.100 |
|         5 | 21:00:47.9 |      0.065 |      0.800 |           0.800 |      2.200 |           2.200 |
|         6 | 21:00:47.9 |      0.059 |      0.900 |           0.900 |      2.200 |           2.200 |
|         7 | 21:00:48.0 |      0.074 |      0.900 |           0.900 |      2.100 |           2.100 |
|         8 | 21:00:48.0 |      0.090 |      0.900 |           0.900 |      2.000 |           2.000 |
|         9 | 21:00:48.0 |      0.110 |      0.900 |           0.900 |      1.900 |           1.900 |
|        10 | 21:00:48.1 |      0.132 |      0.900 |           0.900 |      1.800 |           1.800 |
|        11 | 21:00:48.1 |      0.120 |      1.000 |           1.000 |      1.800 |           1.800 |
|        12 | 21:00:48.1 |      0.100 |      1.000 |           1.000 |      1.900 |           1.900 |
|        13 | 21:00:48.2 |      0.082 |      1.000 |           1.000 |      2.000 |           2.000 |
|        14 | 21:00:48.2 |      0.067 |      1.000 |           1.000 |      2.100 |           2.100 |
|        15 | 21:00:48.3 |      0.054 |      1.000 |           1.000 |      2.200 |           2.200 |
|        16 | 21:00:48.3 |      0.049 |      1.100 |           1.100 |      2.200 |           2.200 |
|        17 | 21:00:48.3 |      0.060 |      1.100 |           1.100 |      2.100 |           2.100 |
|        18 | 21:00:48.4 |      0.074 |      1.100 |           1.100 |      2.000 |           2.000 |
|        19 | 21:00:48.4 |      0.090 |      1.100 |           1.100 |      1.900 |           1.900 |
|        20 | 21:00:48.5 |      0.108 |      1.100 |           1.100 |      1.800 |           1.800 |
|        21 | 21:00:48.5 |      0.096 |      1.200 |           1.200 |      1.800 |           1.800 |
|        22 | 21:00:48.5 |      0.080 |      1.200 |           1.200 |      1.900 |           1.900 |
|        23 | 21:00:48.6 |      0.066 |      1.200 |           1.200 |      2.000 |           2.000 |
|        24 | 21:00:48.6 |      0.054 |      1.200 |           1.200 |      2.100 |           2.100 |
|        25 | 21:00:48.6 |      0.043 |      1.200 |           1.200 |      2.200 |           2.200 |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
generator rel_grid_scan ['f35b0a7d'] (scan num: 2)
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|   seq_num |       time |       det4 |     motor1 | motor1_setpoint |     motor2 | motor2_setpoint |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|         1 | 21:00:48.8 |      0.014 |      0.800 |           0.800 |      2.800 |           2.800 |
|         2 | 21:00:48.8 |      0.011 |      0.800 |           0.800 |      2.900 |           2.900 |
|         3 | 21:00:48.8 |      0.008 |      0.800 |           0.800 |      3.000 |           3.000 |
|         4 | 21:00:48.9 |      0.006 |      0.800 |           0.800 |      3.100 |           3.100 |
|         5 | 21:00:48.9 |      0.004 |      0.800 |           0.800 |      3.200 |           3.200 |
|         6 | 21:00:48.9 |      0.004 |      0.900 |           0.900 |      3.200 |           3.200 |
|         7 | 21:00:49.0 |      0.005 |      0.900 |           0.900 |      3.100 |           3.100 |
|         8 | 21:00:49.0 |      0.007 |      0.900 |           0.900 |      3.000 |           3.000 |
|         9 | 21:00:49.0 |      0.010 |      0.900 |           0.900 |      2.900 |           2.900 |
|        10 | 21:00:49.0 |      0.013 |      0.900 |           0.900 |      2.800 |           2.800 |
|        11 | 21:00:49.1 |      0.012 |      1.000 |           1.000 |      2.800 |           2.800 |
|        12 | 21:00:49.1 |      0.009 |      1.000 |           1.000 |      2.900 |           2.900 |
|        13 | 21:00:49.1 |      0.007 |      1.000 |           1.000 |      3.000 |           3.000 |
|        14 | 21:00:49.2 |      0.005 |      1.000 |           1.000 |      3.100 |           3.100 |
|        15 | 21:00:49.2 |      0.004 |      1.000 |           1.000 |      3.200 |           3.200 |
|        16 | 21:00:49.2 |      0.003 |      1.100 |           1.100 |      3.200 |           3.200 |
|        17 | 21:00:49.3 |      0.004 |      1.100 |           1.100 |      3.100 |           3.100 |
|        18 | 21:00:49.3 |      0.006 |      1.100 |           1.100 |      3.000 |           3.000 |
|        19 | 21:00:49.3 |      0.008 |      1.100 |           1.100 |      2.900 |           2.900 |
|        20 | 21:00:49.4 |      0.011 |      1.100 |           1.100 |      2.800 |           2.800 |
|        21 | 21:00:49.4 |      0.010 |      1.200 |           1.200 |      2.800 |           2.800 |
|        22 | 21:00:49.4 |      0.007 |      1.200 |           1.200 |      2.900 |           2.900 |
|        23 | 21:00:49.5 |      0.005 |      1.200 |           1.200 |      3.000 |           3.000 |
|        24 | 21:00:49.5 |      0.004 |      1.200 |           1.200 |      3.100 |           3.100 |
|        25 | 21:00:49.5 |      0.003 |      1.200 |           1.200 |      3.200 |           3.200 |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
generator rel_grid_scan ['81bb23f1'] (scan num: 3)
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|   seq_num |       time |       det4 |     motor1 | motor1_setpoint |     motor2 | motor2_setpoint |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|         1 | 21:00:49.7 |      0.144 |      1.800 |           1.800 |      0.800 |           0.800 |
|         2 | 21:00:49.7 |      0.132 |      1.800 |           1.800 |      0.900 |           0.900 |
|         3 | 21:00:49.7 |      0.120 |      1.800 |           1.800 |      1.000 |           1.000 |
|         4 | 21:00:49.8 |      0.108 |      1.800 |           1.800 |      1.100 |           1.100 |
|         5 | 21:00:49.8 |      0.096 |      1.800 |           1.800 |      1.200 |           1.200 |
|         6 | 21:00:49.8 |      0.080 |      1.900 |           1.900 |      1.200 |           1.200 |
|         7 | 21:00:49.9 |      0.090 |      1.900 |           1.900 |      1.100 |           1.100 |
|         8 | 21:00:49.9 |      0.100 |      1.900 |           1.900 |      1.000 |           1.000 |
|         9 | 21:00:49.9 |      0.110 |      1.900 |           1.900 |      0.900 |           0.900 |
|        10 | 21:00:50.0 |      0.119 |      1.900 |           1.900 |      0.800 |           0.800 |
|        11 | 21:00:50.0 |      0.098 |      2.000 |           2.000 |      0.800 |           0.800 |
|        12 | 21:00:50.0 |      0.090 |      2.000 |           2.000 |      0.900 |           0.900 |
|        13 | 21:00:50.1 |      0.082 |      2.000 |           2.000 |      1.000 |           1.000 |
|        14 | 21:00:50.1 |      0.074 |      2.000 |           2.000 |      1.100 |           1.100 |
|        15 | 21:00:50.1 |      0.066 |      2.000 |           2.000 |      1.200 |           1.200 |
|        16 | 21:00:50.2 |      0.054 |      2.100 |           2.100 |      1.200 |           1.200 |
|        17 | 21:00:50.2 |      0.060 |      2.100 |           2.100 |      1.100 |           1.100 |
|        18 | 21:00:50.2 |      0.067 |      2.100 |           2.100 |      1.000 |           1.000 |
|        19 | 21:00:50.3 |      0.074 |      2.100 |           2.100 |      0.900 |           0.900 |
|        20 | 21:00:50.3 |      0.080 |      2.100 |           2.100 |      0.800 |           0.800 |
|        21 | 21:00:50.4 |      0.065 |      2.200 |           2.200 |      0.800 |           0.800 |
|        22 | 21:00:50.4 |      0.059 |      2.200 |           2.200 |      0.900 |           0.900 |
|        23 | 21:00:50.4 |      0.054 |      2.200 |           2.200 |      1.000 |           1.000 |
|        24 | 21:00:50.5 |      0.049 |      2.200 |           2.200 |      1.100 |           1.100 |
|        25 | 21:00:50.5 |      0.043 |      2.200 |           2.200 |      1.200 |           1.200 |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
generator rel_grid_scan ['f30d034d'] (scan num: 4)
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|   seq_num |       time |       det4 |     motor1 | motor1_setpoint |     motor2 | motor2_setpoint |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|         1 | 21:00:50.6 |      0.039 |      1.800 |           1.800 |      1.800 |           1.800 |
|         2 | 21:00:50.7 |      0.033 |      1.800 |           1.800 |      1.900 |           1.900 |
|         3 | 21:00:50.7 |      0.027 |      1.800 |           1.800 |      2.000 |           2.000 |
|         4 | 21:00:50.8 |      0.022 |      1.800 |           1.800 |      2.100 |           2.100 |
|         5 | 21:00:50.8 |      0.018 |      1.800 |           1.800 |      2.200 |           2.200 |
|         6 | 21:00:50.8 |      0.015 |      1.900 |           1.900 |      2.200 |           2.200 |
|         7 | 21:00:50.9 |      0.018 |      1.900 |           1.900 |      2.100 |           2.100 |
|         8 | 21:00:50.9 |      0.022 |      1.900 |           1.900 |      2.000 |           2.000 |
|         9 | 21:00:51.0 |      0.027 |      1.900 |           1.900 |      1.900 |           1.900 |
|        10 | 21:00:51.0 |      0.033 |      1.900 |           1.900 |      1.800 |           1.800 |
|        11 | 21:00:51.0 |      0.027 |      2.000 |           2.000 |      1.800 |           1.800 |
|        12 | 21:00:51.1 |      0.022 |      2.000 |           2.000 |      1.900 |           1.900 |
|        13 | 21:00:51.1 |      0.018 |      2.000 |           2.000 |      2.000 |           2.000 |
|        14 | 21:00:51.2 |      0.015 |      2.000 |           2.000 |      2.100 |           2.100 |
|        15 | 21:00:51.2 |      0.012 |      2.000 |           2.000 |      2.200 |           2.200 |
|        16 | 21:00:51.2 |      0.010 |      2.100 |           2.100 |      2.200 |           2.200 |
|        17 | 21:00:51.3 |      0.012 |      2.100 |           2.100 |      2.100 |           2.100 |
|        18 | 21:00:51.3 |      0.015 |      2.100 |           2.100 |      2.000 |           2.000 |
|        19 | 21:00:51.3 |      0.018 |      2.100 |           2.100 |      1.900 |           1.900 |
|        20 | 21:00:51.4 |      0.022 |      2.100 |           2.100 |      1.800 |           1.800 |
|        21 | 21:00:51.4 |      0.018 |      2.200 |           2.200 |      1.800 |           1.800 |
|        22 | 21:00:51.5 |      0.015 |      2.200 |           2.200 |      1.900 |           1.900 |
|        23 | 21:00:51.5 |      0.012 |      2.200 |           2.200 |      2.000 |           2.000 |
|        24 | 21:00:51.5 |      0.010 |      2.200 |           2.200 |      2.100 |           2.100 |
|        25 | 21:00:51.6 |      0.008 |      2.200 |           2.200 |      2.200 |           2.200 |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
generator rel_grid_scan ['13a42d3f'] (scan num: 5)
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|   seq_num |       time |       det4 |     motor1 | motor1_setpoint |     motor2 | motor2_setpoint |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
|         1 | 21:00:51.7 |      0.004 |      1.800 |           1.800 |      2.800 |           2.800 |
|         2 | 21:00:51.8 |      0.003 |      1.800 |           1.800 |      2.900 |           2.900 |
|         3 | 21:00:51.8 |      0.002 |      1.800 |           1.800 |      3.000 |           3.000 |
|         4 | 21:00:51.9 |      0.002 |      1.800 |           1.800 |      3.100 |           3.100 |
|         5 | 21:00:51.9 |      0.001 |      1.800 |           1.800 |      3.200 |           3.200 |
|         6 | 21:00:51.9 |      0.001 |      1.900 |           1.900 |      3.200 |           3.200 |
|         7 | 21:00:52.0 |      0.001 |      1.900 |           1.900 |      3.100 |           3.100 |
|         8 | 21:00:52.0 |      0.002 |      1.900 |           1.900 |      3.000 |           3.000 |
|         9 | 21:00:52.0 |      0.002 |      1.900 |           1.900 |      2.900 |           2.900 |
|        10 | 21:00:52.1 |      0.003 |      1.900 |           1.900 |      2.800 |           2.800 |
|        11 | 21:00:52.1 |      0.003 |      2.000 |           2.000 |      2.800 |           2.800 |
|        12 | 21:00:52.2 |      0.002 |      2.000 |           2.000 |      2.900 |           2.900 |
|        13 | 21:00:52.2 |      0.002 |      2.000 |           2.000 |      3.000 |           3.000 |
|        14 | 21:00:52.3 |      0.001 |      2.000 |           2.000 |      3.100 |           3.100 |
|        15 | 21:00:52.3 |      0.001 |      2.000 |           2.000 |      3.200 |           3.200 |
|        16 | 21:00:52.3 |      0.001 |      2.100 |           2.100 |      3.200 |           3.200 |
|        17 | 21:00:52.4 |      0.001 |      2.100 |           2.100 |      3.100 |           3.100 |
|        18 | 21:00:52.4 |      0.001 |      2.100 |           2.100 |      3.000 |           3.000 |
|        19 | 21:00:52.5 |      0.002 |      2.100 |           2.100 |      2.900 |           2.900 |
|        20 | 21:00:52.5 |      0.002 |      2.100 |           2.100 |      2.800 |           2.800 |
|        21 | 21:00:52.6 |      0.002 |      2.200 |           2.200 |      2.800 |           2.800 |
|        22 | 21:00:52.6 |      0.001 |      2.200 |           2.200 |      2.900 |           2.900 |
|        23 | 21:00:52.6 |      0.001 |      2.200 |           2.200 |      3.000 |           3.000 |
|        24 | 21:00:52.7 |      0.001 |      2.200 |           2.200 |      3.100 |           3.100 |
|        25 | 21:00:52.7 |      0.001 |      2.200 |           2.200 |      3.200 |           3.200 |
+-----------+------------+------------+------------+-----------------+------------+-----------------+
generator rel_grid_scan ['1129ae81'] (scan num: 6)
```

```from bluesky.plans import relative_outer_product_scan
from bluesky.plan_stubs import abs_set, wait
from bluesky.preprocessors import (run_decorator, stage_decorator,
subs_decorator)
from bluesky.callbacks import LiveTable, LivePlot
from ophyd.sim import det4, motor1, motor2
from bluesky import RunEngine

def grid_in_grid(samples):
"""
Scan a grid around the neighborhood of each sample.

Parameters
----------
sample : dict
mapping each sample's name to its (x, y) position
"""

# In this example we hard-code the hardware and other parameters. For more
# flexibility, they could instead be parameters to the function.
detector = det4
x = motor1
y = motor2
x_range = y_range = 0.2
x_num = y_num = 5

@subs_decorator([LiveTable([detector, x, y]),
LivePlot('motor2', 'motor1')])
def plan():
for name, position in samples.items():
# Prepare metadata.
md = {'sample': name}

# Move to the cetner of the sample position.
x_pos, y_pos = position
yield from abs_set(x, x_pos)
yield from abs_set(y, y_pos)
yield from wait()

# Scan a grid around that position.
yield from relative_outer_product_scan([detector],
x, -x_range, x_range, x_num,
y, -y_range, y_range, y_num,
True, md=md)

yield from plan()

# Example usage:

RE = RunEngine({})

samples = {'A': (1, 1),
'B': (1, 2),
'C': (1, 3),
'D': (2, 1),
'E': (2, 2),
'F': (2, 3)}

RE(grid_in_grid(samples))
```

Total running time of the script: ( 0 minutes 6.399 seconds)

Gallery generated by Sphinx-Gallery