Ejemplo n.º 1
0
 private boolean addAxisType(NetcdfDataset ds, String vname, AxisType a) {
   if (vname == null) return false;
   Variable v = ds.findVariable(vname);
   if (v == null) return false;
   addAxisType(v, a);
   return true;
 }
Ejemplo n.º 2
0
  private JoinArray readJoinArray(NetcdfDataset ds, Element joinElement) {
    JoinArray.Type type = JoinArray.Type.valueOf(joinElement.getAttributeValue("type"));
    Element paramElem = joinElement.getChild("param");
    String paramS = paramElem.getText();
    Integer param = Integer.parseInt(paramS);

    Element varElem = joinElement.getChild("variable");
    String varName = varElem.getText();
    VariableDS v = (VariableDS) ds.findVariable(varName);
    return new JoinArray(v, type, param);
  }
  /**
   * Set extra information used by station obs datasets. Use stnIdVName or stnIndexVName.
   *
   * @param stnIdVName the obs variable that is used to find the station in the stnHash; may be type
   *     int or a String (char).
   * @param stnDescVName optional station var containing station description
   */
  public void setStationInfo(
      String stnIdVName, String stnDescVName, String stnIndexVName, StationHelper stationHelper) {
    this.stnIdVName = stnIdVName;
    this.stnDescVName = stnDescVName;
    this.stnIndexVName = stnIndexVName;
    this.stationHelper = stationHelper;

    if (stnIdVName != null) {
      Variable stationVar = ncfile.findVariable(stnIdVName);
      stationIdType = stationVar.getDataType();
    }
  }
  /**
   * Constructor.
   *
   * @param ncfile the netccdf file
   * @param typedDataVariables list of data variables; all record variables will be added to this
   *     list, except . You can remove extra
   * @param obsTimeVName observation time variable name (required)
   * @param nomTimeVName nominal time variable name (may be null)
   * @throws IllegalArgumentException if ncfile has no unlimited dimension and recDimName is null.
   */
  public RecordDatasetHelper(
      NetcdfDataset ncfile,
      String obsTimeVName,
      String nomTimeVName,
      List<VariableSimpleIF> typedDataVariables,
      String recDimName,
      Formatter errBuffer) {
    this.ncfile = ncfile;
    this.obsTimeVName = obsTimeVName;
    this.nomTimeVName = nomTimeVName;
    this.errs = errBuffer;

    // check if we already have a structure vs if we have to add it.

    if (this.ncfile.hasUnlimitedDimension()) {
      this.ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      this.recordVar = (StructureDS) this.ncfile.getRootGroup().findVariable("record");
      this.obsDim = ncfile.getUnlimitedDimension();

    } else {
      if (recDimName == null)
        throw new IllegalArgumentException(
            "File <"
                + this.ncfile.getLocation()
                + "> has no unlimited dimension, specify psuedo record dimension with observationDimension global attribute.");
      this.obsDim = this.ncfile.getRootGroup().findDimension(recDimName);
      this.recordVar = new StructurePseudoDS(this.ncfile, null, "record", null, obsDim);
    }

    // create member variables
    List<Variable> recordMembers = ncfile.getVariables();
    for (Variable v : recordMembers) {
      if (v == recordVar) continue;
      if (v.isScalar()) continue;
      if (v.getDimension(0) == this.obsDim) typedDataVariables.add(v);
    }

    // need the time units
    Variable timeVar = ncfile.findVariable(obsTimeVName);
    String timeUnitString =
        ncfile.findAttValueIgnoreCase(timeVar, CDM.UNITS, "seconds since 1970-01-01");
    try {
      timeUnit = new DateUnit(timeUnitString);
    } catch (Exception e) {
      if (null != errs) errs.format("Error on string = %s == %s%n", timeUnitString, e.getMessage());
      try {
        timeUnit = new DateUnit("seconds since 1970-01-01");
      } catch (Exception e1) {
        // cant happen
      }
    }
  }
