Example #1
0
    def value_interpolated(self, point, k=3):
        """
        """
        # FIXME: Current implementation fails across borders (height matrix). Fallback to point.

        if not self.geotransform:
            # Data is not available
            raise AssertionError(
                "No elevation data available for the given point.")

        x, y = (point[0], point[1])
        if self.crs != 'epsg:4326':
            x, y = self.crs_transformer.transform(x, y)

        # Transform to raster point coordinates
        #pixel_is_area = False #if pixel_is_area:
        raster_x = int((x - self.geotransform[0]) / self.geotransform[1])
        raster_y = int((y - self.geotransform[3]) / self.geotransform[5])
        coords_x = ((raster_x * self.geotransform[1])) + self.geotransform[0]
        coords_y = ((raster_y * self.geotransform[5])) + self.geotransform[3]
        offset_x = -(coords_x - x) / self.geotransform[1]
        offset_y = -(coords_y - y) / self.geotransform[5]

        try:
            if k == 1:
                height_matrix = self.layer.GetRasterBand(1).ReadAsArray(
                    raster_x, raster_y, 2, 2)
            elif k == 3:
                height_matrix = self.layer.GetRasterBand(1).ReadAsArray(
                    raster_x - 1, raster_y - 1, 4, 4)
        except Exception as e:
            # TODO: Better support for borders
            return self.value_simple(point)

        # Correct EUDEM11 <10000 values
        try:
            height_matrix = np.maximum(height_matrix, 0)
        except TypeError as e:
            # Fix None values (empty / missing)
            # TODO: Except on sea, this should possibly get the average from surrounding values
            return 0

        if k == 1:
            interpolated = interp2d([0, 1], [0, 1], height_matrix,
                                    'linear')  # , copy=False
        elif k == 3:
            interpolated = interp2d([-1, 0, 1, 2], [-1, 0, 1, 2],
                                    height_matrix, 'cubic')  # , copy=False
        else:
            raise AssertionError()

        value = interpolated(offset_x, offset_y)

        return float(value)
Example #2
0
def interpolateArray(data,shape,kind='linear'):
        X,Y = np.meshgrid(np.arange(data.shape[0]),np.arange(data.shape[1]))
        outgrid = interpolate.interp2d(X,Y,data,kind=kind)
        xi = np.linspace(0,data.shape[0],shape[0])
        yi = np.linspace(0,data.shape[1],shape[1])
        z = outgrid(xi,yi)
        return z
Example #3
0
def calc_prob_spec(t, lambdas, spec, t0, zp):

    count = 0
    chisquare = np.nan

    for key in data_out:
        wav2, flux2, error = data_out[key]["lambda"], data_out[key][
            "data"], data_out[key]["error"]
        ii = np.where(np.not_equal(flux2, 0))[0]
        wav2, flux2, error = wav2[ii], flux2[ii], error[ii]

        sigma_y = np.abs(error / (flux2 * np.log(10)))
        sigma = np.sqrt((np.log10(1 + opts.errorbudget))**2 + sigma_y**2)

        f = interp.interp2d(t + t0, lambdas, np.log10(spec), kind='cubic')
        flux1 = (10**(f(float(key), wav2))).T

        zp_factor = 10**(zp / -2.5)
        flux1 = flux1 * zp_factor

        if Global.doAbsorption:
            #lambdas_lowpass, spec_lowpass, spec_envelope = lightcurve_utils.get_envelope(wav2,flux1[0])
            #flux1 = spec_lowpass/spec_envelope
            flux1 = flux1 / np.nanmax(flux1)
            chisquarevals = ((flux1 - flux2) / opts.errorbudget)**2
            chisquarevals = chisquarevals[0]
        else:
            zp_factor = 10**(zp / -2.5)
            flux1 = flux1 * zp_factor
            flux1 = np.log10(np.abs(flux1))
            flux2 = np.log10(np.abs(flux2))
            chisquarevals = ((flux1 - flux2) / sigma)**2
            chisquarevals = chisquarevals[0]

        chisquaresum = np.sum(chisquarevals)
        chisquaresum = (1 / float(len(chisquarevals) - 1)) * chisquaresum

        if count == 0:
            chisquare = chisquaresum
        else:
            chisquare = chisquare + chisquaresum
        count = count + 1

    if np.isnan(chisquare):
        prob = -np.inf
    else:
        prob = scipy.stats.chi2.logpdf(chisquare, 1, loc=0, scale=1)

    if np.isnan(prob):
        prob = -np.inf

    if prob == 0.0:
        prob = -np.inf

    #if np.isfinite(prob):
    #    print T, F, prob

    return prob
Example #4
0
    def value_interpolated(self, point):
        """
        """
        # FIXME: Current implementation fails across borders (height matrix). Fallback to point.

        if not self.geotransform:
            # Data is not available
            raise AssertionError(
                "No elevation data available for the given point.")

        x, y = (point[0], point[1])
        if self.crs != 'epsg:4326':
            x, y = self.crs_transformer.transform(x, y)

        # Transform to raster point coordinates
        pixel_is_area = True  # in Vigo, True seems more accurate
        if pixel_is_area:
            raster_x = int(
                round((x - self.geotransform[0]) / self.geotransform[1]))
            raster_y = int(
                round((y - self.geotransform[3]) / self.geotransform[5]))
            coords_x = (
                (raster_x * self.geotransform[1])) + self.geotransform[0]
            coords_y = (
                (raster_y * self.geotransform[5])) + self.geotransform[3]
            # Pixel offset, centerted on 0, from the point to the pixel center
            offset_x = -(coords_x - x) / self.geotransform[1]
            offset_y = -(coords_y - y) / self.geotransform[5]
        else:
            raster_x = int(
                round((x - self.geotransform[0]) / self.geotransform[1]))
            raster_y = int(
                round((y - self.geotransform[3]) / self.geotransform[5]))
            coords_x = (((0.5 + raster_x) *
                         self.geotransform[1])) + self.geotransform[0]
            coords_y = (((0.5 + raster_y) *
                         self.geotransform[5])) + self.geotransform[3]
            # Pixel offset, centerted on 0, from the point to the pixel center
            offset_x = -(coords_x - x) / self.geotransform[1]
            offset_y = -(coords_y - y) / self.geotransform[5]

        try:
            height_matrix = self.layer.GetRasterBand(1).ReadAsArray(
                raster_x - 1, raster_y - 1, 3, 3)
        except Exception as e:
            # Safeguard
            #logger.exception("Exception obtaining 3x3 height matrix around point %s", point)
            return self.value_simple(point)

        interpolated = interp2d([-1, 0, 1], [-1, 0, 1], height_matrix,
                                'linear')  # , copy=False
        value = interpolated(offset_x, offset_y)

        #logger.debug("Elevation: point=%.1f,%.1f, offset=%.8f,%.8f, value=%.1f", x, y, offset_x, offset_y, value)

        return float(value)
