def generate_contact_length_variation(contoursDir,
                                      contact_angle,
                                      show=False):
    '''
    @description: compute the normalized difference of
    the contact length as function of time and create
    a graph
    '''

    contactLghPath = os.path.join(contoursDir,'contact_lgh.txt')
    volumePath     = os.path.join(contoursDir,'volume.txt')

    compute_contact_length_variation(contactLghPath,
                                     volumePath,
                                     contact_angle)

    contoursNPath    = os.path.join(contoursDir,'contact_lgh_n.txt')
    contoursNFigPath = os.path.join(contoursDir,'contact_lgh_n.eps')

    # extract the contact length:
    contactLgh = np.loadtxt(contoursNPath)
    print 'contact_lgh: ', contactLgh[-1]

    create_graph(contoursNPath,
                 xlabel='$t$',
                 ylabel='contact length difference',
                 figPath=contoursNFigPath,
                 width=3,
                 logScale=False,
                 show=show)
def generate_st_graphs(ncFolder,
                       timeRange,
                       contactAngle,
                       contourType,
                       phase_check=False,
                       contourPer=0.1,
                       genContours=False,
                       maxNbBubbleContours='None',
                       show=True,
                       x_limits='None',
                       y_limits='None',
                       x_figsize=12,
                       y_figsize=6,
                       reflection=False,
                       select_end_time=True,
                       select_detach_time=False,
                       scalingTimesteps='None'):
    '''
    @description: generate the contours of the bubble
    at different timesteps, extract the contact length
    of the bubble at the wall, the volume of the bubble
    in time and plot the contours at different timesteps
    as well as the spherical cap approximation    
    '''

    # determine the paths to the folders
    ncRootPath  = os.path.join(ncFolder,'data')
    contoursDir = os.path.join(ncFolder,'contours')
    contoursRootPath = os.path.join(contoursDir,'contours')

    
    # choose whether to create the graph with the spherical cap
    # approximation
    add_spherical_cap_approx = not phase_check

    
    # if there is no existing contour folder
    # create one    
    if(not os.path.isdir(contoursDir)):
        os.makedirs(contoursDir)


    ## extract the contact length and the volume
    ## as functions of time
    if(genContours):
        generate_time_contour_data(
            ncRootPath,
            contoursRootPath,
            timeRange=timeRange,
            var='mass',
            contourPer=contourPer,
            contourType=contourType,
            phase_check=phase_check,
            reflection=reflection)
    

    # paths for saving the contact angle and volume figures
    contact_lgh_path = os.path.join(contoursDir,'contact_lgh.txt')
    volume_path      = os.path.join(contoursDir,'volume.txt')
    mass_path        = os.path.join(contoursDir,'mass.txt')
    contour_path     = os.path.join(contoursDir,'contour.txt')
    temperature_path = os.path.join(contoursDir,'temperature.txt')
    dataRootPath     = contoursDir

    contactLghFigPath  = os.path.join(contoursDir,'contact_lgh.eps')
    volumeFigPath      = os.path.join(contoursDir,'volume.eps')
    massFigPath        = os.path.join(contoursDir,'mass.eps')
    contourFigPath     = os.path.join(contoursDir,'mass_contour.eps')
    temperatureFigPath = os.path.join(contoursDir,'temperature.eps')

    contoursFigPath    = os.path.join(contoursDir,'contours.eps')
    contoursStFigPath  = os.path.join(contoursDir,'contours_st.eps')

    contoursFigPath = os.path.join(
        '/home/jdesmarais/Code/augeanstables/scripts_py/wall_steady_state_automatization/postprocessing',
        'figs',
        'fig'+os.path.basename(ncFolder).replace('dim2d','').replace('hca0.0_','')+'_approx.eps')
    
    draw_other_than_contour = False


    # plot the contact length as funtion of time
    if(draw_other_than_contour):

        # plot the contact length
        curves_to_contact_lgh(ncFolder)
        curves_to_volume(ncFolder)

        create_graph(contact_lgh_path,
                     contactAngle=contactAngle,
                     xlabel='$t$',
                     ylabel='contact length',
                     figPath=contactLghFigPath,
                     width=3,
                     logScale=False,
                     show=show,
                     plotLengthEq=add_spherical_cap_approx,
                     volumePath=volume_path)
    
        # plot the volume as function of time
        create_graph(volume_path,
                     xlabel='$t$',
                     ylabel='volume',
                     figPath=volumeFigPath,
                     width=3,
                     logScale=False,
                     show=False)

        # plot the mass as function of time
        create_graph(mass_path,
                     xlabel='$t$',
                     ylabel='mass',
                     figPath=massFigPath,
                     width=3,
                     logScale=False,
                     show=False)

        # plot the mass chosen to draw the
        # contours as function of time
        create_graph(contour_path,
                     xlabel='$t$',
                     ylabel='mass',
                     figPath=contourFigPath,
                     width=3,
                     logScale=False,
                     show=show)

        # plot the temperature chosen to draw the
        # contours as function of time
        if(os.path.isfile(temperature_path)):
            create_graph(temperature_path,
                         xlabel='$t$',
                         ylabel='$T$',
                         figPath=temperatureFigPath,
                         width=3,
                         logScale=False,
                         show=show)


    # plot the contour at different time steps:
    # choose the timesteps to have only maxNbBubbleContours
    #------------------------------------------------------------

    # get the first timestep with a bubble
    volumePath = dataRootPath+'/volume.txt'
    volume = np.loadtxt(volumePath)
    for i in range(0,len(volume[:,0])):
        if(volume[i,2]>0):
            start_i = i
            break
    start_i = max(start_i,timeRange[0])


    # select the last timestep before the volume is zero
    end_i = len(volume[:,0])-1
    if(select_end_time):
        for i in range(len(volume[:,0])-1,start_i,-1):
            if(volume[i,2]>0):
                end_i = volume[i,0]
                break
    end_i = min(end_i,timeRange[1])
    end_i_time = end_i
    
    nt = len(volume[:,0])


    # select the last timestep before the contact length
    # is zero (detachment)
    if(select_detach_time):
        contact_lgh = np.loadtxt(contact_lgh_path)
        end_i = len(contact_lgh[:,0])-1

        for i in range(len(contact_lgh[:,0])-1,start_i,-1):
            if(contact_lgh[i,2]>0):
                end_i = contact_lgh[i,0]
                break

        end_i*=1.3
        end_i = int(end_i)

        end_i = min(end_i,end_i_time)

    print 'end_i: ', end_i


    # select the timesteps
    if(maxNbBubbleContours=='None'):
        nbContours = end_i-start_i+1
    else:
        nbContours = maxNbBubbleContours

    if(scalingTimesteps=='None'):
        scaling=1.0
    else:
        scaling=scalingTimesteps


    # extraction of the timesteps to
    # display the contours
    times   = []
    times_t = []

    #times.append(start_i)
    #times_t.append(volume[start_i,1])

    for i in range(0,nbContours):

        step = float(end_i-start_i)*(float(i)/float(nbContours-1))**scaling
        if(step>0):
            step = max(1,iround(step))
            step-= step%2
        else:
            step = 0

        timestep = start_i + step

        times.append(timestep)
        times_t.append(volume[timestep/timeRange[2],1])

    #times = [0,82,166,248,300,400,498] #to see the break-up for 135.0, ux=0.4

    times_p = np.array(times_t)
    np.set_printoptions(precision=5)
    print 'Timesteps for contours: '
    print times
    print 'Time extracted for contours: '
    print times_p

    # create the graph with only the contours at different
    # relevant times
    create_st_graph(dataRootPath,
                    times,
                    contactAngle,
                    xlabel='$x$',
                    ylabel='',
                    figPath=contoursFigPath,
                    width=3,
                    show=show,
                    x_limits=x_limits,
                    y_limits=y_limits,
                    x_figsize=x_figsize,
                    y_figsize=y_figsize)

    # create the graph with only the last contours and the
    # spherical cap approximation
    if(add_spherical_cap_approx):
        create_sph_graph(dataRootPath,
                         times[-1],
                         contactAngle,
                         figPath=contoursStFigPath,
                         show=show,
                         x_limits=x_limits,
                         y_limits=y_limits)