Advertisement
Guest User

VetoParser.java

a guest
Aug 19th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Arrays;
  4. import java.math.BigDecimal;
  5. import java.lang.System;
  6. import java.lang.Exception;
  7.  
  8. public class VetoParser {
  9.     public static VetoSet[] interpBetweenVetoSets(VetoSet[] input, int power, int numSets, int numPoints) throws Exception {
  10.         VetoSet[] interpInput = Interpolate.interpVetoArray(input, power, numPoints);
  11.         for(int i = 0; i < input.length; i++) {
  12.             System.out.println(input[i].getVetoPairs().length);
  13.         }
  14.         for(int i = 0; i < interpInput.length; i++) {
  15.             System.out.println(interpInput[i].getVetoPairs().length);
  16.         }
  17.         BigDecimal[] depths = getDepthsOfVetoSets(interpInput);
  18.         BigDecimal[] interpDepths = interpDepthArray(depths, power, numSets);
  19.         Pair[][] rearrangedPairs = rearrangeVetoSets(interpInput);
  20.         Pair[][] interpArray = new Pair[rearrangedPairs[0].length][rearrangedPairs.length];
  21.         for(int i = 0; i < interpArray.length; i++)
  22.             interpArray[i] = Interpolate.fixValues(rearrangedPairs[i], power, numSets);
  23.         Pair[][] resultPairArray = rotatePairArray(interpArray);
  24.         List<VetoSet> resultList = new LinkedList<VetoSet>();
  25.         for(int i = 0; i < resultPairArray.length; i++)
  26.             resultList.add(new VetoSet(interpDepths[i], resultPairArray[i]));
  27.         VetoSet[] result = new VetoSet[resultList.size()];
  28.         resultList.toArray(result);
  29.         return result;
  30.     }
  31.    
  32.     private static BigDecimal[] interpDepthArray(BigDecimal[] input, int power, int numPoints) {
  33.         Pair[] depthPairArray = new Pair[input.length];
  34.         for(int i = 0; i < input.length; i++)
  35.             depthPairArray[i] = new Pair(new BigDecimal(i), input[i]);
  36.         return Utility.unzipSnd(Interpolate.fixValues(depthPairArray, power, numPoints));
  37.     }
  38.    
  39.     private static BigDecimal[] getDepthsOfVetoSets(VetoSet[] input) {
  40.         List<BigDecimal> depthList = new LinkedList<BigDecimal>();
  41.         for(VetoSet v : input)
  42.             depthList.add(v.getDepth());
  43.         BigDecimal[] depthArray = new BigDecimal[depthList.size()];
  44.         depthList.toArray(depthArray);
  45.         return depthArray;
  46.     }
  47.    
  48.     private static Pair[][] rearrangeVetoSets(VetoSet[] input) throws Exception {
  49.         return rotatePairArray(vetoSetToPairArray(input));
  50.     }
  51.    
  52.     private static Pair[][] rotatePairArray(Pair[][] input) {
  53.         Pair[][] result = new Pair[input.length][input[0].length];
  54.         for(int x = 0; x < input.length; x++) {
  55.             for(int y = 0; y < input[0].length; y++) {
  56.                 System.out.println("Switched: " + x + ", " + y);
  57.                 result[x][y] = input[y][x];
  58.             }
  59.         }
  60.         return result;
  61.     }
  62.    
  63.     private static Pair[][] vetoSetToPairArray(VetoSet[] input) throws Exception {
  64.         if(!validVetoSetArray(input)) { throw new Exception("Bad VSA"); }
  65.         Pair[][] pairArray = new Pair[input[0].getVetoPairs().length][input.length];
  66.         for(int i = 0; i < input.length; i++)
  67.             pairArray[i] = input[i].getVetoPairs();
  68.         return pairArray;
  69.     }
  70.    
  71.     // This function returns false if the VetoSet array (pairs) isn't rectangular
  72.     private static boolean validVetoSetArray(VetoSet[] input) {
  73.         int l = input[0].getVetoPairs().length;
  74.         for(VetoSet v : input)
  75.             if(v.getVetoPairs().length != l) { return false; }
  76.         return true;
  77.     }
  78.    
  79.     public static VetoSet getVeto(String filename) throws Exception {
  80.         return parseVeto(readVeto(filename));
  81.     }
  82.    
  83.     public static VetoSet parseVeto(List<String[]> input) throws Exception {
  84.         if(!checkVeto(input))
  85.             throw new Exception("Bad file data");
  86.         String header = input.get(0)[0]; // The first line should only have one token
  87.         BigDecimal depth = new BigDecimal(header);
  88.         BigDecimal a, b;
  89.         Pair[] pairArray = new Pair[input.size() - 1];
  90.         for(int i = 1; i < input.size(); i++) {
  91.             a = new BigDecimal(input.get(i)[0]);
  92.             b = new BigDecimal(input.get(i)[1]);
  93.             pairArray[i - 1] = new Pair(a, b);
  94.         }
  95.         return new VetoSet(depth, pairArray);
  96.     }
  97.    
  98.     // This function returns false if the input is badly formed
  99.     private static boolean checkVeto(List<String[]> input) {
  100.         if(input.get(0).length != 1) { return false; }
  101.         for(int i = 1; i < input.size(); i++)
  102.             if(input.get(i).length != 2) { return false; }
  103.         return true;
  104.     }
  105.    
  106.     public static List<String[]> readVeto(String filename) throws Exception {
  107.         return ReadFile.readCSV(filename);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement