用NetCDF创建和读取NC文件

杀马特。学长 韩版系。学妹 提交于 2020-12-23 18:38:45
package examples;

import java.io.IOException;
import java.util.ArrayList;

import ucar.ma2.ArrayFloat;
import ucar.ma2.DataType;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.Attribute;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFileWriter;
import ucar.nc2.Variable;

public class Pres_temp_4D_wr {

	public static void main(String[] args) {		
		final int NLVL = 2;
	    final int NLAT = 6;
	    final int NLON = 12;
	    final int NREC = 2;

	    final float SAMPLE_PRESSURE = 900.0f;
	    final float SAMPLE_TEMP = 9.0f;
	    final float START_LAT = 25.0f;
	    final float START_LON = -125.0f;

	    // 定义文件名
	    String filename = "pres_temp_4D.nc";
	    NetcdfFileWriter dataFile = null;

	    try {
	      //创建netcdf3文件
	      dataFile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, filename);

	      //定义维度(dimensions)
	      Dimension lvlDim = dataFile.addDimension(null, "level", NLVL);
	      Dimension latDim = dataFile.addDimension(null, "latitude", NLAT);
	      Dimension lonDim = dataFile.addDimension(null, "longitude", NLON);
	      Dimension timeDim = dataFile.addUnlimitedDimension("time");

	      // 定义坐标变量(Variable)
	      Variable latVar = dataFile.addVariable(null, "latitude", DataType.FLOAT, "latitude");
	      Variable lonVar = dataFile.addVariable(null, "longitude", DataType.FLOAT, "longitude");

	      // 定义变量的属性(Attribute)
	      dataFile.addVariableAttribute(latVar, new Attribute("units", "degrees_north"));
	      dataFile.addVariableAttribute(lonVar, new Attribute("units", "degrees_east"));

	    //定义数据变量:温度和气压
//	      ArrayList<Dimension> dims = new ArrayList<Dimension>();
//	      dims.add(lvlDim);
//	      dims.add(latDim);
//	      dims.add(lonDim);
	      String dims = "time level latitude longitude";
	
	      
	      Variable presVar = dataFile.addVariable(null, "pressure", DataType.FLOAT, dims);
	      Variable tempVar = dataFile.addVariable(null, "temperature", DataType.FLOAT, dims);

	      // 定义数据属性(Attribute)
	      dataFile.addVariableAttribute(presVar, new Attribute("units", "hPa"));
	      dataFile.addVariableAttribute(tempVar, new Attribute("units", "celsius"));
	      
	      

	      //定义一维数组(为坐标设定数值)
	      ArrayFloat.D1 lats = new ArrayFloat.D1(latDim.getLength());
	      ArrayFloat.D1 lons = new ArrayFloat.D1(lonDim.getLength());
	      int i, j;
	      for (i = 0; i < latDim.getLength(); i++) {
	        lats.set(i, START_LAT + 5.f * i);
	      }

	      for (j = 0; j < lonDim.getLength(); j++) {
	        lons.set(j, START_LON + 5.f * j);
	      }

	      // 定义4维数组(为数据设定值)
	      ArrayFloat.D4 dataTemp = new ArrayFloat.D4(NREC, lvlDim.getLength(), latDim.getLength(), lonDim.getLength());
	      ArrayFloat.D4 dataPres = new ArrayFloat.D4(NREC, lvlDim.getLength(), latDim.getLength(), lonDim.getLength());

	      for (int record = 0; record < NREC; record++) {
	        i = 0;
	        for (int lvl = 0; lvl < NLVL; lvl++)
	          for (int lat = 0; lat < NLAT; lat++)
	            for (int lon = 0; lon < NLON; lon++) {
	              dataPres.set(record, lvl, lat, lon, SAMPLE_PRESSURE + i);
	              dataTemp.set(record, lvl, lat, lon, SAMPLE_TEMP + i++);
	            }
	      }

	      //创建NC文件
	      dataFile.create();

	      // A newly created Java integer array to be initialized to zeros.
	      int[] origin = new int[4];
	      
	      //数据写入NC文件
	      dataFile.write(latVar, lats);
	      dataFile.write(lonVar, lons);
	      dataFile.write(presVar, origin, dataPres);
	      dataFile.write(tempVar, origin,dataTemp);


	    } catch (IOException e) {
	      e.printStackTrace(System.err);

	    } catch (InvalidRangeException e) {
	      e.printStackTrace(System.err);

	    } finally {
	      if (dataFile != null)
	        try {
	          dataFile.close();
	        } catch (IOException ioe) {
	          ioe.printStackTrace();
	        }
	    }
	    System.out.println("*** SUCCESS writing example file " + filename);
	    

	}
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!