#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Elena Graverini
# @Date:   2015-12-15 11:41:23
# @Last Modified by:   Elena Graverini
# @Last Modified time: 2016-01-13 12:34:41

# This file should be run on lxplus
from __future__ import print_function
import os, sys
import subprocess

if not sys.argv:
    lastYear = 2015
else:
    lastYear = int(sys.argv[1])
years = range(2009, lastYear + 1)
location_tt = '/group/st/sw/RadiationMonitoring/LeakageCurrent/InputData/TT/Temperatures'
location_it = '/group/st/sw/RadiationMonitoring/LeakageCurrent/InputData/IT/Temperatures'
for y in years:
    try:
        subprocess.check_call(['scp', 'egraveri@lbgw:%s/tt_box_temperatures_%s.txt' % (location_tt, y), '.'])
        subprocess.check_call(['scp', 'egraveri@lbgw:%s/it_box_temperatures_%s.txt' % (location_it, y), '.'])
    except:
        print('\tCould not find temperature files for year %s' % y)
output_location = '/afs/cern.ch/user/e/egraveri/cmtuser/STMonitoring/STAging/data/temperature'
output_tt = output_location + '/tttemp.txt'
output_it = output_location + '/ittemp.txt'
print('\tBacking up old files...')
try:
    subprocess.call(['mv %s %s_BK' % (output_tt, output_tt)])
    subprocess.call(['mv %s %s_BK' % (output_it, output_it)])
except:
    print('\tCould not backup old files %s and/or %s (files not found or unable to write)' % (output_tt, output_it))
    raw_input('\tContinue anyway? Press CTRL-C to exit.')


def concatenate(filelist, destination):
    with open(destination, 'w') as outfile:
        for fname in filelist:
            with open(fname, 'r') as infile:
                for line in infile:
                    outfile.write(line)

files_tt = ['tt_box_temperatures_%s.txt' % y for y in years if os.path.isfile(os.getcwd() + '/tt_box_temperatures_%s.txt' % y)]
files_it = ['it_box_temperatures_%s.txt' % y for y in years if os.path.isfile(os.getcwd() + '/it_box_temperatures_%s.txt' % y)]

print('\tReceived files: ', files_tt)
print('\tReceived files: ', files_it)

print('\tConcatenating...')

concatenate(files_tt, output_tt)
concatenate(files_it, output_it)

print('\tDeleting temporary files...')
for f in files_tt:
    try:
        os.remove(f)
    except:
        print('\tCould not find %s' % f)
for f in files_it:
    try:
        os.remove(f)
    except:
        print('\tCould not find %s' % f)

print('\tAll done.')