<commit_before>#   This Python module is part of the PyRate software package.
#
#   Copyright 2020 Geoscience Australia
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""
This Python module implements an algorithm to search for the location
of the interferometric reference pixel
"""
import os
from os.path import join
from typing import Tuple

from itertools import product

import numpy as np
from numpy import isnan, std, mean, sum as nsum
from joblib import Parallel, delayed

from pyrate.core import ifgconstants as ifc, config as cf, mpiops
from pyrate.core import mpiops
from pyrate.core.shared import Ifg
from pyrate.core.shared import joblib_log_level
from pyrate.core.logger import pyratelogger as log
from pyrate.core import prepifg_helper
from pyrate.configuration import Configuration

MAIN_PROCESS = 0


def update_refpix_metadata(ifg_paths, refx, refy, transform, params):
    """
    Function that adds metadata about the chosen reference pixel to each interferogram.
    """
    pyrate_refpix_lon, pyrate_refpix_lat = mpiops.run_once(convert_pixel_value_to_geographic_coordinate, refx, refy, transform)


    process_ifgs_paths = mpiops.array_split(ifg_paths)

    for ifg_file in process_ifgs_paths:
        log.debug("Updating metadata for: "+ifg_file)
        ifg = Ifg(ifg_file)
        log.debug("Open dataset")
        ifg.open(readonly=True)
        log.debug("Set no data value")
        ifg.nodata_value = params["noDataValue"]
        log.debug("Update no data values in dataset")
        ifg.convert_to_nans()
        log.debug("Convert mm")
        ifg.convert_to_mm()
        half_patch_size = params["refchipsize"] // 2
        x, y = refx, refy
        log.debug("Extract reference pixel windows")
        data = ifg.phase_data[y - half_patch_size: y + half_patch_size + 1,
                              x - half_patch_size: x + half_patch_size + 1]
        log.debug("Calculate standard deviation for reference window")
        stddev_ref_area = np.nanstd(data)
        log.debug("Calculate mean for reference window")
        mean_ref_area = np.nanmean(data)
        ifg.add_metadata(**{
            ifc.PYRATE_REFPIX_X: str(refx),
            ifc.PYRATE_REFPIX_Y: str(refy),
            ifc.PYRATE_REFPIX_LAT: str(pyrate_refpix_lat),
            ifc.PYRATE_REFPIX_LON: str(pyrate_refpix_lon),
            ifc.PYRATE_MEAN_REF_AREA: str(mean_ref_area),
            ifc.PYRATE_STDDEV_REF_AREA: str(stddev_ref_area)
        })

        ifg.close()


def convert_pixel_value_to_geographic_coordinate(refx, refy, transform):
    """
    Converts a pixel coordinate to a latitude/longitude coordinate given the
    geotransform of the image.
    Args:
        refx: The pixel x coordinate.
        refy: The pixel ye coordinate.
        transform: The geotransform array of the image.
    Returns:
        Tuple of lon, lat geographic coordinate.
    """

    xOrigin = transform[0]
    yOrigin = transform[3]
    pixelWidth = transform[1]
    pixelHeight = -transform[5]

    lon = refx*pixelWidth + xOrigin
    lat = yOrigin - refy*pixelHeight

    return lon, lat


def convert_geographic_coordinate_to_pixel_value(lon, lat, transform):
    """
    Converts a latitude/longitude coordinate to a pixel coordinate given the
    geotransform of the image.
    Args:
        lon: Pixel longitude.
        lat: Pixel latitude.
        transform: The geotransform array of the image.
    Returns:
        Tuple of refx, refy pixel coordinates.
    """

    xOrigin = transform[0]
    yOrigin = transform[3]
    pixelWidth = transform[1]
    pixelHeight = -transform[5]<commit_msg>refactoring lon/lat function for vectorisation<commit_after>#   This Python module is part of the PyRate software package.
#
#   Copyright 2020 Geoscience Australia
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""
This Python module implements an algorithm to search for the location
of the interferometric reference pixel
"""
import os
from os.path import join
from typing import Tuple