Example #5
0
def get_point_from_GFS_slice_for_coords(gfs_data: np.ndarray, coords: Coords):
    nearest_coords = get_nearest_coords(coords)
    lat, lon = get_nearest_lat_lon_from_coords(nearest_coords, coords)
    lat_index = int((GFS_SPACE.nlat - lat) * 4)
    lon_index = int((GFS_SPACE.elon - lon) * 4)

    return interpolate.interp2d(
        nearest_coords[0], nearest_coords[1],
        gfs_data[lat_index:lat_index + 2,
                 lon_index:lon_index + 2])(coords.nlat, coords.elon).item()
Example #6
0
    def _get_spline(self, data):
        xs = data['xs']
        ys = data['ys']
        zs = data['zs']

        splines = []
        for i in range(0, len(zs[0])):
            splines.append(interp2d(xs, ys, [z[i] for z in zs], kind='cubic'))

        def _spline(x, y):
            return [spline(x, y)[0] for spline in splines]

        return _spline
 def _get_spline(self, data):
     xs = data['xs']
     ys = data['ys']
     zs = data['zs']
     
     splines = []
     for i in range(0,len(zs[0])):
         splines.append(interp2d(xs, ys, [z[i] for z in zs], kind='cubic'))
     
     def _spline(x, y):
         return [spline(x, y)[0] for spline in splines]
     
     return _spline
Example #8
0
 def _get_spirrid_evaluation_cached(self):
     interpolators_lst = []
     for i, spirr in enumerate(self.spirrid_lst):
         Lfi = self.short_reinf_lst[i].Lf
         def minfunc_short_fibers(w):
             spirr.eps_vars=dict(w=np.array([w]),
                                 x=np.array([0.0]))
             return -spirr.mu_q_arr.flatten()
         w_maxi = fminbound(minfunc_short_fibers, 0.0, Lfi/3., maxfun=20, disp=0)
         w_arri = np.hstack((np.linspace(0.0, w_maxi, 15),np.linspace(w_maxi + 1e-10, Lfi/2., 15)))
         spirr.eps_vars = dict(w=w_arri,
                               x=self.x_arr)
         interpolators_lst.append(interp2d(self.x_arr, w_arri, spirr.mu_q_arr, fill_value=0.0))
     return interpolators_lst
 def _get_spirrid_evaluation_cached(self):
     interpolators_lst = []
     for i, spirr in enumerate(self.spirrid_lst):
         Lfi = self.short_reinf_lst[i].Lf
         def minfunc_short_fibers(w):
             spirr.eps_vars=dict(w=np.array([w]),
                                 x=np.array([0.0]))
             return -spirr.mu_q_arr.flatten()
         w_maxi = fminbound(minfunc_short_fibers, 0.0, Lfi/3., maxfun=20, disp=0)
         w_arri = np.hstack((np.linspace(0.0, w_maxi, 15),np.linspace(w_maxi + 1e-10, Lfi/2., 15)))
         spirr.eps_vars = dict(w=w_arri,
                               x=self.x_arr)
         interpolators_lst.append(interp2d(self.x_arr, w_arri, spirr.mu_q_arr, fill_value=0.0))
     return interpolators_lst
Example #10
0
def interpLonLat(lons, lats, multiple):
    
    row, col = lons.shape
    x = multiple * np.arange(col) + (multiple - 1) / 2.
    y = multiple * np.arange(row) + (multiple - 1) / 2.
    
    # set y minus where lons == -999
    index = np.where(lons[:, 0] < -900)
    # set minus value the point will not participate interp, 
    # but each y should not be the same
    y[index] = y[index] * -1.

    # interp
    z = lons
    f = interp2d(x, y, z)
    xx = np.arange(col * multiple)
    yy = np.arange(row * multiple)
    lons1 = f(xx, yy)

    z = lats
    f = interp2d(x, y, z)
    lats1 = f(xx, yy)
    
    return lons1, lats1
Example #11
0
def subpix2(z,ii,jj):
    trange = np.arange(7)
    ttrange = np.arange(7)
    X,Y = np.meshgrid(trange,ttrange)
    outgrid = interpolate.interp2d(X,Y,z,kind='quintic')
    xx=yy=np.arange(61)/10.
    l=outgrid(xx,yy)
    l=l[30-9:30+10,30-9:30+10]
    ind=find(l,l==np.amax(l))
    #print l
    #print ind[0][0],ind[1][0]
    ni=ii+(ind[0][0]-9.)/10
    nj=jj+(ind[1][0]-9.)/10
    #print ii,jj
    #print ni,nj
    return[ni,nj]
