#include 
#include 
#include 


void
check_err(const int stat, const int line, const char *file) {
    if (stat != NC_NOERR) {
        (void)fprintf(stderr,"line %d of %s: %s\n", line, file, nc_strerror(stat));
        fflush(stderr);
        exit(1);
    }
}

int
main() {/* create mslp.nc */

    int  stat;  /* return status */
    int  ncid;  /* netCDF id */

    /* dimension ids */
    int lat_dim;
    int lon_dim;
    int time_dim;

    /* dimension lengths */
    size_t lat_len = 6;
    size_t lon_len = 4;
    size_t time_len = NC_UNLIMITED;

    /* variable ids */
    int lat_id;
    int lon_id;
    int time_id;
    int pr_id;

    /* rank (number of dimensions) for each variable */
#   define RANK_lat 1
#   define RANK_lon 1
#   define RANK_time 1
#   define RANK_pr 3

    /* variable shapes */
    int lat_dims[RANK_lat];
    int lon_dims[RANK_lon];
    int time_dims[RANK_time];
    int pr_dims[RANK_pr];

    /* enter define mode */
    stat = nc_create("mslp.nc", NC_CLOBBER, &ncid);
    check_err(stat,__LINE__,__FILE__);

    /* define dimensions */
    stat = nc_def_dim(ncid, "lat", lat_len, &lat_dim);
    check_err(stat,__LINE__,__FILE__);
    stat = nc_def_dim(ncid, "lon", lon_len, &lon_dim);
    check_err(stat,__LINE__,__FILE__);
    stat = nc_def_dim(ncid, "time", time_len, &time_dim);
    check_err(stat,__LINE__,__FILE__);

    /* define variables */

    lat_dims[0] = lat_dim;
    stat = nc_def_var(ncid, "lat", NC_FLOAT, RANK_lat, lat_dims, &lat_id);
    check_err(stat,__LINE__,__FILE__);

    lon_dims[0] = lon_dim;
    stat = nc_def_var(ncid, "lon", NC_FLOAT, RANK_lon, lon_dims, &lon_id);
    check_err(stat,__LINE__,__FILE__);

    time_dims[0] = time_dim;
    stat = nc_def_var(ncid, "time", NC_DOUBLE, RANK_time, time_dims, &time_id);
    check_err(stat,__LINE__,__FILE__);

    pr_dims[0] = time_dim;
    pr_dims[1] = lat_dim;
    pr_dims[2] = lon_dim;
    stat = nc_def_var(ncid, "pr", NC_FLOAT, RANK_pr, pr_dims, &pr_id);
    check_err(stat,__LINE__,__FILE__);

    /* assign global attributes */
    { /* title */
    stat = nc_put_att_text(ncid, NC_GLOBAL, "title", 20, "example for workshop");
    check_err(stat,__LINE__,__FILE__);
    }


    /* assign per-variable attributes */
    { /* units */
    stat = nc_put_att_text(ncid, lat_id, "units", 13, "degrees_north");
    check_err(stat,__LINE__,__FILE__);
    }
    { /* units */
    stat = nc_put_att_text(ncid, lon_id, "units", 12, "degrees_east");
    check_err(stat,__LINE__,__FILE__);
    }
    { /* units */
    stat = nc_put_att_text(ncid, time_id, "units", 24, "seconds since 2009-01-01");
    check_err(stat,__LINE__,__FILE__);
    }
    { /* standard_name */
    stat = nc_put_att_text(ncid, pr_id, "standard_name", 25, "air_pressure_at_sea_level");
    check_err(stat,__LINE__,__FILE__);
    }
    { /* units */
    stat = nc_put_att_text(ncid, pr_id, "units", 3, "hPa");
    check_err(stat,__LINE__,__FILE__);
    }


    /* leave define mode */
    stat = nc_enddef (ncid);
    check_err(stat,__LINE__,__FILE__);

    /* assign variable data */
    {
    float lat_data[6] = {25, 30, 35, 40, 45, 50} ;
    size_t lat_startset[1] = {0} ;
    size_t lat_countset[1] = {6} ;
    stat = nc_put_vara(ncid, lat_id, lat_startset, lat_countset, lat_data);
    stat = nc_put_vara(ncid, lat_id, lat_startset, lat_countset, lat_data);
    check_err(stat,__LINE__,__FILE__);
    }

    {
    float lon_data[4] = {-125, -110, -95, -80} ;
    size_t lon_startset[1] = {0} ;
    size_t lon_countset[1] = {4} ;
    stat = nc_put_vara(ncid, lon_id, lon_startset, lon_countset, lon_data);
    stat = nc_put_vara(ncid, lon_id, lon_startset, lon_countset, lon_data);
    check_err(stat,__LINE__,__FILE__);
    }

    {
    double time_data[2] = {7776000, 15552000} ;
    size_t time_startset[1] = {0} ;
    size_t time_countset[1] = {2} ;
    stat = nc_put_vara(ncid, time_id, time_startset, time_countset, time_data);
    stat = nc_put_vara(ncid, time_id, time_startset, time_countset, time_data);
    check_err(stat,__LINE__,__FILE__);
    }

    {
    float pr_data[48] = {900.5, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995} ;
    size_t pr_startset[3] = {0, 0, 0} ;
    size_t pr_countset[3] = {2, 6, 4} ;
    stat = nc_put_vara(ncid, pr_id, pr_startset, pr_countset, pr_data);
    stat = nc_put_vara(ncid, pr_id, pr_startset, pr_countset, pr_data);
    check_err(stat,__LINE__,__FILE__);
    }


    stat = nc_close(ncid);
    check_err(stat,__LINE__,__FILE__);
    return 0;
}