import java.util.*;
import ucar.ma2.*;
import ucar.nc2.*;
import ucar.nc2.NetcdfFile.*;

public class Main
{

static public void main(String[] argv) throws Exception
{

    /* dimension lengths */
    final int lat_len = 6;
    final int lon_len = 4;
    final int time_len = 0;


    /* enter define mode */
    NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew("mslp.nc", true);

    /* define dimensions */
    Dimension lat_dim = ncfile.addDimension("lat", lat_len);
    Dimension lon_dim = ncfile.addDimension("lon", lon_len);
    Dimension time_dim = ncfile.addUnlimitedDimension("time");

    /* define variables */

    ArrayList lat_dimlist = new ArrayList();
    lat_dimlist.add(lat_dim);
    ncfile.addVariable("lat", DataType.FLOAT, lat_dimlist);

    ArrayList lon_dimlist = new ArrayList();
    lon_dimlist.add(lon_dim);
    ncfile.addVariable("lon", DataType.FLOAT, lon_dimlist);

    ArrayList time_dimlist = new ArrayList();
    time_dimlist.add(time_dim);
    ncfile.addVariable("time", DataType.DOUBLE, time_dimlist);

    ArrayList pr_dimlist = new ArrayList();
    pr_dimlist.add(time_dim);
    pr_dimlist.add(lat_dim);
    pr_dimlist.add(lon_dim);
    ncfile.addVariable("pr", DataType.FLOAT, pr_dimlist);

    /* assign global attributes */
    /* attribute: title */
    ncfile.addGlobalAttribute("title","example for workshop");


    /* assign per-variable attributes */
    /* attribute: units */
    ncfile.addVariableAttribute("lat","units","degrees_north");
    /* attribute: units */
    ncfile.addVariableAttribute("lon","units","degrees_east");
    /* attribute: units */
    ncfile.addVariableAttribute("time","units","seconds since 2009-01-01");
    /* attribute: standard_name */
    ncfile.addVariableAttribute("pr","standard_name","air_pressure_at_sea_level");
    /* attribute: units */
    ncfile.addVariableAttribute("pr","units","hPa");

    ncfile.create();

    /* assign variable data */

    {
    float[] contents = new float[] {(float)25, (float)30, (float)35, (float)40, (float)45, (float)50};
    ArrayFloat data = new ArrayFloat(new int[]{6});
    IndexIterator iter = data.getIndexIterator();
    int count = 0;
    while(iter.hasNext())
        iter.setFloatNext(contents[count++]);
    int[] origin = new int[]{0};
    ncfile.write("lat",origin,data);
    }

    {
    float[] contents = new float[] {(float)-125, (float)-110, (float)-95, (float)-80};
    ArrayFloat data = new ArrayFloat(new int[]{4});
    IndexIterator iter = data.getIndexIterator();
    int count = 0;
    while(iter.hasNext())
        iter.setFloatNext(contents[count++]);
    int[] origin = new int[]{0};
    ncfile.write("lon",origin,data);
    }

    {
    double[] contents = new double[] {7776000, 15552000};
    ArrayDouble data = new ArrayDouble(new int[]{2});
    IndexIterator iter = data.getIndexIterator();
    int count = 0;
    while(iter.hasNext())
        iter.setDoubleNext(contents[count++]);
    int[] origin = new int[]{0};
    ncfile.write("time",origin,data);
    }

    {
    float[] contents = new float[] {(float)900.5, (float)901, (float)902, (float)903, (float)904, (float)905, (float)906, (float)907, (float)908, (float)909, (float)910, (float)911, (float)912, (float)913, (float)914, (float)915, (float)916, (float)917, (float)918, (float)919, (float)920, (float)921, (float)922, (float)923, (float)972, (float)973, (float)974, (float)975, (float)976, (float)977, (float)978, (float)979, (float)980, (float)981, (float)982, (float)983, (float)984, (float)985, (float)986, (float)987, (float)988, (float)989, (float)990, (float)991, (float)992, (float)993, (float)994, (float)995};
    ArrayFloat data = new ArrayFloat(new int[]{2, 6, 4});
    IndexIterator iter = data.getIndexIterator();
    int count = 0;
    while(iter.hasNext())
        iter.setFloatNext(contents[count++]);
    int[] origin = new int[]{0, 0, 0};
    ncfile.write("pr",origin,data);
    }

    ncfile.close();

    }
}