Ejemplo n.º 5
0
  /**
   * Clones the attributes of the input file(s) to the output file
   *
   * @param infile
   * @param inVarName
   * @param outfile
   * @param outVarName
   */
  private void cloneAttributes(
      NetcdfDataset infile, String inVarName, NetcdfFileWriteable outfile, String outVarName) {

    // Find the variable

    Variable vi = infile.findVariable(inVarName);

    List<Attribute> l = vi.getAttributes();

    for (Attribute a : l) {
      outfile.addVariableAttribute(outVarName, a);
    }
  }
  @Test
  public void testH5StructureDS() throws java.io.IOException {
    int a_name = 0;
    String[] b_name =
        new String[] {
          "A fight is a contract that takes two people to honor.",
          "A combative stance means that you've accepted the contract.",
          "In which case, you deserve what you get.",
          "  --  Professor Cheng Man-ch'ing"
        };
    String c_name = "Hello!";

    // H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header"));
    try (NetcdfDataset ncfile =
        NetcdfDataset.openDataset(TestH5.testDir + "complex/compound_complex.h5")) {

      Variable dset = ncfile.findVariable("CompoundComplex");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 6);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 6);

      Structure s = (Structure) dset;

      // read all with the iterator
      StructureDataIterator iter = s.getStructureIterator();
      while (iter.hasNext()) {
        StructureData sd = iter.next();
        assert sd.getScalarInt("a_name") == a_name;
        a_name++;
        assert sd.getScalarString("c_name").equals(c_name);
        String[] results = sd.getJavaArrayString(sd.findMember("b_name"));
        assert results.length == b_name.length;
        int count = 0;
        for (String r : results) assert r.equals(b_name[count++]);

        for (StructureMembers.Member m : sd.getMembers()) {
          Array data = sd.getArray(m);
          NCdumpW.printArray(data, m.getName(), out, null);
        }
      }
    }
    System.out.println("*** testH5StructureDS ok");
  }
  public void setLocationInfo(String latVName, String lonVName, String zcoordVName) {
    this.latVName = latVName;
    this.lonVName = lonVName;
    this.zcoordVName = zcoordVName;

    // check for meter conversion
    if (zcoordVName != null) {
      Variable v = ncfile.findVariable(zcoordVName);
      zcoordUnits = ncfile.findAttValueIgnoreCase(v, CDM.UNITS, null);
      if (zcoordUnits != null)
        try {
          altScaleFactor = getMetersConversionFactor(zcoordUnits);
        } catch (Exception e) {
          if (errs != null) errs.format("%s", e.getMessage());
        }
    }
  }
