Ejemplo n.º 1
0
/***********************************************************************//**
 * @brief Read CTA cube background model from XML element
 *
 * @param[in] xml XML element.
 *
 * Set up CTA cube background model from the information provided by
 * an XML element. The XML element is expected to have the following
 * structure
 *
 *     <source name="..." type="..." instrument="...">
 *       <spectrum type="...">
 *         ...
 *       </spectrum>
 *     </source>
 *
 * Optionally, a temporal model may be provided using the following
 * syntax
 *
 *     <source name="..." type="..." instrument="...">
 *       <spectrum type="...">
 *         ...
 *       </spectrum>
 *       <temporalModel type="...">
 *         ...
 *       </temporalModel>
 *     </source>
 *
 * If no temporal component is found a constant model is assumed.
 ***************************************************************************/
void GCTAModelCubeBackground::read(const GXmlElement& xml)
{
    // Clear model
    clear();

    // Initialise XML elements
    const GXmlElement* spectral = NULL;
    const GXmlElement* temporal = NULL;

    // Get pointer on spectrum
    spectral = xml.element("spectrum", 0);

    // Extract spectral model
    m_spectral = xml_spectral(*spectral);

    // Optionally get temporal model
    if (xml.elements("temporalModel")) {
        temporal   = xml.element("temporalModel", 0);
        m_temporal = xml_temporal(*temporal);
    }
    else {
        GModelTemporalConst constant;
        m_temporal = constant.clone();
    }

    // Read model attributes
    read_attributes(xml);

    // Set parameter pointers
    set_pointers();

    // Return
    return;
}
Ejemplo n.º 2
0
/***********************************************************************//**
 * @brief Read exposure cube from FITS object
 *
 * @param[in] fits FITS object.
 *
 * Read the exposure cube from a FITS object.
 ***************************************************************************/
