/*******************************************************************************
*
*  McStas, neutron ray-tracing package
*  Copyright(C) 2007 Risoe National Laboratory.
*
* %I
* Written by: Mads Bertelsen
* Date: 19.06.20
* Version: $Revision: 0.1 $
* Origin: ESS DMSC
*
* Logger of absorption along two dimensions of space
*
* %D
* Part of the Union components, a set of components that work together and thus
*  separates geometry and physics within McStas.
* The use of this component requires other components to be used.
*
* 1) One specifies a number of processes using process components
* 2) These are gathered into material definitions using Union_make_material
* 3) Geometries are placed using Union_box/cylinder/sphere, assigned a material
* 4) Logger and conditional components can be placed which will record what happens
* 5) A Union_master component placed after all of the above
*
* Only in step 5 will any simulation happen, and per default all geometries
*  defined before this master, but after the previous will be simulated here.
*
* There is a dedicated manual available for the Union_components
*
* This component is an absorption logger, and thus placed in point 4) above.
*
* A absorption logger will log something for each absorption event happening
*  in the geometry or geometries on which it is attached. These are specified
*  in the target_geometry string. By leaving it blank, all geometries are
*  logged, even the ones not defined at this point in the instrument file.
*  Multiple geometries are specified as a comma separated list.
*
* This absorption logger stores the absorbed weight in a histogram spanning two
*  spatial directions. The position of the event is projected onto this plane.
*  The plotted data is very useful when ensuring the simulated geometry matches
*  the intentions. It is not recommended to use this tool for safety concerns,
*  as for example to estimate activity of a sample after irradiation.
*
* The spatial plane in which the histogram is performed are chosen with the
*  D_direction_1 and D_direction_2 parameters which can be "x", "y" or "z".
*  The D1_min and D1_max parameters sets the limits for the first axis, and
*  D2_min / D2_max likewise for the second axis.
*
* This absorption logger needs to be placed in space, the position is recorded in
*  the coordinate system of the logger component.
*
* It is possible to attach one or more conditional components to this absorption
*  logger. Such a conditional component would impose a condition on the state of
*  the neutron after the Union_master component that executes the simulation,
*  and the absorption logger will only record the event if this condition is true.
*
* To use the logger_conditional_extend function, set it to some integer value n
*  and make and extend section to the master component that runs the geometry.
* In this extend function, logger_conditional_extend[n] is 1 if the conditional
*  stack evaluated to true, 0 if not. This way one can check what rays is logged
*  using regular McStas monitors. Only works if a conditional is applied to this
*  logger.
*
* %P
* INPUT PARAMETERS:
* D_direction_1:   [string] Direction for first axis ("x", "y" or "z")
* D1_max:          [m]      Histogram boundary, max position value for first axis
* D1_min:          [m]      Histogram boundary, min position value for first axis
* n1:              [1]      Number of bins for first axis
* D_direction_2:   [string] Direction for second axis ("x", "y" or "z")
* D2_max:          [m]      Histogram boundary, max position value for second axis
* D2_min:          [m]      Histogram boundary, min position value for second axis
* n2:              [1]      Number of bins for second axis
* target_geometry: [string] Comma separated list of geometry names that will be logged, leave empty for all volumes (even not defined yet)
* filename:        [string] Filename of produced data file
* order_total:     [1]      Only log rays that have scattered n times, -1 for all orders
* order_volume:    [1]      Only log rays that have scattered n times in the same geometry, -1 for all orders
* logger_conditional_extend_index: [1] If a conditional is used with this logger, the result of each conditional calculation can be made available in extend as a array called "logger_conditional_extend", and one would then access logger_conditional_extend[n] if logger_conditional_extend_index is set to n
* init:            [string] Name of Union_init component (typically "init", default)
*
* CALCULATED PARAMETERS:
*
* GLOBAL PARAMETERS:
*
* %L
*
* %E
******************************************************************************/

DEFINE COMPONENT Union_abs_logger_2D_space

SETTING PARAMETERS(string target_geometry="NULL",
                   string D_direction_1="x", D1_min=-0.2, D1_max=5, n1=0.2,
                   string D_direction_2="z", D2_min=-0.2, D2_max=5, n2=0.2,
                   string filename="NULL", order_total=-1, order_volume=-1, logger_conditional_extend_index=-1, string init="init")