Example #12
0
def load_interp2d(xz_data_path: str, y: list):
    """
    Setup 2D interpolation

    Example:
    x1, y1, z1, x2, y2, z2\n
    1,   3,  5,  1,  4,  6\n
    2,   3,  6,  2,  4,  7\n
    3,   3   7,  3,  4,  8\n

    xy_data_path will lead to a file such as:

    1,5,6\n
    2,6,7\n
    3,7,8\n

    y will be: [3, 4]

    :param xz_data_path: path to csv file with columnated data, e.g. 'x1,z1,z2,...,zn'
    :param y: list of *constant* values for the second independent variable
    :return initialized interp2d instance
    """

    data = np.genfromtxt(xz_data_path, delimiter=',')
    _, num_col = data.shape

    num_series = num_col - 1

    # check to make sure number of columns and length of 'y' match
    if num_series != len(y):
        ValueError("Number of columns in '{}' inconsistent with 'y'".format(
            xz_data_path))

    x = data[:, 0]
    z = []
    for idx in range(1, num_series + 1):
        z.append(data[:, idx])

    return interp2d(x, y, z)
                if "Xlan1e" in keySplit[6]:
                    Xlan0 = 10**float(keySplit[6].replace("Xlan1e", ""))
                elif "Xlan1e" in keySplit[5]:
                    Xlan0 = 10**float(keySplit[5].replace("Xlan1e", ""))

            specs[key]["mej"] = mej0
            specs[key]["vej"] = vej0
            specs[key]["Xlan"] = Xlan0
        elif keySplit[0] == "SED":
            specs[key]["mej"], specs[key]["vej"], specs[key][
                "Ye"] = lightcurve_utils.get_macronovae_rosswog(key)

        data = specs[key]["data"].T
        data[data == 0.0] = 1e-20
        f = interp.interp2d(specs[key]["t"],
                            specs[key]["lambda"],
                            np.log10(data),
                            kind='cubic')
        #specs[key]["data"] = (10**(f(tt,lambdas))).T
        specs[key]["data"] = f(tt, lambdas).T

    speckeys = specs.keys()
    param_array = []
    for key in speckeys:
        if opts.model == "BaKa2016":
            param_array.append([specs[key]["mej"], specs[key]["vej"]])
        elif opts.model == "Ka2017":
            param_array.append(
                [specs[key]["mej"], specs[key]["vej"], specs[key]["Xlan"]])
        elif opts.model == "RoFe2017":
            param_array.append(
                [specs[key]["mej"], specs[key]["vej"], specs[key]["Ye"]])
Example #14
0
from scipy.interpolate import interpolate
from numpy.random import randn
from numpy import meshgrid, arange, array, dot
import oban

data = randn(16).reshape(4, 4)
x = y = arange(4)
X, Y = meshgrid(x, y)

x_loc = 1.4
y_loc = 2.7

full_bilin = interpolate.interp2d(X, Y, data, kind="linear")
partial_bilin = interpolate.interp2d(X[1:, 1:], Y[1:, 1:], data[1:, 1:], kind="linear")
single_bilin = interpolate.interp2d(X[2:4, 1:3], Y[2:4, 1:3], data[2:4, 1:3], kind="linear")

print full_bilin(x_loc, y_loc)
print partial_bilin(x_loc, y_loc)
print single_bilin(x_loc, y_loc)
top = data[2, 1] + (data[2, 2] - data[2, 1]) * 0.4
bottom = data[3, 1] + (data[3, 2] - data[3, 1]) * 0.4
print top + (bottom - top) * 0.7
xw = 0.4
yw = 0.7
xws = array([1 - xw, xw])
yws = array([1 - yw, yw])
print dot(yws, dot(data[2:4, 1:3], xws))
print oban.bilinear(x, y, data, x_loc, y_loc)

print data[2:4, 1:3]
Example #15
0
lightcurvesDir = opts.lightcurvesDir
spectraDir = opts.spectraDir

if opts.doAbsorption:
    Global.doAbsorption = 1

ModelPath = '%s/svdmodels' % (opts.outputDir)
if not os.path.isdir(ModelPath):
    os.makedirs(ModelPath)

fileDir = os.path.join(opts.outputDir, opts.model)
filenames = glob.glob('%s/*_spec.dat' % fileDir)
specs, names = lightcurve_utils.read_files_spec(filenames)
speckeys = specs.keys()
for key in speckeys:
    f = interp.interp2d(specs[key]["t"], specs[key]["lambda"],
                        specs[key]["data"].T)
    specs[key]["f"] = f

n_live_points = 100
#n_live_points = 10
evidence_tolerance = 0.5
#evidence_tolerance = 10
max_iter = 0

if opts.doModels:
    data_out_all = lightcurve_utils.loadModelsSpec(opts.outputDir, opts.name)
    keys = data_out_all.keys()
    if not opts.name in data_out_all:
        print "%s not in file..." % opts.name
        exit(0)