void GCTACubeExposure::read(const GFits& fits)
{
    // Clear object
    clear();

    // Get HDUs
    const GFitsImage& hdu_expcube = *fits.image("Primary");
    const GFitsTable& hdu_ebounds = *fits.table("EBOUNDS");
    const GFitsTable& hdu_gti     = *fits.table("GTI");

    // Read cube
    m_cube.read(hdu_expcube);

    // Read cube attributes
    read_attributes(hdu_expcube);

    // Read energy boundaries
    m_ebounds.read(hdu_ebounds);

    // Read GTIs
    m_gti.read(hdu_gti);

    // Set energy node array
    set_eng_axis();

    // Return
    return;
}
Ejemplo n.º 3
0
static int
read_container(char* contname)
{
    struct stat sb;
    int	 fd, num_exts, ret = 0, flags = 0;

    if (ram_drive)
	flags = O_RDONLY;
    else
	flags =  O_RDONLY | O_DIRECT;

    fd = open(contname, flags);
    if (fd < 0) {
	printf("open error: %d\n", errno);
	exit(1);
    }

    if (print_temp || print_content_len || print_for_cliweb) {
	attr_hash = g_hash_table_new(g_str_hash, g_str_equal);
	if (read_attributes(contname)) {
	    ret = 0;
	    goto exit;
	}
    }

    if (stat(contname, &sb) < 0) {
	fprintf(stderr, "stat error: %d\n", errno);
	exit(1);
    }

    /* If the container file is empty, return now */
    if (!sb.st_size)
	return ret;

    num_exts = (sb.st_size - NKN_CONTAINER_HEADER_SZ) / DEV_BSIZE;
    if (print_one_entry_per) {
	combine_uri_segments(fd, contname, num_exts);
    } else if (print_compact_output == 0)
	default_output(fd, contname, num_exts);
    else
	compact_output(fd, contname, num_exts);

 exit:
    if (attr_hash) {
	g_hash_table_foreach(attr_hash, free_an, NULL);
	g_hash_table_destroy(attr_hash);
	attr_hash = NULL;
    }
    close(fd);
    return ret;
}	/* read_container */
Ejemplo n.º 4
0
static readstat_error_t read_generic_list(int attributes, rdata_ctx_t *ctx) {
    readstat_error_t retval = READSTAT_OK;
    int32_t length;
    int i;
    rdata_sexptype_info_t sexptype_info;
    
    
    if ((retval = read_length(&length, ctx)) != READSTAT_OK)
        goto cleanup;
    
    for (i=0; i<length; i++) {        
        if ((retval = read_sexptype_header(&sexptype_info, ctx)) != READSTAT_OK)
            goto cleanup;
        
        if (sexptype_info.header.type == RDATA_SEXPTYPE_CHARACTER_VECTOR) {
            int32_t vec_length;
            
            if ((retval = read_length(&vec_length, ctx)) != READSTAT_OK)
                goto cleanup;
            if (ctx->column_handler) {
                if (ctx->column_handler(NULL, READSTAT_TYPE_STRING, NULL, NULL, vec_length, ctx->user_ctx)) {
                    retval = READSTAT_ERROR_USER_ABORT;
                    goto cleanup;
                }
            }
            retval = read_string_vector(vec_length, ctx->text_value_handler, ctx->user_ctx, ctx);
        } else {
            retval = read_value_vector(sexptype_info.header, NULL, ctx);
        }
        if (retval != READSTAT_OK)
            goto cleanup;
    }
    
    if (attributes) {
        if ((retval = read_attributes(&handle_data_frame_attribute, ctx)) != READSTAT_OK)
            goto cleanup;
    }
    
cleanup:
    
    return retval;
}
Ejemplo n.º 5
0
static void
combine_uri_segments(int fd,
		     char *contname,
		     unsigned num_exts)
{
    GHashTable		*uol_hash;
    char		*cont_head = NULL, *sector = NULL;
    char		*slash;
    dm2_container_t	*c;
    DM2_disk_extent_t	*dext;
    DM2_disk_extent_v2_t	*dext_v2;
    GList		*uol_list;
    nkn_uol_wrap_t	*uolw;
    int			nbytes;
    unsigned		i, deleted = 0;

    if (print_content_len) {
	attr_hash = g_hash_table_new(g_str_hash, g_str_equal);
	if (read_attributes(contname))
	    goto free_mem;
    }

    uol_hash = g_hash_table_new(g_str_hash, g_str_equal);

    if (posix_memalign((void *)&cont_head, DEV_BSIZE,
		       NKN_CONTAINER_HEADER_SZ)) {
	printf("posix_memalign failed: %d %d\n", DEV_BSIZE,
	       NKN_CONTAINER_HEADER_SZ);
	exit(1);
    }
    if (posix_memalign((void *)&sector, DEV_BSIZE, DEV_BSIZE)) {
	printf("posix_memalign failed: %d %d\n", DEV_BSIZE, DEV_BSIZE);
	exit(1);
    }
    c = (dm2_container_t *)cont_head;
    dext = (DM2_disk_extent_t *)sector;
    dext_v2 = (DM2_disk_extent_v2_t *)sector;

    nbytes = read(fd, cont_head, NKN_CONTAINER_HEADER_SZ);
    if (nbytes != NKN_CONTAINER_HEADER_SZ) {
	printf("ERROR: read header problem: %d %d\n",
	       nbytes, nbytes == -1 ? errno : 0);
	goto free_mem;
    }
    slash = strrchr(contname, '/');
    if (print_dirname) {
	*slash = '\0';
	printf("%s\n", contname);
    } else
	*(slash+1) = '\0';
    if (print_header) {
	if (print_content_len)
	    printf("%12s%12s%12s %s\n", "offset", "length", "content_len",
		   "URI");
	else
	    printf("%12s%12s %s\n", "offset", "length", "URI");
    }

    for (i = 0; i < num_exts; i++) {
	/* Take a nap to give room for other IO activities */
	if (0 == (i % NAP_FREQUENCY))
		usleep(NAP_TIME);

	nbytes = read(fd, sector, DEV_BSIZE);
	if (nbytes != DEV_BSIZE) {
	    printf("ERROR: read extent problem: %d %d %d\n", i, nbytes,
		   nbytes == -1 ? errno : 0);
	    goto free_mem;
	}
	if (dext->dext_header.dext_magic != DM2_DISK_EXT_DONE &&
	    dext->dext_header.dext_magic != DM2_DISK_EXT_MAGIC) {
	    printf("Bad MAGIC: URI=%s offset=%ld length=%ld "
		   "(expected=0x%x|0x%x/got=0x%x)\n", dext->dext_basename,
		   dext->dext_offset, dext->dext_length,
		   DM2_DISK_EXT_MAGIC, DM2_DISK_EXT_DONE,
		   dext->dext_header.dext_magic);
	    continue;
	}
	if (dext->dext_header.dext_version != DM2_DISK_EXT_VERSION &&
	    dext->dext_header.dext_version != DM2_DISK_EXT_VERSION_V2) {
	    printf("Bad Version: URI=%s offset=%ld length=%ld "
		   "(expected=%d,%d/got=%d)\n", dext->dext_basename,
		   dext->dext_offset, dext->dext_length,
		   DM2_DISK_EXT_VERSION, DM2_DISK_EXT_VERSION_V2,
		   dext->dext_header.dext_version);
	    continue;
	}
	deleted = 0;

	/* Skip deleted entries */
	if (skip_deleted && dext->dext_header.dext_magic == DM2_DISK_EXT_DONE)
	    continue;
	if (dext->dext_header.dext_magic == DM2_DISK_EXT_DONE)
	    deleted = 1;

	uolw = calloc(1, sizeof(nkn_uol_wrap_t));
	if (dext->dext_header.dext_version == DM2_DISK_EXT_VERSION) {
	    uolw->uol.offset = dext->dext_offset;
	    uolw->uol.length = dext->dext_length;
	    uolw->uol.uri = strdup(dext->dext_basename);
	} else {
	    uolw->uol.offset = dext_v2->dext_offset;
	    uolw->uol.length = dext_v2->dext_length;
	    uolw->uol.uri = strdup(dext->dext_basename);
	}
	if (deleted)
	    uolw->deleted = 1;

	uol_list = g_hash_table_lookup(uol_hash, dext->dext_basename);
	uol_list = g_list_prepend(uol_list, uolw);
	g_hash_table_insert(uol_hash, uolw->uol.uri, uol_list);
    }
    g_hash_table_foreach(uol_hash, uol_print, contname);
    g_hash_table_destroy(uol_hash);

 free_mem:
    if (cont_head)
	free(cont_head);
    if (sector)
	free(sector);
}	/* combine_uri_segments */
Ejemplo n.º 6
0
static readstat_error_t recursive_discard(rdata_sexptype_header_t sexptype_header, rdata_ctx_t *ctx) {
    uint32_t length;
    rdata_sexptype_info_t info;
    rdata_sexptype_info_t prot, tag;
    
    readstat_error_t error = 0;
    int i;

    switch (sexptype_header.type) {
        case RDATA_SEXPTYPE_SYMBOL:
            if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                goto cleanup;
            
            if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                goto cleanup;
            break;
        case RDATA_PSEUDO_SXP_PERSIST:
        case RDATA_PSEUDO_SXP_NAMESPACE:
        case RDATA_PSEUDO_SXP_PACKAGE:
            if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                goto cleanup;
            
            if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                goto cleanup;
            break;
        case RDATA_SEXPTYPE_BUILTIN_FUNCTION:
        case RDATA_SEXPTYPE_SPECIAL_FUNCTION:
            error = discard_character_string(0, ctx);
            break;
        case RDATA_SEXPTYPE_PAIRLIST:
            error = discard_pairlist(sexptype_header, ctx);
            break;
        case RDATA_SEXPTYPE_CHARACTER_STRING:
            error = discard_character_string(1, ctx);
            break;
        case RDATA_SEXPTYPE_RAW_VECTOR:
            error = discard_vector(sexptype_header, 1, ctx);
            break;
        case RDATA_SEXPTYPE_LOGICAL_VECTOR:
            error = discard_vector(sexptype_header, 4, ctx);
            break;
        case RDATA_SEXPTYPE_INTEGER_VECTOR:
            error = discard_vector(sexptype_header, 4, ctx);
            break;
        case RDATA_SEXPTYPE_REAL_VECTOR:
            error = discard_vector(sexptype_header, 8, ctx);
            break;
        case RDATA_SEXPTYPE_COMPLEX_VECTOR:
            error = discard_vector(sexptype_header, 16, ctx);
            break;
        case RDATA_SEXPTYPE_CHARACTER_VECTOR:
            if (read_st(ctx, &length, sizeof(length)) != sizeof(length)) {
                return READSTAT_ERROR_READ;
            }
            if (ctx->machine_needs_byteswap)
                length = byteswap4(length);
            
            for (i=0; i<length; i++) {
                error = read_sexptype_header(&info, ctx);
                if (error != READSTAT_OK)
                    goto cleanup;
                if (info.header.type != RDATA_SEXPTYPE_CHARACTER_STRING) {
                    error = READSTAT_ERROR_PARSE;
                    goto cleanup;
                }
                
                error = discard_character_string(0, ctx);
                if (error != READSTAT_OK)
                    goto cleanup;
            }
            break;
        case RDATA_SEXPTYPE_GENERIC_VECTOR:
        case RDATA_SEXPTYPE_EXPRESSION_VECTOR:
            if (read_st(ctx, &length, sizeof(length)) != sizeof(length)) {
                return READSTAT_ERROR_READ;
            }
            if (ctx->machine_needs_byteswap)
                length = byteswap4(length);
            
            for (i=0; i<length; i++) {
                if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                    goto cleanup;
                if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                    goto cleanup;
            }
            if (sexptype_header.attributes) {
                if ((error = read_attributes(NULL, ctx)) != READSTAT_OK)
                    goto cleanup;
            }
            break;
        case RDATA_SEXPTYPE_DOT_DOT_DOT:
        case RDATA_SEXPTYPE_PROMISE:
        case RDATA_SEXPTYPE_LANGUAGE_OBJECT: 
        case RDATA_SEXPTYPE_CLOSURE:
            if (sexptype_header.attributes) {
                if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                    goto cleanup;
                
                if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                    goto cleanup;
            }
            if (sexptype_header.tag) {
                if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                    goto cleanup;
                
                if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                    goto cleanup;
            }
            /* CAR */
            if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                goto cleanup;
            
            if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                goto cleanup;
            
            /* CDR */
            if ((error = read_sexptype_header(&info, ctx)) != READSTAT_OK)
                goto cleanup;
            
            if ((error = recursive_discard(info.header, ctx)) != READSTAT_OK)
                goto cleanup;
            break;
        case RDATA_SEXPTYPE_EXTERNAL_POINTER:
            read_sexptype_header(&prot, ctx);
            recursive_discard(prot.header, ctx);
            
            read_sexptype_header(&tag, ctx);
            recursive_discard(tag.header, ctx);
            break;
        case RDATA_SEXPTYPE_ENVIRONMENT:
            /* locked */
            if (lseek_st(ctx, sizeof(uint32_t)) == -1) {
                return READSTAT_ERROR_READ;
            }
            
            rdata_sexptype_info_t enclosure, frame, hash_table, attributes;
            read_sexptype_header(&enclosure, ctx);
            recursive_discard(enclosure.header, ctx);
            
            read_sexptype_header(&frame, ctx);
            recursive_discard(frame.header, ctx);
            
            read_sexptype_header(&hash_table, ctx);
            recursive_discard(hash_table.header, ctx);
            
            read_sexptype_header(&attributes, ctx);
            recursive_discard(attributes.header, ctx);
            /*
             if (sexptype_header.attributes) {
             if (lseek(ctx->fd, sizeof(uint32_t), SEEK_CUR) == -1) {
             return READSTAT_ERROR_READ;
             }
             } */
            break;
        case RDATA_PSEUDO_SXP_REF:
        case RDATA_PSEUDO_SXP_NIL:
        case RDATA_PSEUDO_SXP_GLOBAL_ENVIRONMENT:
        case RDATA_PSEUDO_SXP_UNBOUND_VALUE:
        case RDATA_PSEUDO_SXP_MISSING_ARGUMENT:
        case RDATA_PSEUDO_SXP_BASE_NAMESPACE:
        case RDATA_PSEUDO_SXP_EMPTY_ENVIRONMENT:
        case RDATA_PSEUDO_SXP_BASE_ENVIRONMENT:
            break;
        default:
            return READSTAT_ERROR_READ;
    }
cleanup:
    
    return error;
}
Ejemplo n.º 7
0
static readstat_error_t read_value_vector(rdata_sexptype_header_t header, const char *name, rdata_ctx_t *ctx) {
    readstat_error_t retval = READSTAT_OK;
    int32_t length;
    size_t input_elem_size = 0;
    void *vals = NULL;
    size_t buf_len = 0;
    int output_data_type;
    int i;
    
    switch (header.type) {
        case RDATA_SEXPTYPE_REAL_VECTOR:
            input_elem_size = sizeof(double);
            output_data_type = READSTAT_TYPE_DOUBLE;
            break;
        case RDATA_SEXPTYPE_INTEGER_VECTOR:
            input_elem_size = sizeof(int32_t);
            output_data_type = READSTAT_TYPE_DOUBLE;
            break;
        case RDATA_SEXPTYPE_LOGICAL_VECTOR:
            input_elem_size = sizeof(int32_t);
            output_data_type = READSTAT_TYPE_DOUBLE;
            break;
        default:
            retval = READSTAT_ERROR_PARSE;
            break;
    }
    if (retval != READSTAT_OK)
        goto cleanup;

    if ((retval = read_length(&length, ctx)) != READSTAT_OK)
        goto cleanup;

    buf_len = length * input_elem_size;
    
    vals = malloc(buf_len);
    if (vals == NULL) {
        retval = READSTAT_ERROR_MALLOC;
        goto cleanup;
    }
    
    if (read_st(ctx, vals, buf_len) != buf_len) {
        retval = READSTAT_ERROR_READ;
        goto cleanup;
    }
    
    if (ctx->machine_needs_byteswap) {
        if (input_elem_size == sizeof(double)) {
            double *d_vals = (double *)vals;
            for (i=0; i<buf_len/sizeof(double); i++) {
                d_vals[i] = byteswap_double(d_vals[i]);
            }
        } else {
            uint32_t *i_vals = (uint32_t *)vals;
            for (i=0; i<buf_len/sizeof(uint32_t); i++) {
                i_vals[i] = byteswap4(i_vals[i]);
            }
        }
    }
    
    ctx->class_is_posixct = 0;
    if (header.attributes) {
        if ((retval = read_attributes(&handle_vector_attribute, ctx)) != READSTAT_OK)
            goto cleanup;
    }
    
    if (ctx->column_handler) {
        if (header.type == RDATA_SEXPTYPE_LOGICAL_VECTOR ||
                header.type == RDATA_SEXPTYPE_INTEGER_VECTOR) {
            double *real_vals = malloc(length * sizeof(double));
            int32_t *i_vals = (int32_t *)vals;
            for (i=0; i<length; i++) {
                if (i_vals[i] == INT32_MIN) {
                    real_vals[i] = NAN;
                } else {
                    real_vals[i] = i_vals[i];
                }
            }
            if (ctx->column_handler(name, output_data_type, NULL, real_vals, length, ctx->user_ctx)) {
                retval = READSTAT_ERROR_USER_ABORT;
                goto cleanup;
            }
            free(real_vals);
        } else {
            if (ctx->column_handler(name, output_data_type, ctx->class_is_posixct ? "%ts" : NULL, vals, length, ctx->user_ctx)) {
                retval = READSTAT_ERROR_USER_ABORT;
                goto cleanup;
            }
        }
    }

cleanup:
    
    return retval;
}
Ejemplo n.º 8
0
/* Opens an HDF file and reads all its SDS metadata, returning an SDSInfo
 * structure containing this metadata.  Returns NULL on error.
 */
