Search in sources :

Example 1 with ScanModel

use of org.eclipse.scanning.api.scan.models.ScanModel in project gda-core by openGDA.

the class ScannableNexusWrapperScanTest method checkNexusFile.

private void checkNexusFile(final IRunnableDevice<ScanModel> scanner, int... sizes) throws Exception {
    final ScanModel scanModel = ((AbstractRunnableDevice<ScanModel>) scanner).getModel();
    assertThat(scanner.getDeviceState(), is(DeviceState.ARMED));
    final String filePath = ((AbstractRunnableDevice<ScanModel>) scanner).getModel().getFilePath();
    final NexusFile nf = org.eclipse.dawnsci.nexus.ServiceHolder.getNexusFileFactory().newNexusFile(filePath);
    nf.openToRead();
    final TreeFile nexusTree = NexusUtils.loadNexusTree(nf);
    final NXroot rootNode = (NXroot) nexusTree.getGroupNode();
    final NXentry entry = rootNode.getEntry();
    final NXinstrument instrument = entry.getInstrument();
    final NXsample sample = entry.getSample();
    // check the scan points have been written correctly
    assertDiamondScanGroup(entry, false, false, sizes);
    DataNode dataNode = null;
    IDataset dataset = null;
    int[] shape = null;
    // check metadata scannables
    checkMetadataScannables(scanModel, entry);
    checkAttributeScannable(instrument);
    checkBeamSizeScannable(sample);
    final IPosition pos = scanModel.getPointGenerator().iterator().next();
    final Collection<String> scannableNames = pos.getNames();
    final String dataGroupName = scanModel.getDetectors().get(0).getName();
    final NXdata nxData = entry.getData(dataGroupName);
    assertThat(nxData, is(notNullValue()));
    // Check axes
    final List<String> expectedAxesNames = Stream.concat(scannableNames.stream().map(x -> x + NexusConstants.FIELD_SEPERATOR + FIELD_NAME_VALUE_SET), Arrays.asList("real", "imaginary").stream()).collect(Collectors.toList());
    assertAxes(nxData, expectedAxesNames.toArray(new String[expectedAxesNames.size()]));
    final int[] defaultDimensionMappings = IntStream.range(0, sizes.length).toArray();
    int scannableIndex = -1;
    for (final String scannableName : scannableNames) {
        scannableIndex++;
        NXpositioner positioner = instrument.getPositioner(scannableName);
        final boolean inLocationMap = locationMap.containsKey(scannableName);
        if (inLocationMap) {
            final NXcollection scannablesCollection = (NXcollection) instrument.getGroupNode(COLLECTION_NAME_SCANNABLES);
            assertThat(scannablesCollection, is(notNullValue()));
            positioner = (NXpositioner) scannablesCollection.getGroupNode(scannableName);
        }
        assertThat(positioner, is(notNullValue()));
        assertThat(positioner.getGroupNodeNames(), is(empty()));
        assertThat(positioner.getNumberOfGroupNodes(), is(0));
        assertThat(positioner.getAttributeNames(), containsInAnyOrder(NXCLASS, ATTR_NAME_GDA_SCANNABLE_NAME, ATTR_NAME_GDA_SCAN_ROLE));
        assertThat(positioner.getNumberOfAttributes(), is(3));
        assertThat(positioner.getAttrString(null, ATTR_NAME_GDA_SCANNABLE_NAME), is(equalTo(scannableName)));
        assertThat(positioner.getAttrString(null, ATTR_NAME_GDA_SCAN_ROLE), is(equalTo(ScanRole.SCANNABLE.toString().toLowerCase())));
        final Scannable legacyScannable = getScannable(scannableName);
        final List<String> inputFieldNames = new ArrayList<>();
        inputFieldNames.addAll(Arrays.asList(legacyScannable.getInputNames()));
        inputFieldNames.addAll(Arrays.asList(legacyScannable.getExtraNames()));
        final List<String> outputFieldNames = new ArrayList<>(inputFieldNames);
        if (outputFieldNames.contains(scannableName)) {
            outputFieldNames.set(outputFieldNames.indexOf(scannableName), NXpositioner.NX_VALUE);
        }
        // check the number of data nodes, num fields of legacy scannable + name + demand_value
        final List<String> additionalDataNodeNames = Arrays.asList(NXpositioner.NX_NAME, FIELD_NAME_VALUE_SET, NXpositioner.NX_SOFT_LIMIT_MIN, NXpositioner.NX_SOFT_LIMIT_MAX);
        final String[] expectedDataNodeNames = Stream.of(outputFieldNames, additionalDataNodeNames).flatMap(Collection::stream).toArray(String[]::new);
        assertThat(positioner.getDataNodeNames(), containsInAnyOrder(expectedDataNodeNames));
        assertThat(positioner.getNumberOfDataNodes(), is(expectedDataNodeNames.length));
        assertThat(positioner.getNameScalar(), is(equalTo(scannableName)));
        // Demand values should be 1D
        dataNode = positioner.getDataNode(FIELD_NAME_VALUE_SET);
        dataset = dataNode.getDataset().getSlice();
        shape = dataset.getShape();
        assertThat(shape.length, is(1));
        assertThat(shape[0], is(sizes[scannableIndex]));
        String nxDataFieldName = scannableName + NexusConstants.FIELD_SEPERATOR + FIELD_NAME_VALUE_SET;
        assertThat(nxData.getDataNode(nxDataFieldName), is(sameInstance(dataNode)));
        assertIndices(nxData, nxDataFieldName, scannableIndex);
        assertTarget(nxData, nxDataFieldName, rootNode, "/entry/instrument/" + (inLocationMap ? COLLECTION_NAME_SCANNABLES + "/" : "") + scannableName + "/" + FIELD_NAME_VALUE_SET);
        final String[] paths = locationMap.containsKey(scannableName) ? ((SingleScannableWriter) locationMap.get(scannableName)).getPaths() : null;
        final String[] expectedUnits = getExpectedUnits(legacyScannable);
        assertThat(positioner.getNameScalar(), is(equalTo(scannableName)));
        final DataNode setValueDataNode = positioner.getDataNode(NXpositioner.NX_VALUE + "_set");
        assertThat(setValueDataNode, is(notNullValue()));
        dataset = setValueDataNode.getDataset().getSlice();
        shape = dataset.getShape();
        assertThat(shape, is(equalTo(new int[] { sizes[scannableIndex] })));
        assertThat(positioner.getSoft_limit_minScalar(), is(equalTo(((ScannableMotion) legacyScannable).getLowerGdaLimits()[0])));
        assertThat(positioner.getSoft_limit_maxScalar(), is(equalTo(((ScannableMotion) legacyScannable).getUpperGdaLimits()[0])));
        for (int fieldIndex = 0; fieldIndex < outputFieldNames.size(); fieldIndex++) {
            final String valueFieldName = outputFieldNames.get(fieldIndex);
            dataNode = positioner.getDataNode(valueFieldName);
            assertThat(valueFieldName, dataNode, is(notNullValue()));
            assertThat(dataNode.getAttribute(ATTR_NAME_LOCAL_NAME), is(notNullValue()));
            assertThat(valueFieldName, positioner.getAttrString(valueFieldName, ATTR_NAME_LOCAL_NAME), is(equalTo(scannableName + NexusConstants.FIELD_SEPERATOR + inputFieldNames.get(fieldIndex))));
            assertThat(dataNode.getAttribute(ATTR_NAME_GDA_FIELD_NAME), is(notNullValue()));
            assertThat(valueFieldName, positioner.getAttrString(valueFieldName, ATTR_NAME_GDA_FIELD_NAME), is(equalTo(inputFieldNames.get(fieldIndex))));
            // Actual values should be scanD
            dataset = dataNode.getDataset().getSlice();
            shape = dataset.getShape();
            assertThat(valueFieldName, shape, is(equalTo(sizes)));
            if (fieldIndex == 0) {
                // currently only the first field of a Scannable is linked to from an NXdata group,
                // this is probably incorrect, see JIRA DAQ-311
                nxDataFieldName = scannableName + NexusConstants.FIELD_SEPERATOR + valueFieldName;
                assertThat(nxData.getDataNode(nxDataFieldName), is(sameInstance(dataNode)));
                assertIndices(nxData, nxDataFieldName, defaultDimensionMappings);
                assertTarget(nxData, nxDataFieldName, rootNode, "/entry/instrument/" + (inLocationMap ? COLLECTION_NAME_SCANNABLES + "/" : "") + scannableName + "/" + valueFieldName);
                if (paths != null && paths.length > fieldIndex) {
                    // check the same datanode can also be found at the path for this fieldname in the location map
                    final DataNode expectedLinkedDataNode = NexusUtils.getDataNode(entry, paths[fieldIndex]);
                    assertThat(valueFieldName, expectedLinkedDataNode, is(sameInstance(dataNode)));
                    if (expectedUnits != null && expectedUnits.length > fieldIndex) {
                        // check the units attribute has been written according to the location map
                        final Attribute unitsAttribute = dataNode.getAttribute(ATTR_NAME_UNITS);
                        assertThat(unitsAttribute, is(notNullValue()));
                        assertThat(unitsAttribute.getFirstElement(), is(equalTo(expectedUnits[fieldIndex])));
                    }
                }
            }
        }
    }
}
Also used : NexusFile(org.eclipse.dawnsci.nexus.NexusFile) Attribute(org.eclipse.dawnsci.analysis.api.tree.Attribute) ArrayList(java.util.ArrayList) NXroot(org.eclipse.dawnsci.nexus.NXroot) NXdata(org.eclipse.dawnsci.nexus.NXdata) DataNode(org.eclipse.dawnsci.analysis.api.tree.DataNode) NXinstrument(org.eclipse.dawnsci.nexus.NXinstrument) IDataset(org.eclipse.january.dataset.IDataset) NXcollection(org.eclipse.dawnsci.nexus.NXcollection) ScannableMotion(gda.device.ScannableMotion) IPosition(org.eclipse.scanning.api.points.IPosition) TreeFile(org.eclipse.dawnsci.analysis.api.tree.TreeFile) ScanModel(org.eclipse.scanning.api.scan.models.ScanModel) NXpositioner(org.eclipse.dawnsci.nexus.NXpositioner) NXsample(org.eclipse.dawnsci.nexus.NXsample) AbstractRunnableDevice(org.eclipse.scanning.api.device.AbstractRunnableDevice) DummyScannable(gda.device.scannable.DummyScannable) Scannable(gda.device.Scannable) IScannable(org.eclipse.scanning.api.IScannable) DummyMultiFieldUnitsScannable(gda.device.scannable.DummyMultiFieldUnitsScannable) DummyUnitsScannable(gda.device.scannable.DummyUnitsScannable) NXentry(org.eclipse.dawnsci.nexus.NXentry)

