<commit_before>package se.raddo.raddose3D;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 *This class defines the absorption properties of the container
 * encasing the irradiated sample. This is used to add an additional
 * attenuation factor to the beam before it reaches the sample.
 */
public class Container {

  /**
   * The material that the container is made from
   */
  private final String material;

  /**
   * The thickness of the container in microns
   */
  private final double thickness;

  /**
   * The density of the material in grams per centimetre cubed
   */
  private final double density;

  /**
   * Constructor for the Container class.
   * @param conMaterial
   *        String type argument giving the material of the container
   *        encasing the irradiated sample.
   * @param conThickness
   *        Double type argument giving the thickness of the
   *        container.
   */
  public Container(double conThickness, String conMaterial, double conDensity){
    /**
     * Initialise the instance variables
     */
    this.material = conMaterial;
    this.thickness = conThickness;
    this.density = conDensity;
  }

  /**
   * This method downloads the mass attenuation coefficients from NIST table
   * for the corresponding material and uses the beam energy to interpolate the
   * mass attenuation coefficient.
   * @param beam
   *        beam object describing the beam used.
   */
  public void extractMassAttenuationCoef(Beam beam){

    //Define/Initialise the local variables
    URL nistURL = null;
    URLConnection nistConnection = null;
    BufferedReader br = null;
    String inputLine;

    //Get the URL string
    String urlString = getNISTURL();

    //Create a URL object for the relevant NIST table
    try {
        nistURL = new URL(urlString);
    } catch (MalformedURLException e) {
        System.out.println("URL " + urlString + " is malformed");
        e.printStackTrace();
    }

    //Open a URL connection
    try {
        nistConnection = nistURL.openConnection();
    } catch (IOException e) {
        System.out.print("Cannot read from URL: " + urlString);
        e.printStackTrace();
    }

    //Read the data from the NIST table webpage into a buffered reader
    try {
        br = new BufferedReader(
                new InputStreamReader(nistConnection.getInputStream()));
    } catch (IOException e) {
        System.out.println("Cannot read from URL: " + urlString);
        e.printStackTrace();
    }

    //Read and parse the data from the buffered reader
    try {
        while ((inputLine = br.readLine()) != null) {
            System.out.println(inputLine);
        }
    } catch (IOException e) {
        System.out.println("Cannot read from URL: " + urlString);
        e.printStackTrace();
    }
  }

  /**
   * Uses the container material given in the input file to generate the URL
   * to the NIST table with the corresponding mass attenuation coefficients
   *
   * @return
   *        String of the URL pointing to the location of the mass attenuation
   *        coefficients.
   */
  private String getNISTURL(){
    return String.format("http://physics.nist.gov/PhysRefData/XrayMassCoef"
        + "/ComTab/pyrex.html", this.material);
  }<commit_msg>Finshed the extractMassAttenuationCoef method with comments<commit_after>package se.raddo.raddose3D;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *This class defines the absorption properties of the container
 * encasing the irradiated sample. This is used to add an additional
 * attenuation factor to the beam before it reaches the sample.
 */
public class Container {

  /**
   * Conversion of beam energy from MeV to KeV
   */
  private static final double MEV_TO_KEV = 1000.0;

  /**
   * The material that the container is made from
   */
  private final String material;

  /**
   * The thickness of the container in microns
   */
  private final double thickness;

  /**
   * The density of the material in grams per centimetre cubed
   */
  private final double density;

  /**
   * Constructor for the Container class.
   * @param conMaterial
   *        String type argument giving the material of the container
   *        encasing the irradiated sample.
   * @param conThickness
   *        Double type argument giving the thickness of the
   *        container.
   */
  public Container(double conThickness, String conMaterial, double conDensity){
    /**
     * Initialise the instance variables
     */
    this.material = conMaterial;
    this.thickness = conThickness;
    this.density = conDensity;
  }