SDSInfo *open_h4_sds(const char *path)
{
    int i, status;

    int sd_id = SDstart(path, DFACC_READ);
    CHECK_HDF_ERROR(path, sd_id);

    // get dataset and global att counts
    int32 n_datasets, n_global_atts;
    status = SDfileinfo(sd_id, &n_datasets, &n_global_atts);
    CHECK_HDF_ERROR(path, status);

    SDSInfo *sds = NEW0(SDSInfo);
    sds->path = xstrdup(path);
    sds->type = SDS_HDF4_FILE;
    sds->id = sd_id;

    // read global attributes
    sds->gatts = read_attributes(path, sd_id, n_global_atts);

    // read variables ('datasets')
    for (i = 0; i < n_datasets; i++) {
        int sds_id = SDselect(sd_id, i);
        CHECK_HDF_ERROR(path, sds_id);

        char buf[_H4_MAX_SDS_NAME + 1];
        memset(buf, 0, sizeof(buf));
        int32 rank, dim_sizes[H4_MAX_VAR_DIMS], type, natts;
        status = SDgetinfo(sds_id, buf, &rank, dim_sizes, &type, &natts);
        CHECK_HDF_ERROR(path, status);

        SDSVarInfo *var = NEW0(SDSVarInfo);
        var->name = xstrdup(buf);
        var->type = h4_to_sdstype(type);
        var->iscoord = SDiscoordvar(sds_id);
        var->ndims = rank;
        var->dims = read_dimensions(sds, sds_id, rank, dim_sizes);
        var->atts = read_attributes(path, sds_id, natts);
        var->id = i; // actually the sds_index

        comp_coder_t comp_type;
        comp_info c_info;
        status = SDgetcompinfo(sds_id, &comp_type, &c_info);
        CHECK_HDF_ERROR(path, status);
        switch (comp_type) {
        case COMP_CODE_NONE:
            var->compress = 0;
            break;
        case COMP_CODE_DEFLATE:
            var->compress = c_info.deflate.level;
            break;
        default:
            // any other compression method is 'worth' 1 imo *trollface*
            // better than claiming 0 to the user
            var->compress = 1;
            break;
        }

        var->sds = sds;

        var->next = sds->vars;
        sds->vars = var;

        status = SDendaccess(sds_id);
        CHECK_HDF_ERROR(path, status);
    }
    sds->vars = (SDSVarInfo *)list_reverse((List *)sds->vars);
    sds->dims = (SDSDimInfo *)list_reverse((List *)sds->dims);

    sds->funcs = &h4_funcs;
    return sds;
}