|
| 1 | +package edu.iris.dmc.station.conditions; |
| 2 | + |
| 3 | +import edu.iris.dmc.fdsn.station.model.Channel; |
| 4 | +import edu.iris.dmc.fdsn.station.model.Network; |
| 5 | +import edu.iris.dmc.fdsn.station.model.Response; |
| 6 | +import edu.iris.dmc.fdsn.station.model.ResponseStage; |
| 7 | +import edu.iris.dmc.fdsn.station.model.Station; |
| 8 | +import edu.iris.dmc.station.exceptions.StationxmlException; |
| 9 | +import edu.iris.dmc.station.restrictions.Restriction; |
| 10 | +import edu.iris.dmc.station.rules.Message; |
| 11 | +import edu.iris.dmc.station.rules.NestedMessage; |
| 12 | +import edu.iris.dmc.station.rules.Result; |
| 13 | + |
| 14 | +public class InstrumentUnitsStageCondition extends ChannelRestrictedCondition { |
| 15 | + private Restriction[] restrictions; |
| 16 | + |
| 17 | + public InstrumentUnitsStageCondition(boolean required, String description, Restriction[] restrictions) { |
| 18 | + super(required, description); |
| 19 | + this.restrictions = restrictions; |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public Message evaluate(Network network) { |
| 24 | + throw new IllegalArgumentException("Not supported!"); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Message evaluate(Station station) { |
| 29 | + throw new IllegalArgumentException("Not supported!"); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public Message evaluate(Channel channel) { |
| 34 | + if (channel == null) { |
| 35 | + return Result.success(); |
| 36 | + } |
| 37 | + return this.evaluate(channel, channel.getResponse()); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Message evaluate(Channel channel, Response response) { |
| 42 | + if (isRestricted(channel)) { |
| 43 | + return Result.success(); |
| 44 | + } |
| 45 | + if (this.required) { |
| 46 | + if (response == null) { |
| 47 | + return Result.error("expected response but was null"); |
| 48 | + } |
| 49 | + } |
| 50 | + NestedMessage nestedMessage = new NestedMessage(); |
| 51 | + boolean returnmessage = false; |
| 52 | + String inputUnit =""; |
| 53 | + String code = channel.getCode(); |
| 54 | + try { |
| 55 | + if(channel.getResponse().getStage().size()==0) { |
| 56 | + throw new StationxmlException("Response is missing from input StationXML"); |
| 57 | + } |
| 58 | + ResponseStage stage1 = channel.getResponse().getStage().get(0); |
| 59 | + if(stage1.getCoefficients() != null) { |
| 60 | + inputUnit = stage1.getCoefficients().getInputUnits().getName(); |
| 61 | + }else if(stage1.getPolesZeros() != null){ |
| 62 | + inputUnit = stage1.getPolesZeros().getInputUnits().getName(); |
| 63 | + }else if(stage1.getResponseList() != null){ |
| 64 | + inputUnit = stage1.getResponseList().getInputUnits().getName(); |
| 65 | + }else if(stage1.getFIR()!=null) { |
| 66 | + inputUnit = stage1.getFIR().getInputUnits().getName(); |
| 67 | + }else if(stage1.getPolynomial()!= null){ |
| 68 | + inputUnit = stage1.getPolynomial().getInputUnits().getName(); |
| 69 | + }else { |
| 70 | + return Result.success(); |
| 71 | + } |
| 72 | + for (Restriction r : this.restrictions) { |
| 73 | + if (r.qualifies(channel)) { |
| 74 | + return Result.success(); |
| 75 | + } |
| 76 | + } |
| 77 | + int lastindex = channel.getResponse().getStage().size()-1; |
| 78 | + ResponseStage stagelast = channel.getResponse().getStage().get(lastindex); |
| 79 | + String outputUnit =""; |
| 80 | + if(stagelast.getCoefficients() != null) { |
| 81 | + outputUnit = stagelast.getCoefficients().getOutputUnits().getName(); |
| 82 | + }else if(stagelast.getPolesZeros() != null){ |
| 83 | + outputUnit = stagelast.getPolesZeros().getOutputUnits().getName(); |
| 84 | + }else if(stagelast.getResponseList() != null){ |
| 85 | + outputUnit = stagelast.getResponseList().getOutputUnits().getName(); |
| 86 | + }else if(stagelast.getFIR()!=null) { |
| 87 | + outputUnit = stagelast.getFIR().getOutputUnits().getName(); |
| 88 | + }else if(stagelast.getPolynomial()!= null){ |
| 89 | + outputUnit = stagelast.getPolynomial().getOutputUnits().getName(); |
| 90 | + }else { |
| 91 | + return Result.success(); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + if (channel.getResponse().getInstrumentSensitivity() != null) { |
| 96 | + String instrumentInputUnit = channel.getResponse(). |
| 97 | + getInstrumentSensitivity().getInputUnits().getName(); |
| 98 | + String instrumentOutputUnit = channel.getResponse(). |
| 99 | + getInstrumentSensitivity().getOutputUnits().getName(); |
| 100 | + if(!inputUnit.equalsIgnoreCase(instrumentInputUnit)){ |
| 101 | + nestedMessage.add(Result.error("InsturmentSensitivity input " |
| 102 | + + "units "+ instrumentInputUnit+" must equal Stage[01] input " |
| 103 | + + "units "+ inputUnit)); |
| 104 | + returnmessage=true; |
| 105 | + } |
| 106 | + if(!outputUnit.equalsIgnoreCase(instrumentOutputUnit)){ |
| 107 | + if (lastindex+1 < 10 ) { |
| 108 | + nestedMessage.add(Result.error("InsturmentSensitivity output " |
| 109 | + + "units "+ instrumentOutputUnit+" must equal Stage[" |
| 110 | + +String.format("%02d",(lastindex+1))+"] output units "+ outputUnit)); |
| 111 | + returnmessage=true; |
| 112 | + }else { |
| 113 | + nestedMessage.add(Result.error("InsturmentSensitivity output " |
| 114 | + + "units "+ instrumentOutputUnit+" must equal Stage[" |
| 115 | + +(lastindex+1)+"] output units "+ outputUnit)); |
| 116 | + returnmessage=true; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + }else{ |
| 121 | + String instrumentInputUnit = channel.getResponse(). |
| 122 | + getInstrumentPolynomial().getInputUnits().getName(); |
| 123 | + String instrumentOutputUnit = channel.getResponse(). |
| 124 | + getInstrumentPolynomial().getOutputUnits().getName(); |
| 125 | + if(!inputUnit.equalsIgnoreCase(instrumentInputUnit)){ |
| 126 | + nestedMessage.add(Result.error("InsturmentPolynomial input " |
| 127 | + + "units "+ instrumentInputUnit+" must equal Stage[01] input " |
| 128 | + + "units "+ inputUnit)); |
| 129 | + returnmessage=true; |
| 130 | + } |
| 131 | + if(!outputUnit.equalsIgnoreCase(instrumentOutputUnit)){ |
| 132 | + if (lastindex+1 < 10 ) { |
| 133 | + nestedMessage.add(Result.error("InsturmentPolynomial output " |
| 134 | + + "units "+ instrumentOutputUnit+" must equal Stage[" |
| 135 | + +String.format("%02d",(lastindex+1))+"] output units "+ outputUnit)); |
| 136 | + returnmessage=true; |
| 137 | + }else { |
| 138 | + nestedMessage.add(Result.error("InsturmentPolynomial output " |
| 139 | + + "units "+ instrumentOutputUnit+" must equal Stage[" |
| 140 | + +(lastindex+1)+"] output units "+ outputUnit)); |
| 141 | + returnmessage=true; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + }catch(Exception e) { |
| 149 | + |
| 150 | + } |
| 151 | + if(returnmessage==true) { |
| 152 | + return nestedMessage; |
| 153 | + }else { |
| 154 | + return Result.success(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
| 159 | + |
| 160 | + |
0 commit comments