Example #16
0
def calc_svd_spectra(tini,
                     tmax,
                     dt,
                     lambdaini,
                     lambdamax,
                     dlambda,
                     n_coeff=100,
                     model="BaKa2016"):

    print("Calculating SVD model of lightcurve spectra...")

    if model == "BaKa2016":
        fileDir = "../output/barnes_kilonova_spectra"
    elif model == "Ka2017":
        fileDir = "../output/kasen_kilonova_grid"
    elif model == "RoFe2017":
        fileDir = "../output/macronovae-rosswog_wind"

    filenames = glob.glob('%s/*_spec.dat' % fileDir)

    specs, names = lightcurve_utils.read_files_spec(filenames)
    speckeys = specs.keys()

    tt = np.arange(tini, tmax + dt, dt)
    lambdas = np.arange(lambdaini, lambdamax + dlambda, dlambda)

    for key in speckeys:
        keySplit = key.split("_")
        if keySplit[0] == "rpft":
            mej0 = float("0." + keySplit[1].replace("m", ""))
            vej0 = float("0." + keySplit[2].replace("v", ""))
            specs[key]["mej"] = mej0
            specs[key]["vej"] = vej0
        elif keySplit[0] == "knova":
            mej0 = float(keySplit[3].replace("m", ""))
            vej0 = float(keySplit[4].replace("vk", ""))
            if len(keySplit) == 6:
                Xlan0 = 10**float(keySplit[5].replace("Xlan1e", ""))
            elif len(keySplit) == 7:
                #del specs[key]
                #continue
                if "Xlan1e" in keySplit[6]:
                    Xlan0 = 10**float(keySplit[6].replace("Xlan1e", ""))
                elif "Xlan1e" in keySplit[5]:
                    Xlan0 = 10**float(keySplit[5].replace("Xlan1e", ""))

            #if (mej0 == 0.05) and (vej0 == 0.2) and (Xlan0 == 1e-3):
            #    del specs[key]
            #    continue

            specs[key]["mej"] = mej0
            specs[key]["vej"] = vej0
            specs[key]["Xlan"] = Xlan0
        elif keySplit[0] == "SED":
            specs[key]["mej"], specs[key]["vej"], specs[key][
                "Ye"] = lightcurve_utils.get_macronovae_rosswog(key)

        data = specs[key]["data"].T
        data[data == 0.0] = 1e-20
        f = interp.interp2d(specs[key]["t"],
                            specs[key]["lambda"],
                            np.log10(data),
                            kind='cubic')
        #specs[key]["data"] = (10**(f(tt,lambdas))).T
        specs[key]["data"] = f(tt, lambdas).T

    speckeys = specs.keys()
    param_array = []
    for key in speckeys:
        if model == "BaKa2016":
            param_array.append(
                [np.log10(specs[key]["mej"]), specs[key]["vej"]])
        elif model == "Ka2017":
            param_array.append([
                np.log10(specs[key]["mej"]), specs[key]["vej"],
                np.log10(specs[key]["Xlan"])
            ])
        elif model == "RoFe2017":
            param_array.append([
                np.log10(specs[key]["mej"]), specs[key]["vej"],
                specs[key]["Ye"]
            ])

    param_array_postprocess = np.array(param_array)
    param_mins, param_maxs = np.min(param_array_postprocess,
                                    axis=0), np.max(param_array_postprocess,
                                                    axis=0)
    for i in range(len(param_mins)):
        param_array_postprocess[:, i] = (param_array_postprocess[:, i] -
                                         param_mins[i]) / (param_maxs[i] -
                                                           param_mins[i])

    svd_model = {}
    for jj, lambda_d in enumerate(lambdas):
        if np.mod(jj, 1) == 0:
            print("%d / %d" % (jj, len(lambdas)))

        spec_array = []
        for key in speckeys:
            spec_array.append(specs[key]["data"][:, jj])

        spec_array_postprocess = np.array(spec_array)
        mins, maxs = np.min(spec_array_postprocess,
                            axis=0), np.max(spec_array_postprocess, axis=0)
        for i in range(len(mins)):
            spec_array_postprocess[:, i] = (spec_array_postprocess[:, i] -
                                            mins[i]) / (maxs[i] - mins[i])
        spec_array_postprocess[np.isnan(spec_array_postprocess)] = 0.0
        UA, sA, VA = np.linalg.svd(spec_array_postprocess, full_matrices=True)
        VA = VA.T

        n, n = UA.shape
        m, m = VA.shape

        cAmat = np.zeros((n_coeff, n))
        cAvar = np.zeros((n_coeff, n))
        ErrorLevel = 2
        for i in range(n):
            cAmat[:, i] = np.dot(spec_array_postprocess[i, :], VA[:, :n_coeff])
            errors = ErrorLevel * spec_array_postprocess[i, :]
            cAvar[:, i] = np.diag(
                np.dot(VA[:, :n_coeff].T,
                       np.dot(np.diag(np.power(errors, 2.)), VA[:, :n_coeff])))
        cAstd = np.sqrt(cAvar)

        nsvds, nparams = param_array_postprocess.shape
        kernel = 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1)

        gps = []
        for i in range(n_coeff):
            gp = GaussianProcessRegressor(kernel=kernel,
                                          n_restarts_optimizer=0)
            gp.fit(param_array_postprocess, cAmat[i, :])
            gps.append(gp)

        svd_model[lambda_d] = {}
        svd_model[lambda_d]["n_coeff"] = n_coeff
        svd_model[lambda_d]["param_array"] = param_array
        svd_model[lambda_d]["cAmat"] = cAmat
        svd_model[lambda_d]["cAstd"] = cAstd
        svd_model[lambda_d]["VA"] = VA
        svd_model[lambda_d]["param_mins"] = param_mins
        svd_model[lambda_d]["param_maxs"] = param_maxs
        svd_model[lambda_d]["mins"] = mins
        svd_model[lambda_d]["maxs"] = maxs
        svd_model[lambda_d]["gps"] = gps
        svd_model[lambda_d]["tt"] = tt

    print("Finished calculating SVD model of lightcurve spectra...")

    return svd_model
Example #17
0
from scipy.interpolate.interpolate import interp2d

# File parameters
save = 0  #Switch to save the animation (1=save, 0=don't save)
data_file = '/Users/Sam/Documents/University/internship2/python_dist/cox_etal_2014.txt'  #Data file
out_file = 'mymovie.mp4'  #Filename for output movie (must contain desired file extension)
nframes = 200  #Number of frames in movie
fps = 20  #Frames per second
dpi = 300  #Dots per inch resolution

#Create function to interpolate data file to pass into flow()
v = np.genfromtxt(data_file, delimiter=',')
n_r, n_t = v.shape
t = np.linspace(0, n_t - 1, n_t)
r_data = np.linspace(0, 1, n_r)
f = interp2d(t, r_data, v)

d_max = np.max(np.round(np.max(v), decimals=1), np.round(np.min(v),
                                                         decimals=1))
d_min = -d_max
# Variables passed into flow() function [loop counter, any other variables user desires].
# Must contain the first variable (loop counter) as it is hard coded lower down. Any others are
# optional to the user however flow() must be editted to correctly unpack these variables.
variables = [0, f, n_t, nframes]

# Plot parameters
title = 'test'  #Title of plot
cmap = 'jet'  #Colourmap used in plot
resolution = 100  #Number of points in radius and theta.
levels = np.linspace(d_min, d_max, 60)  #Contour levels
c_ticks = np.linspace(levels[0], levels[-1], 5)  #Tick values for colourbar.
Example #18
0
    def interpolate(self,theta, phi):
        interp_f = interpolate.interp2d(self.phi, self.theta, self.z, kind='cubic')

        answer = interp_f([phi],[theta])
        return str(answer)
