Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Code as below Can you please help plotting Plot_Data_Elem_processed impor

ID: 3918887 • Letter: P

Question

Python Code as below

Can you please help plotting Plot_Data_Elem_processed

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker, rcParams
import sys
import os

def find_files(root,filename):
   for directory,subdirs,files in os.walk(root):
       if filename in files:
           yield directory


max_depth = 29870

depth_start = 100
depth_end = 170

dp = 1

labels = ["r"]


window_start = 0
window_end = len(labels)
window_size = 120

labels = labels[window_start:window_end]


def get_snapshot(data, index, max_depth):
   start = index * max_depth
   end = (index + 1) * max_depth
   return data[start:end, :]

def pp(data):
   return data[:,3]

def GS(data):
   return data[:,5]

def L(data, dsf):
   pp = data[:,3]
   zb = -data[:,2]
   tempdsf = np.ones(pp.shape) * dsf
   g = 9.8
   pwg = 1046 * g
   pb = 1844.8
   sv = ((pwg * tempdsf) + (pb * g * zb)) # vector
   #print(pwg * (dsf + zb))
   f = (pp - (pwg * (tempdsf + zb))) / (sv - (pwg * (tempdsf + zb)))
   return f

#, 515.94, 503.43, 490.91, 478.40, 465.88, 453.37, 440.85, 428.34

#, "84", "75", "66", "57", "48", "39", "30", "21"

#x = [(577, "120"), (516, "110")]
#x[0][0] # depth 1
#x[0][1] # year 1

files = sys.argv[1:]

for i, f in enumerate(files):
   data = np.zeros(((depth_end * dp - depth_start * dp), window_size))

   name = f.replace("/", "_")
   print(str(i) + "/" + str(len(files)) + " doing " + name)
   A = np.loadtxt(f, skiprows=1) #read the data

   first = get_snapshot(A, 0, max_depth)
   labels = list(first[0:window_size][0])
  
   if len(A) < 26000:
       print("skipping " + name + " - not complete")
       continue

   snapshot = get_snapshot(A, 0, max_depth)
   print("get snapshot {} - {}".format(0, max_depth))
   for rowI in range(max_depth - 1):
       row = snapshot[rowI]
       z = abs(int(row[1] * dp))
       r = row[0]
       if z >= depth_start * dp and z < depth_end * dp:
           data[z - depth_start, labels.index(r)] = row[4]

   #print(data)
   rcParams.update({'font.size': 24})
   fig = plt.figure(1, figsize=(20, 20))
   im = plt.imshow(data, cmap='jet', aspect='auto', interpolation='none', extent=[0, len(labels) -1, depth_end, depth_start])
#   im = plt.imshow(data, cmap='jet', aspect='auto', interpolation='bicubic', extent=[0, len(labels) - 1, depth_end, depth_start])#, vmax=1, vmin=0
#   plt.colorbar(im, orientation='vertical', label='$lambda$')
   plt.colorbar(im, orientation='vertical', label='Gas Saturation (%)')
#plt.colorbar(im, orientation='vertical', label='Temperature (°C)')
   def my_formatter_fun(x, p):
       if x == 0:
           return x / dp
       return -x / dp
   plt.gca().get_yaxis().set_major_formatter(ticker.FuncFormatter(my_formatter_fun))
   plt.xticks(range(len(labels)), labels, rotation='vertical')
   plt.ylabel("Depth (m)")
#   plt.xlabel("Kyrs S$_h$")
   plt.xlabel("r (m)")
#   plt.title("sealevel-568....permeability = 10E-17")
#   fig.savefig('plots/568/gas/' + name + ".eps")
   plt.show()
   plt.clf()

   #print(snapshot_P)
   #print(snapshot_index)
   #print(snapshot_start, snapshot_end)

   #Create the time vector in thousands of years
   #T=[] #Time vector
   #for i in range(0,16):
   # t=1000*(i+1)*np.ones(1000)
   # T=np.append(T,t)
  
   #Plot
   #plt.imshow(PR.T[0:200,:],aspect='auto',extent=[np.min(T),np.max(T),200,0])
   #plt.xlabel('Time (y)'),plt.ylabel('Depth (m)')
   #plt.colorbar()

Explanation / Answer

I think the code is correct but it would be nice if you share the sample file as without data it is hard to find the error. Though i would like to correct few things what is the need of find_files() when not using it. If you are using sys.argv[1:] i hope you are entering the file from terminal otherwise the code will simply run without any error, so make usre you are giving the proper path of file. Secondly you can avoid the function pp function when you can make data[:, 3] which will reduce unnecessary function call, function L is as aslo not used anywehere if possible you can avoid those. Other than that unless the sample is provided it is difficult to find the issue, but syntactically the code is correct. Please provide the sample so we can make sure logically also it is correct.