'''

Functions for dealing with the image.out files created by RADMC-3D

'''

#-----------------------------------------------------------------------------
# Copyright (c) 2013, yt Development Team.
#
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

import numpy as np

'''

Parses the header lines from the image.out file.
Returns a dictionary containing the image parameters.

'''

# an integer flag describing the image format.
# this function only works for images made in
# "observer at infinity" mode, i.e. iformat is 1
iformat = np.int64(lines[0].strip())
assert(iformat == 1)

# The number of pixels in the x and y-directions
Nx, Ny = [np.int64(Npix) for Npix in lines[1].strip().split()]

# You can produce images at multiple wavelenths in a single
# pass. This function assumes that there is only 1.
num_wavelengths = np.int64(lines[2].strip())
assert(num_wavelengths == 1)

# The size of pixel in each direction. Note that
# this only makes sense if iformat is 1
pixel_size_cm_x, pixel_size_cm_y = \
[np.float64(Npix) for Npix in lines[3].strip().split()]

# The wavelength at which the image was produced.
# We assume there is only 1 image here.
wavelength_microns = np.float64(lines[4].strip())  # assume 1 wavelength

'''

Returns an np.array that contains the image data
as well as a dictionary with some useful metadata.

'''

fileh = open(filename, 'r')
fileh.close()

# The header should always be 5 lines long,
# as per the radmc-3d manual

# The rest of the lines are the image data, with the
# possible exception of a newline at the end
image_lines = lines[6:]
image = np.array([np.float64(line.strip()) for line in image_lines
if not line.isspace()])

# This had better be true
assert(image.size == Nx*Ny)