Reposting question explaining it better.
I am doing TOA corrections on Landsat8 Images in Python(3.5) by reading all image_bands and after processing them creating new GTiff of those images with GDAL, example of code:
#THIS part saves new image
def array2raster(fname, nx, ny, matriz, geot, proj):
drv = gdal.GetDriverByName("GTiff")
dst_ds = drv.Create(fname, nx, ny, 1, gdal.GDT_Byte)
dst_ds.SetGeoTransform(geot)
dst_ds.SetProjection(proj)
dst_ds.GetRasterBand(1).WriteArray(matriz)
# dst_ds.GetRasterBand(1).SetMetadata({"Band": "%d" % band, "Units": "W/(m2*ster*um)", "Data": "TOA Radiance"})
dst_ds.GetRasterBand(1).FlushCache()
#Here is made the convertion
def convert_to_radiance(fname, band, gain, bias):
global img_rad, img_original
g = gdal.Open(fname)
gl = g.ReadAsArray()
print("Dimension: ", gl.shape, gl.dtype)
radiance = gl * gain[band] + bias[band]
img_original[:, :, band] = gl
img_rad[:, :, band] = radiance
a=fname
output_fname = a.replace(".TIF", "_TOARAD.TIF")
print ("Original fname " , fname)
print ("Creating output " , output_fname)
# Create output dataset if `first_time` is true
nx = g.RasterXSize
ny = g.RasterYSize
print("Dimension imagen: ", nx, ny)
proj = g.GetProjectionRef()
geoT = g.GetGeoTransform()
array2raster(output_fname, nx, ny, radiance, geoT, proj)
#this is the main function:
def main():
global prefix
metadata_file = get_metadata('D:\Minam\LC80030692016324LGN00\LC80030692016324LGN00_MTL.txt')
prefix = metadata_file.split("_")[0]
print ("path de metadata:")
print (metadata_file)
(bias_rad, gain_rad, bias_ref, gain_ref,lradmax, lrefmax, nx, ny, sun, desun) = process_metadata(metadata_file) #here i Open the metadata to get values
start_time = time.time()
for the_band in [1, 2, 3, 4, 5, 6, 7, 9]:
input_file = prefix + "_B%d.TIF" % the_band
print("--------------------")
print("Procesando imagen: {}".format(the_band))
print(input_file)
convert_to_radiance(input_file, the_band, gain_rad, bias_rad)
convert_to_reflectance(input_file, the_band, gain_ref, bias_ref, sun)
If is the first time processing the bands there is no problem saving the images and the metadata still exists, problem appears when trying to do it again with the images already created, here is when metedata file dissappear, debugging code, it happen on the array2raster
function but can't explain why because the metadata file is never called in the function again, what could be causing it?