def __init__(self, folder, filen_0, dim_1, dim_2, dim_3, label='_'):
        ''' Folder where to save the data files,
        filen_0 is the base name added to each file,
        dim_1,2,3 are the sweep objects used to grab data length and names,
        label is something that can be added to the filename
        '''
        gc.collect()
        self._folder = folder
        self.filen = []
        self.head = []
        self.filen.append(filen_0 + label + '_cI1I1' + '.mtx')
        self.filen.append(filen_0 + label + '_cQ1Q1' + '.mtx')
        self.filen.append(filen_0 + label + '_cI2I2' + '.mtx')
        self.filen.append(filen_0 + label + '_cQ2Q2' + '.mtx')
        self.filen.append(filen_0 + label + '_cI1Q1' + '.mtx')
        self.filen.append(filen_0 + label + '_cI2Q2' + '.mtx')
        self.filen.append(filen_0 + label + '_cI1I2' + '.mtx')
        self.filen.append(filen_0 + label + '_cQ1Q2' + '.mtx')
        self.filen.append(filen_0 + label + '_cI1Q2' + '.mtx')
        self.filen.append(filen_0 + label + '_cQ1I2' + '.mtx')
        self.filen.append(filen_0 + label + '_SqMag' + '.mtx')
        self.filen.append(filen_0 + label + '_SqPhs' + '.mtx')
        self.filen.append(filen_0 + label + '_SqMag2' + '.mtx')
        self.filen.append(filen_0 + label + '_cPow' + '.mtx')
        for i in range(10):
            self.head.append(make_header(dim_1, dim_2, dim_3, 'cCov'))

        self.head.append(make_header(dim_1, dim_2, dim_3, 'Sq-Mag'))
        self.head.append(make_header(dim_1, dim_2, dim_3, 'Sq-Phs'))
        self.head.append(make_header(dim_1, dim_2, dim_3, 'Sq-Mag2'))
        self.head.append(make_header(dim_1, dim_2, dim_3, 'cPower'))
        self.matrix3d = np.memmap(folder+'cov.dat', dtype=np.float32, mode='w+',
                                  shape=(14, dim_3.pt, dim_2.pt, dim_1.pt))
 def __init__(self, folder, filen_0, dim_1, dim_2, dim_3, label='_'):
     self._folder = folder
     self._filen_3 = filen_0 + label + '_mag' + '.mtx'
     self._filen_4 = filen_0 + label + '_phase' + '.mtx'
     self._head_3 = make_header(dim_1, dim_2, dim_3, 'S11 _mag')
     self._head_4 = make_header(dim_1, dim_2, dim_3, 'S11 _phase')
     self._matrix3d_3 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_4 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
 def __init__(self, folder, filen_0, dim_1, dim_2, dim_3):
     self._folder = folder
     self._filen_1 = filen_0 + '_real' + '.mtx'
     self._filen_2 = filen_0 + '_imag' + '.mtx'
     self._filen_3 = filen_0 + '_mag' + '.mtx'
     self._filen_4 = filen_0 + '_phase' + '.mtx'
     self._head_1 = make_header(dim_1, dim_2, dim_3, 'S11 _real')
     self._head_2 = make_header(dim_1, dim_2, dim_3, 'S11 _imag')
     self._head_3 = make_header(dim_1, dim_2, dim_3, 'S11 _mag')
     self._head_4 = make_header(dim_1, dim_2, dim_3, 'S11 _phase')
     self._matrix3d_1 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_2 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_3 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_4 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
 def prepare_data_save(self, folder, filen_0, dim1, dim_2, dim_3):
     if self.pt > 1:
         dim_1 = self
     else:
         dim_1 = dim1
     self._folder = folder
     self._filen_1 = filen_0 + '_r' + '.mtx'
     self._filen_2 = filen_0 + '_i' + '.mtx'
     self._filen_3 = filen_0 + '_m' + '.mtx'
     self._filen_4 = filen_0 + '_p' + '.mtx'
     self._head_1 = make_header(dim_1, dim_2, dim_3, 'S11 real')
     self._head_2 = make_header(dim_1, dim_2, dim_3, 'S11 imag')
     self._head_3 = make_header(dim_1, dim_2, dim_3, 'S11 mag')
     self._head_4 = make_header(dim_1, dim_2, dim_3, 'S11 phase')
     self._matrix3d_1 = np.memmap('VNAreal.mem', dtype=np.float32, mode='w+', shape=(dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_2 = np.memmap('VNAimag.mem', dtype=np.float32, mode='w+', shape=(dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_3 = np.memmap('VNAmag.mem', dtype=np.float32, mode='w+', shape=(dim_3.pt, dim_2.pt, dim_1.pt))
     self._matrix3d_4 = np.memmap('VNAphase.mem', dtype=np.float32, mode='w+', shape=(dim_3.pt, dim_2.pt, dim_1.pt))
 def __init__(self, folder, filen_0, dim_1, dim_2, dim_3,
              label='_', cname='Voltage x1k'):
     self._folder = folder
     self._filen_1 = filen_0 + label + '.mtx'
     self._head_1 = make_header(dim_1, dim_2, dim_3, cname)
     self._matrix3d_1 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
     self.UD = False
     if dim_1.UD is True:
         self._filen_2 = filen_0 + label + '_2' + '.mtx'
         self.UD = True
         self._matrix3d_2 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
            stop=sPar[4],
            pt=sPar[8],
            scale=1)
        for sPar in d.stepItems[n]]
'''
'''
d.n2[0].lin =  (d.n2[0].lin-xoff)/x1flux + 0.5
d.n2[0].start = d.n2[0].lin[0]
d.n2[0].stop =  d.n2[0].lin[-1]
d.n2[0].name = 'Flux/Flux0'
d.n2[1].lin =  (d.n2[1].lin-xoff)/x1flux + 0.5
d.n2[1].start = d.n2[1].lin[0]
d.n2[1].stop =  d.n2[1].lin[-1]
d.n2[1].name = 'Flux/Flux0'
'''
'''
M2 = np.zeros((MAT1.shape[0], d.n2[0].pt, d.n3.pt))
M4 = np.zeros((MAT1.shape[0], d.n2[1].pt, d.n3.pt))
M2 = MAT1[:, :d.n2[0].pt, :]
M3 = MAT1[:, d.n2[0].pt-1:, :]

header1 = make_header(d.n3, d.n2[0], d.n1, meas_data=('a.u.'))
savemtx('mtx_out//' + filein + '.mtx', M2, header=header1)
header2 = make_header(d.n3, d.n2[1], d.n1, meas_data=('a.u.'))
savemtx('mtx_out//' + filein + '2' + '.mtx', M3, header=header2)
'''

header1 = make_header(d.n1, d.n2, d.n3, meas_data=('Photon Flux'))
# header1 = make_header(d.n1, d.n2, d.n3, meas_data=('Pow [W]'))
savemtx('mtx_out//' + filein + '.mtx', MAT1, header=header1)
for i, vc.snr in enumerate(np.linspace(0, 8, num0)):
    vc.make_cvals()
    BLnM[i, :, :], BLnM2[i, :, :], MIneq[i, :, :], Mn1[i, :, :], Mn2[i, :, :] = NMatrix(vc, vc2)
vc.d1.name = 'Signal/Noise'
vc.d1.pt = num0
vc.d1.start = 0.0
vc.d1.stop = 8.0

# convert to effective Phi0
vc.d3.start = vc.d3.start*0.0113
vc.d3.stop = vc.d3.stop*0.0113

ext = 'med_'

savename3 = fname+ext+'n1.mtx'
headtxt3 = make_header(vc.d3, vc.d2, vc.d1, meas_data='n1')
savemtx(savename3, Mn1, headtxt3)

savename4 = fname+ext+'n2.mtx'
headtxt4 = make_header(vc.d3, vc.d2, vc.d1, meas_data='n2')
savemtx(savename4, Mn2, headtxt4)

savename5 = fname+ext+'IneQ.mtx'
headtxt5 = make_header(vc.d3, vc.d2, vc.d1, meas_data='Ineq')
savemtx(savename5, MIneq, headtxt5)

savename = fname+ext+'LogN.mtx'
headtxt = make_header(vc.d3, vc.d2, vc.d1, meas_data='Log-Negativity')
savemtx(savename, BLnM, headtxt)

savename2 = fname+ext+'ineqSq.mtx'
            stop=sPar[4],
            pt=sPar[8],
            scale=1)
        for sPar in d.stepItems[n]]
'''
'''
d.n2[0].lin =  (d.n2[0].lin-xoff)/x1flux + 0.5
d.n2[0].start = d.n2[0].lin[0]
d.n2[0].stop =  d.n2[0].lin[-1]
d.n2[0].name = 'Flux/Flux0'
d.n2[1].lin =  (d.n2[1].lin-xoff)/x1flux + 0.5
d.n2[1].start = d.n2[1].lin[0]
d.n2[1].stop =  d.n2[1].lin[-1]
d.n2[1].name = 'Flux/Flux0'
'''
'''
M2 = np.zeros((MAT1.shape[0], d.n2[0].pt, d.n3.pt))
M4 = np.zeros((MAT1.shape[0], d.n2[1].pt, d.n3.pt))
M2 = MAT1[:, :d.n2[0].pt, :]
M3 = MAT1[:, d.n2[0].pt-1:, :]

header1 = make_header(d.n3, d.n2[0], d.n1, meas_data=('a.u.'))
savemtx('mtx_out//' + filein + '.mtx', M2, header=header1)
header2 = make_header(d.n3, d.n2[1], d.n1, meas_data=('a.u.'))
savemtx('mtx_out//' + filein + '2' + '.mtx', M3, header=header2)
'''

# header1 = make_header(d.n1, d.n2, d.n3, meas_data=('Photon Flux'))
header1 = make_header(d.n1, d.n2, d.n3, meas_data=('Pow [W]'))
savemtx('mtx_out//' + filein + '.mtx', MAT1, header=header1)
'''
#Other Equipment
execfile('keithley2000.py')
vm = instrument3('GPIB0::29::INSTR')
vm.optimise()
sleep(0.1)
vm.testspeed()
'''

#for the VNA I want 4 data files for Real, Imag, MAG, Phase
filen_1 = filen_0 + '_real'  + '.mtx'
filen_2 = filen_0 + '_imag'  + '.mtx'
filen_3 = filen_0 + '_mag'   + '.mtx'
filen_4 = filen_0 + '_phase' + '.mtx'
head_1 = make_header(dim_1, dim_2, dim_3, 'S21 _real')
head_2 = make_header(dim_1, dim_2, dim_3, 'S21 _imag')
head_3 = make_header(dim_1, dim_2, dim_3, 'S21 _mag')
head_4 = make_header(dim_1, dim_2, dim_3, 'S21 _phase')
matrix3d_1 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
matrix3d_2 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
matrix3d_3 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
matrix3d_4 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))

