Example #1
0
  private void showDeclaration(BeanTableSorted from, boolean isNcml) {
    Variable v = getCurrentVariable(from);
    if (v == null) return;
    infoTA.clear();
    if (isNcml) {
      Formatter out = new Formatter();
      try {
        NCdumpW.writeNcMLVariable(v, out);
      } catch (IOException e) {
        e.printStackTrace();
      }
      infoTA.appendLine(out.toString());

    } else {
      infoTA.appendLine(v.toString());
    }

    if (Debug.isSet("Xdeveloper")) {
      infoTA.appendLine("\n");
      infoTA.appendLine("FULL NAME = " + v.getFullName());
      infoTA.appendLine("\n");
      infoTA.appendLine(v.toStringDebug());
    }
    infoTA.gotoTop();
    infoWindow.setTitle("Variable Info");
    infoWindow.show();
  }
Example #2
0
 private void setExecutablePermissions(File gradleHome) {
   if (isWindows()) {
     return;
   }
   File gradleCommand = new File(gradleHome, "bin/gradle");
   String errorMessage = null;
   try {
     ProcessBuilder pb = new ProcessBuilder("chmod", "755", gradleCommand.getCanonicalPath());
     Process p = pb.start();
     if (p.waitFor() == 0) {
       System.out.println("Set executable permissions for: " + gradleCommand.getAbsolutePath());
     } else {
       BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
       Formatter stdout = new Formatter();
       String line;
       while ((line = is.readLine()) != null) {
         stdout.format("%s%n", line);
       }
       errorMessage = stdout.toString();
     }
   } catch (IOException e) {
     errorMessage = e.getMessage();
   } catch (InterruptedException e) {
     errorMessage = e.getMessage();
   }
   if (errorMessage != null) {
     System.out.println(
         "Could not set executable permissions for: " + gradleCommand.getAbsolutePath());
     System.out.println("Please do this manually if you want to use the Gradle UI.");
   }
 }
Example #3
0
 @Override
 public String augmentSetup(String setup, String alias, Map<String, String> parameters)
     throws Exception {
   Formatter f = new Formatter();
   f.format("%s", setup);
   try {
     f.format("\n#\n# Change disk layout to fit maven\n#\n\n");
     f.format("-outputmask = ${@bsn}-${version;===S;${@version}}.jar\n");
     f.format("src=src/main/java\n");
     f.format("bin=target/classes\n");
     f.format("testsrc=src/test/java\n");
     f.format("testbin=target/test-classes\n");
     f.format("target-dir=target\n");
     return f.toString();
   } finally {
     f.close();
   }
 }
  private String listSupportFiles(List<File> toDelete) throws Exception {
    Formatter f = new Formatter();
    try {
      if (toDelete == null) {
        toDelete = new ArrayList<File>();
      }
      int precount = toDelete.size();
      File confFile = IO.getFile(platform.getConfigFile()).getCanonicalFile();
      if (confFile.exists()) {
        f.format("    * %s \t0 Config file%n", confFile);
        toDelete.add(confFile);
      }

      String result = (toDelete.size() > precount) ? f.toString() : null;
      return result;
    } finally {
      f.close();
    }
  }
  private String listFiles(final File cache, List<File> toDelete) throws Exception {
    boolean stopServices = false;
    if (toDelete == null) {
      toDelete = new ArrayList<File>();
    } else {
      stopServices = true;
    }
    int count = 0;
    Formatter f = new Formatter();

    f.format(" - Cache:%n    * %s%n", cache.getCanonicalPath());
    f.format(" - Commands:%n");
    for (CommandData cdata : getCommands(new File(cache, COMMANDS))) {
      f.format("    * %s \t0 handle for \"%s\"%n", cdata.bin, cdata.name);
      toDelete.add(new File(cdata.bin));
      count++;
    }
    f.format(" - Services:%n");
    for (ServiceData sdata : getServices(new File(cache, SERVICE))) {
      if (sdata != null) {
        f.format("    * %s \t0 service directory for \"%s\"%n", sdata.sdir, sdata.name);
        toDelete.add(new File(sdata.sdir));
        File initd = platform.getInitd(sdata);
        if (initd != null && initd.exists()) {
          f.format("    * %s \t0 init.d file for \"%s\"%n", initd.getCanonicalPath(), sdata.name);
          toDelete.add(initd);
        }
        if (stopServices) {
          Service s = getService(sdata);
          try {
            s.stop();
          } catch (Exception e) {
          }
        }
        count++;
      }
    }
    f.format("%n");

    String result = (count > 0) ? f.toString() : null;
    f.close();
    return result;
  }
