Package ucar.nc2.units

Examples of ucar.nc2.units.DateFormatter


        Array dataT = Array.factory(DataType.DOUBLE.getPrimitiveClassType(), new int[] { 1 }, tdata);

        taxis.setCachedData(dataT, false);

        DateFormatter formatter = new DateFormatter();

        taxis.addAttribute(new Attribute("units", "msecs since " + formatter.toDateTimeStringISO(new Date(0))));
        ncfile.addVariable(null, taxis);
        dims.add(dimT);

        Dimension jDim = new Dimension("lat", numY, true, false, false);
        Dimension iDim = new Dimension("lon", numX, true, false, false);
View Full Code Here


    assert time.getDimension(0) == ncfile.findDimension("time");

    String units = time.getUnitsString();
    DateUnit du = new DateUnit(units);
    DateFormatter df = new DateFormatter();

    String[] result = new String[] {"2006-06-07T12:00:00Z",   "2006-06-07T13:00:00Z",   "2006-06-07T14:00:00Z"};
    try {
      Array data = time.read();
      assert data.getRank() == 1;
      assert data.getSize() == 3;
      assert data.getShape()[0] == 3;
      assert data.getElementType() == double.class;

      NCdump.printArray(data, "time coord", System.out, null);

      int count = 0;
      IndexIterator dataI = data.getIndexIterator();
      while (dataI.hasNext()) {
        double val = dataI.getDoubleNext();
        Date dateVal = du.makeDate(val);
        String dateS = df.toDateTimeStringISO(dateVal);
        assert dateS.equals( result[count]) : dateS+" != "+ result[count];
        count++;
      }

    } catch (IOException io) {
View Full Code Here

        int numLines    = ad.getLines();
        int numBands    = ad.getNumberOfBands();
        bandMap  = ad.getBands();
        navBlock = af.getNav();
        Date          nomTime = ad.getNominalTime();
        DateFormatter df      = new DateFormatter();
        try {
            nav = AREAnav.makeAreaNav(navBlock, af.getAux());
        } catch (McIDASException me) {
            throw new AreaFileException(me.getMessage());
        }
        int    sensor  = dirBlock[af.AD_SENSORID];
        String calName = McIDASUtil.intBitsToString(dirBlock[af.AD_CALTYPE]);
        int    calType = getCalType(calName);

        // TODO:  Need to support calibrated data.
        if ((af.getCal() != null)
                && CalibratorFactory.hasCalibrator(sensor)) {
            //System.out.println("can calibrate");
            try {
                calibrator = CalibratorFactory.getCalibrator(sensor, calType,
                        af.getCal());
            } catch (CalibratorException ce) {
                // System.out.println("can't make calibrator");
                calibrator = null;
            }
            //System.out.println("calibrator = " + calibrator);

        }
        calUnit  = ad.getCalibrationUnitName();
        calScale = (1.0f / ad.getCalibrationScaleFactor());

        // make the dimensions
        Dimension elements     = new Dimension("elements", numElements, true);
        Dimension       lines  = new Dimension("lines", numLines, true);
        Dimension       bands  = new Dimension("bands", numBands, true);
        Dimension       time   = new Dimension("time", 1, true);
        Dimension       dirDim = new Dimension("dirSize", af.AD_DIRSIZE,
                                     true);
        Dimension navDim = new Dimension("navSize", navBlock.length, true);
        List<Dimension> image  = new ArrayList<Dimension>();
        image.add(time);
        image.add(bands);
        image.add(lines);
        image.add(elements);
        ncfile.addDimension(null, elements);
        ncfile.addDimension(null, lines);
        ncfile.addDimension(null, bands);
        ncfile.addDimension(null, time);
        ncfile.addDimension(null, dirDim);
        ncfile.addDimension(null, navDim);


        Array varArray;

        // make the variables

        // time
        Variable timeVar = new Variable(ncfile, null, null, "time");
        timeVar.setDataType(DataType.INT);
        timeVar.setDimensions("time");
        timeVar.addAttribute(new Attribute("units",
                                           "seconds since "
                                           + df.toDateTimeString(nomTime)));
        timeVar.addAttribute(new Attribute("long_name", "time"));
        varArray = new ArrayInt.D1(1);
        ((ArrayInt.D1) varArray).set(0, 0);
        timeVar.setCachedData(varArray, false);
        ncfile.addVariable(null, timeVar);


        // lines and elements
        Variable lineVar = new Variable(ncfile, null, null, "lines");
        lineVar.setDataType(DataType.INT);
        lineVar.setDimensions("lines");
        //lineVar.addAttribute(new Attribute("units", "km"));
        lineVar.addAttribute(new Attribute("standard_name",
                                           "projection_y_coordinate"));
        varArray = new ArrayInt.D1(numLines);
        for (int i = 0; i < numLines; i++) {
            int pos = nav.isFlippedLineCoordinates()
                      ? i
                      : numLines - i - 1;
            ((ArrayInt.D1) varArray).set(i, pos);
        }
        lineVar.setCachedData(varArray, false);
        ncfile.addVariable(null, lineVar);

        Variable elementVar = new Variable(ncfile, null, null, "elements");
        elementVar.setDataType(DataType.INT);
        elementVar.setDimensions("elements");
        //elementVar.addAttribute(new Attribute("units", "km"));
        elementVar.addAttribute(new Attribute("standard_name",
                "projection_x_coordinate"));
        varArray = new ArrayInt.D1(numElements);
        for (int i = 0; i < numElements; i++) {
            ((ArrayInt.D1) varArray).set(i, i);
        }
        elementVar.setCachedData(varArray, false);
        ncfile.addVariable(null, elementVar);


        // TODO: handle bands and calibrations
        Variable bandVar = new Variable(ncfile, null, null, "bands");
        bandVar.setDataType(DataType.INT);
        bandVar.setDimensions("bands");
        bandVar.addAttribute(new Attribute("long_name",
                                           "spectral band number"));
        bandVar.addAttribute(new Attribute("axis", "Z"));
        Array bandArray = new ArrayInt.D1(numBands);
        for (int i = 0; i < numBands; i++) {
            ((ArrayInt.D1) bandArray).set(i, bandMap[i]);
        }
        bandVar.setCachedData(bandArray, false);
        ncfile.addVariable(null, bandVar);

        // the image
        Variable imageVar = new Variable(ncfile, null, null, "image");
        imageVar.setDataType(DataType.INT);
        imageVar.setDimensions(image);
        setCalTypeAttributes(imageVar, getCalType(calName));
        imageVar.addAttribute(new Attribute(getADDescription(af.AD_CALTYPE),
                                            calName));
        imageVar.addAttribute(new Attribute("bands", bandArray));
        imageVar.addAttribute(new Attribute("grid_mapping", "AREAnav"));
        ncfile.addVariable(null, imageVar);


        Variable dirVar = new Variable(ncfile, null, null, "areaDirectory");
        dirVar.setDataType(DataType.INT);
        dirVar.setDimensions("dirSize");
        setAreaDirectoryAttributes(dirVar);
        ArrayInt.D1 dirArray = new ArrayInt.D1(AreaFile.AD_DIRSIZE);
        for (int i = 0; i < AreaFile.AD_DIRSIZE; i++) {
            dirArray.set(i, dirBlock[i]);
        }
        dirVar.setCachedData(dirArray, false);
        ncfile.addVariable(null, dirVar);

        Variable navVar = new Variable(ncfile, null, null, "navBlock");
        navVar.setDataType(DataType.INT);
        navVar.setDimensions("navSize");
        setNavBlockAttributes(navVar);
        ArrayInt.D1 navArray = new ArrayInt.D1(navBlock.length);
        for (int i = 0; i < navBlock.length; i++) {
            navArray.set(i, navBlock[i]);
        }
        navVar.setCachedData(navArray, false);
        ncfile.addVariable(null, navVar);


        // projection variable
        ProjectionImpl projection = new McIDASAreaProjection(af);
        Variable       proj = new Variable(ncfile, null, null, "AREAnav");
        proj.setDataType(DataType.CHAR);
        proj.setDimensions("");

        List params = projection.getProjectionParameters();
        for (int i = 0; i < params.size(); i++) {
            Parameter p = (Parameter) params.get(i);
            proj.addAttribute(new Attribute(p));
        }

        // For now, we have to overwrite the parameter versions of thes
        proj.addAttribute(
            new Attribute(
                "grid_mapping_name", McIDASAreaProjection.GRID_MAPPING_NAME));
        /*
        proj.addAttribute(new Attribute(McIDASAreaProjection.ATTR_AREADIR,
                                        dirArray));
        proj.addAttribute(new Attribute(McIDASAreaProjection.ATTR_NAVBLOCK,
                                        navArray));
        */
        varArray = new ArrayChar.D0();
        ((ArrayChar.D0) varArray).set(' ');
        proj.setCachedData(varArray, false);

        ncfile.addVariable(null, proj);

        // add the attributes
        ncfile.addAttribute(null, new Attribute("Conventions", "CF-1.0"));
        ncfile.addAttribute(null, new Attribute("netCDF-Java", "4.0"));
        ncfile.addAttribute(null,
                            new Attribute("nominal_image_time",
                                          df.toDateTimeString(nomTime)));
        String encStr = "netCDF encoded on "
                        + df.toDateTimeString(new Date());
        ncfile.addAttribute(null, new Attribute("history", encStr));

        //Lastly, finish the file
        ncfile.finish();
        return true;
View Full Code Here

    return new DateRange(timeDates[0], timeDates[timeDates.length - 1]);
  }

  @Override
  public List<NamedObject> getNames() {
    DateFormatter formatter = new DateFormatter();
    int n = (int) getSize();
    List<NamedObject> names = new ArrayList<NamedObject>(n);
    for (int i = 0; i < n; i++) {
      names.add(new NamedAnything(formatter.toDateTimeString(getTimeDate(i)), "date/time"));
    }
    return names;
  }
