import os
import time

from crossnetwork_processing_functions import set_file_permissions_ownership, check_directory_existence
from crossnetwork_processing_functions import create_GHOST_stage_logfile
from GHOST_standards import standard_temporal_resolutions

class Compress:

    '''Class which compresses all netCDF files of a component of a specific resolution, per region, 
       of a specific network, into one file.
    '''
    
    def __init__(self, temporal_resolution_code, region, run_details, parameter_details, network_details):

        #add passed variables to self
        self.temporal_resolution_code = temporal_resolution_code
        self.region = region
        self.run_details = run_details
        self.parameter_details = parameter_details
        self.network_details = network_details

        self.run_details['log_file_str'] += 'COMPRESSING netCDFs\n'

    def do_work(self):
        '''Function which does executes the functions of the class in a pipeline'''

        #compress component
        self.compress()

    def compress(self):
        '''Function which compresses all netCDF files of a specific resolution, per region, of a specific network, 
           into one file.
        '''
    
        #set network directory where to read/write data
        if self.region == 'GLOBAL':
            network_write_dir = self.run_details['network']
        else:
            network_write_dir = '{}_{}'.format(self.run_details['network'], self.region)

        # get full name of temporal resolution being processed
        resolution = standard_temporal_resolutions[self.temporal_resolution_code]['temporal_resolution_path']

        # set compressed output directory / filename of compressed directory
        compress_dir = '{}GHOST_v{}/{}/{}'.format(self.run_details['compress_root'], self.run_details['ghost_version'], 
                                                  network_write_dir, resolution)

        compress_fname = '{}/{}.tar.xz'.format(compress_dir, self.parameter_details['bsc_parameter_name'])

        #check output dir exists
        check_directory_existence('{}/'.format(compress_dir), directories_not_to_test=self.run_details['compress_root'])
        
        # save current directory
        current_dir = os.getcwd()

        # change directory to directory to just outside the component directory to compress 
        os.chdir('{}{}/{}/{}'.format(self.run_details['data_root'], network_write_dir, 
                                     self.run_details['ghost_version'], resolution))

        # compress directory
        os.system("XZ_OPT=-9 tar --exclude='{}/temporary' -cJf {} {}".format(self.parameter_details['bsc_parameter_name'], 
                                                                             compress_fname, 
                                                                             self.parameter_details['bsc_parameter_name']))
        
        #give 770 permissions for file and make owner bsc32
        set_file_permissions_ownership(compress_fname)   

        #return to original directory
        os.chdir(current_dir)

        #return valid process logfile (0)
        self.run_details['log_file_str'] += str((time.time() - self.run_details['stage_process_start_time'])/60.)
        create_GHOST_stage_logfile(0,self.run_details)