Package ucar.nc2

Examples of ucar.nc2.NetcdfFile


    public void run() {
      try {
        //System.out.printf("acquire %s%n", location);
        FileCacheable fc = cache.acquire(factory, location, null);
        NetcdfFile ncfile = (NetcdfFile) fc;
        assert !ncfile.isUnlocked();
        assert (null != ncfile.getIosp());
        Thread.sleep(wait);
        ncfile.close();
        int d = done.incrementAndGet();
        if (d % PRINT_EVERY == 0) System.out.printf(" done %d%n", d);

      } catch (InterruptedException e) {
        return;
View Full Code Here


  double streamOutputWriter(File f) throws IOException, InvalidRangeException {
    Socket s = new Socket(host, port);
    DataOutputStream stream = new DataOutputStream(new BufferedOutputStream(s.getOutputStream(), bufferSize));

    NetcdfFile ncfile = NetcdfFile.open(f.getPath());
    N3outputStreamWriter writer = new N3outputStreamWriter(ncfile);

    writer.writeHeader(stream, -1);
    writer.writeDataAll(stream);
    stream.close();
View Full Code Here

  double streamChannelWriter(File f) throws IOException, InvalidRangeException {
    InetSocketAddress sadd = new InetSocketAddress(host, port);
    SocketChannel sc = java.nio.channels.SocketChannel.open(sadd);

    NetcdfFile ncfile = NetcdfFile.open(f.getPath());

    N3channelWriter.writeToChannel(ncfile, sc);
    sc.close();

    return ((double) f.length()) / (1000 * 1000);
 
View Full Code Here

  double copyChannelFromNetcdf(File f) throws IOException, InvalidRangeException {
    InetSocketAddress sadd = new InetSocketAddress(host, port);
    SocketChannel sc = java.nio.channels.SocketChannel.open(sadd);

    int done = 0;
    NetcdfFile ncfile = NetcdfFile.open(f.getPath());
    String want = "T,u,v,RH,Z,omega,absvor,T_fhg,u_fhg,v_fhg,RH_fhg";
    StringTokenizer stoke = new StringTokenizer(want, ",");
    while (stoke.hasMoreTokens()) {
      ParsedSectionSpec cer = ParsedSectionSpec.parseVariableSection(ncfile, stoke.nextToken());
      done += cer.v.readToByteChannel(cer.section, sc);
View Full Code Here

    System.out.println("Rewrite2 .nc files from " + fileIn + " to " + fileOut + " inMemory= " + inMemory);

    long start = System.currentTimeMillis();

    // do it in memory for speed
    NetcdfFile ncfile = inMemory ? NetcdfFile.openInMemory(fileIn) : NetcdfFile.open(fileIn);
    NetcdfDataset ncd = new NetcdfDataset(ncfile);

    StringBuilder errlog = new StringBuilder();
    PointObsDataset pobsDataset = (PointObsDataset) TypedDatasetFactory.open(FeatureType.POINT, ncd, null, errlog);
    if (pobsDataset == null) return false;
View Full Code Here

    System.out.println("Rewrite2 .nc files from " + fileIn + " to " + fileOut + " inMemory= " + inMemory);

    long start = System.currentTimeMillis();

    // do it in memory for speed
    NetcdfFile ncfile = inMemory ? NetcdfFile.openInMemory(fileIn) : NetcdfFile.open(fileIn);
    NetcdfDataset ncd = new NetcdfDataset(ncfile);

    Formatter errlog = new Formatter();
    FeatureDataset fd = FeatureDatasetFactoryManager.wrap(FeatureType.ANY_POINT, ncd, null, errlog);
    if (fd == null) return false;
View Full Code Here

    System.out.println("****************\n");
    System.out.println("result= "+result);
    System.out.println(" copied contents to "+file.getPath());

    if (isNetcdf) {
      NetcdfFile ncfile = NetcdfFile.open(file.getPath());
      assert ncfile != null;
    }
    //showRead( getURL);
  }
View Full Code Here

   final float START_LAT = 25.0f;
   final float START_LON = -125.0f;

   // Open the file.
   String filename = "pres_temp_4D.nc";
   NetcdfFile dataFile = null;
   try {

     dataFile = NetcdfFile.open(filename, null);

     // Get the latitude and longitude Variables.
     Variable latVar = dataFile.findVariable("latitude");
     if (latVar == null) {
       System.out.println("Cant find Variable latitude");
       return;
     }

     Variable lonVar = dataFile.findVariable("longitude");
     if (lonVar == null) {
       System.out.println("Cant find Variable longitude");
       return;
     }

     // Get the lat/lon data from the file.
     ArrayFloat.D1 latArray;
     ArrayFloat.D1 lonArray;

     latArray = (ArrayFloat.D1) latVar.read();
     lonArray = (ArrayFloat.D1) lonVar.read();


     // Check the coordinate variable data.
     for (int lat = 0; lat < NLAT; lat++)
       if (latArray.get(lat) != START_LAT + 5. * lat)
         System.err.println("ERROR incorrect value in variable latitude");

     for (int lon = 0; lon < NLON; lon++)
       if (lonArray.get(lon) != START_LON + 5. * lon)
         System.err.println("ERROR incorrect value in variable longtitude");

     // Get the pressure and temperature variables.
     Variable presVar = dataFile.findVariable("pressure");
     if (presVar == null) {
       System.out.println("Cant find Variable pressure");
       return;
     }

     Variable tempVar = dataFile.findVariable("temperature");
     if (lonVar == null) {
       System.out.println("Cant find Variable temperature");
       return;
     }

     int [] shape = presVar.getShape();
     int recLen = shape[0]; // number of times

     int[] origin = new int[4];
     shape[0] = 1; // only one rec per read

     // loop over the rec dimension
     for (int rec = 0; rec < recLen; rec++) {
       origin[0] = rec;  // read this index

       // read 3D array for that index
       ArrayFloat.D3 presArray, tempArray;

       presArray = (ArrayFloat.D3) (presVar.read(origin, shape).reduce());
       tempArray = (ArrayFloat.D3) (tempVar.read(origin, shape).reduce());


       // now checking the value
       int count = 0;
       for (int lvl = 0; lvl < NLVL; lvl++)
         for (int lat = 0; lat < NLAT; lat++)
           for (int lon = 0; lon < NLON; lon++) {
             if ((presArray.get(lvl, lat, lon) != SAMPLE_PRESSURE + count) ||
                     (tempArray.get(lvl, lat, lon) != SAMPLE_TEMP + count))
               System.err.println("ERROR incorrect value in variable pressure or temperature");
             count++;
           }
     }

     // The file is closed no matter what by putting inside a try/catch block.
   } catch (java.io.IOException e) {
       e.printStackTrace();
       return;
   } catch (InvalidRangeException e) {
       e.printStackTrace();
       return;
   } finally {
     if (dataFile != null)
       try {
         dataFile.close();
       } catch (IOException ioe) {
         ioe.printStackTrace();
       }
   }
   System.out.println("*** SUCCESS reading example file "+filename);
View Full Code Here

       // This is the array we will read.
       int[][] dataIn = new int[NX][NY];

       // Open the file. The ReadOnly parameter tells netCDF we want
       // read-only access to the file.
       NetcdfFile dataFile = null;
       String filename = "simple_xy.nc";
       // Open the file.
       try {

           dataFile = NetcdfFile.open(filename, null);

           // Retrieve the variable named "data"
            Variable dataVar = dataFile.findVariable("data");

            if (dataVar == null) {
                System.out.println("Cant find Variable data");
                return;
            }

           // Read all the values from the "data" variable into memory.
            int [] shape = dataVar.getShape();
            int[] origin = new int[2];

            ArrayInt.D2 dataArray;
            
            dataArray = (ArrayInt.D2) dataVar.read(origin, shape);

           // Check the values.
            assert shape[0] == NX;
            assert shape[1] == NY;

            for (int j=0; j<shape[0]; j++) {
               for (int i=0; i<shape[1]; i++) {
                  dataIn[j][i] = dataArray.get(j,i);
               }
            }


       // The file is closed no matter what by putting inside a try/catch block.
       } catch (java.io.IOException e) {
                e.printStackTrace();
                return;
       catch (InvalidRangeException e) {
                e.printStackTrace();
       } finally {
           if (dataFile != null)
           try {
             dataFile.close();
           } catch (IOException ioe) {
             ioe.printStackTrace();
           }
        }
View Full Code Here

        float [] lonsIn = new float[NLON];


        // Open the file and check to make sure it's valid.
        String filename = "sfc_pres_temp.nc";
        NetcdfFile dataFile = null;

        try {

            dataFile = NetcdfFile.open(filename, null);

            Variable latVar = dataFile.findVariable("latitude");
            if (latVar == null) {
                System.out.println("Cant find Variable latitude");
                return;
            }


            Variable lonVar= dataFile.findVariable("longitude");
            if (lonVar == null) {
                System.out.println("Cant find Variable longitude");
                return;
            }


            Variable presVar= dataFile.findVariable("pressure");
            if (presVar == null) {
                System.out.println("Cant find Variable pressure");
                return;
            }

            Variable tempVar= dataFile.findVariable("temperature");
            if (tempVar == null) {
                System.out.println("Cant find Variable temperature");
                return;
            }


            if(latVar.getDimensions().size() != 1) {
              System.out.println(" fail to get the dimensions of variable latitude");
                return;
            }
            if(presVar.getDimensions().size() != 2) {
               System.out.println(" fail to get the dimensions of variable pressure");
                return;
            }

            // Read the latitude and longitude coordinate variables into arrays
            // latsIn and lonsIn.

            ArrayFloat.D1 latArray;
            ArrayFloat.D1 lonArray;

            latArray = (ArrayFloat.D1)latVar.read();
            lonArray = (ArrayFloat.D1)lonVar.read();


            int[] shape = latArray.getShape();
            for (int i=0; i<shape[0]; i++) {
                latsIn[i] = latArray.get(i);
            }

            shape = lonArray.getShape();
            for (int j=0; j<shape[0]; j++) {
                lonsIn[j] = lonArray.get(j);
            }

            // Check the coordinate variable data.
            for(int lat = 0; lat < NLAT; lat++)
              if (latsIn[lat] != START_LAT + 5. * lat)
                  System.err.println("ERROR reading variable latitude");

            // Check longitude values.
            for (int lon = 0; lon < NLON; lon++)
              if (lonsIn[lon] != START_LON + 5. * lon)
                 System.err.println("ERROR reading variable longitude");


            // Read the data. Since we know the contents of the file we know
            // that the data arrays in this program are the correct size to
            // hold all the data.
            ArrayFloat.D2 presArray, tempArray;

            presArray = (ArrayFloat.D2)presVar.read();
            tempArray = (ArrayFloat.D2)tempVar.read();

            int [] shape1 = presArray.getShape();

            for (int i=0; i<shape1[0]; i++) {
              for (int j=0; j<shape1[1]; j++) {
                presIn[i][j] = presArray.get(i,j);
                tempIn[i][j] = tempArray.get(i,j);
              }
            }


            // Check the data.
            for (int lat = 0; lat < NLAT; lat++)
              for (int lon = 0; lon < NLON; lon++)
                if (presIn[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat)
                 || tempIn[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
                 System.err.println("ERROR reading variable pressure or temperature");

            // Each of the netCDF variables has a "units" attribute. Let's read
            // them and check them.

            if(!latVar.findAttributeIgnoreCase("units").getStringValue().equalsIgnoreCase("degrees_north"))
                  System.err.println("ERROR reading variable latitude units");

            if(!lonVar.findAttributeIgnoreCase("units").getStringValue().equalsIgnoreCase("degrees_east"))
                  System.err.println("ERROR reading variable longitude units");

            if(!presVar.findAttributeIgnoreCase("units").getStringValue().equalsIgnoreCase("hPa"))
                  System.err.println("ERROR reading variable pressure units");

            if(!tempVar.findAttributeIgnoreCase("units").getStringValue().equalsIgnoreCase("celsius"))
                  System.err.println("ERROR reading variable temperature units");


        } catch (java.io.IOException e) {
              System.out.println(" fail = "+e);
              e.printStackTrace();
        } finally {
           if (dataFile != null)
           try {
             dataFile.close();
           } catch (IOException ioe) {
             ioe.printStackTrace();
           }
        }
        System.out.println("*** SUCCESS reading example file sfc_pres_temp.nc!");
View Full Code Here

TOP

Related Classes of ucar.nc2.NetcdfFile

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.