Ejemplo n.º 1
0
static int packed_read_raw_ref(struct ref_store *ref_store,
			       const char *refname, struct object_id *oid,
			       struct strbuf *referent, unsigned int *type)
{
	struct packed_ref_store *refs =
		packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
	struct snapshot *snapshot = get_snapshot(refs);
	const char *rec;

	*type = 0;

	rec = find_reference_location(snapshot, refname, 1);

	if (!rec) {
		/* refname is not a packed reference. */
		errno = ENOENT;
		return -1;
	}

	if (get_oid_hex(rec, oid))
		die_invalid_line(refs->path, rec, snapshot->eof - rec);

	*type = REF_ISPACKED;
	return 0;
}
Ejemplo n.º 2
0
static struct ref_iterator *packed_ref_iterator_begin(
		struct ref_store *ref_store,
		const char *prefix, unsigned int flags)
{
	struct packed_ref_store *refs;
	struct snapshot *snapshot;
	const char *start;
	struct packed_ref_iterator *iter;
	struct ref_iterator *ref_iterator;
	unsigned int required_flags = REF_STORE_READ;

	if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
		required_flags |= REF_STORE_ODB;
	refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");

	/*
	 * Note that `get_snapshot()` internally checks whether the
	 * snapshot is up to date with what is on disk, and re-reads
	 * it if not.
	 */
	snapshot = get_snapshot(refs);

	if (!snapshot->buf)
		return empty_ref_iterator_begin();

	iter = xcalloc(1, sizeof(*iter));
	ref_iterator = &iter->base;
	base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);

	iter->snapshot = snapshot;
	acquire_snapshot(snapshot);

	if (prefix && *prefix)
		start = find_reference_location(snapshot, prefix, 0);
	else
		start = snapshot->buf + snapshot->header_len;

	iter->pos = start;
	iter->eof = snapshot->eof;
	strbuf_init(&iter->refname_buf, 0);

	iter->base.oid = &iter->oid;

	iter->flags = flags;

	if (prefix && *prefix)
		/* Stop iteration after we've gone *past* prefix: */
		ref_iterator = prefix_ref_iterator_begin(ref_iterator, prefix, 0);

	return ref_iterator;
}
/*************************************************************************
Name: ias_los_model_create_lunar_projection

Purpose: Creates a lunar projection with the indicated parameters.  If
    requested with the use_cache parameter, it creates a cache of moon 
    positions for each line of the image since calculating the moon position
    is slow.  The cache should be used if a lot of projection transformations
    need to be done.

Returns: Pointer to the created projection or NULL if an error.

**************************************************************************/
IAS_LUNAR_PROJECTION *ias_los_model_create_lunar_projection
(
    const IAS_LOS_MODEL *model,  /* I: model information */
    int band_index,              /* I: Band index */
    int sca_index,               /* I: SCA index */
    IAS_SENSOR_DETECTOR_TYPE dettype, /* I: Detector type */
    double unit_scale,           /* I: Scale for output coordinate units */
    int use_cache                /* I: Flag to use caching to speed up
                                       transformations when a lot of
                                       transformations will be done */
)
{
    int ref_band_index;     /* Reference location on the focal plane */
    int ref_sca_index;
    int ref_image_line;
    int ref_detector;
    int line;               /* Line in the input image */
    double samp;            /* Sample in the input image */
    IAS_LUNAR_PROJECTION *proj = NULL;
    double pi = ias_math_get_pi();

    /* Get the reference location */
    if (find_reference_location(model, &ref_band_index, &ref_sca_index,
            &ref_image_line, &ref_detector) != SUCCESS)
    {
        IAS_LOG_ERROR("Could not find the focal plane reference location");
        return NULL;
    }
    samp = ref_detector;

    /* Allocate the memory for the cached information */
    proj = calloc(1, sizeof(*proj));
    if (!proj)
    {
        IAS_LOG_ERROR("Failed allocating memory Lunar projection cache");
        return NULL;
    }

    /* Fill in the constants in the projection structure */
    proj->model = model;
    proj->band_index = band_index;
    proj->sca_index = sca_index;
    proj->dettype = dettype;
    proj->unit_scale = unit_scale;

    /* assume we won't cache information */
    proj->img_lines = 0;

    /* Find moon's position at the center of L0R. This will serve as the
       reference.  All other points are adjusted according to it's moon
       position and this location.  Note the reference always uses the nominal
       detector type */
    if (ias_los_model_get_moon_position_at_location(model, ref_band_index,
            ref_sca_index, ref_image_line, ref_detector, IAS_NOMINAL_DETECTOR,
            &proj->ref_rasc, &proj->ref_dec, &proj->ref_dist) != SUCCESS)
    {
        IAS_LOG_ERROR("Error in the calculating moon position for L/S");
        free(proj);
        return NULL;
    }

    IAS_LOG_DEBUG("Ref RASC: %13.8lf  Ref DEC: %13.8lf  Ref Dist: %17.6lf",
            proj->ref_rasc, proj->ref_dec, proj->ref_dist);

    /* Set up the cache if it was requested */
    if (use_cache)
    {
        /* Calculate the number of lines in the input image using the model
           information */
        proj->img_lines = model->sensor.bands[band_index].frame_count
            * model->sensor.bands[band_index].sampling_char.lines_per_frame;

        /* Allocate the buffers for the moon position at each line of the
           image */
        proj->img_rasc = (double *)malloc(sizeof(double) * proj->img_lines);
        if (!proj->img_rasc)
        {
            IAS_LOG_ERROR("Error allocating RA look up table");
            ias_los_model_free_lunar_projection(proj);
            return NULL;
        }

        proj->img_dec = (double *)malloc(sizeof(double) * proj->img_lines);
        if (!proj->img_dec)
        {
            IAS_LOG_ERROR("Error allocating DEC look up table");
            ias_los_model_free_lunar_projection(proj);
            return NULL;
        }

        proj->img_dist = (double *)malloc(sizeof(double) * proj->img_lines);
        if (!proj->img_dist)
        {
            IAS_LOG_ERROR("Error allocating Dist look up table");
            ias_los_model_free_lunar_projection(proj);
            return NULL;
        }

        /* Build look up table for each line in the image */
        for (line = 0; line < proj->img_lines; line++)
        {
            if (ias_los_model_get_moon_position_at_location(model, band_index,
                    sca_index, line, samp, dettype, 
                    &proj->img_rasc[line], &proj->img_dec[line],
                    &proj->img_dist[line]) != SUCCESS)
            {
                IAS_LOG_ERROR("Error calculating moon position look up table "
                    "for line %d", line);
                ias_los_model_free_lunar_projection(proj);
                return NULL;
            }

            /* Make sure the RASC doesn't walk across +/- 180 degree line */
            if (line > 0)
            {
                if ((proj->img_rasc[line] - proj->img_rasc[line - 1]) >  pi/2.0)
                    proj->img_rasc[line] -= 2.0 * pi;
                if ((proj->img_rasc[line] - proj->img_rasc[line - 1]) < -pi/2.0)
                    proj->img_rasc[line] += 2.0 * pi;
            }
        }
    }

    return proj;
}