/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */

SHARE
%{
  #ifndef Union
  #define Union $Revision: 0.8 $
  #error "The Union_init component must be included before this Union_abs_logger_2D_space component"
  #endif

  struct temp_2D_abs_data_element_struct {
    int index_1;
    int index_2;
    double weight;
  };

  struct temp_2D_abs_data_struct {
    int num_elements;
    int allocated_elements;
    struct temp_2D_abs_data_element_struct* elements;
  };

  struct a_2D_abs_storage_struct {
    struct Detector_2D_struct Detector_2D;
    struct temp_2D_abs_data_struct temp_2D_abs_data;
    int dim_1_choice;
    int dim_2_choice;
    int order;
    int order_in_this_volume;
    int order_process_in_this_volume;

    Coords position;
    Rotation rotation;
    Rotation t_rotation;
  };

  // record_to_temp
  // Would be nice if x y z, k_new and k_old were all coords
  void
  record_to_temp_2D_abs (Coords* position, double* k, double p, double time, int scattered_in_this_volume, int total_number_of_scattering_events,
                         struct abs_logger_struct* abs_logger, struct abs_logger_with_data_struct* abs_logger_with_data_array) {

    struct a_2D_abs_storage_struct* storage;
    storage = abs_logger->data_union.p_2D_abs_storage;

    int add_point = 1;

    if (storage->order != -1) {
      if (storage->order == total_number_of_scattering_events)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_in_this_volume != -1) {
      if (storage->order_in_this_volume == scattered_in_this_volume)
        add_point = 1;
      else
        add_point = 0;
    }

    if (add_point == 1) {

      double p1, p2;

      // dim_1_choice = 0 for x, 1 for y, 2 for z. Done in initialize from input. "x" "y" "z".
      if (storage->dim_1_choice == 0)
        // p1 = position->x - storage->position.x;
        p1 = position->x;
      else if (storage->dim_1_choice == 1)
        // p1 = position->y;
        p1 = position->y - storage->position.y;
      else if (storage->dim_1_choice == 2)
        // p1 = position->z - storage->position.z;
        p1 = position->z;

      if (storage->dim_2_choice == 0)
        // p2 = position->x - storage->position.x;
        p2 = position->x;
      else if (storage->dim_2_choice == 1)
        // p2 = position->y - storage->position.y;
        p2 = position->y;
      else if (storage->dim_2_choice == 2)
        // p2 = position->z - storage->position.z;
        p2 = position->z;

      int i, j;

      // Find bin in histogram
      if (p1 > storage->Detector_2D.D1min && p1 < storage->Detector_2D.D1max && p2 > storage->Detector_2D.D2min && p2 < storage->Detector_2D.D2max) {
        i = floor ((p1 - storage->Detector_2D.D1min) * storage->Detector_2D.bins_1 / (storage->Detector_2D.D1max - storage->Detector_2D.D1min));
        j = floor ((p2 - storage->Detector_2D.D2min) * storage->Detector_2D.bins_2 / (storage->Detector_2D.D2max - storage->Detector_2D.D2min));

        // Save bin in histogram to temp (may need to allocate more memory)
        int index;
        // printf("number of data points used: %d space allocated for %d data points.
        // \n",storage->temp_2D_abs_data.num_elements,storage->temp_2D_abs_data.allocated_elements);

        if (storage->temp_2D_abs_data.num_elements < storage->temp_2D_abs_data.allocated_elements) {
          storage->temp_2D_abs_data.elements[storage->temp_2D_abs_data.num_elements].index_1 = i;
          storage->temp_2D_abs_data.elements[storage->temp_2D_abs_data.num_elements].index_2 = j;
          storage->temp_2D_abs_data.elements[storage->temp_2D_abs_data.num_elements++].weight = p;
        } else {
          // No more space, need to allocate a larger buffer for this logger. Wish I had generics.

          // copy current data to temp
          struct temp_2D_abs_data_struct temporary_storage;
          temporary_storage.num_elements = storage->temp_2D_abs_data.num_elements;
          temporary_storage.elements = malloc (temporary_storage.num_elements * sizeof (struct temp_2D_abs_data_element_struct));

          for (index = 0; index < storage->temp_2D_abs_data.num_elements; index++) {
            temporary_storage.elements[index].index_1 = storage->temp_2D_abs_data.elements[index].index_1;
            temporary_storage.elements[index].index_2 = storage->temp_2D_abs_data.elements[index].index_2;
            temporary_storage.elements[index].weight = storage->temp_2D_abs_data.elements[index].weight;
          }

          // free current data
          free (storage->temp_2D_abs_data.elements);

          // allocate larger array (10 larger)
          storage->temp_2D_abs_data.allocated_elements = 10 + storage->temp_2D_abs_data.num_elements;
          storage->temp_2D_abs_data.elements = malloc (storage->temp_2D_abs_data.allocated_elements * sizeof (struct temp_2D_abs_data_element_struct));

          // copy back from temp
          for (index = 0; index < storage->temp_2D_abs_data.num_elements; index++) {
            storage->temp_2D_abs_data.elements[index].index_1 = temporary_storage.elements[index].index_1;
            storage->temp_2D_abs_data.elements[index].index_2 = temporary_storage.elements[index].index_2;
            storage->temp_2D_abs_data.elements[index].weight = temporary_storage.elements[index].weight;
          }

          // free temporary data
          free (temporary_storage.elements);

          // add new data point
          storage->temp_2D_abs_data.elements[storage->temp_2D_abs_data.num_elements].index_1 = i;
          storage->temp_2D_abs_data.elements[storage->temp_2D_abs_data.num_elements].index_2 = j;
          storage->temp_2D_abs_data.elements[storage->temp_2D_abs_data.num_elements++].weight = p;
        }

        // If this is the first time this ray is being recorded in this logger, add it to the list of loggers that write to temp and may get it moved to perm
        if (storage->temp_2D_abs_data.num_elements == 1)
          add_to_abs_logger_with_data (abs_logger_with_data_array, abs_logger);
      }
    }
  }

  // clear_temp
  void
  clear_temp_2D_abs (union abs_logger_data_union* data_union) {
    data_union->p_2D_abs_storage->temp_2D_abs_data.num_elements = 0;
  }

  // record_to_perm
  void
  record_to_perm_2D_abs (Coords* position, double* k, double p, double time, int scattered_in_this_volume, int total_number_of_scattering_events,
                         struct abs_logger_struct* abs_logger, struct abs_logger_with_data_struct* abs_logger_with_data_array) {

    // printf("In record to permanent \n");
    struct a_2D_abs_storage_struct* storage;
    storage = abs_logger->data_union.p_2D_abs_storage;

    int add_point = 1;

    if (storage->order != -1) {
      if (storage->order == total_number_of_scattering_events)
        add_point = 1;
      else
        add_point = 0;
    }

    if (storage->order_in_this_volume != -1) {
      if (storage->order_in_this_volume == scattered_in_this_volume)
        add_point = 1;
      else
        add_point = 0;
    }

    if (add_point == 1) {
      // printf("storage was set \n");
      double p1, p2;

      // dim_1_choice = 0 for x, 1 for y, 2 for z. Done in initialize from input. "x" "y" "z".
      if (storage->dim_1_choice == 0)
        // p1 = position->x - storage->position.x;
        p1 = position->x;
      else if (storage->dim_1_choice == 1)
        // p1 = position->y;
        p1 = position->y - storage->position.y;
      else if (storage->dim_1_choice == 2)
        // p1 = position->z - storage->position.z;
        p1 = position->z;

      if (storage->dim_2_choice == 0)
        // p2 = position->x - storage->position.x;
        p2 = position->x;
      else if (storage->dim_2_choice == 1)
        // p2 = position->y - storage->position.y;
        p2 = position->y;
      else if (storage->dim_2_choice == 2)
        // p2 = position->z - storage->position.z;
        p2 = position->z;

      int i, j;

      // Find bin in histogram
      if (p1 > storage->Detector_2D.D1min && p1 < storage->Detector_2D.D1max && p2 > storage->Detector_2D.D2min && p2 < storage->Detector_2D.D2max) {

        i = floor ((p1 - storage->Detector_2D.D1min) * (double)storage->Detector_2D.bins_1 / (storage->Detector_2D.D1max - storage->Detector_2D.D1min));
        j = floor ((p2 - storage->Detector_2D.D2min) * (double)storage->Detector_2D.bins_2 / (storage->Detector_2D.D2max - storage->Detector_2D.D2min));

        // printf("Added to statistics for monitor [%d] [%d] \n",i,j);
        // printf("indicies found\n");

        storage->Detector_2D.Array_N[i][j]++;
        storage->Detector_2D.Array_p[i][j] += p;
        storage->Detector_2D.Array_p2[i][j] += p * p;
      }
    }
  }

  // write_temp_to_perm
  void
  write_temp_to_perm_2D_abs (union abs_logger_data_union* data_union) {
    struct a_2D_abs_storage_struct* storage;
    storage = data_union->p_2D_abs_storage;

    int index;
    // Add all data points to the historgram, they are saved as index / weight combinations
    for (index = 0; index < storage->temp_2D_abs_data.num_elements; index++) {

      storage->Detector_2D.Array_N[storage->temp_2D_abs_data.elements[index].index_1][storage->temp_2D_abs_data.elements[index].index_2]++;

      storage->Detector_2D.Array_p[storage->temp_2D_abs_data.elements[index].index_1][storage->temp_2D_abs_data.elements[index].index_2]
          += storage->temp_2D_abs_data.elements[index].weight;

      storage->Detector_2D.Array_p2[storage->temp_2D_abs_data.elements[index].index_1][storage->temp_2D_abs_data.elements[index].index_2]
          += storage->temp_2D_abs_data.elements[index].weight * storage->temp_2D_abs_data.elements[index].weight;
    }

    clear_temp_2D_abs (data_union);
  }

  // write_temp_to_perm_final_p
  void
  write_temp_to_perm_final_p_2D_abs (union abs_logger_data_union* data_union, double final_weight) {
    struct a_2D_abs_storage_struct* storage;
    storage = data_union->p_2D_abs_storage;

    int index;
    // Add all data points to the historgram, they are saved as index / weight combinations
    for (index = 0; index < storage->temp_2D_abs_data.num_elements; index++) {

      storage->Detector_2D.Array_N[storage->temp_2D_abs_data.elements[index].index_1][storage->temp_2D_abs_data.elements[index].index_2]++;

      storage->Detector_2D.Array_p[storage->temp_2D_abs_data.elements[index].index_1][storage->temp_2D_abs_data.elements[index].index_2] += final_weight;

      storage->Detector_2D.Array_p2[storage->temp_2D_abs_data.elements[index].index_1][storage->temp_2D_abs_data.elements[index].index_2]
          += final_weight * final_weight;
    }

    clear_temp_2D_abs (data_union);
  }

  // Only need to define linking function for loggers once.
  #ifndef UNION_ABS_LOGGER
  #define UNION_ABS_LOGGER Dummy
  // Linking function for loggers, finds the indicies of the specified geometries on the global_geometry_list
  void
  manual_linking_function_abs_logger_volumes (char* input_string, struct pointer_to_global_geometry_list* global_geometry_list,
                                              struct pointer_to_1d_int_list* accepted_volumes, char* component_name) {
    // Need to check a input_string of text for an occurance of name. If it is in the inputstring, yes return 1, otherwise 0.
    char* token;
    int loop_index;
    char local_string[512];

    strcpy (local_string, input_string);
    // get the first token
    token = strtok (local_string, ",");

    // walk through other tokens
    while (token != NULL) {
      // printf( " %s\n", token );
      for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++) {
        if (strcmp (token, global_geometry_list->elements[loop_index].name) == 0) {
          add_element_to_int_list (accepted_volumes, loop_index);
          break;
        }

        if (loop_index == global_geometry_list->num_elements - 1) {
          // All possible geometry names have been looked through, and the break was not executed.
          // Alert the user to this problem by showing the geometry name that was not found and the currently available geometires
          printf ("\n");
          printf ("ERROR: The target_geometry string \"%s\" in Union logger component \"%s\" had an entry that did not match a specified geometry. \n",
                  input_string, component_name);
          printf ("       The unrecoignized geometry name was: \"%s\" \n", token);
          printf ("       The geometries available at this point (need to be defined before the logger): \n");
          for (loop_index = 0; loop_index < global_geometry_list->num_elements; loop_index++)
            printf ("         %s\n", global_geometry_list->elements[loop_index].name);
          exit (1);
        }
      }

      // Updates the token
      token = strtok (NULL, ",");
    }
  }
  #endif

  double**
  allocate2Ddouble_2D_abs (int count_x, int count_y) {
    // This function is needed to dynamically declare an array
    //  that has continous data as a static array would have,
    //  as that is the format expected by DETECTOR_OUT_2D.
    int i;

    // allocate space for actual data
    double* data = malloc (sizeof (double) * count_x * count_y);

    // create array or pointers to first elem in each 2D row
    double** ptr_array = malloc (sizeof (double*) * count_x);
    for (i = 0; i < count_x; i++) {
      ptr_array[i] = data + (i * count_y);
    }
    return ptr_array;
  }

  void
  free2Ddouble_2D_abs (double** ptr_array) {
    if (!ptr_array)
      return;
    if (ptr_array[0])
      free (ptr_array[0]);
    free (ptr_array);
  }
