How to open .dlb extension file in Julia?

Hi , I want to open .dlb extension file using Julia and extract some data on it. I have .dlb file in my laptop. This data.6000.dlb file is in this tar. See Python code below :point_down:

def load_data(directory, N_r, N_theta, mu):
   """Loads data from .dbl files, returns numpy arrays."""
   # initialize arrays
   num_of_files = 0
   for file in glob.iglob(directory + "*.dbl"):
       num_of_files += 1
   print ("Number of files: %d" % num_of_files)
   # rho,vr,vtheta, vphi,br,btheta, bphi, prs, tracer
   var = np.zeros((9, num_of_files, N_r, N_theta))
   N_points = N_r * N_theta
   count = 0

   for file in sorted(glob.iglob(directory+'*.dbl')):
       f = open(file, 'rb')
       data = np.fromfile(file, dtype='d')

       for j in range(0, 9):
           temp = np.zeros(N_points)
           for i in range(0, N_points):
               temp[i] = data[i + j*N_points]
           temp = temp.reshape(N_theta, N_r).T
           var[j, count] = temp
           del temp
       count += 1
       f.close()

   # add dipole field to B field because PLUTO outputs change from dipole field
   #ccm100217--and multiply by np.sqrt(4.*np.pi) as PLUTO normalizes to it.
   for i in range(0, N_theta):
       var[4, :, :, i] += 2. * mu * np.cos(theta[i]) / (r*r*r)
       var[4, :, :, i] *= np.sqrt(4.0*np.pi)
       var[5, :, :, i] += mu * np.sin(theta[i]) / (r*r*r)
       var[5, :, :, i] *= np.sqrt(4.0*np.pi)

   return var[0],var[1],var[2],var[3],var[4],var[5],var[6],var[7],var[8]

I want to convert this Python code to Julia.

Where did you get the file from? Is there any specification of the file format? Which programs use this file format?

Thanks for adding the Python code.

But what do you want to get after reading the file? An array? Or a DataFrame? Or something else?

What have you tried? What isn’t working?

If your question is just “please rewrite this Python code in Julia for me” then ChatGPT might be a more appropriate venue then this forum?

1 Like

I don’t know what should be output. I get following error when i run code. I think output should be an array.

TypeError                                 Traceback (most recent call last)
Cell In[22], line 37
     35 theta = np.arange(0, N_theta) * (np.pi/1.98) / N_theta
     36 print(load_data(directory, N_r, N_theta, mu))
---> 37 rho, v_r, v_theta, v_phi, B_r, B_theta, B_phi, prs, tracer  = load_data(directory, N_r, N_theta, mu)
     40 print( 'N_r = %d' %(N_r))
     41 print( 'N_theta = %d' %(N_theta))

TypeError: cannot unpack non-iterable NoneType object

So you’re saying the Python code isn’t working?

No, Code in mc_2zoom_rhov_halfpi.py is working fine. But when i run it separate cells in jupyter then it shows error. You can download code given in VelocPythJul.tar.gz - Google Drive and change directory variable in code accordingly.