Example #19
0
lightcurvesDir = opts.lightcurvesDir
spectraDir = opts.spectraDir

errorbudget = opts.errorbudget
n_live_points = 1000
evidence_tolerance = 0.5

if opts.doModels:
    data_out = lightcurve_utils.loadModelsSpec(opts.outputDir, opts.name)
    keys = data_out.keys()
    if not opts.name in data_out:
        print "%s not in file..." % opts.name
        exit(0)

    data_out = data_out[opts.name]
    f = interp.interp2d(data_out["t"], data_out["lambda"], data_out["data"].T)
    xnew = opts.T0
    ynew = data_out["lambda"]
    znew = f(xnew, ynew)
    data_out = {}
    data_out["lambda"] = ynew
    data_out["data"] = np.squeeze(znew)

elif opts.doEvent:
    #events = opts.name.split(",")
    #data_out = {}
    #for event in events:
    #    filename = "%s/%s.dat"%(spectraDir,event)
    #    data_out_event = lightcurve_utils.loadEventSpec(filename)
    #    data_out[event] = data_out_event
Example #20
0
spec_all = {}
for model in models:
    spec_all[model] = {}
    for key, color in zip(keys, colors):
        spec_all[model][key] = np.empty((0, len(data_out[key]["lambda"])))

for model in models:
    for row in model_tables[model]:
        t, lambdas, spec = row["t"], row["lambda"], row["spec"]

        if np.sum(spec) == 0.0:
            #print "No luminosity..."
            continue

        for key, color in zip(keys, colors):
            f = interp.interp2d(t, lambdas, np.log10(spec), kind='cubic')
            flux1 = (10**(f(float(key), data_out[key]["lambda"]))).T
            flux1 = flux1[0]
            spec_all[model][key] = np.append(spec_all[model][key], [flux1],
                                             axis=0)

colors_names = cm.rainbow(np.linspace(0, 1, len(models)))
color2 = 'coral'
color1 = 'cornflowerblue'
colors_names = [color1, color2]

plotName = "%s/spec_panels.pdf" % (plotDir)
plotNamePNG = "%s/spec_panels.png" % (plotDir)
plt.figure(figsize=(20, 28))

cnt = 0
Example #21
0
    'ir': np.arange(np.shape(image['ir'])[1]),
    'red': np.arange(np.shape(image['red'])[1]),
    'blue': np.arange(np.shape(image['blue'])[1])
}

y = {
    'ir': np.arange(np.shape(image['ir'])[0]),
    'red': np.arange(np.shape(image['red'])[0]),
    'blue': np.arange(np.shape(image['blue'])[0])
}

# resample (higher density pixels)
newx, newy, xx, yy, zz, f = {}, {}, {}, {}, {}, {}

print('\n Resampling models to higher density ... `ir`, ', end='')
f['ir'] = interpolate.interp2d(x['ir'], y['ir'], image['ir'])
newx['ir'] = np.linspace(0, len(x['ir']) - 1, 1000)
newy['ir'] = np.linspace(0, len(y['ir']) - 1, 1000)
#xx['ir'], yy['ir'] = np.meshgrid(newx['ir'], newy['ir'])
zz['ir'] = f['ir'](newx['ir'], newy['ir'])

print('`red`, ', end='')
f['red'] = interpolate.interp2d(x['red'], y['red'], image['red'])
newx['red'] = np.linspace(0, len(x['red']) - 1, 1000)
newy['red'] = np.linspace(0, len(y['red']) - 1, 1000)
#xx['red'], yy['red'] = np.meshgrid(newx['red'], newy['red'])
zz['red'] = f['red'](newx['red'], newy['red'])

