import numpy as np
import matplotlib.pyplot as plt

def read_spec ( filename ):
  with open(filename) as f:
    rec = '!'
    while rec[0] == '!': rec = f.readline()
    flds = rec.split()
    npts = abs ( int(flds[0]) )
    wno1 = float(flds[1])
    wnod = float(flds[2])
    if wnod > 0:         # regular grid
      spc = np.fromfile(f,sep=" ")
      wno = wno1 + np.arange(npts)*wnod
    else:                # irregular grid
      dat = np.fromfile(f,sep=" ").reshape(npts,2)
      wno = dat[:,0]
      spc = dat[:,1]
  return wno, spc


w,r10 = read_spec('coo_10000.asc')
w,r30 = read_spec('coo_30000.asc')
w,r50 = read_spec('coo_50000.asc')
w,r70 = read_spec('coo_70000.asc')

s10 = '10km ' + f'{np.sum(r10):5.2f}' + 'K/day'
plt.plot(w,r10,label=s10)

s30 = '30km ' + f'{np.sum(r30):5.2f}' + 'K/day'
plt.plot(w,r30,label=s30)

s50 = '50km ' + f'{np.sum(r50):5.2f}' + 'K/day'
plt.plot(w,r50,label=s50)

s70 = '70km ' + f'{np.sum(r70):5.2f}' + 'K/day'
plt.plot(w,r70,label=s70)


plt.xlabel('Wavenumber [cm$^{-1}$]')
plt.ylabel('Cooling Rate [K/(day cm$^{-1}$)]')
plt.title('CO$_2$ 15$\mu$m Band Cooling Rate')
plt.margins(x=0.0)
plt.legend()
plt.savefig('plot.png')