ask_overwrite(folder+filen_1)
copy_file(thisfile, filen_0, folder) #backup this script

dim_1.lin = np.linspace(dim_1.start,dim_1.stop,dim_1.pt)
dim_2.lin = np.linspace(dim_2.start,dim_2.stop,dim_2.pt)
dim_3.lin = np.linspace(dim_3.start,dim_3.stop,dim_3.pt)
MAT1[1] = d.data[:, 5, :]
MAT1[2], MAT1[3] = get_MP(d, 6)
MAT1[4], MAT1[5] = get_MP(d, 8)
MAT1[6], MAT1[7] = get_MP(d, 10)
MAT1[8], MAT1[9] = get_MP(d, 12)

# MAT2[-1] = d.data[:, -1, :]

M2 = np.zeros((MAT1.shape[0], d.n2[0].pt, d.n3.pt))
M3 = np.zeros((MAT1.shape[0], d.n2[1].pt, d.n3.pt))
M3 = MAT1[:, :d.n2[0].pt, :]
M2 = MAT1[:, d.n2[0].pt-1:, :]

# meas specific to change mag field to flux
# simply comment this paragraph out
xoff = 140.5e-3  # 139.3e-3
x1flux = 479.6e-3
d.n2[0].lin =  (d.n2[0].lin-xoff)/x1flux + 0.5
d.n2[0].start = d.n2[0].lin[0]
d.n2[0].stop =  d.n2[0].lin[-1]
d.n2[0].name = 'Flux/Flux0'
d.n2[1].lin =  (d.n2[1].lin-xoff)/x1flux + 0.5
d.n2[1].start = d.n2[1].lin[0]
d.n2[1].stop =  d.n2[1].lin[-1]
d.n2[1].name = 'Flux/Flux0'

