public static UnitOfMeasure ToDisplayUnit(this ISOProcessDataVariable pdv, RepresentationMapper mapper, ISO11783_TaskData taskData) { ISOUnit userUnit = null; if (!string.IsNullOrEmpty(pdv.ValuePresentationIdRef)) { ISOValuePresentation vpn = taskData.ChildElements.OfType <ISOValuePresentation>().FirstOrDefault(v => v.ValuePresentationID == pdv.ValuePresentationIdRef); if (vpn != null) { userUnit = new ISOUnit(vpn); } } if (userUnit != null) { UnitOfMeasure adaptUnit = null; try { adaptUnit = userUnit.ToAdaptUnit(); } catch { //Suppressing this as a non-critical exception } return(adaptUnit); } return(null); }
internal static RxRate ImportAndConvertRate(int productId, ISOProcessDataVariable pdv, Prescription prescription) { ISOUnit isoUnit = UnitFactory.Instance.GetUnitByDDI(pdv.ProcessDataDDI.AsInt32DDI()); if (isoUnit != null) { double rate = isoUnit.ConvertFromIsoUnit(pdv.ProcessDataValue); return(ImportRate(productId, rate, prescription)); } else { return(null); } }
private ISOProcessDataVariable ExportProcessDataVariable(NumericRepresentationValue value, string isoProductIdRef, ISOUnit unit) { if (value != null && value.Value != null) { UnitOfMeasure adaptUnit = unit.ToAdaptUnit(); var dataVariable = new ISOProcessDataVariable { ProductIdRef = isoProductIdRef, ProcessDataValue = value.AsIntViaMappedDDI(RepresentationMapper), ProcessDataDDI = DetermineVariableDDI(value.Representation, adaptUnit).AsHexDDI() }; return(dataVariable); } return(null); }
private string WriteType2GridFile(RasterGridPrescription prescription, ISOTreatmentZone treatmentZone) { var gridFileName = GenerateId(5); Dictionary <string, ISOUnit> unitsByDDI = new Dictionary <string, ISOUnit>(); Dictionary <int, Tuple <ISOProcessDataVariable, string> > treatmentZoneDataByLookupId = new Dictionary <int, Tuple <ISOProcessDataVariable, string> >(); using (var binaryWriter = CreateWriter(Path.ChangeExtension(gridFileName, ".bin"))) { byte[] previousBytes = BitConverter.GetBytes(0); foreach (var rxCellLookup in prescription.Rates) { if (rxCellLookup.RxRates == null || !rxCellLookup.RxRates.Any()) { //If there is null or no rate, write the previous rate (or 0 if we have not yet entered a valid rate) binaryWriter.Write(previousBytes, 0, previousBytes.Length); } else { foreach (var rxRate in rxCellLookup.RxRates) { if (!treatmentZoneDataByLookupId.ContainsKey(rxRate.RxProductLookupId)) { var lookup = prescription.RxProductLookups.First(x => x.Id.ReferenceId == rxRate.RxProductLookupId); string isoPDTDesignator = TaskDataMapper.InstanceIDMap.GetISOID(lookup.ProductId.Value); ISOProcessDataVariable dataVariable = treatmentZone.ProcessDataVariables.First(i => i.ProductIdRef == isoPDTDesignator); treatmentZoneDataByLookupId.Add(rxRate.RxProductLookupId, new Tuple <ISOProcessDataVariable, string>(dataVariable, lookup.UnitOfMeasure.Code)); } ISOProcessDataVariable pdv = treatmentZoneDataByLookupId[rxRate.RxProductLookupId].Item1; string srcUnitCode = treatmentZoneDataByLookupId[rxRate.RxProductLookupId].Item2; ISOUnit unit; if (!unitsByDDI.ContainsKey(pdv.ProcessDataDDI)) { unit = UnitFactory.Instance.GetUnitByDDI(pdv.ProcessDataDDI.AsInt32DDI()); unitsByDDI.Add(pdv.ProcessDataDDI, unit); } unit = unitsByDDI[pdv.ProcessDataDDI]; previousBytes = BitConverter.GetBytes((int)Math.Round(unit.ConvertToIsoUnit(rxRate.Rate, srcUnitCode), 0)); binaryWriter.Write(previousBytes, 0, previousBytes.Length); } } } } return(gridFileName); }
private NumericRepresentationValue ImportTreatmentZoneAsNumericRepValue(ISOTreatmentZone treatmentZone, RxProductLookup productLookup) { if (treatmentZone.ProcessDataVariables == null || treatmentZone.ProcessDataVariables.Count == 0) { return(null); } if (productLookup == null) //This clause supports the obsolete placement of default rates on the prescription { return(treatmentZone.ProcessDataVariables.First().AsNumericRepresentationValue(RepresentationMapper, ISOTaskData)); //In this situation, there should be only one PDV } else { ISOProcessDataVariable productPDV = treatmentZone.ProcessDataVariables.FirstOrDefault(pdv => productLookup.ProductId == TaskDataMapper.InstanceIDMap.GetADAPTID(pdv.ProductIdRef)); return(productPDV?.AsNumericRepresentationValue(RepresentationMapper, ISOTaskData)); } }
private void ExportManualPresciption(ISOTask task, ManualPrescription rx) { ISOTreatmentZone tzn = new ISOTreatmentZone(); tzn.TreatmentZoneDesignator = "Default Treatment Zone"; tzn.TreatmentZoneCode = 1; task.DefaultTreatmentZoneCode = tzn.TreatmentZoneCode; foreach (ProductUse productUse in rx.ProductUses) { var isoUnit = DetermineIsoUnit(rx.RxProductLookups.First(p => p.ProductId == productUse.ProductId).UnitOfMeasure); string productIDRef = TaskDataMapper.InstanceIDMap.GetISOID(productUse.ProductId); ISOProcessDataVariable pdv = ExportProcessDataVariable(productUse.Rate, productIDRef, isoUnit); tzn.ProcessDataVariables.Add(pdv); } task.TreatmentZones.Add(tzn); }
private ISOProcessDataVariable ExportProcessDataVariable(RxRate rxRate, Prescription rx) { ISOProcessDataVariable processDataVariable = new ISOProcessDataVariable(); RxProductLookup lookup = rx.RxProductLookups.FirstOrDefault(l => l.Id.ReferenceId == rxRate.RxProductLookupId); if (lookup != null) { processDataVariable.ProductIdRef = TaskDataMapper.InstanceIDMap.GetISOID(lookup.ProductId.Value); processDataVariable.ProcessDataDDI = DetermineVariableDDI(lookup.Representation, lookup.UnitOfMeasure).AsHexDDI(); ISOUnit unit = UnitFactory.Instance.GetUnitByDDI(processDataVariable.ProcessDataDDI.AsInt32DDI()); if (unit != null) { processDataVariable.ProcessDataValue = (int)unit.ConvertToIsoUnit(rxRate.Rate); } else { throw new ApplicationException("Missing unit on rate calculation from PDV."); } } return(processDataVariable); }
private string WriteType2GridFile(RasterGridPrescription prescription, ISOTreatmentZone treatmentZone) { var gridFileName = GenerateId(5); Dictionary <string, ISOUnit> unitsByDDI = new Dictionary <string, ISOUnit>(); using (var binaryWriter = CreateWriter(Path.ChangeExtension(gridFileName, ".bin"))) { byte[] previousBytes = BitConverter.GetBytes(0); foreach (var rxCellLookup in prescription.Rates) { if (rxCellLookup.RxRates == null || !rxCellLookup.RxRates.Any()) { //If there is null or no rate, write the previous rate (or 0 if we have not yet entered a valid rate) binaryWriter.Write(previousBytes, 0, previousBytes.Length); } else { for (int index = 0; index < rxCellLookup.RxRates.Count; index++) { ISOProcessDataVariable pdv = treatmentZone.ProcessDataVariables[index]; var rate = rxCellLookup.RxRates[index].Rate; ISOUnit unit = null; if (!unitsByDDI.ContainsKey(pdv.ProcessDataDDI)) { unit = UnitFactory.Instance.GetUnitByDDI(pdv.ProcessDataDDI.AsInt32DDI()); unitsByDDI.Add(pdv.ProcessDataDDI, unit); } unit = unitsByDDI[pdv.ProcessDataDDI]; previousBytes = BitConverter.GetBytes((int)Math.Round(unit.ConvertToIsoUnit(rate), 0)); binaryWriter.Write(previousBytes, 0, previousBytes.Length); } } } } return(gridFileName); }
private ISOTreatmentZone ExportTreatmentZonesForType2(ISOTask task, RasterGridPrescription prescription) { if (prescription.ProductIds == null) { TaskDataMapper.AddError($"No Products are present for Grid Type 2 Prescription export: {prescription.Description}", prescription.Id.ReferenceId.ToString()); return(null); } var lossOfSignalTreatmentZone = new ISOTreatmentZone { TreatmentZoneDesignator = "Loss of GPS", ProcessDataVariables = new List <ISOProcessDataVariable>() }; var outOfFieldTreatmentZone = new ISOTreatmentZone { TreatmentZoneDesignator = "Out of Field", ProcessDataVariables = new List <ISOProcessDataVariable>() }; var defaultTreatmentZone = new ISOTreatmentZone { TreatmentZoneDesignator = "Default", ProcessDataVariables = new List <ISOProcessDataVariable>() }; foreach (var productId in prescription.ProductIds) { var isoUnit = DetermineIsoUnit(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure); string isoProductId = TaskDataMapper.InstanceIDMap.GetISOID(productId) ?? string.Empty; ISOProcessDataVariable lossPDV = ExportProcessDataVariable(prescription.LossOfGpsRate, isoProductId, isoUnit); if (lossPDV != null) { lossOfSignalTreatmentZone.ProcessDataVariables.Add(lossPDV); } ISOProcessDataVariable oofPDV = ExportProcessDataVariable(prescription.OutOfFieldRate, isoProductId, isoUnit); if (oofPDV != null) { outOfFieldTreatmentZone.ProcessDataVariables.Add(oofPDV); } ISOProcessDataVariable defaultPDV = ExportProcessDataVariable(prescription.LossOfGpsRate, isoProductId, isoUnit); //ADAPT doesn't have a separate Default Rate. Using Loss of GPS Rate as a logical equivalent for a default rate. if (defaultPDV == null) { //Add 0 as the default rate so that we have at least one PDV to reference var defaultRate = new NumericRepresentationValue(null, new NumericValue(prescription.RxProductLookups.First().UnitOfMeasure, 0)); defaultPDV = ExportProcessDataVariable(defaultRate, isoProductId, isoUnit); } defaultTreatmentZone.ProcessDataVariables.Add(defaultPDV); } if (lossOfSignalTreatmentZone.ProcessDataVariables.Count > 0) { lossOfSignalTreatmentZone.TreatmentZoneCode = 253; task.TreatmentZones.Add(lossOfSignalTreatmentZone); task.PositionLostTreatmentZoneCode = lossOfSignalTreatmentZone.TreatmentZoneCode; } if (outOfFieldTreatmentZone.ProcessDataVariables.Count > 0) { outOfFieldTreatmentZone.TreatmentZoneCode = 254; task.TreatmentZones.Add(outOfFieldTreatmentZone); task.OutOfFieldTreatmentZoneCode = outOfFieldTreatmentZone.TreatmentZoneCode; } defaultTreatmentZone.TreatmentZoneCode = 1; task.TreatmentZones.Add(defaultTreatmentZone); task.DefaultTreatmentZoneCode = defaultTreatmentZone.TreatmentZoneCode; return(defaultTreatmentZone); }
public static NumericRepresentationValue AsNumericRepresentationValue(this ISOProcessDataVariable pdv, RepresentationMapper mapper, ISO11783_TaskData taskData) { return(pdv.ProcessDataValue.AsNumericRepresentationValue(pdv.ProcessDataDDI, mapper, pdv.ToDisplayUnit(mapper, taskData))); }
private ISOTreatmentZone ExportTreatmentZonesForType2(ISOTask task, RasterGridPrescription prescription) { if (prescription.ProductIds == null) { TaskDataMapper.AddError($"No Products are present for Grid Type 2 Prescription export: {prescription.Description}", prescription.Id.ReferenceId.ToString()); return(null); } var lossOfSignalTreatmentZone = new ISOTreatmentZone { TreatmentZoneDesignator = "Loss of GPS", ProcessDataVariables = new List <ISOProcessDataVariable>() }; var outOfFieldTreatmentZone = new ISOTreatmentZone { TreatmentZoneDesignator = "Out of Field", ProcessDataVariables = new List <ISOProcessDataVariable>() }; var defaultTreatmentZone = new ISOTreatmentZone { TreatmentZoneDesignator = "Default", ProcessDataVariables = new List <ISOProcessDataVariable>() }; foreach (var productId in prescription.ProductIds) { var isoUnit = DetermineIsoUnit(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure); string isoProductId = TaskDataMapper.InstanceIDMap.GetISOID(productId) ?? string.Empty; RxProductLookup productLookup = prescription.RxProductLookups.FirstOrDefault(p => p.ProductId == productId); ISOProcessDataVariable lossPDV = ExportProcessDataVariable(productLookup?.LossOfGpsRate ?? prescription.LossOfGpsRate, isoProductId, isoUnit); if (lossPDV != null) { lossOfSignalTreatmentZone.ProcessDataVariables.Add(lossPDV); } ISOProcessDataVariable oofPDV = ExportProcessDataVariable(productLookup?.OutOfFieldRate ?? prescription.OutOfFieldRate, isoProductId, isoUnit); if (oofPDV != null) { outOfFieldTreatmentZone.ProcessDataVariables.Add(oofPDV); } NumericRepresentation defaultRepresentation = productLookup?.LossOfGpsRate.Representation; //We can reuse the loss of gps representation here if it exists if (defaultRepresentation == null) { //Determine the representation based on the unit of the product to be applied var unitDimension = isoUnit.ToAdaptUnit().Dimension; if (UnitFactory.DimensionToDdi.ContainsKey(unitDimension)) { int ddi = UnitFactory.DimensionToDdi[unitDimension]; RepresentationMapper representationMapper = new RepresentationMapper(); var representation = representationMapper.Map(ddi) as NumericRepresentation; if (representation == null) { representation = new NumericRepresentation { Code = ddi.ToString(), CodeSource = RepresentationCodeSourceEnum.ISO11783_DDI }; } } else { TaskDataMapper.AddError($"Unable to identify a default representation: {prescription.Description}", prescription.Id.ReferenceId.ToString()); return(null); } } //Add 0 as the default rate in the PDV; actual values are in the binary var defaultRate = new NumericRepresentationValue(defaultRepresentation, new NumericValue(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure, 0d)); ISOProcessDataVariable defaultPDV = ExportProcessDataVariable(defaultRate, isoProductId, isoUnit); defaultTreatmentZone.ProcessDataVariables.Add(defaultPDV); } if (lossOfSignalTreatmentZone.ProcessDataVariables.Count > 0) { lossOfSignalTreatmentZone.TreatmentZoneCode = 253; task.TreatmentZones.Add(lossOfSignalTreatmentZone); task.PositionLostTreatmentZoneCode = lossOfSignalTreatmentZone.TreatmentZoneCode; } if (outOfFieldTreatmentZone.ProcessDataVariables.Count > 0) { outOfFieldTreatmentZone.TreatmentZoneCode = 254; task.TreatmentZones.Add(outOfFieldTreatmentZone); task.OutOfFieldTreatmentZoneCode = outOfFieldTreatmentZone.TreatmentZoneCode; } defaultTreatmentZone.TreatmentZoneCode = 1; task.TreatmentZones.Add(defaultTreatmentZone); task.DefaultTreatmentZoneCode = defaultTreatmentZone.TreatmentZoneCode; return(defaultTreatmentZone); }