from itertools import product

import numpy as np
from numpy import isnan, std, mean, sum as nsum
from joblib import Parallel, delayed

from pyrate.core import ifgconstants as ifc, config as cf, mpiops
from pyrate.core import mpiops
from pyrate.core.shared import Ifg
from pyrate.core.shared import joblib_log_level
from pyrate.core.logger import pyratelogger as log
from pyrate.core import prepifg_helper
from pyrate.configuration import Configuration

MAIN_PROCESS = 0


def update_refpix_metadata(ifg_paths, refx, refy, transform, params):
    """
    Function that adds metadata about the chosen reference pixel to each interferogram.
    """
    pyrate_refpix_lon, pyrate_refpix_lat = mpiops.run_once(convert_pixel_value_to_geographic_coordinate, refx, refy, transform)


    process_ifgs_paths = mpiops.array_split(ifg_paths)

    for ifg_file in process_ifgs_paths:
        log.debug("Updating metadata for: "+ifg_file)
        ifg = Ifg(ifg_file)
        log.debug("Open dataset")
        ifg.open(readonly=True)
        log.debug("Set no data value")
        ifg.nodata_value = params["noDataValue"]
        log.debug("Update no data values in dataset")
        ifg.convert_to_nans()
        log.debug("Convert mm")
        ifg.convert_to_mm()
        half_patch_size = params["refchipsize"] // 2
        x, y = refx, refy
        log.debug("Extract reference pixel windows")
        data = ifg.phase_data[y - half_patch_size: y + half_patch_size + 1,
                              x - half_patch_size: x + half_patch_size + 1]
        log.debug("Calculate standard deviation for reference window")
        stddev_ref_area = np.nanstd(data)
        log.debug("Calculate mean for reference window")
        mean_ref_area = np.nanmean(data)
        ifg.add_metadata(**{
            ifc.PYRATE_REFPIX_X: str(refx),
            ifc.PYRATE_REFPIX_Y: str(refy),
            ifc.PYRATE_REFPIX_LAT: str(pyrate_refpix_lat),
            ifc.PYRATE_REFPIX_LON: str(pyrate_refpix_lon),
            ifc.PYRATE_MEAN_REF_AREA: str(mean_ref_area),
            ifc.PYRATE_STDDEV_REF_AREA: str(stddev_ref_area)
        })

        ifg.close()


def convert_pixel_value_to_geographic_coordinate(refx, refy, transform):
    """
    Converts a pixel coordinate to a latitude/longitude coordinate given the
    geotransform of the image.
    Args:
        refx: The pixel x coordinate.
        refy: The pixel ye coordinate.
        transform: The geotransform array of the image.
    Returns:
        Tuple of lon, lat geographic coordinate.
    """

    lon = lon_from_pixel_coordinate(refx, transform)

    lat = lat_from_pixel_coordinate(refy, transform)

    return lon, lat


def lat_from_pixel_coordinate(refy, transform):
    yOrigin = transform[3]
    pixelHeight = -transform[5]
    lat = yOrigin - refy * pixelHeight
    return lat


def lon_from_pixel_coordinate(refx, transform):
    xOrigin = transform[0]
    pixelWidth = transform[1]
    lon = refx * pixelWidth + xOrigin
    return lon


def convert_geographic_coordinate_to_pixel_value(lon, lat, transform):
    """
    Converts a latitude/longitude coordinate to a pixel coordinate given the
    geotransform of the image.
    Args:
        lon: Pixel longitude.
        lat: Pixel latitude.
        transform: The geotransform array of the image.
    Returns:
        Tuple of refx, refy pixel coordinates.
    """

    xOrigin = transform[0]
    yOrigin = transform[3]
    pixelWidth = transform[1]
    pixelHeight = -transform[5]
{'fraction_non_alphanumeric': 0.07338264647176236, 'fraction_numerical': 0.004696489374192791, 'mean_word_length': 3.1349514563106795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5865369', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2536, 'n_words': 805}