From fb00a757cd4e15b70dd87cdae759adea8b50f65b Mon Sep 17 00:00:00 2001 From: Douglas Gillespie <50671166+douggillespie@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:58:38 +0100 Subject: [PATCH] Data export A few fixes in Detections export. --- src/tethys/dbxml/DBXMLConnect.java | 3 +- src/tethys/detection/DetectionsHandler.java | 27 ++++------- src/tethys/pamdata/AutoTethysProvider.java | 47 ++++++++++++++++++- src/tethys/pamdata/TethysDataProvider.java | 19 ++++++++ src/tethys/pamdata/TethysParameterPacker.java | 26 +++++++--- .../swing/DatablockDetectionsPanel.java | 21 ++++++++- 6 files changed, 115 insertions(+), 28 deletions(-) diff --git a/src/tethys/dbxml/DBXMLConnect.java b/src/tethys/dbxml/DBXMLConnect.java index e6c603a5..7af700d5 100644 --- a/src/tethys/dbxml/DBXMLConnect.java +++ b/src/tethys/dbxml/DBXMLConnect.java @@ -163,8 +163,9 @@ public class DBXMLConnect { * Quite hard to see much common structure in this, so just look for * two words, and */ - boolean error = importReturn.contains(""); + boolean error = importReturn.contains(""); +// error = !success; might be a better options. if (error) { throw new TethysException("Error posting to Tethys", importReturn); } diff --git a/src/tethys/detection/DetectionsHandler.java b/src/tethys/detection/DetectionsHandler.java index 7598f524..bc5992db 100644 --- a/src/tethys/detection/DetectionsHandler.java +++ b/src/tethys/detection/DetectionsHandler.java @@ -262,30 +262,19 @@ public class DetectionsHandler { // return false; // } - private DetectionEffort getDetectorEffort(Deployment deployment, long effortStart, long effortEnd) { + private DetectionEffort getDetectorEffort(PDeployment pDeployment, PamDataBlock dataBlock, StreamExportParams exportParams) { DetectionEffort effort = new DetectionEffort(); + Deployment deployment = pDeployment.deployment; + Long effortStart = pDeployment.getAudioStart(); + Long effortEnd = pDeployment.getAudioEnd(); effort.setStart(TethysTimeFuncs.xmlGregCalFromMillis(effortStart)); effort.setEnd(TethysTimeFuncs.xmlGregCalFromMillis(effortEnd)); // effort.set // no setter for DetectionEffortKind List effortKinds = effort.getKind(); - DetectionEffortKind kind = new DetectionEffortKind(); - try { - nilus.Helper.createRequiredElements(kind); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InstantiationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + + TethysDataProvider dataProvider = dataBlock.getTethysDataProvider(); + dataProvider.getEffortKinds(pDeployment, effortKinds, exportParams); - kind.getSpeciesId().setValue(BigInteger.valueOf(180537)); - kind.getGranularity().setValue(nilus.GranularityEnumType.CALL); - - effortKinds.add(kind); return effort; } @@ -508,7 +497,7 @@ public class DetectionsHandler { supSoft.add(supportSoft); detections.setAlgorithm(algorithm); detections.setUserId("Unknown user"); - detections.setEffort(getDetectorEffort(deployment.deployment, deployment.getAudioStart(), deployment.getAudioEnd())); + detections.setEffort(getDetectorEffort(deployment, dataBlock, exportParams)); return detections; } diff --git a/src/tethys/pamdata/AutoTethysProvider.java b/src/tethys/pamdata/AutoTethysProvider.java index 26bf3807..ba530ce0 100644 --- a/src/tethys/pamdata/AutoTethysProvider.java +++ b/src/tethys/pamdata/AutoTethysProvider.java @@ -1,6 +1,7 @@ package tethys.pamdata; import java.math.BigInteger; +import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; @@ -26,12 +27,15 @@ import nilus.AlgorithmType.Parameters; import nilus.Deployment; import nilus.DescriptionType; import nilus.Detection; +import nilus.DetectionEffortKind; import nilus.SpeciesIDType; import tethys.TethysControl; import tethys.TethysTimeFuncs; +import tethys.niluswraps.PDeployment; import tethys.output.StreamExportParams; import tethys.output.TethysExportParams; import tethys.species.DataBlockSpeciesManager; +import tethys.species.DataBlockSpeciesMap; import tethys.species.ITISTypes; import tethys.species.SpeciesMapItem; import whistleClassifier.WhistleContour; @@ -136,7 +140,7 @@ public class AutoTethysProvider implements TethysDataProvider { // TODO Auto-generated catch block e.printStackTrace(); } - List genList = paramPacker.packParameters(settings); + List genList = paramPacker.packParameters(pamControlledUnit); if (genList == null || genList.size() == 0) { return null; } @@ -389,4 +393,45 @@ public class AutoTethysProvider implements TethysDataProvider { return species; } + @Override + public void getEffortKinds(PDeployment pDeployment, List effortKinds, StreamExportParams exportParams) { + + DataBlockSpeciesManager speciesManager = pamDataBlock.getDatablockSpeciesManager(); + if (speciesManager == null) { + return; + } + DataBlockSpeciesMap speciesMap = speciesManager.getDatablockSpeciesMap(); + ArrayList speciesCodes = speciesManager.getAllSpeciesCodes(); + if (speciesCodes == null || speciesMap == null) { + return; + } + for (String speciesCode : speciesCodes) { + + SpeciesMapItem mapItem = speciesMap.getItem(speciesCode); + + DetectionEffortKind kind = new DetectionEffortKind(); + try { + nilus.Helper.createRequiredElements(kind); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + kind.getSpeciesId().setValue(BigInteger.valueOf(mapItem.getItisCode())); + kind.getGranularity().setValue(exportParams.granularity); + kind.setCall(mapItem.getCallType()); + + + effortKinds.add(kind); + + } + + } + } diff --git a/src/tethys/pamdata/TethysDataProvider.java b/src/tethys/pamdata/TethysDataProvider.java index efbd47e5..adb5cbbc 100644 --- a/src/tethys/pamdata/TethysDataProvider.java +++ b/src/tethys/pamdata/TethysDataProvider.java @@ -1,11 +1,15 @@ package tethys.pamdata; +import java.util.List; + import PamguardMVC.PamDataUnit; import nilus.AlgorithmType; import nilus.AlgorithmType.Parameters; import nilus.Deployment; import nilus.DescriptionType; import nilus.Detection; +import nilus.DetectionEffortKind; +import tethys.niluswraps.PDeployment; import tethys.output.StreamExportParams; import tethys.output.TethysExportParams; @@ -65,7 +69,22 @@ public interface TethysDataProvider { StreamExportParams streamExportParams); + /** + * Get the algorithm parameters. + * @return + */ public Parameters getAlgorithmParameters(); + + + /** + * Fill in the effort kind list for the top of a Detections document. This must contain a list + * of every species that's going to be output within this effort period. Any species assigned + * to an actual detection must be in this list, or the document will be rejected. + * @param pDeployment + * @param effortKinds tethys object list to add to. + * @param exportParams + */ + public void getEffortKinds(PDeployment pDeployment, List effortKinds, StreamExportParams exportParams); } diff --git a/src/tethys/pamdata/TethysParameterPacker.java b/src/tethys/pamdata/TethysParameterPacker.java index a6ad0e01..9a076bf7 100644 --- a/src/tethys/pamdata/TethysParameterPacker.java +++ b/src/tethys/pamdata/TethysParameterPacker.java @@ -17,6 +17,8 @@ import javax.xml.transform.dom.DOMResult; import org.w3c.dom.Document; import org.w3c.dom.Element; +import PamController.PamControlledUnit; +import PamController.PamSettings; import PamController.settings.output.xml.PamguardXMLWriter; import PamModel.parametermanager.ManagedParameters; import PamModel.parametermanager.PamParameterData; @@ -67,6 +69,8 @@ public class TethysParameterPacker { */ private MarshalXML marshaller; + + private PamguardXMLWriter xmlWriter; /** * @throws JAXBException @@ -78,11 +82,20 @@ public class TethysParameterPacker { marshaller = new MarshalXML(); } catch (JAXBException e) { } + xmlWriter = PamguardXMLWriter.getXMLWriter(); } - - public List packParameters(Object data) { + public List packParameters(PamControlledUnit pamControlledUnit) { + if (pamControlledUnit instanceof PamSettings == false) { + return null; + } + PamSettings pamSettings = (PamSettings) pamControlledUnit; +// return null; +// } +// +// public List packParameters(Object data) { List elList = new ArrayList(); + Object data = pamSettings.getSettingsReference(); ArrayList objectHierarchy = new ArrayList<>(); @@ -121,15 +134,16 @@ public class TethysParameterPacker { try { Object paramData = pamParam.getData(); boolean ok = createElement(doc, el, paramData, pamParam, objectHierarchy); -// if (newEl != null) { -// elList.add(newEl); -// } } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } - } elList.add(el); + Element pgEl = xmlWriter.writeUnitSettings(doc, el, pamSettings); + if (pgEl != null) { + el.appendChild(pgEl); +// elList.add(pgEl); + } return elList; } diff --git a/src/tethys/swing/DatablockDetectionsPanel.java b/src/tethys/swing/DatablockDetectionsPanel.java index 99042e11..be174a5e 100644 --- a/src/tethys/swing/DatablockDetectionsPanel.java +++ b/src/tethys/swing/DatablockDetectionsPanel.java @@ -23,6 +23,7 @@ import tethys.TethysControl; import tethys.dbxml.TethysException; import tethys.detection.StreamDetectionsSummary; import tethys.niluswraps.PDetections; +import tethys.niluswraps.TethysCollections; /** * Table of Detections documents for a single PAMGuard datablock. @@ -103,6 +104,8 @@ public class DatablockDetectionsPanel extends TethysGUIPanel implements StreamTa if (pDets == null) { return; } + + JPopupMenu popMenu = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("Delete " + pDets.detections.getId()); menuItem.addActionListener(new ActionListener() { @@ -111,8 +114,19 @@ public class DatablockDetectionsPanel extends TethysGUIPanel implements StreamTa deleteDocument(pDets); } }); - JPopupMenu popMenu = new JPopupMenu(); popMenu.add(menuItem); + + menuItem = new JMenuItem("Display " + pDets.detections.getId()); + menuItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + displayDocument(pDets); + } + }); + popMenu.add(menuItem); + + + popMenu.show(e.getComponent(), e.getX(), e.getY()); } @@ -126,6 +140,11 @@ public class DatablockDetectionsPanel extends TethysGUIPanel implements StreamTa selectDataBlock(dataBlock); // force table update. } + private void displayDocument(PDetections pDets) { + getTethysControl().displayDocument(TethysCollections.Detections.toString(), pDets.detections.getId()); + + } + private PDetections detectionsForRow(int iRow) { if (streamDetectionsSummary == null || streamDetectionsSummary.detectionsDocs == null) { return null;