View Full Code Here

   * Dump this variable
   *
   * @return the variable
   */
  public String dump() {
    DateFormatter formatter = new DateFormatter();
    Formatter sbuff = new Formatter();
    sbuff.format("%s %d %n", name, records.size());
    for (GridRecord record : records) {
      sbuff.format(" level = %d %f", record.getLevelType1(), record.getLevel1());
      if (null != record.getValidTime())
        sbuff.format(" time = %s", formatter.toDateTimeString(record.getValidTime()));
      sbuff.format("%n");
    }
    return sbuff.toString();
  }
View Full Code Here

    result.addDimension(null, runDim);

    // deal with promoteGlobalAttribute
    // promoteGlobalAttributes((AggregationOuterDimension.DatasetOuterDimension) typicalDataset);

    DateFormatter dateFormatter = new DateFormatter();
    ProxyReader2D proxyReader2D = new ProxyReader2D();

    // extract a copy of the runtimes for thread safety
    // List<Date> runTimes = new ArrayList<Date>(fmrcInv.getRunTimes());

    // create runtime aggregation coordinate variable
    DataType coordType = DataType.DOUBLE; // LOOK getCoordinateType();
    VariableDS runtimeCoordVar = new VariableDS(result, null, null, runtimeDimName, coordType, runtimeDimName, null, null);
    runtimeCoordVar.addAttribute(new Attribute("long_name", "Run time for ForecastModelRunCollection"));
    runtimeCoordVar.addAttribute(new ucar.nc2.Attribute("standard_name", "forecast_reference_time"));
    runtimeCoordVar.addAttribute(new ucar.nc2.Attribute("units", "hours since " + dateFormatter.toDateTimeStringISO(lite.base)));
    runtimeCoordVar.addAttribute(new ucar.nc2.Attribute(_Coordinate.AxisType, AxisType.RunTime.toString()));
    result.removeVariable(null, runtimeCoordVar.getShortName());
    result.addVariable(null, runtimeCoordVar);
    if (logger.isDebugEnabled()) logger.debug("FmrcDataset: added runtimeCoordVar " + runtimeCoordVar.getFullName());

    // make the runtime coordinates
    Array runCoordVals = ArrayDouble.factory(DataType.DOUBLE, new int[] {nruns}, runOffset);
    runtimeCoordVar.setCachedData(runCoordVals);

    // make the time coordinate(s) as 2D
    List<Variable> nonAggVars = result.getVariables();
    for (FmrcInvLite.Gridset gridset : lite.gridSets) {
      Group newGroup = result.getRootGroup(); // can it be different ??

      //int noffsets = runSeq.getNTimeOffsets();
      Dimension timeDim = new Dimension(gridset.gridsetName, gridset.noffsets);
      result.removeDimension(null, gridset.gridsetName); // remove previous declaration, if any
      result.addDimension(null, timeDim);

      DataType dtype = DataType.DOUBLE;
      String dims = getRunDimensionName() + " " + gridset.gridsetName;
      VariableDS timeVar = new VariableDS(result, newGroup, null, gridset.gridsetName, dtype, dims, null, null); // LOOK could just make a CoordinateAxis1D
      timeVar.addAttribute(new Attribute("long_name", "Forecast time for ForecastModelRunCollection"));
      timeVar.addAttribute(new ucar.nc2.Attribute("standard_name", "time"));
      timeVar.addAttribute(new ucar.nc2.Attribute("units", "hours since " + dateFormatter.toDateTimeStringISO(lite.base)));
      timeVar.addAttribute(new ucar.nc2.Attribute("missing_value", Double.NaN));
      timeVar.addAttribute(new ucar.nc2.Attribute(_Coordinate.AxisType, AxisType.Time.toString()));

      // remove the old one if any
      newGroup.removeVariable(gridset.gridsetName);
View Full Code Here

    result.setLocation(lite.collectionName);
    transferGroup(proto.getRootGroup(), result.getRootGroup(), result);
    result.finish();
    addAttributeInfo(result, "history", "FMRC "+timeInv.getName()+" Dataset");

    DateFormatter dateFormatter = new DateFormatter();
    ProxyReader1D proxyReader1D = new ProxyReader1D();

    // make the time coordinate(s) for each runSeq
    List<Variable> nonAggVars = result.getVariables();
    for (FmrcInvLite.Gridset gridset : lite.gridSets) {
View Full Code Here

  public NetcdfDataset getRunTimeDataset(Date wantRuntime) throws IOException {
    if (wantRuntime == null) return null;
    if (!runtimes.contains(wantRuntime)) return null;

    DateFormatter df = new DateFormatter();
    String runTimeString = df.toDateTimeStringISO(wantRuntime);

    NetcdfDataset ncd = createDataset(new RuntimeInvGetter(wantRuntime), RUN, runTimeString);
    ncd.addAttribute(null, new Attribute(_Coordinate.ModelRunDate, runTimeString));
    ncd.finish();
    return ncd;
View Full Code Here

  public NetcdfDataset getForecastTimeDataset(Date forecastTimethrows IOException {
    if (forecastTime == null) return null;
    if (!forecasts.contains(forecastTime)) return null;

    DateFormatter df = new DateFormatter();
    String name = df.toDateTimeStringISO(forecastTime);
    return createDataset(new ForecastInvGetter(forecastTime), FORECAST, name);
  }
View Full Code Here

    String newHistory = "Synthetic dataset from TDS fmrc (" + type + ") aggregation, original data from " + ncd_2dtime.getLocation();
    String history = (oldHistory != null) ? oldHistory + "; " + newHistory : newHistory;
    target.addAttribute(new Attribute("history", history));

    // need this attribute for fmrInventory
    DateFormatter df = new DateFormatter();
    target.addAttribute(new Attribute(_Coordinate.ModelBaseDate, df.toDateTimeStringISO(baseDate)));

    // dimensions
    for (Dimension d : src.getDimensions()) {
      target.addDimension(new Dimension(d.getName(), d));
    }
View Full Code Here

TOP

Related Classes of ucar.nc2.units.DateFormatter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.