code: def _calculate_weights(tile_size):
      """Calculate a weight array for bilinear interpolation of histogram tiles.
  
      The weight array will be used to quickly bilinearly-interpolate the
      histogram equalizations tile size should be the width and height of a tile
      in pixels.
  
      Returns: 4D weight array where the first 2 dimensions correspond to the
          grid of where the tiles are relative to the tile being interpolated.
      """
      # we are essentially making a set of weight masks for an ideal center tile
      # that has all 8 surrounding tiles available
      # create our empty template tiles
      template_tile = np.zeros(
          (3, 3, tile_size, tile_size), dtype=np.float32
      )
      # TEMP FOR TESTING, create a weight tile that does no interpolation
      # template_tile[1,1] = template_tile[1,1] + 1.0
  
      # for ease of calculation, figure out the index of the center pixel in a tile
      # and how far that pixel is from the edge of the tile (in pixel units)
      center_index = int(tile_size / 2)
      center_dist = tile_size / 2.0
  
      # loop through each pixel in the tile and calculate the 9 weights for that pixel
      # were weights for a pixel are 0.0 they are not set (since the template_tile
      # starts out as all zeros)
      for row in range(tile_size):
          for col in range(tile_size):
              vertical_dist = abs(
                  center_dist - row
              )  # the distance from our pixel to the center of our tile, vertically
              horizontal_dist = abs(
                  center_dist - col
              )  # the distance from our pixel to the center of our tile, horizontally
  
              # pre-calculate which 3 adjacent tiles will affect our tile
              # (note: these calculations aren't quite right if center_index equals the row or col)
              horizontal_index = 0 if col < center_index else 2
              vertical_index = 0 if row < center_index else 2
  
              # if this is the center pixel, we only need to use it's own tile
              # for it
              if (row is center_index) and (col is center_index):
                  # all of the weight for this pixel comes from it's own tile
                  template_tile[1, 1][row, col] = 1.0
  
              # if this pixel is in the center row, but is not the center pixel
              # we're going to need to linearly interpolate it's tile and the
              # tile that is horizontally nearest to it
              elif (row is center_index) and (col is not center_index):
                  # linear interp horizontally
  
                  beside_weight = (
                      horizontal_dist / tile_size
                  )  # the weight from the adjacent tile
                  local_weight = (
                      tile_size - horizontal_dist
                  ) / tile_size  # the weight from this tile
  
                  # set the weights for the two relevant tiles
                  template_tile[1, 1][row, col] = local_weight
                  template_tile[1, horizontal_index][
                      row, col
                  ] = beside_weight
  
              # if this pixel is in the center column, but is not the center pixel
              # we're going to need to linearly interpolate it's tile and the
              # tile that is vertically nearest to it
              elif (row is not center_index) and (col is center_index):
                  # linear interp vertical
  
                  beside_weight = (
                      vertical_dist / tile_size
                  )  # the weight from the adjacent tile
                  local_weight = (
                      tile_size - vertical_dist
                  ) / tile_size  # the weight from this tile
  
                  # set the weights for the two relevant tiles
                  template_tile[1, 1][row, col] = local_weight
                  template_tile[vertical_index, 1][
                      row, col
                  ] = beside_weight
  
              # if the pixel is in one of the four quadrants that are above or below the center
              # row and column, we need to bilinearly interpolate it between the
              # nearest four tiles
              else:
                  # bilinear interpolation
  
                  local_weight = (
                      (tile_size - vertical_dist) / tile_size
                  ) * (
                      (tile_size - horizontal_dist) / tile_size
                  )  # the weight from this tile
                  vertical_weight = ((vertical_dist) / tile_size) * (
                      (tile_size - horizontal_dist) / tile_size
                  )  # the weight from the vertically   adjacent tile
                  horizontal_weight = (
                      (tile_size - vertical_dist) / tile_size
                  ) * (
                      (horizontal_dist) / tile_size
                  )  # the weight from the horizontally adjacent tile
                  diagonal_weight = ((vertical_dist) / tile_size) * (
                      (horizontal_dist) / tile_size
                  )  # the weight from the diagonally   adjacent tile
  
                  # set the weights for the four relevant tiles
                  template_tile[1, 1, row, col] = local_weight
                  template_tile[
                      vertical_index, 1, row, col
                  ] = vertical_weight
                  template_tile[
                      1, horizontal_index, row, col
                  ] = horizontal_weight
                  template_tile[
                      vertical_index, horizontal_index, row, col
                  ] = diagonal_weight
  
      # return the weights for an ideal center tile
      return template_tile
  
imports: from __future__ import annotations
  import logging
  import math
  from datetime import datetime
  import dask
  import dask.array
  import numpy
  import xarray
  from satpy.composites import CompositeBase, GenericCompositor
  from satpy.dataset import combine_metadata
label_desc: Use image node_id 9 for calling a global function with example usage: _calculate_weights(tile_size) and returns: template_tile