示例#1
0
static void set_pixel(oskar_Sky* sky, int i, int x, int y, double val,
        const double crval[2], const double crpix[2], const double cdelt[2],
        double image_freq_hz, double spectral_index, int* status)
{
    double ra, dec, l, m;

    /* Convert pixel positions to RA and Dec values. */
    l = cdelt[0] * (x + 1 - crpix[0]);
    m = cdelt[1] * (y + 1 - crpix[1]);
    oskar_convert_relative_directions_to_lon_lat_2d_d(1,
            &l, &m, crval[0], crval[1], &ra, &dec);

    /* Store pixel data in sky model. */
    if (oskar_sky_num_sources(sky) <= i)
        oskar_sky_resize(sky, i + 1000, status);
    oskar_sky_set_source(sky, i, ra, dec, val, 0.0, 0.0, 0.0,
            image_freq_hz, spectral_index, 0.0, 0.0, 0.0, 0.0, status);
}
示例#2
0
oskar_Sky* oskar_sky_load(const char* filename, int type, int* status)
{
    int n = 0;
    FILE* file;
    char* line = 0;
    size_t bufsize = 0;
    oskar_Sky* sky;

    /* Check if safe to proceed. */
    if (*status) return 0;

    /* Get the data type. */
    if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE)
    {
        *status = OSKAR_ERR_BAD_DATA_TYPE;
        return 0;
    }

    /* Open the file. */
    file = fopen(filename, "r");
    if (!file)
    {
        *status = OSKAR_ERR_FILE_IO;
        return 0;
    }

    /* Initialise the sky model. */
    sky = oskar_sky_create(type, OSKAR_CPU, 0, status);

    /* Loop over lines in file. */
    while (oskar_getline(&line, &bufsize, file) != OSKAR_ERR_EOF)
    {
        /* Set defaults. */
        /* RA, Dec, I, Q, U, V, freq0, spix, RM, FWHM maj, FWHM min, PA */
        double par[] = {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.};
        size_t num_param = sizeof(par) / sizeof(double);
        size_t num_required = 3, num_read = 0;

        /* Load source parameters (require at least RA, Dec, Stokes I). */
        num_read = oskar_string_to_array_d(line, num_param, par);
        if (num_read < num_required)
            continue;

        /* Ensure enough space in arrays. */
        if (oskar_sky_num_sources(sky) <= n)
        {
            oskar_sky_resize(sky, n + 100, status);
            if (*status)
                break;
        }

        if (num_read <= 9)
        {
            /* RA, Dec, I, Q, U, V, freq0, spix, RM */
            oskar_sky_set_source(sky, n, par[0] * deg2rad,
                    par[1] * deg2rad, par[2], par[3], par[4], par[5],
                    par[6], par[7], par[8], 0.0, 0.0, 0.0, status);
        }
        else if (num_read == 11)
        {
            /* Old format, with no rotation measure. */
            /* RA, Dec, I, Q, U, V, freq0, spix, FWHM maj, FWHM min, PA */
            oskar_sky_set_source(sky, n, par[0] * deg2rad,
                    par[1] * deg2rad, par[2], par[3], par[4], par[5],
                    par[6], par[7], 0.0, par[8] * arcsec2rad,
                    par[9] * arcsec2rad, par[10] * deg2rad, status);
        }
        else if (num_read == 12)
        {
            /* New format. */
            /* RA, Dec, I, Q, U, V, freq0, spix, RM, FWHM maj, FWHM min, PA */
            oskar_sky_set_source(sky, n, par[0] * deg2rad,
                    par[1] * deg2rad, par[2], par[3], par[4], par[5],
                    par[6], par[7], par[8], par[9] * arcsec2rad,
                    par[10] * arcsec2rad, par[11] * deg2rad, status);
        }
        else
        {
            /* Error. */
            *status = OSKAR_ERR_BAD_SKY_FILE;
            break;
        }
        ++n;
    }

    /* Set the size to be the actual number of elements loaded. */
    oskar_sky_resize(sky, n, status);

    /* Free the line buffer and close the file. */
    if (line) free(line);
    fclose(file);

    /* Check if an error occurred. */
    if (*status)
    {
        oskar_sky_free(sky, status);
        sky = 0;
    }

    /* Return a handle to the sky model. */
    return sky;
}
示例#3
0
oskar_Sky* oskar_sky_from_image(int precision, const oskar_Mem* image,
        int image_width, int image_height, double image_ra_deg,
        double image_dec_deg, double image_cellsize_deg, double image_freq_hz,
        double spectral_index, int* status)
{
    int i, type, x, y;
    double crval[2], crpix[2], cdelt[2], val;
    oskar_Sky* sky;

    /* Check if safe to proceed. */
    if (*status) return 0;

    /* Check the image size is even. */
    if ((image_width % 2) || (image_height % 2))
    {
        *status = OSKAR_ERR_INVALID_ARGUMENT;
        return 0;
    }

    /* Get reference pixels and reference values in radians. */
    crpix[0] = image_width / 2 + 1;
    crpix[1] = image_height / 2 + 1;
    crval[0] = image_ra_deg * M_PI / 180.0;
    crval[1] = image_dec_deg * M_PI / 180.0;

    /* Compute sine of pixel deltas for inverse orthographic projection. */
    cdelt[0] = -sin(image_cellsize_deg * M_PI / 180.0);
    cdelt[1] = -cdelt[0];

    /* Create a sky model. */
    sky = oskar_sky_create(precision, OSKAR_CPU, 0, status);

    /* Store the image pixels. */
    type = oskar_mem_precision(image);
    if (type == OSKAR_SINGLE)
    {
        const float *img = oskar_mem_float_const(image, status);
        for (y = 0, i = 0; y < image_height; ++y)
        {
            for (x = 0; x < image_width; ++x)
            {
                /* Check pixel value. */
                val = (double) (img[image_width * y + x]);
                if (val == 0.0)
                    continue;

                set_pixel(sky, i++, x, y, val, crval, crpix, cdelt,
                        image_freq_hz, spectral_index, status);
            }
        }
    }
    else
    {
        const double *img = oskar_mem_double_const(image, status);
        for (y = 0, i = 0; y < image_height; ++y)
        {
            for (x = 0; x < image_width; ++x)
            {
                /* Check pixel value. */
                val = img[image_width * y + x];
                if (val == 0.0)
                    continue;

                set_pixel(sky, i++, x, y, val, crval, crpix, cdelt,
                        image_freq_hz, spectral_index, status);
            }
        }
    }

    /* Return the sky model. */
    oskar_sky_resize(sky, i, status);
    return sky;
}
示例#4
0
static PyObject* append_sources(PyObject* self, PyObject* args)
{
    oskar_Sky *h = 0;
    PyObject *obj[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    oskar_Mem *ra_c, *dec_c, *I_c, *Q_c, *U_c, *V_c;
    oskar_Mem *ref_c, *spix_c, *rm_c, *maj_c, *min_c, *pa_c;
    PyArrayObject *ra = 0, *dec = 0, *I = 0, *Q = 0, *U = 0, *V = 0;
    PyArrayObject *ref = 0, *spix = 0, *rm = 0, *maj = 0, *min = 0, *pa = 0;
    int status = 0, npy_type, type, flags, num_sources, old_num;

    /* Parse inputs: RA, Dec, I, Q, U, V, ref, spix, rm, maj, min, pa. */
    if (!PyArg_ParseTuple(args, "OOOOOOOOOOOOO", &obj[0],
            &obj[1], &obj[2], &obj[3], &obj[4], &obj[5], &obj[6],
            &obj[7], &obj[8], &obj[9], &obj[10], &obj[11], &obj[12]))
        return 0;
    if (!(h = get_handle(obj[0]))) return 0;

    /* Make sure input objects are arrays. Convert if required. */
    flags = NPY_ARRAY_FORCECAST | NPY_ARRAY_IN_ARRAY;
    type = oskar_sky_precision(h);
    npy_type = numpy_type_from_oskar(type);
    ra   = (PyArrayObject*) PyArray_FROM_OTF(obj[1], npy_type, flags);
    dec  = (PyArrayObject*) PyArray_FROM_OTF(obj[2], npy_type, flags);
    I    = (PyArrayObject*) PyArray_FROM_OTF(obj[3], npy_type, flags);
    Q    = (PyArrayObject*) PyArray_FROM_OTF(obj[4], npy_type, flags);
    U    = (PyArrayObject*) PyArray_FROM_OTF(obj[5], npy_type, flags);
    V    = (PyArrayObject*) PyArray_FROM_OTF(obj[6], npy_type, flags);
    ref  = (PyArrayObject*) PyArray_FROM_OTF(obj[7], npy_type, flags);
    spix = (PyArrayObject*) PyArray_FROM_OTF(obj[8], npy_type, flags);
    rm   = (PyArrayObject*) PyArray_FROM_OTF(obj[9], npy_type, flags);
    maj  = (PyArrayObject*) PyArray_FROM_OTF(obj[10], npy_type, flags);
    min  = (PyArrayObject*) PyArray_FROM_OTF(obj[11], npy_type, flags);
    pa   = (PyArrayObject*) PyArray_FROM_OTF(obj[12], npy_type, flags);
    if (!ra || !dec || !I || !Q || !U || !V ||
            !ref || !spix || !rm || !maj || !min || !pa)
        goto fail;

    /* Check size of input arrays. */
    num_sources = (int) PyArray_SIZE(I);
    if (num_sources != (int) PyArray_SIZE(ra) ||
            num_sources != (int) PyArray_SIZE(dec) ||
            num_sources != (int) PyArray_SIZE(Q) ||
            num_sources != (int) PyArray_SIZE(U) ||
            num_sources != (int) PyArray_SIZE(V) ||
            num_sources != (int) PyArray_SIZE(ref) ||
            num_sources != (int) PyArray_SIZE(spix) ||
            num_sources != (int) PyArray_SIZE(rm) ||
            num_sources != (int) PyArray_SIZE(maj) ||
            num_sources != (int) PyArray_SIZE(min) ||
            num_sources != (int) PyArray_SIZE(pa))
    {
        PyErr_SetString(PyExc_RuntimeError, "Input data dimension mismatch.");
        goto fail;
    }

    /* Pointers to input arrays. */
    ra_c = oskar_mem_create_alias_from_raw(PyArray_DATA(ra),
            type, OSKAR_CPU, num_sources, &status);
    dec_c = oskar_mem_create_alias_from_raw(PyArray_DATA(dec),
            type, OSKAR_CPU, num_sources, &status);
    I_c = oskar_mem_create_alias_from_raw(PyArray_DATA(I),
            type, OSKAR_CPU, num_sources, &status);
    Q_c = oskar_mem_create_alias_from_raw(PyArray_DATA(Q),
            type, OSKAR_CPU, num_sources, &status);
    U_c = oskar_mem_create_alias_from_raw(PyArray_DATA(U),
            type, OSKAR_CPU, num_sources, &status);
    V_c = oskar_mem_create_alias_from_raw(PyArray_DATA(V),
            type, OSKAR_CPU, num_sources, &status);
    ref_c = oskar_mem_create_alias_from_raw(PyArray_DATA(ref),
            type, OSKAR_CPU, num_sources, &status);
    spix_c = oskar_mem_create_alias_from_raw(PyArray_DATA(spix),
            type, OSKAR_CPU, num_sources, &status);
    rm_c = oskar_mem_create_alias_from_raw(PyArray_DATA(rm),
            type, OSKAR_CPU, num_sources, &status);
    maj_c = oskar_mem_create_alias_from_raw(PyArray_DATA(maj),
            type, OSKAR_CPU, num_sources, &status);
    min_c = oskar_mem_create_alias_from_raw(PyArray_DATA(min),
            type, OSKAR_CPU, num_sources, &status);
    pa_c = oskar_mem_create_alias_from_raw(PyArray_DATA(pa),
            type, OSKAR_CPU, num_sources, &status);

    /* Copy source data into the sky model. */
    old_num = oskar_sky_num_sources(h);
    oskar_sky_resize(h, old_num + num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_ra_rad(h), ra_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_dec_rad(h), dec_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_I(h), I_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_Q(h), Q_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_U(h), U_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_V(h), V_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_reference_freq_hz(h), ref_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_spectral_index(h), spix_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_rotation_measure_rad(h), rm_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_fwhm_major_rad(h), maj_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_fwhm_minor_rad(h), min_c,
            old_num, 0, num_sources, &status);
    oskar_mem_copy_contents(oskar_sky_position_angle_rad(h), pa_c,
            old_num, 0, num_sources, &status);

    /* Free memory. */
    oskar_mem_free(ra_c, &status);
    oskar_mem_free(dec_c, &status);
    oskar_mem_free(I_c, &status);
    oskar_mem_free(Q_c, &status);
    oskar_mem_free(U_c, &status);
    oskar_mem_free(V_c, &status);
    oskar_mem_free(ref_c, &status);
    oskar_mem_free(spix_c, &status);
    oskar_mem_free(rm_c, &status);
    oskar_mem_free(maj_c, &status);
    oskar_mem_free(min_c, &status);
    oskar_mem_free(pa_c, &status);

    /* Check for errors. */
    if (status)
    {
        PyErr_Format(PyExc_RuntimeError,
                "Sky model append_sources() failed with code %d (%s).",
                status, oskar_get_error_string(status));
        goto fail;
    }

    Py_XDECREF(ra);
    Py_XDECREF(dec);
    Py_XDECREF(I);
    Py_XDECREF(Q);
    Py_XDECREF(U);
    Py_XDECREF(V);
    Py_XDECREF(ref);
    Py_XDECREF(spix);
    Py_XDECREF(rm);
    Py_XDECREF(maj);
    Py_XDECREF(min);
    Py_XDECREF(pa);
    return Py_BuildValue("");

fail:
    Py_XDECREF(ra);
    Py_XDECREF(dec);
    Py_XDECREF(I);
    Py_XDECREF(Q);
    Py_XDECREF(U);
    Py_XDECREF(V);
    Py_XDECREF(ref);
    Py_XDECREF(spix);
    Py_XDECREF(rm);
    Py_XDECREF(maj);
    Py_XDECREF(min);
    Py_XDECREF(pa);
    return 0;
}