%}

DECLARE
%{
  // From make material
  // Needed for transport to the main component
  // struct global_material_element_struct global_material_element;
  // struct physics_struct this_material;

  int loop_index;
  int found_process;
  int specified_processes;
  char local_string[256];

  // Reused for logger
  struct pointer_to_1d_int_list accepted_processes;

  struct global_abs_logger_element_struct abs_logger_list_element;

  struct pointer_to_1d_int_list accepted_volumes;

  struct abs_logger_struct this_abs_logger;
  struct a_2D_abs_storage_struct this_abs_storage;

  struct abs_loggers_struct* abs_loggers_on_target_volume;
  struct Volume_struct* target_volume;
%}

INITIALIZE
%{

  accepted_processes.elements = NULL;
  accepted_processes.num_elements = 0;

  accepted_volumes.elements = NULL;
  accepted_volumes.num_elements = 0;

  // Initialize storage from input
  if (D1_min >= D1_max) {
    printf ("ERROR, Union logger \"%s\" had D1_min >= D1_max.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_abs_storage.Detector_2D.D1min = D1_min;
  this_abs_storage.Detector_2D.D1max = D1_max;

  if (D2_min >= D2_max) {
    printf ("ERROR, Union logger \"%s\" had D2_min >= D2_max.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_abs_storage.Detector_2D.D2min = D2_min;
  this_abs_storage.Detector_2D.D2max = D2_max;

  if (n1 <= 0) {
    printf ("ERROR, Union logger \"%s\" had n1 <= 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_abs_storage.Detector_2D.bins_1 = n1;

  if (n2 <= 0) {
    printf ("ERROR, Union logger \"%s\" had n2 <= 0.\n", NAME_CURRENT_COMP);
    exit (1);
  }
  this_abs_storage.Detector_2D.bins_2 = n2;

  // printf("past input sanitation \n");

  // Remember to take special care when deallocating this array, use free2Ddouble
  this_abs_storage.Detector_2D.Array_N = allocate2Ddouble_2D_abs (n1, n2); // Here the n1 double is cast to an int
  this_abs_storage.Detector_2D.Array_p = allocate2Ddouble_2D_abs (n1, n2);
  this_abs_storage.Detector_2D.Array_p2 = allocate2Ddouble_2D_abs (n1, n2);

  int l1, l2;
  for (l1 = 0; l1 < n1; l1++) { // n1 is technically a double, but this works fine
    for (l2 = 0; l2 < n2; l2++) {
      this_abs_storage.Detector_2D.Array_N[l1][l2] = 0;
      this_abs_storage.Detector_2D.Array_p[l1][l2] = 0;
      this_abs_storage.Detector_2D.Array_p2[l1][l2] = 0;
    }
  }

  // printf("past 2D pointer assignment \n");

  // Input sanitation for filename apparently done in 2D_detector_out

  if (strcmp (D_direction_1, "x") == 0 || strcmp (D_direction_1, "X") == 0) {
    this_abs_storage.dim_1_choice = 0;
    sprintf (this_abs_storage.Detector_2D.string_axis_1, "x [m]");
    sprintf (this_abs_storage.Detector_2D.title_string, "2D position Union logger in plane x");
  } else if (strcmp (D_direction_1, "y") == 0 || strcmp (D_direction_1, "Y") == 0) {
    this_abs_storage.dim_1_choice = 1;
    sprintf (this_abs_storage.Detector_2D.string_axis_1, "y [m]");
    sprintf (this_abs_storage.Detector_2D.title_string, "2D position Union logger in plane y");
  } else if (strcmp (D_direction_1, "z") == 0 || strcmp (D_direction_1, "Z") == 0) {
    this_abs_storage.dim_1_choice = 2;
    sprintf (this_abs_storage.Detector_2D.string_axis_1, "z [m]");
    sprintf (this_abs_storage.Detector_2D.title_string, "2D position Union logger in plane z");
  } else {
    printf ("ERROR, Union logger 2D_abs named \"%s\" has an invalid D_direction_1 string, it should be \"x\",\"y\" or \"z\".\n", NAME_CURRENT_COMP);
    exit (1);
  }

  char temp_string[2];
  if (strcmp (D_direction_2, "x") == 0 || strcmp (D_direction_2, "X") == 0) {
    this_abs_storage.dim_2_choice = 0;
    sprintf (this_abs_storage.Detector_2D.string_axis_2, "x [m]");
    sprintf (temp_string, "x");
  } else if (strcmp (D_direction_2, "y") == 0 || strcmp (D_direction_2, "Y") == 0) {
    this_abs_storage.dim_2_choice = 1;
    sprintf (this_abs_storage.Detector_2D.string_axis_2, "y [m]");
    sprintf (temp_string, "y");
  } else if (strcmp (D_direction_2, "z") == 0 || strcmp (D_direction_2, "Z") == 0) {
    this_abs_storage.dim_2_choice = 2;
    sprintf (this_abs_storage.Detector_2D.string_axis_2, "z [m]");
    sprintf (temp_string, "z");
  } else {
    printf ("ERROR, Union logger 2D_abs named \"%s\" has an invalid D_direction_2 string, it should be \"x\",\"y\" or \"z\".\n", NAME_CURRENT_COMP);
    exit (1);
  }

  strcat (this_abs_storage.Detector_2D.title_string,
          temp_string); // Connects the title string started in D_direction_1 part with the ending in D_direction_2 part

  sprintf (this_abs_storage.Detector_2D.Filename, "%s", filename);

  this_abs_storage.order = order_total;
  this_abs_storage.order_in_this_volume = order_volume;
  this_abs_storage.temp_2D_abs_data.num_elements = 0;
  // Added 17/11/2016, allocating some elements in initialize makes code during trace simpler
  this_abs_storage.temp_2D_abs_data.allocated_elements = 10;
  this_abs_storage.temp_2D_abs_data.elements = malloc (this_abs_storage.temp_2D_abs_data.allocated_elements * sizeof (struct temp_2D_abs_data_element_struct));

  if (_getcomp_index (init) < 0) {
    fprintf (stderr, "Union_abs_logger_2D_space:%s: Error identifying Union_init component, %s is not a known component name.\n", NAME_CURRENT_COMP, init);
    exit (-1);
  }

  struct global_positions_to_transform_list_struct* global_positions_to_transform_list = COMP_GETPAR3 (Union_init, init, global_positions_to_transform_list);
  struct global_rotations_to_transform_list_struct* global_rotations_to_transform_list = COMP_GETPAR3 (Union_init, init, global_rotations_to_transform_list);
  // Test position and rotation stored in a data storage, and pointers assigned to transform lists
  this_abs_logger.position = POS_A_CURRENT_COMP;
  add_position_pointer_to_list (global_positions_to_transform_list, &this_abs_logger.position);

  rot_copy (this_abs_logger.rotation, ROT_A_CURRENT_COMP);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_abs_logger.rotation);

  rot_transpose (ROT_A_CURRENT_COMP, this_abs_logger.t_rotation);
  add_rotation_pointer_to_list (global_rotations_to_transform_list, &this_abs_logger.t_rotation);

  // printf("past direction choices sanitation \n");

  // Book keeping
  this_abs_logger.abs_logger_extend_index = logger_conditional_extend_index;
  this_abs_logger.function_pointers.active_record_function = &record_to_perm_2D_abs;   // Assume no conditional
  this_abs_logger.function_pointers.inactive_record_function = &record_to_temp_2D_abs; // If an assume is present, these two pointers are switched
  // Temp to perm functions, and standard identifier
  // this_abs_logger.function_pointers.select_t_to_p = 1; // 1: temp_to_perm, 2: temp_to_perm_final_p // Not relevant for abs
  this_abs_logger.function_pointers.temp_to_perm = &write_temp_to_perm_2D_abs;
  this_abs_logger.function_pointers.temp_to_perm_final_p = &write_temp_to_perm_final_p_2D_abs;
  this_abs_logger.function_pointers.clear_temp = &clear_temp_2D_abs;
  // Initializing for conditional
  this_abs_logger.conditional_list.num_elements = 0;

  // this_logger.function_pointers.perm_to_disk = &write_perm_to_disk_2D_abs; //Obsolete

  // printf("past this_logger function assignment \n");

  this_abs_logger.data_union.p_2D_abs_storage = &this_abs_storage;

  sprintf (this_abs_logger.name, "%s", NAME_CURRENT_COMP);

  // printf("past this_logger assignment \n");

  sprintf (abs_logger_list_element.name, "%s", NAME_CURRENT_COMP);
  abs_logger_list_element.component_index = INDEX_CURRENT_COMP;
  abs_logger_list_element.abs_logger = &this_abs_logger;

  // printf("past logger_list_element assignment \n");

  struct pointer_to_global_geometry_list* global_geometry_list = COMP_GETPAR3 (Union_init, init, global_geometry_list);
  struct pointer_to_global_abs_logger_list* global_specific_volumes_abs_logger_list = COMP_GETPAR3 (Union_init, init, global_specific_volumes_abs_logger_list);
  // In order to run the logger at the right times, pointers to this logger is stored in each volume it logs,
  //  and additionally for each avaiable process. If a process is not logged, the pointer is simply not stored.
  int process_index;
  // Need to find the volumes for which the processes should have a reference to this logger
  if (target_geometry && strlen (target_geometry) && strcmp (target_geometry, "NULL") && strcmp (target_geometry, "0")) {
    // Certain volumes were selected, find the indicies in the global_geometry_list
    manual_linking_function_abs_logger_volumes (target_geometry, global_geometry_list, &accepted_volumes, NAME_CURRENT_COMP);
    // Add this logger to the global_specific_volumes_logger_list (so that conditionals can affect it)
    add_element_to_abs_logger_list (global_specific_volumes_abs_logger_list, abs_logger_list_element);

    for (loop_index = 0; loop_index < accepted_volumes.num_elements; loop_index++) {
      target_volume = global_geometry_list->elements[accepted_volumes.elements[loop_index]].Volume;
      // Add an element to its abs_logger list

      add_initialized_abs_logger_in_volume (&target_volume->abs_loggers);
      target_volume->abs_loggers.p_abs_logger[target_volume->abs_loggers.num_elements - 1] = &this_abs_logger;
    }
  } else {
    // Send to global_all_volumes_logger_list
    // Here there is no system for selecting processes as well
    struct pointer_to_global_abs_logger_list* global_all_volume_abs_logger_list = COMP_GETPAR3 (Union_init, init, global_all_volume_abs_logger_list);
    add_element_to_abs_logger_list (global_all_volume_abs_logger_list, abs_logger_list_element);
  }
 %}

TRACE
%{
%}

SAVE
%{
  // Write to disk
  DETECTOR_OUT_2D (this_abs_storage.Detector_2D.title_string, this_abs_storage.Detector_2D.string_axis_1, this_abs_storage.Detector_2D.string_axis_2,
                   this_abs_storage.Detector_2D.D1min, this_abs_storage.Detector_2D.D1max, this_abs_storage.Detector_2D.D2min, this_abs_storage.Detector_2D.D2max,
                   this_abs_storage.Detector_2D.bins_1, this_abs_storage.Detector_2D.bins_2, *this_abs_storage.Detector_2D.Array_N,
                   *this_abs_storage.Detector_2D.Array_p, *this_abs_storage.Detector_2D.Array_p2, this_abs_storage.Detector_2D.Filename);
%}

FINALLY
%{
  // Remember to clean up allocated lists
  if (this_abs_storage.temp_2D_abs_data.allocated_elements > 0)
    free (this_abs_storage.temp_2D_abs_data.elements);

  free2Ddouble_2D_abs (this_abs_storage.Detector_2D.Array_N);
  free2Ddouble_2D_abs (this_abs_storage.Detector_2D.Array_p);
  free2Ddouble_2D_abs (this_abs_storage.Detector_2D.Array_p2);

  if (accepted_processes.num_elements > 0)
    free (accepted_processes.elements);
  if (accepted_volumes.num_elements > 0)
    free (accepted_volumes.elements);
%}

END

