def plot_spiral_quantity(r, phase, quantity, desiredcells=200, rin=udvh.r1,
                         levels=0, max_diff_mean=10000, background_subtract=0,
                         titlestring='', showcolorbar=1, noaxisequalize=0,
                         renormalize=1, labelstring=""):

    #Need to do this resampling
    xi = linspace(-udvh.r2, udvh.r2, desiredcells)
    yi = linspace(-udvh.r2, udvh.r2, desiredcells)
    zi = zeros((desiredcells, desiredcells))

    i = 0
    for x in xi:
        k = 0
        for y in yi:
            r_temp = math.sqrt(x**2 + y**2)
            if ((r_temp > rin) and (r_temp < udvh.r2)):
                element_r = udvh.find_element_r(r, r_temp)

                angle = math.atan2(y,x)
                #if (derotate !=0):
                    ###First put the measured angle on a 0 to 2pi scale
                    #angle = angle + pi
                    ###Now add the derotation angle
                    #angle = angle + derotate_angle
                    #angle = math.fmod(angle, 2.0*pi)
                    ###And now back to the -pi to pi scale
                    #angle = angle - pi

                
                element_phase = 0
                for n in range(0, phase.size):
                    if (phase[n] > angle) & (phase[n-1] < angle):
                        element_phase = n
                        
                zi[k,i] = quantity[element_phase, element_r]
                if ((background_subtract == 1) & (zi[k,i] > max_diff_mean)):
                    zi[k,i] = max_diff_mean
                if ((background_subtract == 1) & (zi[k,i] < -max_diff_mean)):
                    zi[k,i] = -max_diff_mean
            else:
                zi[k,i] = -1e12

            k = k + 1
        i = i + 1
        print i

    #Use numpy's masked values to mask the nans
    zi_masked = ma.masked_values(zi, -1e12)

    zi_masked = zi_masked/renormalize

    if(isscalar(levels) == 1):
        C = contourf(xi, yi, zi_masked, 50)
    else:
        C = contourf(xi, yi, zi_masked, levels)

    xlim(-udvh.r2, udvh.r2)
    ylim(-udvh.r2, udvh.r2)
    if(noaxisequalize==0):
        axes().set_aspect('equal')
    
    xlabel("x [cm]")
    ylabel("y [cm]")
    title(titlestring)
    if(showcolorbar):
        bar_instance = colorbar(format='%.2f', pad=0.01, fraction=0.09)
        #if(background_subtract == 0):
        #    matplotlib.colorbar.ColorbarBase.set_label(bar_instance, r"$v_{\theta}$ [cm/sec]")
        #if(background_subtract == 1):
        #    matplotlib.colorbar.ColorbarBase.set_label(bar_instance, r"$v_{\theta} - \bar{v_{\theta}}$ [cm/sec]")

    if(showcolorbar):
        matplotlib.colorbar.ColorbarBase.set_label(bar_instance, labelstring)
        return C.levels
    else:
        return 0
def extract_spiral_modes_good(filename, channel, omega2, start_time, end_time, desiredcells=200, rin=udvh.r1, filter_threshold=1000, background_subtract=0, derotate=0, maxv=10000, minv=-10000, max_diff_mean=10000, levels=0, smoothradial=0, smoothingfactor=100):

    oscillation_time = end_time-start_time
    #I use the novr option to generate v_t, so that it is less susceptible
    #to noise on the radial transducer, and then run again to get the v_r
    #data separately.
    r_data = rudv.read_ultrasound(filename, 1)
    t_data = rudv.read_ultrasound(filename, channel)
    
    r, v_r, v_t = udvh.reconstruct_avg_velocities_nonaxisymmetric(r_data, t_data, 
                                                                  100, omega2,
                                                                  oscillation_time)
    vt = zeros([r_data['time'].size, r.size])
    vr = zeros([r_data['time'].size, r.size])

    #Because of the resampling interval that I do in 
    #reconstruct_avg_velocities_nonaxisymmetric, I can't go all the way to the
    #ends of the time interval.
    boundary = int(oscillation_time/(t_data['time'][5]-t_data['time'][4]))/2 + 2
    for i in range(boundary, r_data['time'].size-boundary):
        r, v_r, v_t = udvh.reconstruct_avg_velocities_nonaxisymmetric(r_data, t_data, 
                                                                      i, omega2,
                                                                      oscillation_time)
        vt[i,:] = v_t
        vr[i,:] = v_r


    phase = linspace(-pi, pi, 100)
    time = r_data['time']

    if (derotate != 0):
        derotate_angle = start_time*2.0*pi/(end_time-start_time)
        derotate_angle = fmod(derotate_angle, 2.0*pi)

    for i in range(0, time.size):
        if (time[i] > start_time) & (time[i-1] <= start_time):
            start_pos = i
        elif (time[i] > end_time) & (time[i-1] <= end_time):
            end_pos = i-1
            
    #Trim the arrays
    time = time[start_pos-10:end_pos+10]
    vt = vt[start_pos-10:end_pos+10,:]
    vr = vr[start_pos-10:end_pos+10,:]

    #Now convert time to phase
    time = time - start_time
    measured_phase = time*2.0*pi/(end_time-start_time) - pi

    resampled_vt = zeros([phase.size, r.size])
    resampled_vr = zeros([phase.size, r.size])
    
    #Now resample for each element of r
    for i in range(0, r.size):
        #Filter the data to remove spurious measurements
        for k in range (0, 5):
            if (vt[k,i] > maxv):
                vt[k,i] = maxv
            if (vt[k,i] < minv):
                vt[k,i] = minv
        for k in range (5, vt[:,i].size):
            if abs(vt[k,i] - vt[k-5:k-1,i].mean()) > filter_threshold:
                vt[k,i] = vt[k-5:k-1,i].mean()
            if (vt[k,i] > maxv):
                vt[k,i] = maxv
            if (vt[k,i] < minv):
                vt[k,i] = minv
 
        #Now resample.  Array is reversed, since measurements in experiment
        #are made from larger phase down to smaller phase
        #(features flowing past transducer)
        
        tck_vt = scipy.interpolate.splrep(measured_phase, vt[:,i], s=0)
        resampled_vt[:,i] = (scipy.interpolate.splev(phase, tck_vt, der=0))[::-1]

        tck_vr = scipy.interpolate.splrep(measured_phase, vr[:,i], s=0)
        resampled_vr[:,i] = (scipy.interpolate.splev(phase, tck_vr, der=0))[::-1]
        
        if(background_subtract == 1):
            resampled_vt[:,i] = resampled_vt[:,i]-resampled_vt[:,i].mean()

    if(smoothradial == 1):
        for i in range(0, phase.size):
            tck_sr = scipy.interpolate.splrep(r, resampled_vr[i,:],
                                              s=smoothingfactor)
            resampled_vr[i,:] = (scipy.interpolate.splev(r,
                                                         tck_sr, der=0))
        
    #r and phase contain the and theta coordinates
    #vr and vt are arrays, with the first index the phase,
    #and the second index r
    
    data = {'r': r, 'phase': phase, 'vr': resampled_vr, 'vt': resampled_vt}
    return data
