new_contents: """
  Created on Fri Feb  6 00:36:14 2015
  
  @author: pfigueira
  """
  
  """ Script to process weekly TAPAS spectra to form an average atmospheric specturm.
  
  """
  
  import numpy as np
  import string
  
  import IOmodule
  
  #dirmodels = "/home/pfigueira/data/tapas/nIRanalysispaper/"
  dirmodels = "/home/pfigueira/data/tapas/nIRanalysis_visible/"
  list_files = "list_tapas_models.txt"
  
  outdir = "atmmodel/"
  
  def read_TAPAS(filename):
  
      FileConv = IOmodule.read_fullcol(filename)
      properties_dict = {}
      lambdas = []
      flux = []
      for line in FileConv:
          if(line[0] == '|'):
              #this is to ignore the header of the plot
              continue
          elif(line[0] == '\\'):
              #add everything starting with a \\ to the dictionary
              key_dic, value_dic = string.split(line[1:-1], "=")  #remove the "\" form the beggining and the "\n" from the end
              properties_dict[key_dic] = value_dic
          else:
              #these are the values of the file itself
              lambda_act, flux_act = string.split(line)
              lambdas.append(float(lambda_act))
              flux.append(float(flux_act))
  
      lambdas = np.array(lambdas)
      flux = np.array(flux)
      #note that the lambdas are provided in reversed order
      return [properties_dict, lambdas[::-1], flux[::-1]]
  
  def read_allfiles(mask_limit = 0.02):
      """
      reads all the files in list_files
      """
  
      Files = IOmodule.read_fullcol(dirmodels+list_files)
      print("Reading the files...")
      atm_models = [read_TAPAS(dirmodels+file_act[:-1]) for file_act in Files]
      print("done.")
  
      wav = atm_models[0][1]
      mean_flux = []
      std_flux = []
      mask = []
      for i in range(len(wav)):
          flux_at_wav = [model[2][i] for model in atm_models]
          mean_flux.append(np.average(flux_at_wav))
          std_flux.append(np.std(flux_at_wav))
          if(mean_flux[-1] > (1.0-mask_limit)):
              #if transmission above threshold do not mask, otherwise mask
              mask.append(1.0)
          else:
              mask.append(0.0)
  
      write_4col_eeed(outdir+"Average_TAPAS_2014_visible.txt", wav, mean_flux, std_flux, mask)
  
  def write_4col_eeed(filename, data1, data2, data3, data4):
  # Writes data in 3 columns separated by tabs in a "filename" file.
  
      f = open(filename, "w")
  
      for i in range(len(data1)):
   #       f.write("\t"+str(data1[i])+"\t\t"+str(data2[i])+"\t\t"+str(data3[i])+"\n")
          f.write("\t%e\t\t%e\t\t%e\t\t%d\n" % (data1[i], data2[i], data3[i], data4[i]))
  
      f.close();
  
subject: Move version of TAPASproc into bin. Won't run yet but good to have it around to show how was processed)