  /**
   * This method downloads the mass attenuation coefficients from NIST table
   * for the corresponding material and uses the beam energy to interpolate the
   * mass attenuation coefficient.
   * @param beam
   *        beam object describing the beam used.
   */
  public double extractMassAttenuationCoef(Beam beam){

    //Define/Initialise the local variables
    URL nistURL = null;
    URLConnection nistConnection = null;
    BufferedReader br = null;
    String inputLine;
    double nistBeamEnergyInMeV = 0;
    double nistBeamEnergyInKeV = 0;
    double massAttenCoeff = 0;
    double nistBeamEnergyInKeVPrevious = 0;
    double massAttenCoeffPrevious = 0;

    // Variables used in parsing
    boolean readLine = false;
    Pattern openTag = Pattern.compile("<PRE>");
    Pattern closeTag = Pattern.compile("</PRE>");
    Pattern scientificNotation = Pattern.compile("[0-9]E[-+][0-9]");
    Pattern existK = Pattern.compile("K");

    //Get the URL string
    String urlString = getNISTURL();

    //Create a URL object for the relevant NIST table
    try {
        nistURL = new URL(urlString);
    } catch (MalformedURLException e) {
        System.out.println("URL " + urlString + " is malformed");
        e.printStackTrace();
    }

    //Open a URL connection
    try {
        nistConnection = nistURL.openConnection();
    } catch (IOException e) {
        System.out.print("Cannot read from URL: " + urlString);
        e.printStackTrace();
    }

    //Read the data from the NIST table webpage into a buffered reader
    try {
        br = new BufferedReader(
                new InputStreamReader(nistConnection.getInputStream()));
    } catch (IOException e) {
        System.out.println("Cannot read from URL: " + urlString);
        e.printStackTrace();
    }

    //Read and parse the data from the buffered reader
    try {
      //While we haven't reached the end of the file read each line
      while ((inputLine = br.readLine()) != null) {
        //Check if the line contains the "<PRE>" and "<\PRE>" tags
          Matcher openTagMatcher = openTag.matcher(inputLine);
          Matcher closeTagMatcher = closeTag.matcher(inputLine);
          if (openTagMatcher.find()) {
              readLine = true;
          } else if (closeTagMatcher.find()) {
              readLine = false;
          }

          //If we are in inside the "<PRE>" tag block then check for
          //mass coefficient table
          if (readLine) {
              //Look for scientific notation in the current line
              Matcher scientificNotationotationMatcher =
                      scientificNotation.matcher(inputLine);
              if (scientificNotationotationMatcher.find()) {
                //Split the string by whitespace
                  String[] splitLine = inputLine.split("\\s+");
                  Matcher existKMatcher = existK.matcher(inputLine);
                  //Return the beam energy and mass attenuation coefficient values
                  if (existKMatcher.find()) {
                      nistBeamEnergyInMeV = Double.parseDouble(splitLine[3]);
                      massAttenCoeff = Double.parseDouble(splitLine[4]);
                      System.out.println(nistBeamEnergyInMeV + " " + massAttenCoeff);
                  } else {
                      nistBeamEnergyInMeV = Double.parseDouble(splitLine[1]);
                      massAttenCoeff = Double.parseDouble(splitLine[2]);
                      System.out.println(nistBeamEnergyInMeV + " " + massAttenCoeff);
                  }
                  //Convert the beam energy from MeV to KeV
                  nistBeamEnergyInKeV = nistBeamEnergyInMeV * MEV_TO_KEV;

                  //Check if the beam energy on this line of the NIST table
                  //is bigger than the beam energy of the input beam
                  if (nistBeamEnergyInKeV > beam.getPhotonEnergy()) {
                      break;
                  } else{
                      nistBeamEnergyInKeVPrevious = nistBeamEnergyInKeV;
                      massAttenCoeffPrevious = massAttenCoeff;
                  }
              }
          }
      }
  } catch (IOException e) {
      System.out.println("Cannot read from URL: " + urlString);
      e.printStackTrace();
  }

  //Get the mass attenuation coefficient given by a linear interpolation
  //between the values in the NIST table.
  double massAttenuationCoefficient = massAttenCoeffPrevious +
          (massAttenCoeff - massAttenCoeffPrevious) *
          ((beam.getPhotonEnergy() - nistBeamEnergyInKeVPrevious) /
          (nistBeamEnergyInKeV - nistBeamEnergyInKeVPrevious));

  return massAttenuationCoefficient;
  }

  /**
   * Uses the container material given in the input file to generate the URL
   * to the NIST table with the corresponding mass attenuation coefficients
   *
   * @return
   *        String of the URL pointing to the location of the mass attenuation
   *        coefficients.
   */
  private String getNISTURL(){
    return String.format("http://physics.nist.gov/PhysRefData/XrayMassCoef"
        + "/ComTab/pyrex.html", this.material);
  }
{'fraction_non_alphanumeric': 0.08300985368766796, 'fraction_numerical': 0.0019906439733253707, 'mean_word_length': 2.4084124830393487, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4653701', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2644, 'n_words': 874}