#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
File name: Bode_plot_setup.py
Author: Neil E Cotter
Date Created: Sun 05/15/2022 10:45 a.m. from snn_respsurfplot.py
Python Version: 3.8.8

"""


#import sys

import math

import matplotlib.pyplot as plt
import numpy as np

#import View_3dim_in_2dim.two_dim_view as view2D

#from snn_respsurf_include import *
#import snn_respsurf_include as snni

#from syn_waveforms import syn_wave_neg_edge
#from syn_waveforms import syn_wave_shape
#import syn_waveforms as syn

#import snn_neuron

#from snn_math import dec2tri
#import respsurf_X_volumes as Xc
#from respsurf_Xregion_edge_intersect_pts import *
#import snn_respsurf_X_region as Xi
#from respsurf_Xregion_plot import *


######################################################################
def Bode_xlabels(min_w, max_w):
    """
    Create list of labels for freq axis of Bode plot.
    Inputs:
        fig = (ptr to figure)
        ax = (ptr to axes of figure)
        min_w = (opt) minimum freq omega value (rounded to pwr of 10)
        max w = (lpt) maximum freq omega value (rounded to pwr of 10)
    Outputs:
        no values, labels horizontal and vertical axes on Bode plot
    """

    # Start/end at power of 10 in frequency.
    log10_start_w = int(np.floor(np.log10(min_w)))
    print('log10_start_w = ', log10_start_w)
    log10_end_w = int(np.ceil(np.log10(max_w)))
    print('log10_end_w = ', log10_end_w)

    # Find tick locs.
    num_freqs = (log10_end_w - log10_start_w) + 1
    print('num_freqs = ', num_freqs)
    xtick_locs = list(range(0,num_freqs))
    print('xtick_locs = ', xtick_locs)

    # Tick locations
    # List of labels to use.  Index is log10_start_w + 3.
    w_label_list = ['0.001','0.01','0.1', \
                    '1','10','100', \
                    '1k','10k','100k', \
                    '1M','10M','100M', \
                    '1G','10G','100G']

    w_labels = w_label_list[log10_start_w+3: log10_end_w+4]
    print(w_labels)

    return xtick_locs, w_labels


######################################################################
def Bode_ylabels(min_HdB=-60.0, max_HdB =60.0):
    """
    Create list of labels for freq axis of Bode plot.
    Inputs:
        fig = (ptr to figure)
        ax = (ptr to axes of figure)
        min_HdB = (opt) minimum |H| value in dB; default -60.0 dB
        max_HdB = (lpt) maximum |H| value in DB; default  60.0 dB
    Outputs:
        no values, labels horizontal and vertical axes on Bode plot
    """

    # Start/end at multiple of 20 in dB magnitude.
    mag20_start = int(20.0*np.floor(min_HdB/20.0))
    print('mag20_start = ', mag20_start)
    mag20_end = int(20.0*np.ceil(max_HdB/20.0))
    print('mag20_end = ', mag20_end)

    # Find tic mark locations.
    num_mags = int((mag20_end - mag20_start)/20 + 1)
    print('num_mags =', num_mags)
    ytick_locs = list(range(0,num_mags))
    print('ytick_locs = ', ytick_locs)

    # Calculate strings for label at every 20 dB.
    label_dBs = list(range(mag20_start, mag20_end+20, 20))
    HdB_labels = list(map(str,label_dBs))
    print('HdB_labels = ', HdB_labels)

    return ytick_locs, HdB_labels


######################################################################
def Bode_plot_setup():
    """
    Bode_plot_setup_conj_poles()
    return fig_ptr, ax

    Set up Bode plot of magnitude for conjugate poles verses Q.

    Input variables:
    
    
    

    """

    square_flag = True

    # Need subplot in order to make plot square.
    if square_flag == True:
        fig_ptr = plt.figure(1,figsize=(7,7))
        ax = fig_ptr.add_subplot(111, aspect='equal')
    else:
        fig_ptr = plt.figure(1,figsize=(7,5))
        ax = fig_ptr.add_subplot(111)

    # Set omega limits, min and max.
    min_w = 0.01
    max_w = 100
    min_HdB = -60
    max_HdB = 60

    # Use coordinates converted to log-log scales.
    plt.axis([0,7,0,5])

    plt.title('Bode Plot')

    # Axis labels and tick marks.
    plt.xlabel('Omega (r/s)')
    plt.ylabel('|H|')
    
    # Put labels of Q and damping factor eta on plot.
    #plt.text(1.5,3,'Text on Plot')
    #plt.text(2,4,'$y = x^2$')  # LateX-style text on plot.
    
    # Calculate and label log-spaced omega axis tick mark locations.
    xtick_locs, xtick_labels = Bode_xlabels(min_w,max_w)
    plt.xticks(xtick_locs,xtick_labels,fontsize=14)
    # 20 dB spaced ticks for y-axis.
    ytick_locs, ytick_labels = Bode_ylabels(min_HdB,max_HdB)
    plt.yticks(ytick_locs,ytick_labels,fontsize=14)

    # Examples of other useful plot commands, for reference.
    plt.plot([1,2],[3,4],'r-',linewidth=1.0)
    plt.plot([1,2],[4,5],color=(0.5,0.5,0.5),linewidth=1.0)

    #plt.axes().set_aspect('equal', adjustable='datalim')
    #ax.set_aspect('equal',adjustable='box')
    #plt.axes().set_aspect('equal')
    #plt.savefig('response_surface.png')

    return fig_ptr, ax


#######################################################################
def dB(H):
    """
    Convert quantity to dB.
    

    H in dB = 20 * log10(H)

    Inputs:
        H = (horiz vec) quantities (e.g., mag gain) to convert to dB
    
    Outputs:
        HdB = (horiz vec) H values in dB
    """
    
    # Convert to dB.
    HdB = 20*np.log10(H)
    
    return HdB


#######################################################################
def main():
    """
    Howdy
    """

    print(6)


#######################################################################
if __name__ == "__main__":
    main()

