<commit_before>package ucar.nc2.grib;

import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import ucar.nc2.Attribute;
import ucar.nc2.constants.CDM;
import ucar.nc2.dt.GridDataset;
import ucar.nc2.dt.GridDatatype;
import ucar.unidata.util.StringUtil2;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Try to figure out what GRIB name in 4.2 maps to in 4.3 NCEP IDD datasets.
 * Not guaranteed to be correct.
 * See ToolsUI IOSP/GRIB2/GRIB-RENAME
 *
 * @author caron
 * @since 4/7/12
 */
public class GribVariableRenamer {
  static private boolean debug = false;
  static private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GribVariableRenamer.class);

  private static HashMap<String, Renamer> map1;
  private static HashMap<String, Renamer> map2;

  ////////////////////////////////////////////////                        `
  //Map<String, List<String>> mapr = new HashMap<String, List<String>>();

  private void initMap1() {
    List<VariableRenamerBean> beans = readVariableRenameFile("resources/grib1/grib1VarMap.xml");
    map1 = makeMapBeans(beans);
  }

  private void initMap2() {
    List<VariableRenamerBean> beans = readVariableRenameFile("resources/grib2/grib2VarMap.xml");
    map2 = makeMapBeans(beans);
  }

  public List<String> getMappedNamesGrib2(String oldName) {
    List<String> result = new ArrayList<String>();
    Renamer mbean = map2.get(oldName);
    if (mbean == null) return null;
    for (VariableRenamerBean r : mbean.newVars) {
      result.add(r.newName);
    }
    return result;
  }

  public List<String> getMappedNamesGrib1(String oldName) {
    List<String> result = new ArrayList<String>();
    Renamer mbean = map1.get(oldName);
    if (mbean == null) return null;
    for (VariableRenamerBean r : mbean.newVars) {
      result.add(r.newName);
    }
    return result;
  }

    /**
   * Get unique list of old names associated with this new name
   * @param newName the new name
   * @return unique list of old names, or null if not exist
   *<commit_msg>added initializations for map1 and map2 to correct NPE<commit_after>package ucar.nc2.grib;

import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import ucar.nc2.Attribute;
import ucar.nc2.constants.CDM;
import ucar.nc2.dt.GridDataset;
import ucar.nc2.dt.GridDatatype;
import ucar.unidata.util.StringUtil2;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Try to figure out what GRIB name in 4.2 maps to in 4.3 NCEP IDD datasets.
 * Not guaranteed to be correct.
 * See ToolsUI IOSP/GRIB2/GRIB-RENAME
 *
 * @author caron
 * @since 4/7/12
 */
public class GribVariableRenamer {
  static private boolean debug = false;
  static private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GribVariableRenamer.class);

  private static HashMap<String, Renamer> map1;
  private static HashMap<String, Renamer> map2;

  ////////////////////////////////////////////////                        `
  //Map<String, List<String>> mapr = new HashMap<String, List<String>>();

  private void initMap1() {
    List<VariableRenamerBean> beans = readVariableRenameFile("resources/grib1/grib1VarMap.xml");
    map1 = makeMapBeans(beans);
  }

  private void initMap2() {
    List<VariableRenamerBean> beans = readVariableRenameFile("resources/grib2/grib2VarMap.xml");
    map2 = makeMapBeans(beans);
  }

  public List<String> getMappedNamesGrib2(String oldName) {
    if (map2 == null) initMap2();
    List<String> result = new ArrayList<String>();
    Renamer mbean = map2.get(oldName);
    if (mbean == null) return null;
    for (VariableRenamerBean r : mbean.newVars) {
      result.add(r.newName);
    }
    return result;
  }

  public List<String> getMappedNamesGrib1(String oldName) {
    if (map1 == null) initMap1();
    List<String> result = new ArrayList<String>();
    Renamer mbean = map1.get(oldName);
    if (mbean == null) return null;
    for (VariableRenamerBean r : mbean.newVars) {
      result.add(r.newName);
    }
    return result;
  }

    /**
   * Get unique list of old names associated with this new name
   * @param newName the new name
   * @return unique list of old names, or null if not exist
   *
{'fraction_non_alphanumeric': 0.13032348149872003, 'fraction_numerical': 0.017221317198045146, 'mean_word_length': 3.3195979899497488, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2649609', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1418, 'n_words': 358}