print('`blue`, ', end='')
f['blue'] = interpolate.interp2d(x['blue'], y['blue'], image['blue'])
newx['blue'] = np.linspace(0, len(x['blue']) - 1, 1000)
def getCslPsf(angleFromAxis,
              focusPosition,
              angleFromX,
              ccdCode,
              numSubPixels=None,
              numPixels=None,
              psfInputFile="/STER/platoman/data/psfs/csl/cslPsfsOrig.hdf5",
              outputFile=None,
              verbose=False):
    """
    Extracts the CSL PSF for the given field angle and focus position from the given HDF5 file and
    turns it into an HDF5 file that can be handles by PlatoSim.  This means that we have to (linearly)
    interpolate the original CSL PSF to a rectangular grid in detector sub-pixels and normalise the PSF.

    :param angleFromAxis: Requested angular distance from the optical axis, expressed in degrees.
                            Should be an integer or a string with values in [0:2:20].

    :param focusPosition: Requested focus position [µm].  This is the distance from L6S2, ranging
                          (unequidistantly between 2520 and 7480 micron).

    :param angleFromX: Requested position angle of the pixel in the FOV, expressed in degrees.

    :param numSubPixels: Number of sub-pixels per pixel in both directions for the PSF that will be fed
                         into PlatoSim (i.e. after interpolation and cropping).  Default: next power of
                         two w.r.t. the original sampling, with a minimum of 8 sub-pixels per pixel

    :param numPixels: Number of pixels in both directions for the (square) PSF that will be fed into
                      PlatoSim (i.e. after interpolation).  Default: Next power of two w.r.t.
                      the original span of the PSF, with a minimum of 8 pixels.

    :param psfInputFile: Name of the HDF5 with all defocused PSF from CSL, as simulated with Code V.

    :param outputFile: Name of the HDF5 file in which to store the output PSF (i.e. after interpolation
                       and cropping).

    :return result: PSF for the given field angle and focus position, interpolated to a rectangular grid
                    in detector sub-pixels and normalised.
    """

    angleFromAxis = get_nearest_value(np.float(angleFromAxis),
                                      np.arange(0, 20, 2))

    ccdAngle = np.rad2deg(simref.CCD[str(ccdCode)]
                          ["angle"])  # Orientation angle of the CCD [degrees]

    # Extract the requested PSF from the HDF5 file that contains all PSFs from CSL
    # This must be turned into a format that PlatoSim can handle:
    #   - the required PSF must be interpolated to a grid specified in detector sub-pixels
    #     (rather than an arbitrary equidistant spatial grid)
    #   - the interpolated PSF must be stored in a designated HDF5 file

    cslPsf, dz, pixelSizeRatio, numDatapoints = extractCslPsf(angleFromAxis,
                                                              focusPosition,
                                                              psfInputFile,
                                                              verbose=verbose)

    # Rotation over the position angle

    #cslPsfRot = rotate(cslPsf,  - ccdAngle
    # -ccdAngle is kept out
    # cos' ccdAngle should not be considered for anything else than the deviations
    # from a multiple of 90 degrees (misalignment at FPA levels) (+ global CAM rotational misalignment ?)
    cslPsfRot = rotate(cslPsf, (180 - angleFromX))  # - ccdAngle)
    numDatapoints = cslPsfRot.shape[0]

    # Number of sub-pixels per pixel in the PSF that will be handed to PlatoSim
    #    - either specified by the user or derived from the sampling of the CSL PSF
    #    - must be a power of two (use next power of two)
    #    - enforce at least 8 sub-pixels

    # sampling[mm/datapoint] = psf_span[mm] / numberOfPoints --> saved in the original hdf5 file
    # pixelSizeRatio = sampling [mm/datapoint] / pixelsize [mm/pixel] = [pixel/datapoint] --> derived in extractCslPsf

    # pixelSizeRatio = sampling / pixelSizeDetectors
    # sampling = span / nd
    # span = extension of the PSF as delivered by CSL [mm]
    # numSubPixels = 1/pixelSizeRatio = [nb of datapoints in original psf] * pixelSize [0.018 mm] / [original psf size in mm]
    #                                 = [nb of datapoints in original psf] / [nb of pixels spanned by the PSF]  = [nb of datapoints/ pixel]

    numSubPixels = (1.0 / pixelSizeRatio if
                    (numSubPixels is None) else numSubPixels)
    numSubPixels = max(next_power_of_2(numSubPixels), 8)

    # Spatial grid for the original CSL PSF, expressed in detector sub-pixels
    #   - range(0, numDatapoints): pixel coordinates in the CSL PSF
    #   - multiply with pixelSizeRatio: now expressed in detector pixels
    #   - multiply with numSubPixels: now expressed in detector sub-pixels
    # Most likely non-integer values -> we have to make sure we have values for the integer sub-pixel positions
    # (that is why we will perform a linear interpolation to an intermediate grid first)

    initialSpatialGrid = np.arange(
        0, numDatapoints
    ) * pixelSizeRatio * numSubPixels  # [detector sub-pixels] (non-integer)

    interpolatedGridSize = int(np.ceil(initialSpatialGrid[-1]))
    interpolatedSpatialGrid = np.arange(
        interpolatedGridSize)  # [detector sub-pixels] (integer)

    interpolator = interp2d(x=initialSpatialGrid,
                            y=initialSpatialGrid,
                            z=cslPsfRot,
                            kind="linear")  # Source spatial grid (non-integer)
    interpolatedPsf = interpolator(
        interpolatedSpatialGrid,
        interpolatedSpatialGrid)  # Target spatial grid (integer)

    # We only need to copy the part of the interpolated PSF that is non-zero

    nonzero = np.where(
        interpolatedPsf != 0)  # Non-zero region in the interpolated PSF
    xMin, xMax = np.min(nonzero[0]), np.max(
        nonzero[0]
    )  # Min & max x-coordinate for the non-zero region [detector sub-pixels]
    yMin, yMax = np.min(nonzero[1]), np.max(
        nonzero[1]
    )  # Min & max y-coordinate for the non-zero region [detector sub-pixels]
    realSpanSubPixelsX = xMax - xMin + 1  # Size of the non-zero region in the x-direction [detector sub-pixels]
    realSpanSubPixelsY = yMax - yMin + 1  # Size of the non-zero region in the y-direction [detector sub-pixels]
    realSpanPixelsX = int(
        np.ceil(realSpanSubPixelsX / numSubPixels)
    )  # Size of the non-zero region in the x-direction [detector pixels]
    realSpanPixelsY = int(
        np.ceil(realSpanSubPixelsY / numSubPixels)
    )  # Size of the non-zero region in the y-direction [detector pixels]

    # Number of pixels in the PSF that will be handed to PlatoSim
    #   - either specified by the user or derived from the real (i.e. non-zero) span of the interpolated PSF
    #   - must be a power of two (use next power of two)
    #   - enforce at least 8 pixels

    numPixels = (max(realSpanPixelsX, realSpanPixelsY) if (numPixels is None)
                 else max(numPixels, realSpanPixelsX, realSpanPixelsY))
    numPixels = max(next_power_of_2(numPixels), 8)

    # Size of the PSF that will be handed to PlatoSim [detector sub-pixels]

    size = numPixels * numSubPixels
    psf = np.zeros([size, size])  #,dtype=np.float32)

    # Make sure the centre is still the centre

    endCenter = size // 2
    initShiftX = endCenter - (realSpanSubPixelsX // 2)
    initShiftY = endCenter - (realSpanSubPixelsY // 2)

    psf[initShiftX:initShiftX + realSpanSubPixelsX, initShiftY:initShiftY +
        realSpanSubPixelsY] = interpolatedPsf[xMin:xMax + 1, yMin:yMax + 1]

    # Normalisation

    psf /= np.sum(psf)

    if outputFile is not None:

        outfile = h5py.File(outputFile, 'w')
        group = outfile.create_group("T6000/ar00000")
        dataset = group.create_dataset("az0", data=psf)
        #group.attrs["angleFromAxis"] = np.int(angleFromAxis)
        dataset.attrs.create(name="orientation", data=-ccdAngle)
        outfile.attrs.create(name='dz', data=dz)
        outfile.attrs.create(name='numSubPixels', data=numSubPixels)
        outfile.attrs.create(name='numPixels', data=numPixels)
        outfile.attrs.create(name='size', data=size)

        outfile.close()

    # Ratio of peak and total flux

    rebinnedShape = (numPixels, numPixels)
    shape = (rebinnedShape[0], psf.shape[0] // rebinnedShape[0],
             rebinnedShape[1], psf.shape[1] // rebinnedShape[1])
    rebinnedPsf = psf.reshape(shape).mean(-1).mean(1)
    rho = np.max(rebinnedPsf) / np.sum(rebinnedPsf)

    result = {}
    result["psf"] = psf
    result["angleFromAxis"] = int(angleFromAxis)
    result["dz"] = dz
    result["numSubPixels"] = numSubPixels
    result["numPixels"] = numPixels
    result["size"] = size
    result["rho"] = rho

    return result
f = open(pcklFile, 'r')
(data_out, data2, t_best2, lambdas_best2, spec_best2, t0_best2, zp_best2,
 n_params2, labels2, truths2) = pickle.load(f)
f.close()

plotDir3 = '../plots/gws/Ka2017_FixZPT0/u_g_r_i_z_y_J_H_K/0_14/ejecta/GW170817/1.00/'
pcklFile = os.path.join(plotDir3, "data.pkl")
f = open(pcklFile, 'r')
(data_out3, data3, tmag3, lbol3, mag3, t0_best3, zp_best3, n_params3, labels3,
 best3, truths3) = pickle.load(f)
f.close()

spec_best_dic1 = {}
for key in data_out:
    f = interp.interp2d(t_best1 + t0_best1,
                        lambdas_best1,
                        np.log10(spec_best1),
                        kind='cubic')
    flux1 = (10**(f(float(key), data_out[key]["lambda"]))).T
    zp_factor = 10**(zp_best1 / -2.5)
    flux1 = flux1 * zp_factor
    spec_best_dic1[key] = {}
    spec_best_dic1[key]["lambda"] = data_out[key]["lambda"]
    spec_best_dic1[key]["data"] = np.squeeze(flux1)

spec_best_dic2 = {}
for key in data_out:
    f = interp.interp2d(t_best2 + t0_best2,
                        lambdas_best2,
                        np.log10(spec_best2),
                        kind='cubic')
    flux1 = (10**(f(float(key), data_out[key]["lambda"]))).T
Example #24
0
    def make_diff_func(self) :
        ''' everytime parameters are changed, the diffusion 
            function must be REMADE or it's data adapted !! 
        '''
        if  (self.difftype == 'MC_2D4pint_ext1D' 
                    or self.difftype == 'MC_2D4pint_func'):
                from GridUtils import GridFunc2D
                self.diff11   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[2]),False)
                self.diff12   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[3]),False)
                self.diff21   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[4]),False)
                self.diff22   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[5]),False)
                self.diff11_u = None;self.diff12_u = None;self.diff21_u = None;self.diff22_u = None;
                self.diff11_v = None;self.diff12_v = None;self.diff21_v = None;self.diff22_v = None;
                #grideval means a ugrid and vgrid is given, the grid made by this is evaluated
                #this is used in the plotting function
                self.diff11_grideval   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[2]),True)
                self.diff12_grideval   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[3]),True)
                self.diff21_grideval   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[4]),True)
                self.diff22_grideval   = GridFunc2D([self.param[0],self.param[1]],N.asarray(self.param[5]),True)


        elif self.difftype == 'MC_bspline_ext1D' or \
                  self.difftype == 'MC_bspline1storder_ext1D' : 
                self.arrayaware = False           
                weight = ones(self.nrparam)
                # standard dev on diffusion = 0.2, inverse is 5
                weight[:] =5 * weight[:] 
                #create an interp object
                from scipy.interpolate.fitpack import bisplrep,bisplev
                #param[0] should be list of u values
                #param[1] should be list of v values
                #param[2] should be list of D(u,v) values
                #create the data of the bicubic bspline:
                # no smoother so s = 0
                if self.difftype == 'MC_bspline1storder_ext1D' :
                    self.splinedataD11 = bisplrep(self.param[0], self.param[1],
                        self.param[2], w= weight, s=None,
                        kx = 1, ky=1, full_output = 1, quiet = 0)
                else :
                    #nknots = 11
                    #tx = range(nknots)
                    #tx = [self.umin + x/(1.*(nknots-1)) * (self.umax-self.umin) for x in tx]
                    #ty = range(nknots)
                    #ty = [self.vmin + x/(1.*(nknots-1)) * (self.vmax-self.vmin) for x in ty]
                    #tx = tx[0]*3 + tx + tx[-1]*3
                    #tx[:1] = [tx[0]]*4 ; tx[-1:] = [tx[-1]]*4
                    #ty[:1] = [ty[0]]*4 ; ty[-1:] = [ty[-1]]*4
                    #print 'tx,ty',tx, ty
                    self.splinedataD11 = bisplrep(self.param[0], self.param[1],
                        self.param[2], w= weight, s=2100, 
                        kx = 3, ky=3, full_output = 1, quiet = 0)
                #print 'D11 output :', self.splinedataD11[1:]
                if self.splinedataD11[2] > 0 :
                    print 'ERROR in spline interpolation'
                    sys.exit()
                self.splinedataD11 = self.splinedataD11[0]
                self.diff11   = lambda u,v: bisplev([u], [v], self.splinedataD11, \
                            dx = 0, dy=0)
                self.diff11_u = lambda u,v: bisplev([u], [v] , self.splinedataD11,\
                            dx = 1, dy=0)
                self.diff11_v = lambda u,v: bisplev([u], [v] , self.splinedataD11,\
                            dx = 0, dy=1)
                self.diff11_grideval   = lambda u,v: bisplev(u, v, self.splinedataD11, \
                            dx = 0, dy=0)
                self.diff11_u_grideval = lambda u,v: bisplev(u, v , self.splinedataD11,\
                            dx = 1, dy=0)
                self.diff11_v_grideval = lambda u,v: bisplev(u, v , self.splinedataD11,\
                            dx = 0, dy=1)
                if self.difftype == 'MC_bspline1storder_ext1D' :
                    self.splinedataD12 = bisplrep(self.param[0], self.param[1],
                        self.param[3], w= weight, s=None,
                        kx = 1, ky=1, full_output = 1, quiet = 1)
                else :
                    self.splinedataD12 = bisplrep(self.param[0], self.param[1],
                        self.param[3], w= weight, s=None,
                        kx = 3, ky=3, full_output = 1, quiet = 0)
                #print 'D12 output :', self.splinedataD12[1:]
                if self.splinedataD12[2] > 0 :
                    print 'ERROR in spline interpolation'
                    sys.exit()
                self.splinedataD12 = self.splinedataD12[0]
                self.diff12   = lambda u,v: bisplev([u], [v], self.splinedataD12, \
                            dx = 0, dy=0)
                self.diff12_u = lambda u,v: bisplev([u], [v] , self.splinedataD12,\
                            dx = 1, dy=0)
                self.diff12_v = lambda u,v: bisplev([u], [v] , self.splinedataD12,\
                            dx = 0, dy=1)
                self.diff12_grideval   = lambda u,v: bisplev(u, v, self.splinedataD12, \
                            dx = 0, dy=0)
                self.diff12_u_grideval = lambda u,v: bisplev(u, v , self.splinedataD12,\
                            dx = 1, dy=0)
                self.diff12_v_grideval = lambda u,v: bisplev(u, v , self.splinedataD12,\
                            dx = 0, dy=1)
                if self.difftype == 'MC_bspline1storder_ext1D' :
                    self.splinedataD21 = bisplrep(self.param[0], self.param[1],
                        self.param[4], w= weight, s=None,
                        kx = 1, ky=1, full_output = 1, quiet = 1)
                else :
                    self.splinedataD21 = bisplrep(self.param[0], self.param[1],
                        self.param[4], w= weight, s=None,
                        kx = 3, ky=3, full_output = 1, quiet = 0)
                #print 'D21 output :', self.splinedataD21[1:]
                if self.splinedataD21[2] > 0 :
                    print 'ERROR in spline interpolation'
                    sys.exit()
                self.splinedataD21 = self.splinedataD21[0]
                self.diff21   = lambda u,v: bisplev([u], [v], self.splinedataD21, \
                            dx = 0, dy=0)
                self.diff21_u = lambda u,v: bisplev([u], [v] , self.splinedataD21,\
                            dx = 1, dy=0)
                self.diff21_v = lambda u,v: bisplev([u], [v] , self.splinedataD21,\
                            dx = 0, dy=1)
                self.diff21_grideval   = lambda u,v: bisplev(u, v, self.splinedataD21, \
                            dx = 0, dy=0)
                self.diff21_u_grideval = lambda u,v: bisplev(u, v , self.splinedataD21,\
                            dx = 1, dy=0)
                self.diff21_v_grideval = lambda u,v: bisplev(u, v , self.splinedataD21,\
                            dx = 0, dy=1)
                if self.difftype == 'MC_bspline1storder_ext1D' :
                    self.splinedataD22 = bisplrep(self.param[0], self.param[1],
                        self.param[5], w= weight, s=None,
                        kx = 1, ky=1, full_output = 1, quiet = 1)
                else :
                    self.splinedataD22 = bisplrep(self.param[0], self.param[1],
                        self.param[5], w= weight, s=55000.,
                        kx = 3, ky=3, full_output = 1, quiet = 0)
                #print 'D22 output :', self.splinedataD22[1:]
                if self.splinedataD22[2] > 0 :
                    print 'ERROR in spline interpolation'
                    sys.exit()
                self.splinedataD22 = self.splinedataD22[0]
                self.diff22   = lambda u,v: bisplev([u], [v], self.splinedataD22, \
                            dx = 0, dy=0)
                self.diff22_u = lambda u,v: bisplev([u], [v] , self.splinedataD22,\
                            dx = 1, dy=0)
                self.diff22_v = lambda u,v: bisplev([u], [v] , self.splinedataD22,\
                            dx = 0, dy=1)
                self.diff22_grideval   = lambda u,v: bisplev(u, v, self.splinedataD22, \
                            dx = 0, dy=0)
                self.diff22_u_grideval = lambda u,v: bisplev(u, v , self.splinedataD22,\
                            dx = 1, dy=0)
                self.diff22_v_grideval = lambda u,v: bisplev(u, v , self.splinedataD22,\
                            dx = 0, dy=1)
        elif self.difftype == 'MC_interp2d_ext1D':
                from scipy.interpolate.interpolate import interp2d
                self.diff11   = interp2d(self.param[0],self.param[1],self.param[2])
                self.diff12   = interp2d(self.param[0],self.param[1],self.param[3])
                self.diff21   = interp2d(self.param[0],self.param[1],self.param[4])
                self.diff22   = interp2d(self.param[0],self.param[1],self.param[5])
                print 'diff11 test ' , self.diff11(1.5)
                print ' *** DO NOT USE THIS INTERPOLATION, it performs badly ***'
                sys.exit()
        elif self.difftype == 'function' :
            #self.diffij is defined when reading the init file for scalars,
            pass
        else:
            print ('Unknown diffusion type given', self.difftype)
            sys.exit()
        
        #store the functions in a list:
        self.diff = [[self.diff11 , self.diff12],[self.diff21 , self.diff22]]
        self.diff_u = [[self.diff11_u , self.diff12_u],[self.diff21_u , self.diff22_u]]
        self.diff_v = [[self.diff11_v , self.diff12_v],[self.diff21_v , self.diff22_v]]
        #changed parameters are taken into account, so diffusion is now correct
        self.modified = False