Package ucar.nc2

Source Code of ucar.nc2.TestParsedSectionSpec

package ucar.nc2;

import junit.framework.TestCase;
import ucar.ma2.InvalidRangeException;
import ucar.ma2.Section;
import ucar.nc2.dataset.NetcdfDataset;

import java.io.IOException;

/**
* Describe
*
* @author caron
* @since 7/15/11
*/
public class TestParsedSectionSpec extends TestCase {

  public TestParsedSectionSpec(String name) {
    super(name);
  }

  public void testVariableSection() throws InvalidRangeException, IOException {
    NetcdfFile ncfile = TestLocalNC2.openFile("testWrite.nc");
    Variable v = ncfile.findVariable("temperature");
    assert v != null;

    ParsedSectionSpec spec = ParsedSectionSpec.parseVariableSection(ncfile, "temperature");
    System.out.printf("%s%n", spec);
    assert spec.section.equals(v.getShapeAsSection());

    spec = ParsedSectionSpec.parseVariableSection(ncfile, "temperature(1,0:127:2)");
    System.out.printf("%s%n", spec);
    Section s = new Section("1,0:127:2");
    assert spec.section.equals(s) : spec.section + " != " + s;

    spec = ParsedSectionSpec.parseVariableSection(ncfile, "temperature(:,0:127:2)");
    System.out.printf("%s%n", spec);
    s = new Section("0:63,0:127:2");
    assert spec.section.equals(s) : spec.section + " != " + s;

    ncfile.close();
  }

  public void testGroupAndMembers() throws InvalidRangeException, IOException {
    NetcdfFile ncfile = NetcdfFile.open(TestAll.cdmUnitTestDir + "formats/netcdf4/compound/simple_nc4.nc4");
    Variable v = ncfile.findVariable("grp1/data");
    assert v != null;

    ParsedSectionSpec spec = ParsedSectionSpec.parseVariableSection(ncfile, "grp1/data");
    System.out.printf("%s%n", spec);
    assert spec.section.equals(v.getShapeAsSection());

    spec = ParsedSectionSpec.parseVariableSection(ncfile, "grp2/data.i1");
    System.out.printf("%s%n", spec);

    Variable s = ncfile.findVariable("grp2/data");
    assert spec.section.equals(s.getShapeAsSection());

    v = ncfile.findVariable("grp2/data.i1");
    assert spec.child.section.equals(v.getShapeAsSection());

    ncfile.close();
  }


  public void testEscaping() throws InvalidRangeException, IOException {
    NetcdfFile ncfile = NetcdfDataset.openDataset(TestAll.cdmUnitTestDir + "ncml/escapeNames.ncml");
    Group g = ncfile.findGroup("group.name");
    assert g != null;

    Variable v = g.findVariable("var.name");
    assert v != null;

    Variable v2 = ncfile.findVariable("group.name/var.name");
    assert v2 == null;

    v2 = ncfile.findVariable("group\\.name/var\\.name");
    assert v2 != null;
    assert v2.equals(v);

    ParsedSectionSpec spec = ParsedSectionSpec.parseVariableSection(ncfile, "group\\.name/var\\.name");
    System.out.printf("%s%n", spec);
    assert spec.section.equals(v2.getShapeAsSection());

    spec = ParsedSectionSpec.parseVariableSection(ncfile, "group\\.name/var\\.name(1,0:0)");
    System.out.printf("%s%n", spec);
    Section s = new Section("1,0");
    assert spec.section.equals(s);

    ncfile.close();
  }

  public void testEscaping2() throws InvalidRangeException, IOException {
    NetcdfFile ncfile = NetcdfDataset.openDataset(TestAll.cdmUnitTestDir + "ncml/escapeNames.ncml");
    Group g = ncfile.findGroup("group(name");
    assert g != null;

    Variable v = g.findVariable("var(name");
    assert v != null;

    Variable v2 = ncfile.findVariable("group(name/var(name");
    assert v2 != null;
    assert v2.equals(v);

    v2 = ncfile.findVariable("group\\(name/var\\(name");
    assert v2 != null;
    assert v2.equals(v);

    ParsedSectionSpec spec = ParsedSectionSpec.parseVariableSection(ncfile, "group\\(name/var\\(name");
    System.out.printf("%s%n", spec);
    assert spec.section.equals(v2.getShapeAsSection());

    spec = ParsedSectionSpec.parseVariableSection(ncfile, "group\\(name/var\\(name(1,0:0)");
    System.out.printf("%s%n", spec);
    Section s = new Section("1,0");
    assert spec.section.equals(s);

    ncfile.close();
  }

}
TOP

Related Classes of ucar.nc2.TestParsedSectionSpec

TOP
Copyright © 2018 www.massapi.com. 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.