def build_chain(self):
        # create the chain of processing steps.

        stage1_output = product_name.create_result_name(
            self.working_dir, self.product, "", "Orb_Cal_Deb_ML", ".dim")

        stage2_output_vh, stage2_output_vv = self._output(".dim")
        tif_vh, tif_vv = self._output(".tif")
        gzip_vh, gzip_vv = self._output(".tif.gz")

        # initial chain set up and the first step of the intensity process
        chain = [
            # Unzip the product
            lambda: ard.unzip_product(
                product_name.zip_path(self.input_dir, self.product), self.
                working_dir),
            # Run orbit corrections and debursting
            lambda: ard.gpt(
                ard.graph("S1_intensity_stage1"), {
                    "input":
                    product_name.manifest_path(self.working_dir, self.product),
                    "target":
                    stage1_output,
                }),
            # Run speckle filtering, terrain corrections and db conversion
            lambda: ard.gpt(
                ard.graph("S1_intensity_stage2"), {
                    "input": stage1_output,
                    "target1": stage2_output_vh,
                    "target2": stage2_output_vv,
                }),
            # Clean up the stage 1 outputs if requested to
            lambda: ard.delete_dim(stage1_output) if self.clean else None,
            # Convert results to tif
            lambda: ard.convert_to_tif(stage2_output_vh, tif_vh),
            lambda: ard.convert_to_tif(stage2_output_vv, tif_vv),
            # Clean up the stage2 dim files if requested
            lambda: ard.delete_dim(stage2_output_vh) if self.clean else None,
            lambda: ard.delete_dim(stage2_output_vv) if self.clean else None,
            # Gzip the results if requested
            lambda: ard.gzip_file(tif_vh, gzip_vh) if self.gzip else None,
            lambda: ard.gzip_file(tif_vv, gzip_vv) if self.gzip else None,
            # Clean up the tif files that have been compressed if requested.
            lambda: ard.delete_file(tif_vh)
            if self.gzip and self.clean else None,
            lambda: ard.delete_file(tif_vv)
            if self.gzip and self.clean else None,
        ]

        return chain
    def stage2(self, _sub_swath, _polarisation):
        result = [
            lambda: ard.gpt(
                ard.graph("S1_coherence_stage2"), {
                    "input1":
                    self._create_dim_name(
                        _polarisation,
                        f"Orb_{_sub_swath}",
                        self.product_first,
                    ),
                    "input2":
                    self._create_dim_name(
                        _polarisation,
                        f"Orb_{_sub_swath}",
                        self.product_last,
                    ),
                    "target":
                    self._create_dim_name(
                        _polarisation,
                        f"Orb_stack_Ifg_Deb_{_sub_swath}",
                    )
                })
        ]

        if self.clean:
            result.append(lambda: ard.delete_dim(
                self._create_dim_name(_polarisation, f"Orb_{_sub_swath}", self.
                                      products[0])))
            result.append(lambda: ard.delete_dim(
                self._create_dim_name(_polarisation, f"Orb_{_sub_swath}", self.
                                      products[1])))

        return result
    def stage4(self, _polarisation):
        result = [
            lambda: ard.gpt(
                ard.graph("S1_coherence_stage4"), {
                    "input":
                    self._create_dim_name(_polarisation,
                                          "Orb_stack_Ifg_Deb_mrg"),
                    "target":
                    self._create_dim_name(
                        _polarisation, "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC"),
                })
        ]
        if self.clean:
            result.append(lambda: ard.delete_dim(
                self._create_dim_name(_polarisation, "Orb_stack_Ifg_Deb_mrg")))

        # convert the final results to geotif, compress and then we are finally done!
        result.append(lambda: ard.convert_to_tif(
            self._create_dim_name(
                _polarisation,
                "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC",
            ),
            create_result_name(self.output_dir, self.products, _polarisation,
                               "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC", ".tif")))

        if self.clean:
            result.append(lambda: ard.delete_dim(
                self._create_dim_name(
                    _polarisation,
                    "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC",
                )))
        if self.gzip:
            result.append(lambda: ard.gzip_file(
                create_result_name(
                    self.output_dir, self.products, _polarisation,
                    "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC", ".tif"),
                create_result_name(
                    self.output_dir, self.products, _polarisation,
                    "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC", ".tif.gz"),
            ))

            if self.clean:
                result.append(lambda: ard.delete_file(
                    create_result_name(
                        self.output_dir, self.products, _polarisation,
                        "Orb_stack_Ifg_Deb_mrg_DInSAR_Flt_TC", ".tif")))
        return result
 def stage3(self, _polarisation):
     result = [
         lambda: ard.gpt(
             ard.graph("S1_coherence_stage3"), {
                 **product_name.create_s1_swath_dict(
                     "input", 1, 1, self.working_dir, self.products, _polarisation, "Orb_stack_Ifg_Deb", "dim"),
                 "target":
                 self._create_dim_name(_polarisation,
                                       "Orb_stack_Ifg_Deb_mrg"),
             })
     ]
     if self.clean:
         result.append(lambda: ard.delete_dim(
             self._create_dim_name(_polarisation, "Orb_stack_Ifg_Deb_iw1")))
         result.append(lambda: ard.delete_dim(
             self._create_dim_name(_polarisation, "Orb_stack_Ifg_Deb_iw2")))
         result.append(lambda: ard.delete_dim(
             self._create_dim_name(_polarisation, "Orb_stack_Ifg_Deb_iw3")))
     return result
    def stage1(self, product, polarisation):
        """
        Pull a product into three sub swaths for the provided polarisations.

        For dual polarisation (VV+VH) this stage should be called twice for the same image. Once for each polarisation
        :param product: the product to split into swathes
        :param polarisation:  the polarisation to select.
        :return: a lambda that will do the work.
        """
        return lambda: ard.gpt(
            ard.graph(
                "S1_coherence_stage1"), {
                    **product_name.create_s1_swath_dict(
                        "target", 1, 1, self.working_dir, product, polarisation, "Orb", "dim"),
                    "input":
                    product_name.manifest_path(self.working_dir, product),
                    "polarisation":
                    polarisation.upper(),
                })