Home > Enterprise >  Inexplicable SyntaxError when calling a defined function with a file input
Inexplicable SyntaxError when calling a defined function with a file input

Time:11-05

I defined the following function to interpolate grid data:

def interpolate_quadrant(file, r_out):
    f = open(file, 'r')
    x = []
    y = []
    z = []
    for line in f:
        line = line.strip()
        columns = line.split()
        x.append(float(columns[0]))
        y.append(float(columns[1]))
        z.append(float(columns[2]))
    f.close()
    
    xold = np.asarray(x)
    yold = np.asarray(y)
    zold = np.asarray(z)
    
    xx = np.linspace(np.min(xold), np.max(xold), num= r_out)
    yy = np.linspace(np.min(yold), np.max(yold), num= r_out)
    xx, yy = np.meshgrid(xx, yy)
    valsq1 = interpolate.griddata((xold, yold), zold, (xx, yy), method = 'cubic')

    valsq1 = valsq1[1:,:]
    valsq2 = valsq1[::-1,:]
    valsq3 = valsq1[:,::-1]
    valsq4 = valsq1[::-1,::-1]
    
    hdu = fits.PrimaryHDU(data = r)
    hdu.writeto('FITS_file_(1).fits')

When I Shift Enter this in a Python cell, it works and goes through. However, when I call the function in a new cell via:

interpolate_quadrant(/home/ben/Documents/Planetary_Astrophysics/PE_MHD_Project/faceon1cm_mhd_5_30_ml-8.6_VE.out, 200)

I get the syntax error shown in the attached image. The SyntaxError in Jupyter Lab.

I'm uncertain on what's wrong the file link that I'm using as a function argument. The type of file to be inputted is a '.out'.

CodePudding user response:

Seems like the solution is to encode the path in quotes. That's the case in google collab, i have never used jupyter notebook though.

interpolate_quadrant('/home/ben/Documents/Planetary_Astrophysics/PE_MHD_Project/faceon1cm_mhd_5_30_ml-8.6_VE.out', 200)
  • Related