From a1a132f1cc8189747bc775b70c44d3c9fa88606f Mon Sep 17 00:00:00 2001 From: "Fernandez Vilanova, Lucas" Date: Mon, 2 Sep 2024 13:59:04 +0000 Subject: [PATCH] parser for inversion --- aiida_flexpart/calculations/collect_sens.py | 2 +- aiida_flexpart/calculations/inversion.py | 2 +- aiida_flexpart/parsers/inversion.py | 60 +++++++++++++++++++++ pyproject.toml | 3 +- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 aiida_flexpart/parsers/inversion.py diff --git a/aiida_flexpart/calculations/collect_sens.py b/aiida_flexpart/calculations/collect_sens.py index 84b0f33..44a56cc 100644 --- a/aiida_flexpart/calculations/collect_sens.py +++ b/aiida_flexpart/calculations/collect_sens.py @@ -31,7 +31,7 @@ def define(cls, spec): spec.input('metadata.options.custom_scheduler_commands', valid_type=str, default='') spec.input('metadata.options.withmpi', valid_type=bool, default=False) spec.input('metadata.options.output_filename', valid_type=str, default='aiida.out', required=True) - #spec.input('metadata.options.parser_name', valid_type=str, default='collect.sens') + #spec.input('metadata.options.parser_name', valid_type=str, default='collect.sensitivities') #Inputs spec.input_namespace('remote', valid_type=orm.RemoteStashFolderData, required=True) diff --git a/aiida_flexpart/calculations/inversion.py b/aiida_flexpart/calculations/inversion.py index 33d6032..47a7da5 100644 --- a/aiida_flexpart/calculations/inversion.py +++ b/aiida_flexpart/calculations/inversion.py @@ -21,7 +21,7 @@ def define(cls, spec): spec.input('metadata.options.custom_scheduler_commands', valid_type=str, default='') spec.input('metadata.options.withmpi', valid_type=bool, default=False) spec.input('metadata.options.output_filename', valid_type=str, default='aiida.out', required=True) - #spec.input('metadata.options.parser_name', valid_type=str, default='collect.sens') + spec.input('metadata.options.parser_name', valid_type=str, default='inversion.calc') #Inputs spec.input_namespace('remotes', valid_type = NetCDF, required=True) diff --git a/aiida_flexpart/parsers/inversion.py b/aiida_flexpart/parsers/inversion.py new file mode 100644 index 0000000..d5b1a55 --- /dev/null +++ b/aiida_flexpart/parsers/inversion.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +""" +Parsers provided by aiida_flexpart. + +Register parsers via the "aiida.parsers" entry point in setup.json. +""" +from aiida.engine import ExitCode +from aiida.parsers.parser import Parser +from aiida.plugins import CalculationFactory +from aiida.common import exceptions +from aiida.orm import SinglefileData + +InversionCalculation = CalculationFactory('inversion.calc') + + +class InvesrionParser(Parser): + """ + Parser class for parsing output of calculation. + """ + def __init__(self, node): + """ + Initialize Parser instance + + Checks that the ProcessNode being passed was produced by a FlexpartCalculation. + + :param node: ProcessNode of calculation + :param type node: :class:`aiida.orm.ProcessNode` + """ + super().__init__(node) + if not issubclass(node.process_class, InversionCalculation): + raise exceptions.ParsingError('Can only parse FlexpartCalculation') + + def parse(self, **kwargs): + """ + Parse outputs, store results in database. + + :returns: an exit code, if parsing fails (or nothing if parsing succeeds) + """ + output_filename = self.node.get_option('output_filename') + + # Check that folder content is as expected + files_retrieved = self.retrieved.list_object_names() + files_expected = [output_filename] + # Note: set(A) <= set(B) checks whether A is a subset of B + if not set(files_expected) <= set(files_retrieved): + self.logger.error("Found files '{}', expected to find '{}'".format( + files_retrieved, files_expected)) + return self.exit_codes.ERROR_MISSING_OUTPUT_FILES + + # add output file + self.logger.info(f"Parsing '{output_filename}'") + with self.retrieved.open(output_filename, 'r') as handle: + content = handle.read() + output_node = SinglefileData(file=handle) + if 'CONGRATULATIONS' not in content: + self.out('output_file', output_node) + return ExitCode(1) + + self.out('output_file', output_node) + return ExitCode(0) diff --git a/pyproject.toml b/pyproject.toml index 18c7e13..c2f050f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,7 +66,8 @@ docs = [ "flexpart.cosmo" = "aiida_flexpart.parsers.flexpart_cosmo:FlexpartCosmoParser" "flexpart.ifs" = "aiida_flexpart.parsers.flexpart_ifs:FlexpartIfsParser" "flexpart.post" = "aiida_flexpart.parsers.flexpart_post:FlexpartPostParser" -"collect.sens" = "aiida_flexpart.parsers.collect_sens:CollectSensParser" +"collect.sensitivities" = "aiida_flexpart.parsers.collect_sens:CollectSensParser" +"inversion.calc" = "aiida_flexpart.parsers.inversion:InvesrionParser" [project.entry-points."aiida.workflows"] "flexpart.multi_dates" = "aiida_flexpart.workflows.multi_dates_workflow:FlexpartMultipleDatesWorkflow"