Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
add support to compose all read all values
Browse files Browse the repository at this point in the history
  • Loading branch information
gwen-soleil committed Dec 19, 2019
1 parent 14a6527 commit 3522cfe
Showing 1 changed file with 85 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/**
* Copyright (C) : 2012
*
* Synchrotron Soleil
* L'Orme des merisiers
* Saint Aubin
* BP48
* 91192 GIF-SUR-YVETTE CEDEX
*
* <p>
* Synchrotron Soleil
* L'Orme des merisiers
* Saint Aubin
* BP48
* 91192 GIF-SUR-YVETTE CEDEX
* <p>
* This file is part of Tango.
*
* <p>
* Tango is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* Tango is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with Tango. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down Expand Up @@ -48,13 +48,18 @@
import fr.soleil.tango.clientapi.TangoGroupAttribute;

/**
* Write on a group of attributes, read average for scalar attributes. All attributes must be numbers or boolean (but
* Attribute to group several attributes. 2 options: <br>
* <ul>
* <li>Read all attribute values</li>
* <li>Write on a group of attributes, read average for scalar attributes.</li>
* </ul>
* <br>
* All attributes must be numbers or boolean (but
* not string) and of the same format. Write is possible only if at least one attribute is writable.NB1:for spectrum and
* images, if not all same dimensions, some zeros will be added. NB2: read value for images may be not relevant if not
* all attributes have the same dimensions.
*
* @author ABEILLE
*
*/
public class GroupAttribute implements IAttributeBehavior {

Expand All @@ -65,53 +70,89 @@ public class GroupAttribute implements IAttributeBehavior {
private final String[] attributeNames;
private final boolean isExternalRead;
private DeviceAttribute[] readValues;
private boolean isMean = true;

/**
* @param groupAttributeName the name of this attribute
* @param isExternalRead if true, the value has to be set with {@link #setReadValue(DeviceAttribute[])}
* @param attributeNames the attributes in the group
* @throws DevFailed
*/
public GroupAttribute(final String groupAttributeName, final boolean isExternalRead, final String... attributeNames)
throws DevFailed {
name = groupAttributeName;
this.attributeNames = attributeNames;
this.isExternalRead = isExternalRead;
attributeGroup = new TangoGroupAttribute(attributeNames);
attributeWritable = AttrWriteType.READ;
init();
}

/**
*
* @param groupAttributeName
* the name of this attribute
* @param isExternalRead
* if true, the value has to be set with {@link #setReadValue(DeviceAttribute[])}
* @param isMean If true, the attribute value will contain the average. Else, it will contains raw values
* @param attributeNames
* the attributes in the group
* @throws DevFailed
*/
public GroupAttribute(final String groupAttributeName, final boolean isExternalRead, final String... attributeNames)
throws DevFailed {
public GroupAttribute(final String groupAttributeName, final boolean isExternalRead, boolean isMean, final String... attributeNames) throws DevFailed {
this.isMean = isMean;
name = groupAttributeName;
this.attributeNames = attributeNames;
this.isExternalRead = isExternalRead;
attributeGroup = new TangoGroupAttribute(attributeNames);
attributeWritable = AttrWriteType.READ;
init();
}

private void init() throws DevFailed {
AttrDataFormat firstAttributeFormat = AttrDataFormat.SCALAR;
for (int i = 0; i < attributeNames.length; i++) {
final AttributeInfo info = attributeGroup.getGroup().getDevice(attributeNames[i])
.get_attribute_info(TangoUtil.getAttributeName(attributeNames[i]));
if (info.data_type == TangoConst.Tango_DEV_STRING) {
throw DevFailedUtils.newDevFailed(attributeNames[i] + " is a String, not supported");
}
if (i == 0) {
attributeFormat = info.data_format;

firstAttributeFormat = info.data_format;
} else {
if (!info.data_format.equals(attributeFormat)) {
if (!info.data_format.equals(firstAttributeFormat)) {
throw DevFailedUtils.newDevFailed("All attributes must have the same format");
}
if (!info.writable.equals(AttrWriteType.READ)) {
if (isMean && !info.writable.equals(AttrWriteType.READ)) {
// this attribute will be writable if at least one attribute is writable
attributeWritable = AttrWriteType.READ_WRITE;
}
}
}
if (isMean) {
attributeFormat = firstAttributeFormat;
} else {
// composition of raw values
if (firstAttributeFormat.equals(AttrDataFormat.SCALAR))
attributeFormat = AttrDataFormat.SPECTRUM;
else if (firstAttributeFormat.equals(AttrDataFormat.SPECTRUM)) {
attributeFormat = AttrDataFormat.IMAGE;
} else {
throw DevFailedUtils.newDevFailed("image format not supported");
}
}
}


@Override
public AttributeConfiguration getConfiguration() throws DevFailed {
final AttributeConfiguration config = new AttributeConfiguration();
config.setName(name);
config.setTangoType(TangoConst.Tango_DEV_DOUBLE, attributeFormat);
config.setWritable(attributeWritable);
final AttributePropertiesImpl props = new AttributePropertiesImpl();
props.setDescription("manage attributes: " + Arrays.toString(attributeNames)
+ "\nread part: average for scalars, write part: write on all writable attributes");
if (isMean) {
props.setDescription("manage attributes: " + Arrays.toString(attributeNames)
+ "\nread part: average for scalars, write part: write on all writable attributes");
} else {
props.setDescription("read attributes: " + Arrays.toString(attributeNames));
}
config.setAttributeProperties(props);
return config;
}
Expand All @@ -131,11 +172,16 @@ public AttributeValue getValue() throws DevFailed {
}
// calculate average
if (attributeFormat.equals(AttrDataFormat.SCALAR)) {

final double[] data = new double[result.length];
for (int i = 0; i < result.length; i++) {
data[i] = InsertExtractUtils.extractRead(result[i], AttrDataFormat.SCALAR, double.class);
}
value = new AttributeValue(StatUtils.mean(data));
if (isMean) {
value = new AttributeValue(StatUtils.mean(data));
} else {
value = new AttributeValue(data);
}
} else {
final List<double[]> values = new LinkedList<double[]>();
int maxLength = 0;
Expand Down Expand Up @@ -171,17 +217,21 @@ public AttributeValue getValue() throws DevFailed {
}
}
// avg
final double[] avg = new double[maxLength];
for (int i = 0; i < array.length; i++) {
avg[i] = StatUtils.mean(array[i]);
}
if (attributeFormat.equals(AttrDataFormat.SPECTRUM)) {
value = new AttributeValue(avg);
if (isMean) {
final double[] avg = new double[maxLength];
for (int i = 0; i < array.length; i++) {
avg[i] = StatUtils.mean(array[i]);
}
if (attributeFormat.equals(AttrDataFormat.SPECTRUM)) {
value = new AttributeValue(avg);
} else {
// if attribute is an image, don't know how to manage all dimensions properly
value = new AttributeValue(avg);
value.setXDim(maxDimX);
value.setYDim(maxDimY);
}
} else {
// if attribute is an image, don't know how to manage all dimensions properly
value = new AttributeValue(avg);
value.setXDim(maxDimX);
value.setYDim(maxDimY);
value = new AttributeValue(array);
}
}

Expand Down

0 comments on commit 3522cfe

Please sign in to comment.