def extract_spiral_modes(filename, channel, omega2, start_time, end_time, desiredcells=200, rin=udvh.r1, filter_threshold=1000, background_subtract=0, derotate=0, maxv=10000, minv=-10000, max_diff_mean=10000, levels=0, smoothradial=0):

    if(rudv.read_ultrasound("960.BDD", 1)):
        has_radial=1
    else:
        has_radial=0
    

    #I use the novr option to generate v_t, so that it is less susceptible
    #to noise on the radial transducer, and then run again to get the v_r
    #data separately.
    #If we don't have radial data, just use data_vt for the radial dataset,
    #so v_r will just be 0.
    data_vt = udvh.generate_profiles_alltime_novr(filename, channel, omega2)
    if(has_radial):
        data_vr = udvh.generate_profiles_alltime(filename, channel, omega2)
    else:
        data_vr = data_vt

    phase = linspace(-pi, pi, 100)
    time = data_vt['time']
    vt = data_vt['vt']
    vr = data_vr['vr']
    r = data_vt['r']

    if (derotate != 0):
        derotate_angle = start_time*2.0*pi/(end_time-start_time)
        derotate_angle = fmod(derotate_angle, 2.0*pi)

    for i in range(0, time.size):
        if (time[i] > start_time) & (time[i-1] <= start_time):
            start_pos = i
        elif (time[i] > end_time) & (time[i-1] <= end_time):
            end_pos = i-1

    #Trim the arrays
    time = time[start_pos-10:end_pos+10]
    vt = vt[start_pos-10:end_pos+10,:]
    vr = vr[start_pos-10:end_pos+10,:]

    #Now convert time to phase
    time = time - start_time
    measured_phase = time*2.0*pi/(end_time-start_time) - pi

    resampled_vt = zeros([phase.size, r.size])
    resampled_vr = zeros([phase.size, r.size])

    #Now resample for each element of r
    for i in range(0, r.size):
        #Filter the data to remove spurious measurements
        for k in range (0, 5):
            if (vt[k,i] > maxv):
                vt[k,i] = maxv
            if (vt[k,i] < minv):
                vt[k,i] = minv
        for k in range (5, vt[:,i].size):
            if abs(vt[k,i] - vt[k-5:k-1,i].mean()) > filter_threshold:
                vt[k,i] = vt[k-5:k-1,i].mean()
            if (vt[k,i] > maxv):
                vt[k,i] = maxv
            if (vt[k,i] < minv):
                vt[k,i] = minv

        #Need to offset theta to account for the angular extent of the
        #the measurement chord.
        #Note that there is some weirdness because of the branchcuts of asin.
        vt_theta_offset = pi - udvh.tan_probe_angle*pi/180 - (pi - math.asin(udvh.r2*math.sin(udvh.tan_probe_angle*pi/180)/r[i]))
        vt_temp_measured_phase = measured_phase - vt_theta_offset

        #This is just set by the port plugs where the transducers are
        #installed.  The radial transducer leads the azimuthal transducer
        #by 90 degrees
        vr_theta_offset = -pi/2
        vr_temp_measured_phase = measured_phase - vr_theta_offset

        #Now resample.  Array is reversed, since measurements in experiment
        #are made from larger phase down to smaller phase
        #(features flowing past transducer)
        
        tck_vt = scipy.interpolate.splrep(vt_temp_measured_phase, vt[:,i], s=0)
        resampled_vt[:,i] = (scipy.interpolate.splev(phase, tck_vt, der=0))[::-1]

        tck_vr = scipy.interpolate.splrep(vr_temp_measured_phase, vr[:,i], s=0)
        resampled_vr[:,i] = (scipy.interpolate.splev(phase, tck_vr, der=0))[::-1]
        
        if(background_subtract == 1):
            resampled_vt[:,i] = resampled_vt[:,i]-resampled_vt[:,i].mean()

    if(smoothradial == 1):
        for i in range(0, phase.size):
            tck_sr = scipy.interpolate.splrep(r, resampled_vr[i,:], s=100)
            resampled_vr[i,:] = (scipy.interpolate.splev(r,
                                                         tck_sr, der=0))
        
    #r and phase contain the and theta coordinates
    #vr and vt are arrays, with the first index the phase,
    #and the second index r
    
    data = {'r': r, 'phase': phase, 'vr': resampled_vr, 'vt': resampled_vt}
    return data