header2 = make_header(d.n3, d.n2[0], d.n1, meas_data=('a.u.'))
header1 = make_header(d.n3, d.n2[1], d.n1, meas_data=('a.u.'))
savemtx('mtx_out//' + filein + '.mtx', M3, header=header1)
savemtx('mtx_out//' + filein + '2' + '.mtx', M2, header=header2)
    MAT1[n*4+3] = np.angle(compx)

# store Power channels
MAT1[(numOfDigi)*4] = d.data[:, 5, :]
MAT1[(numOfDigi)*4+1] = d.data[:, 8, :]
MAT1[(numOfDigi)*4+2] = d.data[:, -1, :]  # Store measured Voltage

M2 = np.zeros((11, 1501, 70))
M3 = np.zeros((11, 1501, 70))

M3 = MAT1[:, :1501, :]
M2 = MAT1[:, 1500:, :]
d.n2.pt = (d.n2.pt-1)/2 +1

d.n2.stop = d.n2.stop*-1
d.n2.update_lin()
header1 = make_header(d.n1, d.n2, d.n2, meas_data=('a.u.'))
savemtx('mtx_out//' + filein + '.mtx', M3, header=header1)

#flip endpoints for the backsweep
d.n2.stop = d.n2.stop*-1
d.n2.start = d.n2.start*-1
d.n2.update_lin()
header1 = make_header(d.n1, d.n2, d.n2, meas_data=('a.u.'))
savemtx('mtx_out//' + filein +'2'+ '.mtx', M2, header=header1)