Example 2 with ScanModel

use of org.eclipse.scanning.api.scan.models.ScanModel in project gda-core by openGDA.

the class ScannableNexusWrapperScanTest method createGridScan.

private IRunnableDevice<ScanModel> createGridScan(final IRunnableDevice<? extends IDetectorModel> detector, String outerScannableName, int... size) throws Exception {
    // Create scan points for a grid and make a generator
    final TwoAxisGridPointsModel gridModel = new TwoAxisGridPointsModel();
    gridModel.setxAxisName("salong");
    gridModel.setxAxisPoints(size[size.length - 1]);
    gridModel.setyAxisName("saperp");
    gridModel.setyAxisPoints(size[size.length - 2]);
    gridModel.setBoundingBox(new BoundingBox(0, 0, 3, 3));
    final CompoundModel compoundModel = new CompoundModel();
    // We add the outer scans, if any
    if (outerScannableName != null) {
        for (int dim = 0; dim < size.length - 2; dim++) {
            if (size[dim] > 1) {
                // TODO outer scannable name(s)? could use cryostat temperature as an outer scan
                compoundModel.addModel(new AxialStepModel(outerScannableName, 10000, 20000, 9999.99d / (size[dim] - 1)));
            } else {
                // Will generate one value at 10
                compoundModel.addModel(new AxialStepModel(outerScannableName + (dim + 1), 10, 20, 30));
            }
        }
    }
    compoundModel.addModel(gridModel);
    final IPointGenerator<CompoundModel> pointGen = pointGenService.createCompoundGenerator(compoundModel);
    // Create the model for a scan
    final ScanModel scanModel = new ScanModel();
    scanModel.setPointGenerator(pointGen);
    scanModel.setScanPathModel(compoundModel);
    scanModel.setDetector(detector);
    final IScannable<?> attributeScannable = scannableDeviceService.getScannable("attributes");
    final IScannable<?> beamSizeScannable = scannableDeviceService.getScannable("beam");
    scanModel.setMonitorsPerScan(attributeScannable, beamSizeScannable);
    // Create a file to scan into
    final File output = File.createTempFile("test_legacy_nexus", ".nxs");
    output.deleteOnExit();
    scanModel.setFilePath(output.getAbsolutePath());
    System.out.println("File writing to " + scanModel.getFilePath());
    // Create a scan and run it without publishing events
    final IRunnableDevice<ScanModel> scanner = scanService.createScanDevice(scanModel);
    final IPointGenerator<?> fgen = pointGen;
    ((IRunnableEventDevice<ScanModel>) scanner).addRunListener(new IRunListener() {

        @Override
        public void runWillPerform(RunEvent evt) throws ScanningException {
            System.out.println("Running acquisition scan of size " + fgen.size());
        }
    });
    return scanner;
}
Also used : ScanModel(org.eclipse.scanning.api.scan.models.ScanModel) IRunListener(org.eclipse.scanning.api.scan.event.IRunListener) TwoAxisGridPointsModel(org.eclipse.scanning.api.points.models.TwoAxisGridPointsModel) IRunnableEventDevice(org.eclipse.scanning.api.device.IRunnableEventDevice) CompoundModel(org.eclipse.scanning.api.points.models.CompoundModel) ScanningException(org.eclipse.scanning.api.scan.ScanningException) BoundingBox(org.eclipse.scanning.api.points.models.BoundingBox) AxialStepModel(org.eclipse.scanning.api.points.models.AxialStepModel) NexusFile(org.eclipse.dawnsci.nexus.NexusFile) File(java.io.File) TreeFile(org.eclipse.dawnsci.analysis.api.tree.TreeFile) RunEvent(org.eclipse.scanning.api.scan.event.RunEvent)