def global_optimize_transducer_angle(shot1, shot2, channelnum1, channelnum2,
                                     start_time, end_time):
    #Get copies of the real channels
    sh1ch1 = deepcopy(shot1.get_channel(channelnum1))
    sh1ch2 = deepcopy(shot1.get_channel(channelnum2))
    sh2ch1 = deepcopy(shot2.get_channel(channelnum1))
    sh2ch2 = deepcopy(shot2.get_channel(channelnum2))

    #Now get some dummy combined velocity things.
    vel1 = deepcopy(shot1.get_velocity(channelnum1, channelnum2))
    vel2 = deepcopy(shot2.get_velocity(channelnum1, channelnum2))
    
    start_idx = vel1.get_index_near_time(start_time)
    end_idx = vel1.get_index_near_time(end_time)

    figure()
    udvh.plot_two_component_avg_velocities(vel1, start_idx, end_idx)
    udvh.plot_two_component_avg_velocities(vel2, start_idx, end_idx)

    #Find how far to go from the outside to get to the inner and outer
    #points of interest
    indx_in = vel1.r.size - vel1.get_index_near_radius(11.0)
    indx_out = vel1.r.size - vel2.get_index_near_radius(19.0)
    nindx = indx_in-indx_out

    def err_func(As):
        print "Running with A1 = %0.8g, A2 = %0.8g" % (As[0], As[1])
        #First modify the As in the relevant dummy channels.
        sh1ch1.A = As[0]
        sh2ch1.A = As[0]
        sh1ch2.A = As[1]
        sh2ch2.A = As[1]

        #Need to update the radius vectors, too
        sh1ch1.calculate_radius()
        sh1ch2.calculate_radius()
        sh2ch1.calculate_radius()
        sh2ch2.calculate_radius()

        #Now reprocess the dummy velocities.
        vel1.gen_velocity_two_transducers(sh1ch1, sh1ch2)
        vel2.gen_velocity_two_transducers(sh2ch1, sh2ch2)
        print "In index: %d, Out index %d, Velocity size: %d" % (indx_in,
                                                                 indx_out,
                                                                 vel1.r.size)

        #And calculate the error vector. We multiply the vr difference by
        #10 assuming the vr ~ 0.10*vtheta
        error = zeros(2*nindx)
        for i in range(0, nindx):
            error[i] = (vel1.vtheta[start_idx:end_idx, -(i+indx_out)].mean() +
                        vel2.vtheta[start_idx:end_idx, -(i+indx_out)].mean())
        for i in range(nindx, 2*nindx):
            n = i-nindx
            error[i] = 10*(vel1.vr[start_idx:end_idx, -(n+indx_out)].mean() -
                            vel2.vr[start_idx:end_idx, -(n+indx_out)].mean())
        return error

    A0 = [sh1ch1.A, sh1ch2.A]

    Alsq = scipy.optimize.leastsq(err_func, A0, full_output=1)
    
    #Make sure we have the updated As
    A1 = Alsq[0][0]
    A2 = Alsq[0][1]
    sh1ch1.A = A1
    sh2ch1.A = A1
    sh1ch2.A = A2
    sh2ch2.A = A2
    #Now reprocess the dummy velocities.
    vel1.gen_velocity_two_transducers(sh1ch1, sh1ch2)
    vel2.gen_velocity_two_transducers(sh2ch1, sh2ch2)


    #And let's take a look at what we generated.
    print "Optimized parameters: A1 = %0.3g, A2 = %0.3g" % (A1, A2)


    figure()
    udvh.plot_two_component_avg_velocities(vel1, start_idx, end_idx)
    udvh.plot_two_component_avg_velocities(vel2, start_idx, end_idx)