Ejemplo n.º 8
0
  public static void doVar(NetcdfDataset netcdfdataset, String varS, String limits) {
    System.out.println("\r---------\r\r");
    ucar.nc2.Variable var = netcdfdataset.findVariable(varS);
    System.out.println(var.getName() + " " + var.getSize());

    Array arr;
    try {
      arr = var.read(limits);
      NCdump.printArray(arr, "array", System.out, null);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 public String getReadOk() {
   if (!getComplete().equals("true")) return "false";
   if (!getBitsOk().equals("true")) return "false";
   if (!doRead) return "N/A";
   if (readOk == 0)
     try {
       NetcdfDataset ncd = getBufrMessageAsDataset(m);
       SequenceDS v = (SequenceDS) ncd.findVariable(BufrIosp.obsRecord);
       StructureDataIterator iter = v.getStructureIterator(-1);
       while (iter.hasNext()) {
         iter.next();
       }
       readOk = 1;
     } catch (Exception e) {
       readOk = 2;
     }
   return readOk == 1 ? "true" : "false";
 }
  private void setObs(Message m) {

    java.util.List<ObsBean> beanList = new ArrayList<ObsBean>();
    try {
      NetcdfDataset ncd = getBufrMessageAsDataset(m);
      Variable v = ncd.findVariable(BufrIosp.obsRecord);
      if ((v != null) && (v instanceof Structure)) {
        Structure obs = (Structure) v;
        StructureDataIterator iter = obs.getStructureIterator();
        while (iter.hasNext()) {
          beanList.add(new ObsBean(obs, iter.next()));
        }
      }
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(BufrMessageViewer.this, ex.getMessage());
      ex.printStackTrace();
    }
    obsTable.setBeans(beanList);
  }
  public static void main(String args[]) throws Exception {
    long start = System.currentTimeMillis();
    Map<String, ucar.unidata.geoloc.Station> staHash =
        new HashMap<String, ucar.unidata.geoloc.Station>();

    String location = "R:/testdata/sounding/netcdf/Upperair_20070401_0000.nc";
    NetcdfDataset ncfile = NetcdfDataset.openDataset(location);
    ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);

    // look through record varibles, for those that have "manLevel" dimension
    // make a StructureData object for those
    StructureMembers sm = new StructureMembers("manLevel");
    Dimension manDim = ncfile.findDimension("manLevel");
    Structure record = (Structure) ncfile.findVariable("record");
    List<Variable> allList = record.getVariables();
    List<VariableSimpleIF> varList = new ArrayList<VariableSimpleIF>();
    for (Variable v : allList) {
      if ((v.getRank() == 1) && v.getDimension(0).equals(manDim)) {
        // public VariableDS(NetcdfDataset ds, Group group, Structure parentStructure, String
        // shortName, DataType dataType,
        // String dims, String units, String desc) {
        varList.add(
            new VariableDS(
                ncfile,
                null,
                null,
                v.getShortName(),
                v.getDataType(),
                "",
                v.getUnitsString(),
                v.getDescription()));
        // (String name, String desc, String units, DataType dtype, int []shape)
        sm.addMember(
            v.getShortName(),
            v.getDescription(),
            v.getUnitsString(),
            v.getDataType(),
            new int[0]); // scalar
      }
    }

    ArrayStructureMA manAS = new ArrayStructureMA(sm, new int[] {manDim.getLength()});

    // need the date units
    Variable time = ncfile.findVariable("synTime");
    String timeUnits = ncfile.findAttValueIgnoreCase(time, "units", null);
    timeUnits = StringUtil.remove(timeUnits, '('); // crappy fsl'ism
    timeUnits = StringUtil.remove(timeUnits, ')');
    DateUnit timeUnit = new DateUnit(timeUnits);

    // extract stations
    int nrecs = 0;
    StructureDataIterator iter = record.getStructureIterator();
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      String name = sdata.getScalarString("staName");
      ucar.unidata.geoloc.Station s = staHash.get(name);
      if (s == null) {
        float lat = sdata.convertScalarFloat("staLat");
        float lon = sdata.convertScalarFloat("staLon");
        float elev = sdata.convertScalarFloat("staElev");
        s = new StationImpl(name, "", lat, lon, elev);
        staHash.put(name, s);
      }
      nrecs++;
    }
    List<ucar.unidata.geoloc.Station> stnList =
        Arrays.asList(staHash.values().toArray(new ucar.unidata.geoloc.Station[staHash.size()]));
    Collections.sort(stnList);

    // create the writer
    WriterProfileObsDataset writer =
        new WriterProfileObsDataset(location + ".out", "rewrite " + location);
    writer.writeHeader(stnList, varList, nrecs, "prMan");

    // extract records
    iter = record.getStructureIterator();
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      String name = sdata.getScalarString("staName");
      double timeValue = sdata.convertScalarDouble("synTime");
      Date date = timeUnit.makeDate(timeValue);

      // transfer to the ArrayStructure
      List<String> names = sm.getMemberNames();
      for (String mname : names) {
        manAS.setMemberArray(mname, sdata.getArray(mname));
      }

      // each level is weritten as a seperate structure
      int numMand = sdata.getScalarInt("numMand");
      if (numMand >= manDim.getLength()) continue;

      for (int i = 0; i < numMand; i++) {
        StructureData useData = manAS.getStructureData(i);
        writer.writeRecord(name, date, useData);
      }
    }

    writer.finish();

    long took = System.currentTimeMillis() - start;
    System.out.println("That took = " + took);
  }
