static int verify_product(int ncid) { int result; char *convention_str; result = nc_inq_att(ncid, NC_GLOBAL, "Conventions", NULL, NULL); if (result == NC_NOERR) { if (read_string_attribute(ncid, NC_GLOBAL, "Conventions", &convention_str) == 0) { int major, minor; if (harp_parse_file_convention(convention_str, &major, &minor) == 0) { free(convention_str); if (major > HARP_FORMAT_VERSION_MAJOR || (major == HARP_FORMAT_VERSION_MAJOR && minor > HARP_FORMAT_VERSION_MINOR)) { harp_set_error(HARP_ERROR_UNSUPPORTED_PRODUCT, "unsupported HARP format version %d.%d", major, minor); return -1; } return 0; } free(convention_str); } } harp_set_error(HARP_ERROR_UNSUPPORTED_PRODUCT, "not a HARP product"); return -1; }
void read_parameters(const char* filename) { char version[200]; xmlNodePtr root; strcpy(cfilename,filename); i3_config_doc = xmlParseFile(filename); if (i3_config_doc == NULL ) { I3_PRINT_INFO1( I3_INFO_LEVEL_FATAL_ERROR, "%s: XML configuration in not parsed successfully" "check whether all tags are terminated etc). \n", filename ); exit(-1); } root = xmlDocGetRootElement(i3_config_doc); if (root == NULL) { I3_PRINT_INFO1( I3_INFO_LEVEL_FATAL_ERROR, "%s: Empty XML configuration file\n", filename ); exit(-1); } if (xmlStrcmp(root->name, (const xmlChar *) "I3ConfigFile")) { I3_PRINT_INFO1( I3_INFO_LEVEL_FATAL_ERROR, "%s: Document of the wrong type, root node != I3ConfigFile\n", filename ); exit(-1); } read_string_attribute("//I3ConfigFile", "version",version,1); if ( strcmp(version,VER_CONFIG)) { I3_PRINT_INFO3( I3_INFO_LEVEL_FATAL_ERROR, "%s: Incorrect version of configuration file. " "This code uses version %s configuration file, your configuration " "file has version %s, please update.\n", filename,VER_CONFIG,version ); exit(-1); } }
int get_type_ft(hid_t loc_id, const char* path) { char* buf; int type; type = -1; buf = (char *) malloc(200 * sizeof(char)); buf = read_string_attribute(loc_id, path, A_FLOATING_TYPE); if (strcmp(buf, V_SINGLE_REAL) == 0) type = E_SINGLE_REAL; else if (strcmp(buf, V_SINGLE_COMPLEX) == 0) type = E_SINGLE_COMPLEX; else if (strcmp(buf, V_DATA_SET) == 0) type = E_DATA_SET; else if (strcmp(buf, V_VECTOR) == 0) type = E_VECTOR; else if (strcmp(buf, V_ARRAY_SET) == 0) type = E_ARRAY_SET; else printf("Can't get floating type for %s\n", path); free(buf); return type; }
int harp_import_global_attributes_netcdf(const char *filename, double *datetime_start, double *datetime_stop, long dimension[], char **source_product) { char *attr_source_product = NULL; harp_scalar attr_datetime_start; harp_scalar attr_datetime_stop; harp_data_type attr_data_type; long attr_dimension[HARP_NUM_DIM_TYPES]; int result; int ncid; int i; if (datetime_start == NULL && datetime_stop == NULL) { return 0; } if (filename == NULL) { harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "filename is NULL (%s:%u)", __FILE__, __LINE__); return -1; } result = nc_open(filename, 0, &ncid); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } if (verify_product(ncid) != 0) { nc_close(ncid); return -1; } if (datetime_start != NULL) { if (nc_inq_att(ncid, NC_GLOBAL, "datetime_start", NULL, NULL) == NC_NOERR) { if (read_numeric_attribute(ncid, NC_GLOBAL, "datetime_start", &attr_data_type, &attr_datetime_start) != 0) { nc_close(ncid); return -1; } if (attr_data_type != harp_type_double) { harp_set_error(HARP_ERROR_IMPORT, "attribute 'datetime_start' has invalid type"); nc_close(ncid); return -1; } } else { attr_datetime_start.double_data = harp_mininf(); } } if (datetime_stop != NULL) { if (nc_inq_att(ncid, NC_GLOBAL, "datetime_stop", NULL, NULL) == NC_NOERR) { if (read_numeric_attribute(ncid, NC_GLOBAL, "datetime_stop", &attr_data_type, &attr_datetime_stop) != 0) { nc_close(ncid); return -1; } if (attr_data_type != harp_type_double) { harp_set_error(HARP_ERROR_IMPORT, "attribute 'datetime_stop' has invalid type"); nc_close(ncid); return -1; } } else { attr_datetime_stop.double_data = harp_plusinf(); } } if (source_product != NULL) { if (read_string_attribute(ncid, NC_GLOBAL, "source_product", &attr_source_product) != 0) { nc_close(ncid); return -1; } } if (dimension != NULL) { int num_dimensions; int num_variables; int num_attributes; int unlim_dim; int result; for (i = 0; i < HARP_NUM_DIM_TYPES; i++) { attr_dimension[i] = -1; } result = nc_inq(ncid, &num_dimensions, &num_variables, &num_attributes, &unlim_dim); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } for (i = 0; i < num_dimensions; i++) { netcdf_dimension_type netcdf_dim_type; harp_dimension_type harp_dim_type; char name[NC_MAX_NAME + 1]; size_t length; result = nc_inq_dim(ncid, i, name, &length); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } if (parse_dimension_type(name, &netcdf_dim_type) != 0) { return -1; } if (netcdf_dim_type != netcdf_dimension_independent && netcdf_dim_type != netcdf_dimension_string) { if (get_harp_dimension_type(netcdf_dim_type, &harp_dim_type) != 0) { return -1; } attr_dimension[harp_dim_type] = length; } } } result = nc_close(ncid); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); free(attr_source_product); return -1; } if (datetime_start != NULL) { *datetime_start = attr_datetime_start.double_data; } if (datetime_stop != NULL) { *datetime_stop = attr_datetime_stop.double_data; } if (source_product != NULL) { *source_product = attr_source_product; } if (dimension != NULL) { for (i = 0; i < HARP_NUM_DIM_TYPES; i++) { dimension[i] = attr_dimension[i]; } } return 0; }
static int read_product(int ncid, harp_product *product, netcdf_dimensions *dimensions) { int num_dimensions; int num_variables; int num_attributes; int unlim_dim; int result; int i; result = nc_inq(ncid, &num_dimensions, &num_variables, &num_attributes, &unlim_dim); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } for (i = 0; i < num_dimensions; i++) { netcdf_dimension_type dimension_type; char name[NC_MAX_NAME + 1]; size_t length; result = nc_inq_dim(ncid, i, name, &length); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } if (parse_dimension_type(name, &dimension_type) != 0) { return -1; } if (dimensions_add(dimensions, dimension_type, length) != i) { harp_set_error(HARP_ERROR_IMPORT, "duplicate dimensions with name '%s'", name); return -1; } } for (i = 0; i < num_variables; i++) { if (read_variable(product, ncid, i, dimensions) != 0) { return -1; } } result = nc_inq_att(ncid, NC_GLOBAL, "source_product", NULL, NULL); if (result == NC_NOERR) { if (read_string_attribute(ncid, NC_GLOBAL, "source_product", &product->source_product) != 0) { return -1; } } else if (result != NC_ENOTATT) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } result = nc_inq_att(ncid, NC_GLOBAL, "history", NULL, NULL); if (result == NC_NOERR) { if (read_string_attribute(ncid, NC_GLOBAL, "history", &product->history) != 0) { return -1; } } else if (result != NC_ENOTATT) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } return 0; }
static int read_variable(harp_product *product, int ncid, int varid, netcdf_dimensions *dimensions) { harp_variable *variable; harp_data_type data_type; int num_dimensions; harp_dimension_type dimension_type[HARP_MAX_NUM_DIMS]; long dimension[HARP_MAX_NUM_DIMS]; char netcdf_name[NC_MAX_NAME + 1]; nc_type netcdf_data_type; int netcdf_num_dimensions; int netcdf_dim_id[NC_MAX_VAR_DIMS]; int result; long i; result = nc_inq_var(ncid, varid, netcdf_name, &netcdf_data_type, &netcdf_num_dimensions, netcdf_dim_id, NULL); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } if (get_harp_type(netcdf_data_type, &data_type) != 0) { harp_add_error_message(" (variable '%s')", netcdf_name); return -1; } num_dimensions = netcdf_num_dimensions; if (data_type == harp_type_string) { if (num_dimensions == 0) { harp_set_error(HARP_ERROR_IMPORT, "variable '%s' of type '%s' has 0 dimensions; expected >= 1", netcdf_name, harp_get_data_type_name(harp_type_string)); } if (dimensions->type[netcdf_dim_id[num_dimensions - 1]] != netcdf_dimension_string) { harp_set_error(HARP_ERROR_IMPORT, "inner-most dimension of variable '%s' is of type '%s'; expected '%s'", netcdf_name, get_dimension_type_name(dimensions->type[netcdf_dim_id[num_dimensions - 1]]), get_dimension_type_name(netcdf_dimension_string)); return -1; } num_dimensions--; } if (num_dimensions > HARP_MAX_NUM_DIMS) { harp_set_error(HARP_ERROR_IMPORT, "variable '%s' has too many dimensions", netcdf_name); return -1; } for (i = 0; i < num_dimensions; i++) { if (get_harp_dimension_type(dimensions->type[netcdf_dim_id[i]], &dimension_type[i]) != 0) { harp_add_error_message(" (variable '%s')", netcdf_name); return -1; } } for (i = 0; i < num_dimensions; i++) { dimension[i] = dimensions->length[netcdf_dim_id[i]]; } if (harp_variable_new(netcdf_name, data_type, num_dimensions, dimension_type, dimension, &variable) != 0) { return -1; } if (harp_product_add_variable(product, variable) != 0) { harp_variable_delete(variable); return -1; } /* Read data. */ if (data_type == harp_type_string) { char *buffer; long length; assert(netcdf_num_dimensions > 0); length = dimensions->length[netcdf_dim_id[netcdf_num_dimensions - 1]]; buffer = malloc(variable->num_elements * length * sizeof(char)); if (buffer == NULL) { harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", variable->num_elements * length * sizeof(char), __FILE__, __LINE__); return -1; } result = nc_get_var_text(ncid, varid, buffer); if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); free(buffer); return -1; } for (i = 0; i < variable->num_elements; i++) { char *str; str = malloc((length + 1) * sizeof(char)); if (str == NULL) { harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", (length + 1) * sizeof(char), __FILE__, __LINE__); free(buffer); return -1; } memcpy(str, &buffer[i * length], length); str[length] = '\0'; variable->data.string_data[i] = str; } free(buffer); } else { switch (data_type) { case harp_type_int8: result = nc_get_var_schar(ncid, varid, variable->data.int8_data); break; case harp_type_int16: result = nc_get_var_short(ncid, varid, variable->data.int16_data); break; case harp_type_int32: result = nc_get_var_int(ncid, varid, variable->data.int32_data); break; case harp_type_float: result = nc_get_var_float(ncid, varid, variable->data.float_data); break; case harp_type_double: result = nc_get_var_double(ncid, varid, variable->data.double_data); break; default: assert(0); exit(1); } if (result != NC_NOERR) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } } /* Read attributes. */ result = nc_inq_att(ncid, varid, "description", NULL, NULL); if (result == NC_NOERR) { if (read_string_attribute(ncid, varid, "description", &variable->description) != 0) { harp_add_error_message(" (variable '%s')", netcdf_name); return -1; } } else if (result != NC_ENOTATT) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } result = nc_inq_att(ncid, varid, "units", NULL, NULL); if (result == NC_NOERR) { if (read_string_attribute(ncid, varid, "units", &variable->unit) != 0) { harp_add_error_message(" (variable '%s')", netcdf_name); return -1; } } else if (result != NC_ENOTATT) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } result = nc_inq_att(ncid, varid, "valid_min", NULL, NULL); if (result == NC_NOERR) { harp_data_type attr_data_type; if (read_numeric_attribute(ncid, varid, "valid_min", &attr_data_type, &variable->valid_min) != 0) { harp_add_error_message(" (variable '%s')", netcdf_name); return -1; } if (attr_data_type != data_type) { harp_set_error(HARP_ERROR_IMPORT, "attribute 'valid_min' of variable '%s' has invalid type", netcdf_name); return -1; } } else if (result != NC_ENOTATT) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } result = nc_inq_att(ncid, varid, "valid_max", NULL, NULL); if (result == NC_NOERR) { harp_data_type attr_data_type; if (read_numeric_attribute(ncid, varid, "valid_max", &attr_data_type, &variable->valid_max) != 0) { harp_add_error_message(" (variable '%s')", netcdf_name); return -1; } if (attr_data_type != data_type) { harp_set_error(HARP_ERROR_IMPORT, "attribute 'valid_max' of variable '%s' has invalid type", netcdf_name); return -1; } } else if (result != NC_ENOTATT) { harp_set_error(HARP_ERROR_NETCDF, "%s", nc_strerror(result)); return -1; } return 0; }
void read_ushort_attribute(char* elementPath, char *attribName,unsigned short* us,int required) { char str[200]; read_string_attribute(elementPath, attribName, str,required); *us = (unsigned short) atoi(str); }
/** * @param i3_port_num The port on which i3 should listen. */ cl_context *cl_ctx_init(const char *cfg_file, int *rc, int i3_port_num) { char usePingStr[50]; char useTCPStr[50]; cl_context *ctx; // Open the debug file to which all debug and info messages related to i3 are sent, // if it had not be previously opened. // For eg: if the i3 client api is used as part of the i3OCD, the i3OCD // sets up i3DebugFD as soon as the i3OCD dll is loaded. #ifdef _WIN32 if (i3DebugFD == NULL) { i3DebugFD = fopen ("debug_i3.txt", "w"); } #endif if (0 != nw_init()) { I3_PRINT_INFO0 (I3_INFO_LEVEL_FATAL_ERROR, "Unable to initialize the network.\n"); EXIT_ON_ERROR; return NULL; } if (!cfg_file) { *rc = CL_RET_INVALID_CFG_FILE; return NULL; } /* read configuration file */ read_parameters(cfg_file); /* create context */ ctx = cl_create_context(NULL, i3_port_num); if (ctx) *rc = CL_RET_OK; else { *rc = CL_RET_NO_CONTEXT; return ctx; } /* Should TCP be used to find the first hop i3 server? */ read_string_attribute("//I3ServerDetails", "UseTCP", useTCPStr,0); if (strcasecmp(useTCPStr, "yes") == 0 || strcasecmp(useTCPStr, "true") == 0) { ctx->is_tcp = 1; } else { ctx->is_tcp = 0; } /* check whether ping is used to find a nearby server */ read_string_attribute("//I3ServerDetails", "UsePing", usePingStr,0); if (strcasecmp(usePingStr, "yes") == 0 || strcasecmp(usePingStr, "true") == 0) { ctx->use_ping = 1; } else { ctx->use_ping = 0; } if (ctx->use_ping) { #define STATUS_URL_LEN 1024 char status_url[STATUS_URL_LEN]; /* read the url containing the list of i3 serevers */ read_string_attribute("//I3ServerDetails", "ServerListURL", status_url, 0); I3_PRINT_DEBUG1(I3_DEBUG_LEVEL_SUPER, "Using ping, server status url = %s\n\n", status_url); if (strlen(status_url) >= STATUS_URL_LEN) { I3_PRINT_INFO1(I3_INFO_LEVEL_WARNING, "cl_ctx_init: status_url file too long in %s\n", cfg_file); exit(-1); } if (strlen(status_url) == 0) { I3_PRINT_INFO1(I3_INFO_LEVEL_WARNING, "cl_ctx_init: no status_url file in %s\n", cfg_file); exit(-1); } I3_PRINT_DEBUG0(I3_DEBUG_LEVEL_VERBOSE, "Starting ping thread\n"); *rc = cl_init_ping(ctx, status_url); } // Don't read the configuration file after this point. release_params(); if (ctx->num_servers < 1 && ! ctx->use_ping) { I3_PRINT_INFO0 ( I3_INFO_LEVEL_FATAL_ERROR, "No i3 server details have been given and UsePing is turned off.\n" "There is no way to obtain i3 server details. Exiting.\n" ); EXIT_ON_ERROR; } // This function intializes the TCP socket to be used to contact the first hop i3 server, // if UseTCP flag is set in the configuration file. // If UsePing is set, this function blocks till the details of at least // one i3 server is available. init_tcp_ctx(ctx); return ctx; }
/// Read the triqs tag of the group if it is an object. Returns"" if attribute is not present std::string read_triqs_hdf5_data_scheme() const { return read_string_attribute(&_g, "TRIQS_HDF5_data_scheme") ; }