Example #1
0
/******************************************************************************
 * @brief    Get netCDF variable type.
 *****************************************************************************/
int
get_nc_var_type(char  *nc_name,
                char  *var_name)
{
    int    nc_id;
    int    var_id;
    int    status;
    int    xtypep;

    // open the netcdf file
    status = nc_open(nc_name, NC_NOWRITE, &nc_id);
    check_nc_status(status, "Error opening %s", nc_name);

    // get variable id
    status = nc_inq_varid(nc_id, var_name, &var_id);
    check_nc_status(status, "Error getting variable id %s in %s", var_name,
                    nc_name);

    // get type ID
    status = nc_inq_var(nc_id, var_id, NULL, &xtypep, NULL, NULL, NULL);
    check_nc_status(status, "Error getting variable type %s in %s", var_name,
                    nc_name);

    // close the netcdf file
    status = nc_close(nc_id);
    check_nc_status(status, "Error closing %s", nc_name);

    return(xtypep);
}
Example #2
0
/******************************************************************************
 * @brief    Get netCDF dimension.
 *****************************************************************************/
void
get_nc_var_attr(char  *nc_name,
                char  *var_name,
                char  *attr_name,
                char **attr)
{
    int    nc_id;
    int    var_id;
    int    status;
    size_t attr_len;

    // open the netcdf file
    status = nc_open(nc_name, NC_NOWRITE, &nc_id);
    check_nc_status(status, "Error opening %s", nc_name);

    // get variable id
    status = nc_inq_varid(nc_id, var_name, &var_id);
    check_nc_status(status, "Error getting variable id %s in %s", var_name, nc_name);

    // get size of the attribute
    status = nc_inq_attlen(nc_id, var_id, attr_name, &attr_len);
    check_nc_status(status, "Error getting attribute length for %s:%s in %s", var_name,
                attr_name, nc_name);

    // allocate memory for attribute
    *attr = malloc((attr_len + 1) * sizeof(**attr));
    check_alloc_status(*attr, "Memory allocation error.");

    // read attribute text
    status = nc_get_att_text(nc_id, var_id, attr_name, *attr);
    check_nc_status(status, "Error getting netCDF attribute %s for var %s in %s", attr_name,
                var_name, nc_name);

    // we need to null terminate the string ourselves according to NetCDF docs
    (*attr)[attr_len] = '\0';

    // close the netcdf file
    status = nc_close(nc_id);
    check_nc_status(status, "Error closing %s", nc_name);
}