Ejemplo n.º 12
0
  public static void generateObs(NetcdfDataset netcdfdataset) {
    try {
      ucar.nc2.Variable lon = netcdfdataset.findVariable("lon");
      ucar.nc2.Variable lat = netcdfdataset.findVariable("lat");
      ucar.nc2.Variable time = netcdfdataset.findVariable("time");
      ucar.nc2.Variable BAssta = netcdfdataset.findVariable("BAssta");

      Array dataLat = lat.read();
      int[] shapeLat = lat.getShape();
      Index indexLat = dataLat.getIndex();

      Array dataLon = lon.read();
      int[] shapeLon = lon.getShape();
      Index indexLon = dataLon.getIndex();

      Array dataTime = time.read();
      Index indexTime = dataTime.getIndex();

      // mbari
      double minLat = 34.955;
      double maxLat = 38.260;
      double minLon = -124.780;
      double maxLon = -121.809;
      int minIndexLat = -Integer.MAX_VALUE;
      int minIndexLon = Integer.MAX_VALUE;
      int maxIndexlat = Integer.MIN_VALUE;
      int maxIndexLon = Integer.MIN_VALUE;

      // lon is ginve as +

      //			int[][] indexLatLon = new int[shapeLat[0]][shapeLon[0]];
      List lats = new ArrayList<Integer>();
      List lons = new ArrayList<Integer>();

      // get lats

      for (int i = 0; i < shapeLat[0]; i++) {
        Double lat_d = dataLat.getDouble(indexLat.set(i));
        if (lat_d >= minLat && lat_d <= maxLat) {
          minIndexLat = i;
          lats.add(i);
        }
      }

      for (int j = 0; j < shapeLon[0]; j++) {
        Double lon_d = dataLon.getDouble(indexLon.set(j));
        if (lon_d > 180) {
          lon_d = -360 + lon_d;
        }
        if (lon_d >= minLon && lon_d <= maxLon) {
          lons.add(j);
        }
      }

      // get values:

      ucar.nc2.Variable bassta = netcdfdataset.findVariable("BAssta");
      int indMinLat = (Integer) lats.get(0);
      int indMaxLat = (Integer) lats.get(lats.size() - 1);
      int indMinLon = (Integer) lons.get(0);
      int indMaxLon = (Integer) lons.get(lons.size() - 1);
      int indMinTime = 0;
      int indMaxTime = time.getShape()[0] - 1;

      String s = createString(indMinLat, indMaxLat, indMinLon, indMaxLon, indMinTime, indMaxTime);
      // doVar(netcdfdataset, "BAssta", s);
      // Array dataVar= bassta.read();
      // bassta.getShape()

      // System.out.println("lats size: " + lats.size()+" "+"lons size:
      // "+lons.size());

      // for a given lat lon get time series
      // 1st lat lon
      s = createString(indMinLat, indMinLat, indMinLon, indMinLon, indMinTime, indMaxTime);

      Array arr = BAssta.read(s);
      int[] shapeArr = arr.getShape();
      Index index = arr.getIndex();
      System.out.println(shapeArr);
      // [74, 1, 1, 1]
      for (int i = 0; i < shapeArr[0]; i++) {

        Double d = arr.getDouble(index.set(i, 0, 0, 0));

        Double t = dataTime.getDouble(indexTime.set(i));

        String timeS = TimeUtil.getISOFromMillisec(t * 1000);

        System.out.println(t + " " + timeS + " " + d);
      }

      // lat lon are fixed, only varying time
      //			basta[time,alt,lat,lon]

      //			NCdump.printArray(arr, "array", System.out, null);

      //			doVar(netcdfdataset, "BAssta", s);
      //			doVar(netcdfdataset, "time", indMinTime + ":" + indMaxTime);

      // while (i < lon.getSize()) {
      // System.out.println("obs" + i + " " + lat + " " + lon);
      // i++;
      // }

    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Ejemplo n.º 13
0
  public static NetcdfDataset buildStation(IObservationGroup obsGroup) {
    if (obsGroup.getDepths().length > 1) {
      return buildStationProfile(obsGroup);
    }
    NetcdfDataset ncds = null;
    File temp = null;
    try {
      /* Instantiate an empty NetcdfDataset object from the template ncml */
      temp = File.createTempFile("ooi", ".ncml");
      ncds = getNcdsFromTemplate(temp, "station.ncml");

      Map<String, String> allAttributes = new HashMap<String, String>();
      allAttributes.putAll(obsGroup.getAttributes());

      /* Do the station Id */
      ArrayInt.D0 aid = new ArrayInt.D0();
      aid.set(obsGroup.getId());
      ncds.findVariable("stnId").setCachedData(aid);

      /* Do the times */
      Number[] times = obsGroup.getTimes();
      IObservationGroup.DataType tdt = obsGroup.getTimeDataType();
      ncds.findDimension("time").setLength(times.length);
      Variable tvar = ncds.findVariable("time");
      tvar.setDataType(getNcDataType(tdt));
      tvar.setCachedData(getNcArray(times, tdt));

      /* Do the lat & lon */
      IObservationGroup.DataType lldt = obsGroup.getLatLonDataType();
      DataType ncdtLl = getNcDataType(lldt);
      Variable laVar = ncds.findVariable("lat");
      laVar.setDataType(ncdtLl);
      laVar.setCachedData(getNcScalar(obsGroup.getLat(), lldt));
      Variable loVar = ncds.findVariable("lon");
      loVar.setDataType(ncdtLl);
      loVar.setCachedData(getNcScalar(obsGroup.getLon(), lldt));

      /* Do the depth */
      IObservationGroup.DataType ddt = obsGroup.getDepthDataType();
      VariableDS zVar =
          new VariableDS(
              ncds, null, null, "stnDepth", getNcDataType(ddt), "", "m", "station depth");
      zVar.addAttribute(new Attribute("positive", "down"));
      ncds.addVariable(null, zVar);
      Number depth = obsGroup.getDepths()[0];
      zVar.setCachedData(getNcScalar(obsGroup.getDepths()[0], ddt));

      /* Do the data variables */
      for (VariableParams dn : obsGroup.getDataNames()) {
        DataType ncdtData = getNcDataType(dn.getDataType());
        VariableDS dvar =
            new VariableDS(
                ncds,
                null,
                null,
                dn.getShortName(),
                ncdtData,
                "time",
                dn.getUnits(),
                dn.getDescription());
        dvar.addAttribute(new Attribute(CF.COORDINATES, "time lon lat"));
        dvar.addAttribute(new Attribute(CF.STANDARD_NAME, dn.getStandardName()));
        Array adata = Array.factory(ncdtData, new int[] {times.length});
        IndexIterator aii = adata.getIndexIterator();
        dvar.setCachedData(adata);
        ncds.addVariable(null, dvar);

        for (int ti = 0; ti < times.length; ti++) {
          putArrayData(aii, ncdtData, obsGroup.getData(dn, times[ti], depth));
        }
      }

      /* Add global attributes */
      for (String key : allAttributes.keySet()) {
        ncds.addAttribute(null, new Attribute(key, allAttributes.get(key)));
      }

    } catch (FileNotFoundException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      if (temp != null) {
        if (!temp.delete()) {
          temp.deleteOnExit();
        }
      }
      if (ncds != null) {
        ncds.finish();
      }
    }

    return ncds;
  }
Ejemplo n.º 14
0
  public static NetcdfDataset buildTrajectory(List<IObservationGroup> obsGroups) {
    //        IObservationGroup.DataType tdt = IObservationGroup.DataType.values()[0];
    //        IObservationGroup.DataType lldt = IObservationGroup.DataType.values()[0];
    //        IObservationGroup.DataType ddt = IObservationGroup.DataType.values()[0];
    //        for(IObservationGroup og : obsGroups) {
    //            tdt = (og.getTimeDataType().compareTo(tdt) > 0) ? og.getTimeDataType() : tdt;
    //            lldt = (og.getLatLonDataType().compareTo(lldt) > 0) ? og.getLatLonDataType() :
    // lldt;
    //            ddt = (og.getDepthDataType().compareTo(ddt) > 0) ? og.getDepthDataType() : ddt;
    //        }

    NetcdfDataset ncds = null;
    File temp = null;
    try {
      /* Instantiate an empty NetcdfDataset object from the template ncml */
      temp = File.createTempFile("ooi", ".ncml");
      ncds = getNcdsFromTemplate(temp, "trajectory.ncml");

      int nobs = obsGroups.size();
      List<Number> allDepths = new ArrayList<Number>();
      List<VariableParams> allDn = new ArrayList<VariableParams>();
      int nt = nobs;
      int nd = 0;
      for (IObservationGroup og : obsGroups) {
        nd = Math.max(nd, og.getDepths().length);
        for (Number d : og.getDepths()) {
          if (!allDepths.contains(d)) {
            allDepths.add(d);
          }
        }
        for (VariableParams dn : og.getDataNames()) {
          if (!allDn.contains(dn)) {
            allDn.add(dn);
          }
        }
      }

      /* Do the trajectory ID */

      /* Do the times */
      Number[] times = obsGroups.get(0).getTimes();
      IObservationGroup.DataType tdt = obsGroups.get(0).getTimeDataType();
      DataType ncdtTime = getNcDataType(tdt);
      ncds.findDimension("time").setLength(nt);
      Array tarr = Array.factory(ncdtTime, new int[] {nt});
      IndexIterator tii = tarr.getIndexIterator();
      Variable tvar = ncds.findVariable("time");
      tvar.setDataType(getNcDataType(tdt));
      tvar.setCachedData(tarr);

      /* Do the lats */
      IObservationGroup.DataType lldt = obsGroups.get(0).getLatLonDataType();
      DataType ncdtLl = getNcDataType(lldt);
      Array laarr = Array.factory(ncdtLl, new int[] {nt});
      IndexIterator laii = laarr.getIndexIterator();
      Variable lavar = ncds.findVariable("lat");
      lavar.setDataType(ncdtLl);
      lavar.setCachedData(laarr);

      /* Do the lons */
      Array loarr = Array.factory(ncdtLl, new int[] {nt});
      IndexIterator loii = loarr.getIndexIterator();
      Variable lovar = ncds.findVariable("lon");
      lovar.setDataType(ncdtLl);
      lovar.setCachedData(loarr);

      /* Iterate over the observation groups and fill the data */
      Map<String, String> allAttributes = new HashMap<String, String>();
      IObservationGroup og;
      HashMap<String, IndexIterator> darrs = new HashMap<String, IndexIterator>();
      Number time;
      Number depth = allDepths.get(0);
      for (int obs = 0; obs < nobs; obs++) {
        og = obsGroups.get(obs);
        time = og.getTimes()[0];
        putArrayData(tii, ncdtTime, time);
        putArrayData(loii, ncdtLl, og.getLon());
        putArrayData(laii, ncdtLl, og.getLat());

        for (VariableParams dn : allDn) {
          if (og.getDataNames().contains(dn)) {
            DataType ncdtData = getNcDataType(dn.getDataType());
            VariableDS dvar = (VariableDS) ncds.findVariable(dn.getShortName());
            if (dvar == null) {
              dvar =
                  new VariableDS(
                      ncds,
                      null,
                      null,
                      dn.getShortName(),
                      ncdtData,
                      "time",
                      dn.getUnits(),
                      dn.getDescription());
              dvar.addAttribute(new Attribute(CF.COORDINATES, "time lon lat"));
              //                            dvar.addAttribute(new Attribute("missing_value",
              // missingData));
              dvar.addAttribute(new Attribute(CF.STANDARD_NAME, dn.getStandardName()));
              Array darr = Array.factory(ncdtData, new int[] {nt});
              dvar.setCachedData(darr);

              darrs.put(dn.getStandardName(), darr.getIndexIterator());
              ncds.addVariable(null, dvar);
            }
            putArrayData(darrs.get(dn.getStandardName()), ncdtData, og.getData(dn, time, depth));

          } else {
            /*
             * station doesn't have this variable - don't believe
             * this can even happen...
             *
             * NOTE: This would indicate a problem with the above processing where the data-name list
             * has been modified prior to contains-checking
             */
          }
        }
      }

      /* Add global attributes */
      for (String key : allAttributes.keySet()) {
        ncds.addAttribute(null, new Attribute(key, allAttributes.get(key)));
      }
    } catch (FileNotFoundException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(NcdsFactory.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      if (temp != null) {
        if (!temp.delete()) {
          temp.deleteOnExit();
        }
      }
      if (ncds != null) {
        ncds.finish();
      }
    }

    return ncds;
  }
Ejemplo n.º 15
0
  /**
   * Creates the w file
   *
   * @param uFile - the input east-west velocity file
   * @param vFile - the input north-south velocity file
   * @throws InvalidRangeException
   * @throws IOException
   */
  private void generate(NetcdfDataset uFile, NetcdfDataset vFile)
      throws InvalidRangeException, IOException {

    // Set up empty arrays for ranges and dimensions.

    dims = new ArrayList<Dimension>();

    // Create the output file

    outfile = NetcdfFileWriteable.createNew(outputWFile, false);

    // Construct the data set dimensions - Time, Depth, Latitude and
    // Longitude (in order)

    int uTimeLength = uFile.findDimension(inTimeName).getLength();
    int uLayerLength = uFile.findDimension(inLayerName).getLength();
    int uXLength = uFile.findDimension(inXName).getLength();
    int uYLength = uFile.findDimension(inYName).getLength();

    int vTimeLength = vFile.findDimension(inTimeName).getLength();
    int vLayerLength = vFile.findDimension(inLayerName).getLength();
    int vXLength = vFile.findDimension(inXName).getLength();
    int vYLength = vFile.findDimension(inYName).getLength();

    Dimension timeDim = outfile.addDimension(outTimeName, Math.min(uTimeLength, vTimeLength));
    Dimension layerDim = outfile.addDimension(outLayerName, Math.min(uLayerLength, vLayerLength));
    Dimension yDim = outfile.addDimension(outYName, Math.min(uYLength, vYLength));
    Dimension xDim = outfile.addDimension(outXName, Math.min(uXLength, vXLength));

    // Add to a list - this becomes the coordinate system for the output
    // variable. The order is *very* important!

    dims.add(timeDim);
    dims.add(layerDim);
    dims.add(yDim);
    dims.add(xDim);

    // Create variables in the output file

    outfile.addVariable(outTimeName, DataType.DOUBLE, new Dimension[] {timeDim});
    outfile.addVariable(outLayerName, DataType.DOUBLE, new Dimension[] {layerDim});
    outfile.addVariable(outLatName, DataType.DOUBLE, new Dimension[] {yDim, xDim});
    outfile.addVariable(outLonName, DataType.DOUBLE, new Dimension[] {yDim, xDim});

    // outfile.setLargeFile(true);
    outfile.addVariable(outVarName, DataType.FLOAT, dims);

    // Add attribute information (cloned from source)

    cloneAttributes(uFile, inTimeName, outfile, outTimeName);
    cloneAttributes(uFile, inLayerName, outfile, outLayerName);
    cloneAttributes(uFile, inLatName, outfile, outLatName);
    cloneAttributes(uFile, inLonName, outfile, outLonName);

    // Finalizes the structure of the output file, making the changes real.

    outfile.create();

    // Write the static information for 1D axes.

    outfile.write(outTimeName, uFile.findVariable(inTimeName).read());
    outfile.write(outLayerName, uFile.findVariable(inLayerName).read());
    outfile.write(outLatName, uFile.findVariable(inLatName).read());
    outfile.write(outLonName, uFile.findVariable(inLonName).read());

    Variable u = uFile.findVariable(inUName);
    Variable v = vFile.findVariable(inVName);

    Variable latdim = uFile.findVariable(inLatName);
    Variable londim = uFile.findVariable(inLonName);
    Variable zdim = uFile.findVariable(inLayerName);

    // Read the depth array

    Array depth_array = zdim.read(new int[] {0}, new int[] {uLayerLength});

    double[] depths;

    if (depth_array.getClass().getName().contains("Float")) {

      float[] tmp = (float[]) depth_array.copyTo1DJavaArray();
      depths = new double[tmp.length];

      for (int i = 0; i < tmp.length; i++) {
        depths[i] = tmp[i];
      }
    } else {
      depths = (double[]) depth_array.copyTo1DJavaArray();
    }

    // Determine the size and midpoint of the depth bins

    double[] binz = new double[depths.length - 1];
    float[] mpz = new float[depths.length - 1];

    for (int i = 0; i < depths.length - 1; i++) {
      binz[i] = depths[i + 1] - depths[i];
      mpz[i] = (float) (depths[i + 1] + depths[i]) / 2;
    }

    float[] nan = new float[uLayerLength];
    for (int n = 0; n < uLayerLength; n++) {
      nan[n] = Float.NaN;
    }

    // Write the collar as NaNs.

    Array nana = Array.factory(java.lang.Float.class, new int[] {1, uLayerLength, 1, 1}, nan);
    for (int t = 0; t < u.getShape(0); t++) {
      for (int j = 0; j < u.getShape(3); j++) {
        outfile.write(outVarName, new int[] {t, 0, 0, j}, nana);
        outfile.write(outVarName, new int[] {t, 0, u.getShape(2) - 1, j}, nana);
      }
      for (int i = 1; i < u.getShape(2) - 1; i++) {
        outfile.write(outVarName, new int[] {t, 0, i, 0}, nana);
        outfile.write(outVarName, new int[] {t, 0, i, u.getShape(3) - 1}, nana);
      }
    }

    // Calculate w values for each time, depth, lat and lon
    // using 3x3 (horizontal) vertical pencils

    for (int t = 0; t < u.getShape(0); t++) {
      for (int i = 1; i < u.getShape(2) - 1; i++) {
        Array lta =
            latdim
                .read(
                    new int[] {i - 1, 0}, // move for
                    // speed
                    new int[] {3, 1})
                .reduce();
        double currentLat = lta.getDouble(1);
        for (int j = 1; j < u.getShape(3) - 1; j++) {

          float[] w_arr = new float[uLayerLength];

          if (i == 0 || j == 0 || i == u.getShape(2) - 1 || j == u.getShape(3) - 1) {

            for (int n = 0; n < w_arr.length; n++) {
              w_arr[n] = Float.NaN;
            }
            continue;
          }

          Array lna = londim.read(new int[] {0, j - 1}, new int[] {1, 3}).reduce();

          double currentLon = lna.getDouble(1);
          float maxZ = bathy.value_at_lonlat((float) currentLon, (float) currentLat);

          boolean alwaysNaN = true;

          for (int k = u.getShape(1) - 1; k >= 0; k--) {

            // If the bathymetry says we're on land, then all values
            // will be NaN. This type of check is required because
            // the value does not equal java.Float.NaN, but
            // 1.2676506E30.

            if (maxZ > bathy_cutoff) {
              w_arr[k] = Float.NaN;
              continue;
            }

            if (k == 0) {
              if (!alwaysNaN) {
                w_arr[k] = 0;
              } else {
                w_arr[k] = Float.NaN;
              }
              continue;
            }

            if (maxZ < mpz[k - 1]) {
              w_arr[k] = Float.NaN;
              continue;
            }

            Array ua = u.read(new int[] {t, k, i - 1, j - 1}, new int[] {1, 1, 3, 3}).reduce();
            Array va = v.read(new int[] {t, k, i - 1, j - 1}, new int[] {1, 1, 3, 3}).reduce();

            float dz = calcdz(k, mpz, maxZ);

            if (Float.isNaN(dz)) {
              w_arr[k] = dz;
              continue;
            }

            float dwdz = calcdwdz(lta, lna, ua, va);
            if (Float.isNaN(dwdz)) {
              continue;
            }

            if (alwaysNaN) {
              w_arr[k] = dwdz;
              alwaysNaN = false;
            } else {
              w_arr[k] = w_arr[k + 1] + dwdz;
            }
          }
          Array warra =
              Array.factory(java.lang.Float.class, new int[] {1, uLayerLength, 1, 1}, w_arr);

          outfile.write(outVarName, new int[] {t, 0, i, j}, warra);
        }
        System.out.println("\tRow " + i + " complete.");
      }
      System.out.printf("Time %d of " + (u.getShape(0)) + " is complete.\n", t + 1);
    }
  }