Example #1
0
 public boolean ok(String out, String reference) {
   // log.fine("out1: " + a);
   // log.fine("out2: " + b);
   Scanner sa = new Scanner(out);
   Scanner sb = new Scanner(reference);
   while (sa.hasNext() && sb.hasNext()) {
     if (sa.hasNextDouble() || sb.hasNextDouble()) {
       if (!sa.hasNextDouble() || !sb.hasNextDouble()) return true;
       double da = sa.nextDouble();
       double db = sb.nextDouble();
       double d_abs = Math.abs(da - db);
       double d_rel = d_abs / Math.abs(db);
       if (!(d_abs < EPS || d_rel < EPS)) {
         log.fine("NOK, " + da + " too far from " + db);
         return false;
       }
     } else {
       String xa = sa.next();
       String xb = sb.next();
       if (!xa.equals(xb)) {
         log.fine("NOK, " + xa + " != " + xb);
         return false;
       }
     }
   }
   if (sa.hasNext() || sb.hasNext()) {
     log.fine("NOK: different number of tokens.");
     return false;
   }
   return true;
 }
Example #2
0
  /**
   * Loads into a double[][] a plain text file of numbers, with newlines dividing the numbers into
   * rows and tabs or spaces delimiting columns. The Y dimension is not flipped.
   */
  public static double[][] loadTextFile(InputStream stream) throws IOException {
    Scanner scan = new Scanner(stream);

    ArrayList rows = new ArrayList();
    int width = -1;

    while (scan.hasNextLine()) {
      String srow = scan.nextLine().trim();
      if (srow.length() > 0) {
        int w = 0;
        if (width == -1) // first time compute width
        {
          ArrayList firstRow = new ArrayList();
          Scanner rowScan = new Scanner(new StringReader(srow));
          while (rowScan.hasNextDouble()) {
            firstRow.add(new Double(rowScan.nextDouble())); // ugh, boxed
            w++;
          }
          width = w;
          double[] row = new double[width];
          for (int i = 0; i < width; i++) row[i] = ((Double) (firstRow.get(i))).doubleValue();
          rows.add(row);
        } else {
          double[] row = new double[width];
          Scanner rowScan = new Scanner(new StringReader(srow));
          while (rowScan.hasNextDouble()) {
            if (w == width) // uh oh
            throw new IOException("Row lengths do not match in text file");
            row[w] = rowScan.nextDouble();
            w++;
          }
          if (w < width) // uh oh
          throw new IOException("Row lengths do not match in text file");
          rows.add(row);
        }
      }
    }

    if (width == -1) // got nothing
    return new double[0][0];

    double[][] fieldTransposed = new double[rows.size()][];
    for (int i = 0; i < rows.size(); i++) fieldTransposed[i] = ((double[]) (rows.get(i)));

    // now transpose because we have width first
    double[][] field = new double[width][fieldTransposed.length];
    for (int i = 0; i < field.length; i++)
      for (int j = 0; j < field[i].length; j++) field[i][j] = fieldTransposed[j][i];

    return field;
  }
Example #3
0
 public static void main(String[] args) {
   while (scan.hasNextDouble()) {
     double t1 = scan.nextDouble();
     double t2 = scan.nextDouble();
     int i1, i2;
     for (i1 = 0; i1 < s1.length; i1++) {
       if (t1 < s1[i1]) break;
     }
     for (i2 = 0; i2 < s2.length; i2++) {
       if (t2 < s2[i2]) break;
     }
     System.out.println(r[Math.max(i1, i2)]);
   }
 }
  protected static ArrayList<Position> readReferencePositions(String filePath)
      throws FileNotFoundException {
    ArrayList<Position> positions = new ArrayList<Position>();
    Scanner scanner = new Scanner(new File(filePath));

    while (scanner.hasNextDouble()) {
      double lat = scanner.nextDouble();
      double lon = scanner.nextDouble();
      double elevation = scanner.nextDouble();
      positions.add(Position.fromDegrees(lat, lon, elevation));
    }

    return positions;
  }
Example #5
0
 public static void main(String[] args) throws IOException {
   Scanner s = new Scanner(new File("number.txt")).useDelimiter(";");
   int noo1 = 0, noo2 = 0, noo3 = 0, noo4 = 0, noo5 = 0;
   String line;
   while (s.hasNextDouble()) {
     double val = s.nextDouble();
     if ((val > 0) && (val < 20)) noo1++;
     else if ((val > 20) && (val < 40)) noo2++;
     else if ((val > 40) && (val < 60)) noo3++;
     else if ((val > 60) && (val < 80)) noo4++;
     else if ((val > 80) && (val < 100)) noo5++;
   }
   s.close();
   System.out.println("Range 0 to 20: " + noo1);
   System.out.println("Range 20 to 40: " + noo2);
   System.out.println("Range 40 to 60: " + noo3);
   System.out.println("Range 60 to 80: " + noo4);
   System.out.println("Range 80 to 100: " + noo5);
 }
Example #6
0
 private static String countFile(String filename)
 {
   String result = "";
   int row = 0;
   int rowCheck = 0;
   int col = 0;
   
   Scanner fileScanner = new Scanner(File filename));
   
   while(fileScanner.hasNextLine())
   {
     Scanner lineScanner = new Scanner(fileScanner.nextLine());
     while(lineScanner.hasNextDouble())
     {
       row++;
       lineScanner.next();
     }
     col++;
                                       
     if(row = 0)
     {
       rowCheck = row;
     }
     else if(row != rowCheck)
     {
       System.out.println("Number of rows in the file is inconsistent.");
     }
                                       
     lineScanner.close();
   }
   
   fileScanner.close();
   
   result = row + " " col;
   return result;
 }
