1

I have written a code that uses griddata interpolation to interpolate points on an irregular grid onto a regular grid. I also creating contour plots for two dependent variables after the interpolation. I am able to generate the line contour and filled contour plots and they also match the images qualitatively that were directly generated from my simulation. But the images are shown in a horizontal orientation and also the quality is not as per my expectation. I want the images to be shown as vertical i.e (x-axis; width =30.48(always fixed),y-axis; width varies according to a condition). In the plots x and y are interchanged. I did not understand the reason for this. I think it has to do with how the rows and columns are handled by contour function.

My second issue is that I am importing multiple text files and I want to save the generated contour plots separately for each of the imported text files. I am not sure of the best way to do this.

from __future__ import print_function
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate  import griddata
###############################################################################
path = 'location of the files'
FT_init = 5.4311
delt = 0.15
TS_init = 140
dj_length = 2.4384
###############################################################################
def streamfunction2d(y,x,Si_f):
    plt.figure(3)
    Stf= plt.contour(y,x,Si_f,50)
    Stf1 = plt.colorbar(Stf)
    plt.savefig('location of the saved image',format='png',dpi=1200)
    plt.close(3)

###############################################################################    
def tracer2d(y,x,Ti_f):
    plt.figure(1)
    trc= plt.contour(y,x,Ti_f,25)
    trc1 = plt.colorbar(trc)
    plt.clabel(trc,fmt='%.0f',inline=True)
    plt.savefig('location of the saved image',format='png',dpi=1200)
    plt.close(1)

    plt.figure(2)
    trp = plt.pcolormesh(y,x,Ti_f,linestyle='dashed')  ## Alternate method of creating filled-contour plots. 
    plt.savefig('location of the saved image',format='png',dpi=1200)
    plt.close(2) 
###############################################################################
flowtime =np.linspace(500,600,2) ## Here I would include multiple flow times. So I want to save the plots for each flow time in the location provided.
timestep = np.zeros(len(flowtime))

for n in range(len(flowtime)):
    timestep = (flowtime-FT_init)/delt + TS_init
    timestep = np.array(np.round(timestep,-2),dtype = 'int')

for p in range(len(timestep)):

        timestepstring=str(timestep[p]).zfill(4)
        fname = path+"ddn150AE-"+timestepstring+".txt"
        f = open(fname,'r')
        data = np.loadtxt(f,skiprows=1)
        data = data[data[:, 1].argsort()]
        data = data[np.logical_not(data[:,11]== 0)] 

        Y  = data[:,2]  
        limit = np.nonzero(Y==dj_length)[0][0]
        Y  = Y[limit:]

        Vf  = data[:,11] 
        Vf  = Vf[limit:]
        Tr  = data[:,9] 
        Tr  = Tr[limit:]

        X  = data[:,1]  
        X  = X[limit:]
        Y  = data[:,2] 
        Y  = Y[limit:]
        U  = data[:,3]  
        U  = U[limit:]
        V  = data[:,4]  
        V  = V[limit:]
        St = data[:,5]  
        St  = St[limit:]        
###############################################################################     

### The following code will do the interpolation for the dependent variable

        ## Using griddata for interpolation from Unstructured to Structured data
        # resample onto a 100x100 grid

        nx, ny = 100,100

        # (N, 2) arrays of input x,y coords and dependent values
        pts = np.vstack((X,Y )).T
        vals = np.vstack((Tr))
        vals1 = np.vstack((St))

        # The new x and y coordinates for the grid
        x = np.linspace(X.min(), X.max(), nx)
        y = np.linspace(Y.min(), Y.max(), ny)
        r = np.meshgrid(y,x)[::-1]

        # An (nx * ny, 2) array of x,y coordinates to interpolate at
        ipts = np.vstack(a.ravel() for a in r).T

        # An (nx * ny, 2) array of interpolated Tr values
        Ti = griddata(pts, vals, ipts, method='nearest')
        Ti_f = np.reshape(Ti,(len(y),len(x)))
        tracer2d(y,x,Ti_f)

        Si = griddata(pts, vals1, ipts, method='nearest')
        Si_f = np.reshape(Si,(len(y),len(x)))
        streamfunction2d(y,x,Si_f)

Location for the text files Sample Text Files. This is the image that I have from simulation corresponding to 3400 text file.

I would appreciate any advice or help in this.

Thank you

SOLVED:

narayan.p
  • 105
  • 2
  • 12
  • I ran your code, the image is the same orientation as the contour plot. I don't know what your problem, can you explain in detail? – HYRY Jan 17 '16 at 03:18
  • @HYRY What do you mean by "Image"? I have added the actual image directly obtained from the simulation. I want the contour plot obtained from python to be the same as this image. As you can see, the orientation is different with the python plot. Thank you – narayan.p Jan 17 '16 at 03:29
  • Try transpose the matrix: `plt.contour(x,y,Ti_f.T, 25)` – HYRY Jan 17 '16 at 03:36
  • @HYRY Thank you for the response. I will try it out. I also had one more question about saving of images. If I run my code for 5 different flow times, then I would want to save all the 5 images separately in the same path.Could you please help me on that – narayan.p Jan 17 '16 at 15:00
  • @HYRY Transpose worked like a charm. Thank you. – narayan.p Jan 20 '16 at 17:26

0 Answers0