Example #6
0
  private void compareDataset(CompareDialog.Data data) {
    if (data.name == null) return;

    NetcdfFile compareFile = null;
    try {
      compareFile = NetcdfDataset.openFile(data.name, null);

      Formatter f = new Formatter();
      CompareNetcdf2 cn = new CompareNetcdf2(f, data.showCompare, data.showDetails, data.readData);
      if (data.howMuch == CompareDialog.HowMuch.All) cn.compare(ds, compareFile);
      else {
        NestedTable nested = nestedTableList.get(0);
        Variable org = getCurrentVariable(nested.table);
        if (org == null) return;
        Variable ov = compareFile.findVariable(org.getFullNameEscaped());
        if (ov != null) cn.compareVariable(org, ov);
      }

      infoTA.setText(f.toString());
      infoTA.gotoTop();
      infoWindow.setTitle("Compare");
      infoWindow.show();

    } catch (Throwable ioe) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);
      ioe.printStackTrace(new PrintStream(bos));
      infoTA.setText(bos.toString());
      infoTA.gotoTop();
      infoWindow.show();

    } finally {
      if (compareFile != null)
        try {
          compareFile.close();
        } catch (Exception eek) {
        }
    }
  }
 private static void test(String fs) {
   Formatter f = new Formatter(new StringBuilder(), Locale.US);
   f.format(fs, "fail");
   ck(fs, "fail", f.toString());
 }
 private static void test(Locale l, String fs, String exp, Object... args) {
   Formatter f = new Formatter(new StringBuilder(), l);
   f.format(fs, args);
   ck(fs, exp, f.toString());
 }
  private void run(String[] argv) throws IOException {
    int i, index;
    BufferedReader fp = null, fp_restore = null;
    String save_filename = null;
    String restore_filename = null;
    String data_filename = null;

    for (i = 0; i < argv.length; i++) {
      if (argv[i].charAt(0) != '-') break;
      ++i;
      switch (argv[i - 1].charAt(1)) {
        case 'l':
          lower = Double.parseDouble(argv[i]);
          break;
        case 'u':
          upper = Double.parseDouble(argv[i]);
          break;
        case 'y':
          y_lower = Double.parseDouble(argv[i]);
          ++i;
          y_upper = Double.parseDouble(argv[i]);
          y_scaling = true;
          break;
        case 's':
          save_filename = argv[i];
          break;
        case 'r':
          restore_filename = argv[i];
          break;
        default:
          System.err.println("unknown option");
          exit_with_help();
      }
    }

    if (!(upper > lower) || (y_scaling && !(y_upper > y_lower))) {
      System.err.println("inconsistent lower/upper specification");
      System.exit(1);
    }
    if (restore_filename != null && save_filename != null) {
      System.err.println("cannot use -r and -s simultaneously");
      System.exit(1);
    }

    if (argv.length != i + 1) exit_with_help();

    data_filename = argv[i];
    try {
      fp = new BufferedReader(new FileReader(data_filename));
    } catch (Exception e) {
      System.err.println("can't open file " + data_filename);
      System.exit(1);
    }

    /* assumption: min index of attributes is 1 */
    /* pass 1: find out max index of attributes */
    max_index = 0;

    if (restore_filename != null) {
      int idx, c;

      try {
        fp_restore = new BufferedReader(new FileReader(restore_filename));
      } catch (Exception e) {
        System.err.println("can't open file " + restore_filename);
        System.exit(1);
      }
      if ((c = fp_restore.read()) == 'y') {
        fp_restore.readLine();
        fp_restore.readLine();
        fp_restore.readLine();
      }
      fp_restore.readLine();
      fp_restore.readLine();

      String restore_line = null;
      while ((restore_line = fp_restore.readLine()) != null) {
        StringTokenizer st2 = new StringTokenizer(restore_line);
        idx = Integer.parseInt(st2.nextToken());
        max_index = Math.max(max_index, idx);
      }
      fp_restore = rewind(fp_restore, restore_filename);
    }

    while (readline(fp) != null) {
      StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
      System.out.println(line);
      st.nextToken();
      while (st.hasMoreTokens()) {
        index = Integer.parseInt(st.nextToken());
        max_index = Math.max(max_index, index);
        st.nextToken();
        num_nonzeros++;
      }
    }

    try {
      feature_max = new double[(max_index + 1)];
      feature_min = new double[(max_index + 1)];
    } catch (OutOfMemoryError e) {
      System.err.println("can't allocate enough memory");
      System.exit(1);
    }

    for (i = 0; i <= max_index; i++) {
      feature_max[i] = -Double.MAX_VALUE;
      feature_min[i] = Double.MAX_VALUE;
    }

    fp = rewind(fp, data_filename);

    /* pass 2: find out min/max value */
    while (readline(fp) != null) {
      int next_index = 1;
      double target;
      double value;

      StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
      target = Double.parseDouble(st.nextToken());
      y_max = Math.max(y_max, target);
      y_min = Math.min(y_min, target);

      while (st.hasMoreTokens()) {
        index = Integer.parseInt(st.nextToken());
        value = Double.parseDouble(st.nextToken());

        for (i = next_index; i < index; i++) {
          feature_max[i] = Math.max(feature_max[i], 0);
          feature_min[i] = Math.min(feature_min[i], 0);
        }

        feature_max[index] = Math.max(feature_max[index], value);
        feature_min[index] = Math.min(feature_min[index], value);
        next_index = index + 1;
      }

      for (i = next_index; i <= max_index; i++) {
        feature_max[i] = Math.max(feature_max[i], 0);
        feature_min[i] = Math.min(feature_min[i], 0);
      }
    }

    fp = rewind(fp, data_filename);

    /* pass 2.5: save/restore feature_min/feature_max */
    if (restore_filename != null) {
      // fp_restore rewinded in finding max_index
      int idx, c;
      double fmin, fmax;

      fp_restore.mark(2); // for reset
      if ((c = fp_restore.read()) == 'y') {
        fp_restore.readLine(); // pass the '\n' after 'y'
        StringTokenizer st = new StringTokenizer(fp_restore.readLine());
        y_lower = Double.parseDouble(st.nextToken());
        y_upper = Double.parseDouble(st.nextToken());
        st = new StringTokenizer(fp_restore.readLine());
        y_min = Double.parseDouble(st.nextToken());
        y_max = Double.parseDouble(st.nextToken());
        y_scaling = true;
      } else fp_restore.reset();

      if (fp_restore.read() == 'x') {
        fp_restore.readLine(); // pass the '\n' after 'x'
        StringTokenizer st = new StringTokenizer(fp_restore.readLine());
        lower = Double.parseDouble(st.nextToken());
        upper = Double.parseDouble(st.nextToken());
        String restore_line = null;
        while ((restore_line = fp_restore.readLine()) != null) {
          StringTokenizer st2 = new StringTokenizer(restore_line);
          idx = Integer.parseInt(st2.nextToken());
          fmin = Double.parseDouble(st2.nextToken());
          fmax = Double.parseDouble(st2.nextToken());
          if (idx <= max_index) {
            feature_min[idx] = fmin;
            feature_max[idx] = fmax;
          }
        }
      }
      fp_restore.close();
    }

    if (save_filename != null) {
      Formatter formatter = new Formatter(new StringBuilder());
      BufferedWriter fp_save = null;

      try {
        fp_save = new BufferedWriter(new FileWriter(save_filename));
      } catch (IOException e) {
        System.err.println("can't open file " + save_filename);
        System.exit(1);
      }

      if (y_scaling) {
        formatter.format("y\n");
        formatter.format("%.16g %.16g\n", y_lower, y_upper);
        formatter.format("%.16g %.16g\n", y_min, y_max);
      }
      formatter.format("x\n");
      formatter.format("%.16g %.16g\n", lower, upper);
      for (i = 1; i <= max_index; i++) {
        if (feature_min[i] != feature_max[i])
          formatter.format("%d %.16g %.16g\n", i, feature_min[i], feature_max[i]);
      }
      fp_save.write(formatter.toString());
      fp_save.close();
    }

    /* pass 3: scale */
    while (readline(fp) != null) {
      int next_index = 1;
      double target;
      double value;

      StringTokenizer st = new StringTokenizer(line, " \t\n\r\f:");
      target = Double.parseDouble(st.nextToken());
      output_target(target);
      while (st.hasMoreElements()) {
        index = Integer.parseInt(st.nextToken());
        value = Double.parseDouble(st.nextToken());
        for (i = next_index; i < index; i++) output(i, 0);
        output(index, value);
        next_index = index + 1;
      }

      for (i = next_index; i <= max_index; i++) output(i, 0);
      System.out.print("\n");
    }
    if (new_num_nonzeros > num_nonzeros)
      System.err.print(
          "WARNING: original #nonzeros "
              + num_nonzeros
              + "\n"
              + "         new      #nonzeros "
              + new_num_nonzeros
              + "\n"
              + "Use -l 0 if many original feature values are zeros\n");

    fp.close();
  }
  private void createIndex(
      File indexFile, List<Group> groups, ArrayList<String> filenames, Formatter f)
      throws IOException {
    Grib2Record first = null; // take global metadata from here

    if (indexFile.exists()) indexFile.delete(); // replace it
    f.format(" createIndex for %s%n", indexFile.getPath());

    RandomAccessFile raf = new RandomAccessFile(indexFile.getPath(), "rw");
    raf.order(RandomAccessFile.BIG_ENDIAN);
    try {
      //// header message
      raf.write(MAGIC_START.getBytes("UTF-8"));
      raf.writeInt(version);
      long lenPos = raf.getFilePointer();
      raf.writeLong(0); // save space to write the length of the record section
      long countBytes = 0;
      int countRecords = 0;
      for (Group g : groups) {
        g.fileSet = new HashSet<Integer>();
        for (Grib2Rectilyser.VariableBag vb : g.rect.getGribvars()) {
          if (first == null) first = vb.first;
          GribCollectionProto.VariableRecords vr = writeRecordsProto(vb, g.fileSet);
          byte[] b = vr.toByteArray();
          vb.pos = raf.getFilePointer();
          vb.length = b.length;
          raf.write(b);
          countBytes += b.length;
          countRecords += vb.recordMap.length;
        }
      }
      long bytesPerRecord = countBytes / ((countRecords == 0) ? 1 : countRecords);
      f.format(
          "  write RecordMaps: bytes = %d record = %d bytesPerRecord=%d%n",
          countBytes, countRecords, bytesPerRecord);

      if (first == null) {
        logger.error("GribCollection {}: has no files\n{}", gc.getName(), f.toString());
        throw new IllegalArgumentException("GribCollection " + gc.getName() + " has no files");
      }

      long pos = raf.getFilePointer();
      raf.seek(lenPos);
      raf.writeLong(countBytes);
      raf.seek(pos); // back to the output.

      GribCollectionProto.GribCollectionIndex.Builder indexBuilder =
          GribCollectionProto.GribCollectionIndex.newBuilder();
      indexBuilder.setName(gc.getName());

      for (String fn : filenames) indexBuilder.addFiles(fn);

      for (Group g : groups) indexBuilder.addGroups(writeGroupProto(g));

      /* int count = 0;
      for (DatasetCollectionManager dcm : collections) {
        indexBuilder.addParams(makeParamProto(new Parameter("spec" + count, dcm.())));
        count++;
      } */

      // what about just storing first ??
      Grib2SectionIdentification ids = first.getId();
      indexBuilder.setCenter(ids.getCenter_id());
      indexBuilder.setSubcenter(ids.getSubcenter_id());
      indexBuilder.setMaster(ids.getMaster_table_version());
      indexBuilder.setLocal(ids.getLocal_table_version());

      Grib2Pds pds = first.getPDS();
      indexBuilder.setGenProcessType(pds.getGenProcessType());
      indexBuilder.setGenProcessId(pds.getGenProcessId());
      indexBuilder.setBackProcessId(pds.getBackProcessId());

      GribCollectionProto.GribCollectionIndex index = indexBuilder.build();
      byte[] b = index.toByteArray();
      NcStream.writeVInt(raf, b.length); // message size
      raf.write(b); // message  - all in one gulp
      f.format("  write GribCollectionIndex= %d bytes%n", b.length);

    } finally {
      f.format("  file size =  %d bytes%n", raf.length());
      raf.close();
      if (raf != null) raf.close();
    }
  }