#!/usr/bin/env python3
'''
.. module:: SQUIDutils
:synopsis: Methods and utility functions for the HDF_SQUID scripts
.. moduleauthor:: Cor de Vries <c.p.de.vries@sron.nl>
'''
import numpy
import h5py
import sys,os
[docs]def getspectra(filename,ext='squidnoise'):
'''
Get the spectra and axes from the 'squidnoise' extension in the given HDF file
Args:
* `filename` = file name of the HDF file
Kwargs:
* `ext` = extension of the SQUID data in the file
Returns:
* `hdf` = open hdf file object
* `data` = dictionary containing all the squidnoise data and axes information
'''
hdf=h5py.File(filename,'r') # open HDf file
ee=hdf[ext] # retrieve squidnoise extension
data={}
for key in ee.keys(): # read all extension tables
data[key]=numpy.array(ee[key]) # put in dictionary with table name as dictionary key
return (hdf,data)
[docs]def logindx(linsize,compress=5000):
'''
Compute logarithmically spaced indices, pointing to a given linear size.
Args:
* `linsize` = linear size of index table
Kwargs:
* `compress` = number of logarithmically spaced indices to generate
Returns:
* `index` = array of logarithmically spaced index pointers (floats)
'''
mm=numpy.log10(linsize)
lindx=numpy.array((10.0**(numpy.arange(compress,dtype=float)/float(compress)*mm)),dtype=float)
return lindx
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
[docs]def fillplot(plot,image,xax=None,yax=None,xtitle='',ytitle='',ptitle='',\
vrange=[1e-3,2.0],compress=5000,xlim=[500.0,1e4]):
'''
Plot image of 2-dim set of stacked spectra into given plot instance.
It will use of logarithmic scale for the X-axis
Args:
* `plot` = plot instance for plotting
* `image` = 2-dim set of stacked spectra
Kwargs:
* `xax` = X axis values for image (frequency axis)
* `yax` = Y axis values for image
* `xtitle` = title for X axis
* `ytitle` = title for Y axis
* `ptitle` = plot title
* `vrange` = range for colorscale
* `compress` = number of bins for logarithmically sampled X-axis
* `xlim` = range for X-axis
Returns:
* `im` = plotted object
'''
plot.set_xlabel(xtitle)
plot.set_ylabel(ytitle)
plot.set_title(ptitle)
if xax is None:
xax=numpy.arange(xax[:,0].size)
if yax is None:
yax=numpy.arange(yax[0.:].size)
plot.set_xscale('log')
if int(matplotlib.__version__.split('.')[0]) >= 2: # matplotlib versions >=2 are not compatible with vs. 1 !!
im=plot.imshow(image,norm=LogNorm(vmin=vrange[0], vmax=vrange[1]),\
extent=(xax[0],xax[-1],yax[0],yax[-1]),interpolation='bicubic',\
aspect=None,cmap=plt.get_cmap('jet'))
else:
lxax=logindx(xax.size,compress=compress)
limage=numpy.empty((yax.size,lxax.size),dtype=float)
xxx=numpy.arange(xax.size)
for iy in numpy.arange(yax.size):
limage[iy,:]=numpy.interp(lxax,xxx,image[iy,:])
im=plot.imshow(limage,norm=LogNorm(vmin=vrange[0], vmax=vrange[1]),\
extent=(xax[lxax[0]],xax[lxax[-1]],yax[0],yax[-1]),interpolation='bicubic',\
aspect=None,cmap=plt.get_cmap('jet'))
plot.set_xlim(xlim)
plot.set_ylim([yax.min(),yax.max()])
return im
[docs]def fillgraph(plot,norm_cal,xax=None,ytitle='',xtitle='',ptitle=''):
'''
Make plot on page
Args:
* `plot` = plot instance for plotting
* `norm_cal` = array with values to be plotted
Kwargs:
* `xax` = X-axis values
* `ytitle` = Title for Y-axis
* `xtitle` = Title for X-axis
* `ptitle` = Title for plot
Returns:
* `pl` = plotted object
'''
if xax is None:
xax=numpy.arange(norm_cal.size)
pl=plot.plot(xax,norm_cal,'b-')
plot.set_xlabel(xtitle)
plot.set_ylabel(ytitle)
plot.set_title(ptitle)
plot.ticklabel_format(axis='y',style='sci',scilimits=(0,0))
return pl