# for jj, filext in enumerate(d.channel[5:]):
#     MAT2[jj+2] = d.data[:, jj+5, :]
#     header1 = make_header(d.n1, d.n2, d.n2, meas_data=filext[1])
#     # savemtx(folder + filein + filext[0] + '.mtx', MAT1[jj+2], header=header1)
d.n1.start = d.n1.lin[0]
d.n1.stop = d.n1.lin[-1]
d.n1.name = 'Flux/Flux0'

MAT1 = np.zeros([8, d.shape[0], d.shape[1]])
MAT1[0] = d.D1mag
MAT1[1] = d.D2mag
MAT1[2] = d.D1phase
MAT1[3] = d.D2phase
MAT1[4] = d.D1Pow
MAT1[5] = d.D2Pow
MAT1[6] = d.D1Pow2
MAT1[7] = d.D2Pow2


header1 = make_header(d.n1, d.n2, d.n2, meas_data='est. Photon #')
savemtx('output//S1_480_V.mtx', MAT1, header=header1)

y = (d.n2.lin*d.n2.lin/50.0)  # position of the data

MAT2 = np.zeros([2, d.shape[0]*10, d.shape[1]])
y2, MAT2[0] = interp_y(y, d.D1Pow2, factor=10)
y2, MAT2[1] = interp_y(y, d.D2Pow2, factor=10)

d.dim_y2 = d.n2
d.dim_y2.start = y2[0]*1e3
d.dim_y2.stop = y2[-1]*1e3
d.dim_y2.pt = len(y2)
d.dim_y2.lin = y2*1e3
d.dim_y2.name = 'Pump power (mW)'
           start = -1,
           stop = 1,
           pt = 1001,
           scale = flux0)
freq = dim(name = 'Frequency (GHz)',
           start = 4,
           stop = 8,
           pt = 101,
           scale = 1e9)
dim_3 = handler(name = 'mag/phase',
           start = 0,
           stop = 10,
           pt = 11) #8 pts for S 4x2 values
dim_3._Z0 = 50

head1 = make_header(magnet, freq, dim_3, 'S11 S12 S21 S22 Z L')
dim_3.prepare_data_save(magnet, freq, dim_3)


for jj, f0 in enumerate(freq.lin):
    for ii, flux in enumerate(magnet.lin):
        # b = k = 2pi/wavelength; wavelength = velocity / frequency
        b = 2.0*pi*f0/v
        L = flux0 / (Ic*2.0*pi* abs(cos(pi*flux/flux0)))
        Ysq = (1.0/R + 1.0/(i*2.0*pi*f0*L +i*1e-90) + i*2.0*pi*f0*Cap)
        Zsq = 1.0/Ysq

        ABCD_Matrix = tline(70,b,0.01)*tline(50,b,0.3)*tline(10,b,900e-6)*sres(Zsq)*shunt(0.1)

        #record stuff into dim_3._SMat
        dim_3.record_SM(ABCD_Matrix,jj,ii)
 def prepare_data_save(self, folder, filen_0, dim_1, dim_2, dim_3, colour_name):
     '''Prepare an empty matrix which will be filled with measurement data'''
     self._folder = folder
     self._filen_1 = filen_0 + '.mtx'
     self._head_1 = make_header(dim_1, dim_2, dim_3, colour_name)
     self.matrix3d_1 = np.zeros((dim_3.pt, dim_2.pt, dim_1.pt))
d.shape = d.data.shape

xoff = 139.3e-3
x1flux = 479.6e-3
d.dim_1.lin = (d.dim_1.lin-xoff)/x1flux + 0.5
d.dim_1.start = d.dim_1.lin[0]
d.dim_1.stop = d.dim_1.lin[-1]

MAT1 = np.zeros([6, d.shape[0], d.shape[2]])
MAT1[0] = d.data[:, 3, :]
MAT1[1] = d.data[:, 4, :]
MAT1[2] = d.D1mag
MAT1[3] = d.D1ang
MAT1[4] = d.D1pow
MAT1[5] = d.D1lev

y = (d.dim_2.lin*d.dim_2.lin/50.0)  # position of the data
y2, MAT2 = interp_y(y, d.D1pow)
d.dim_y2 = d.dim_2
d.dim_y2.start = y2[0]
d.dim_y2.stop = y2[-1]
d.dim_y2.pt = len(y2)
d.dim_y2.lin = y2


header1 = make_header(d.dim_1, d.dim_2, d.dim_3, meas_data='(a.u)')
savemtx('S1_471_DCE_MAP2_volt.mtx', MAT1, header=header1)

header2 = make_header(d.dim_1, d.dim_y2, d.dim_3, meas_data='(a.u)')
savemtx('S1_471_DCE_MAP2_wats.mtx', MAT2, header=header2)