Aggregations

TreeFile (org.eclipse.dawnsci.analysis.api.tree.TreeFile)2 NexusFile (org.eclipse.dawnsci.nexus.NexusFile)2 ScanModel (org.eclipse.scanning.api.scan.models.ScanModel)2 Scannable (gda.device.Scannable)1 ScannableMotion (gda.device.ScannableMotion)1 DummyMultiFieldUnitsScannable (gda.device.scannable.DummyMultiFieldUnitsScannable)1 DummyScannable (gda.device.scannable.DummyScannable)1 DummyUnitsScannable (gda.device.scannable.DummyUnitsScannable)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Attribute (org.eclipse.dawnsci.analysis.api.tree.Attribute)1 DataNode (org.eclipse.dawnsci.analysis.api.tree.DataNode)1 NXcollection (org.eclipse.dawnsci.nexus.NXcollection)1 NXdata (org.eclipse.dawnsci.nexus.NXdata)1 NXentry (org.eclipse.dawnsci.nexus.NXentry)1 NXinstrument (org.eclipse.dawnsci.nexus.NXinstrument)1 NXpositioner (org.eclipse.dawnsci.nexus.NXpositioner)1 NXroot (org.eclipse.dawnsci.nexus.NXroot)1 NXsample (org.eclipse.dawnsci.nexus.NXsample)1 IDataset (org.eclipse.january.dataset.IDataset)1