Reading NetCDF variables to Repast models
1. Introduction
NetCDF (network Common Data Form) is a set of software libraries and machine-independent data formats that support the creation, access, and sharing of array-oriented scientific data. While Repast Simphony is a famous platform for building Agent-Based Models. This article mainly introduces the steps on how to read and write variables to a NetCDF file by showing a Repast Temperature model. In this model, each grid has a property—temperature—represented by different colour depth. We read the temperatures from a known structured NetCDF file, and initialize the model. In each step, we change each grid's temperature randomly. At last, when stopping the simulation, save the new temperatures to that NetCDF file.
2. Steps
2.1 Pre-requisite
- Down load NetCDF for Java library from ftp://ftp.unidata.ucar.edu/pub/netcdf-java/v4.1/netcdfAll-4.1.jar
- Create a Repast model, copy the NetCDF library to Repast model's lib directory and add path
- Download example NetCDF files in the attachments:examples.zip.
The data structure is like this:netcdf H:/Path/to/example3.nc { dimensions: time = UNLIMITED; // (0 currently) lat = 2; lon = 4; variables: float lat(lat=2); :units = "degrees_north"; :_CoordinateAxisType = "Lat"; float lon(lon=4); :units = "degrees_east"; :_CoordinateAxisType = "Lon"; int temp(lat=2, lon=4); :title = "Example Data"; data: temp = 6, 18, 5, 2, 10, 4, 17, 3; }The corresponding NCML file is:
<?xml version="1.0" encoding="UTF-8"?> <netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="H:/Test3/example3.nc"> <dimension name="time" length="0" isUnlimited="true" /> <dimension name="lat" length="2" /> <dimension name="lon" length="4" /> <attribute name="title" value="Example Data" /> <variable name="temp" shape="lat lon" type="int" /> <variable name="lat" shape="lat" type="float"> <attribute name="units" value="degrees_north" /> <attribute name="_CoordinateAxisType" value="Lat" /> </variable> <variable name="lon" shape="lon" type="float"> <attribute name="units" value="degrees_east" /> <attribute name="_CoordinateAxisType" value="Lon" /> </variable> </netcdf>
2.2 A Repast Temperature model
- Define the score file:
- Write a Wrapper class for reading and writing NetCDF file using NetCDF APIs, some codes list below:
public ReadNetcdf(String fN){ try { nc = new NetcdfFileWriteable(fN // open it readonly ); * Read the latitudes into an array of double, reading one value at * a time. This works regardless of the external type of the "lat" * variable. */ Listld = nc.getDimensions(); // int nlats=0,nlon=0; for (Dimension d : ld) { // System.out.println(d.getName()); if (d.getName().equalsIgnoreCase("lat")) { nlats = d.getLength(); } if (d.getName().equalsIgnoreCase("lon")) { nlon = d.getLength(); } } System.out.println("lat:" + nlats); System.out.println("lon:" + nlon); } catch (java.io.IOException e) { e.printStackTrace(); } } public void dispose() { try { this.nc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }public Array getVariable(String vName) throws IOException { Array av = null; Listlv = nc.getVariables(); for (Variable v : lv) { if (v.getName().equalsIgnoreCase(vName)) { System.out.println(vName + " variable"); // int[] rShape=v.getShapeAll(); // int[][] rData=new int[rShape[0]][rShape[1]]; // System.out.println(rData[1][1]); av = v.read(); for (int i = 0; i < av.getSize(); i++) { System.out.println(av.getInt(i)); } System.out.println(v.read()); } } return av; } public void writeNC(Array av) throws IOException { try { nc.write("temp", av); } catch (InvalidRangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } - Define every Agent:
- Simple Agent: Read the original temperature from the NetCDF file, change it randomly each step.
- Simple Manager: Write every Simple Agent's new temperature to global NetCDF writer stream.
- Simple Context: The Context Agents lie in. It opens a NetCDF file, inialize the grids and all the agents from the NetCDF file, finally close the NetCDF file and save it when user press "stop" to stop the simulation.
- Runtime snapshot:
3. References
The NetCDF Markup Language (NcML): http://www.unidata.ucar.edu/software/netcdf/ncml/
http://www.unidata.ucar.edu/software/netcdf/
NetCDF Java Library: http://www.unidata.ucar.edu/software/netcdf-java/
Basic NcML Tutorial: http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Tutorial.html
NetCDF API: http://www.unidata.ucar.edu/software/netcdf-java/v4.1/javadocAll/index.html
NcML Cookbook Examples: http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Cookbook.html
NcML Aggregation: http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Aggregation.html
| Attachment | Size |
|---|---|
| TemperatureModelScore.jpg | 10.89 KB |
| TempModelRun.jpg | 4.48 KB |
| examples.zip | 717 bytes |

Recent comments
56 min 54 sec ago
57 min 18 sec ago
1 hour 57 min ago
1 hour 57 min ago
1 hour 57 min ago
4 hours 3 sec ago
4 hours 10 sec ago
4 hours 39 sec ago
5 hours 2 min ago
5 hours 2 min ago