Example #7
0
  public static void main(String[] args) {
    //////////////////////////
    // The values in following 4 lines should be user input
    int startPos = 200;
    int endPos = 1000;
    int totalNumPixel = 1044;
    double threshhold = 0.0001;
    /////////////////////////
    int numPixel = endPos - startPos;
    double wavelength[] = new double[totalNumPixel];
    double photonCount[] = new double[totalNumPixel];
    double SpecNoBk[] = new double[numPixel];
    double ThisSpectrum[] = new double[numPixel];
    double ThisXaxis[] = new double[numPixel];
    double Po[] = new double[numPixel];
    double Re[] = new double[numPixel];
    double P[] = new double[6];
    double Re2[] = new double[numPixel - 1];
    double mySUM[] = new double[numPixel];
    int ind[];
    double DEV;
    double prevDEV;
    Connection connection = null;
    Statement stmt = null;
    String pattern = "##.##";

    try {
      Scanner in = new Scanner(new FileReader(args[0]));
      int i = 0;
      while (in.hasNextDouble()) {
        wavelength[i] = in.nextDouble();
        photonCount[i] = in.nextDouble();
        ++i;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    ThisSpectrum = Arrays.copyOfRange(photonCount, startPos, endPos);
    ThisXaxis = Arrays.copyOfRange(wavelength, startPos, endPos);
    final WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < numPixel; i++) {
      obs.add(ThisXaxis[i], ThisSpectrum[i]);
    }

    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(5);
    P = fitter.fit(obs.toList());
    Polyval pVal = new Polyval(P, ThisXaxis, numPixel);
    Po = pVal.evl();

    for (int i = 0; i < numPixel; i++) {
      Re[i] = ThisSpectrum[i] - Po[i];
    }

    for (int i = 0; i < numPixel - 1; i++) {
      Re2[i] = Re[i + 1] - Re[i];
    }

    DEV = Math.sqrt(StatUtils.populationVariance(Re2, 0, Re2.length));
    for (int i = 0; i < numPixel; i++) {
      mySUM[i] = Po[i] + DEV;
    }

    int jj = 0; // jj is the length of points to be removed
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        jj++;
        ;
      }
    }
    ind = new int[jj];

    int jjj = 0;
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        ind[jjj] = i;
        jjj++;
      }
    }

    int indKeepLength = numPixel - ind.length;
    int indKeep[] = new int[indKeepLength];
    int k = 0;
    for (int i = 0; i < numPixel; i++) {
      if (!ArrayUtils.contains(ind, i)) {
        indKeep[k] = i;
        k++;
      }
    }
    double ThisSpectrumKeep[] = new double[indKeepLength];
    double ThisXaxisKeep[] = new double[indKeepLength];
    double PoKeep[] = new double[indKeepLength];
    double ReKeep[] = new double[indKeepLength];
    double Re2Keep[] = new double[indKeepLength - 1];
    double mySUMKeep[] = new double[indKeepLength];

    for (int i = 0; i < indKeepLength; i++) {
      ThisSpectrumKeep[i] = ThisSpectrum[indKeep[i]];
      ThisXaxisKeep[i] = ThisXaxis[indKeep[i]];
    }

    prevDEV = DEV;

    // at the point, ThisSpectrum and ThisXaxis should have reduced size
    final WeightedObservedPoints obs1 = new WeightedObservedPoints();

    for (int i = 0; i < indKeepLength; i++) {
      obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
    }

    while (true) {
      final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(5);
      P = fitter1.fit(obs1.toList());
      Polyval pVal1 = new Polyval(P, ThisXaxisKeep, indKeepLength);
      PoKeep = pVal1.evl();

      for (int i = 0; i < indKeepLength; i++) {
        ReKeep[i] = ThisSpectrumKeep[i] - PoKeep[i];
      }

      for (int i = 0; i < indKeepLength - 1; i++) {
        Re2Keep[i] = ReKeep[i + 1] - ReKeep[i];
      }

      DEV = Math.sqrt(StatUtils.populationVariance(Re2Keep, 0, Re2Keep.length));

      for (int i = 0; i < indKeepLength; i++) {
        mySUMKeep[i] = PoKeep[i] + DEV;
      }

      for (int i = 0; i < indKeepLength; i++) {
        if (ThisSpectrumKeep[i] > mySUMKeep[i]) ThisSpectrumKeep[i] = mySUMKeep[i];
      }
      if ((Math.abs(DEV - prevDEV) / DEV) < threshhold) break;
      prevDEV = DEV;

      obs1.clear();
      for (int i = 0; i < indKeepLength; i++) {
        obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
      }
    }
    Polyval pVal2 = new Polyval(P, ThisXaxis, numPixel);
    double FLbk[] = pVal2.evl();
    for (int i = 0; i < ThisXaxis.length; i++) {
      SpecNoBk[i] = ThisSpectrum[i] - FLbk[i];
    }

    // the write-to-file part is only for testing purpose, ThisXaxis and SpecNoBk are two outputs
    try {
      FileWriter fr = new FileWriter(args[1]);
      BufferedWriter br = new BufferedWriter(fr);
      PrintWriter out = new PrintWriter(br);
      DecimalFormat df = new DecimalFormat(pattern);
      for (int j = 0; j < ThisXaxis.length; j++) {
        if (Double.toString(wavelength[j]) != null) out.write(ThisXaxis[j] + "\t" + SpecNoBk[j]);
        out.write("\r\n");
      }
      out.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }