def move_to_archive(self, variable):
        '''Moves grb and hdr files to archive location.

        Precondition:
            Header and Grib files for variable exist in current directory
        Postcondition:
            Header and Grib files for variable exist in archive directory
            Header and Grib files for variable don't exist in current directory
        '''
        logger = logging.getLogger(__name__)

        dest_path = self.get_internal_drectory()  # Determine the directory
        hdr_name = self.get_internal_filename(variable, 'hdr')
        grb_name = self.get_internal_filename(variable, 'grb')

        System.create_directory(dest_path)  # create it if it does not exist

        # Archive the files
        logger.info('Archiving into [{0}]'.format(dest_path))
        # GRIB
        dest_file = os.path.join(dest_path, grb_name)
        shutil.copyfile(grb_name, dest_file)
        # HEADER
        dest_file = os.path.join(dest_path, hdr_name)
        shutil.copyfile(hdr_name, dest_file)

        # Cleanup the working directory
        if os.path.exists(grb_name):
            os.unlink(grb_name)
        if os.path.exists(hdr_name):
            os.unlink(hdr_name)
예제 #2
0
    def move_to_archive(self, variable):
        '''Moves grb and hdr files to archive location.

        Precondition:
            Header and Grib files for variable exist in current directory
        Postcondition:
            Header and Grib files for variable exist in archive directory
            Header and Grib files for variable don't exist in current directory
        '''
        logger = logging.getLogger(__name__)

        dest_path = self.get_internal_drectory()  # Determine the directory
        hdr_name = self.get_internal_filename(variable, 'hdr')
        grb_name = self.get_internal_filename(variable, 'grb')

        System.create_directory(dest_path)  # create it if it does not exist

        # Archive the files
        logger.info('Archiving into [{0}]'.format(dest_path))
        # GRIB
        dest_file = os.path.join(dest_path, grb_name)
        shutil.copyfile(grb_name, dest_file)
        # HEADER
        dest_file = os.path.join(dest_path, hdr_name)
        shutil.copyfile(hdr_name, dest_file)

        # Cleanup the working directory
        if os.path.exists(grb_name):
            os.unlink(grb_name)
        if os.path.exists(hdr_name):
            os.unlink(hdr_name)
    def process_grib_for_variable(self, variable, grib_file):
        '''
        Description:
            Extract the specified variable from the grib file and archive it.
        '''

        self.logger.info("Processing [{0}]".format(grib_file))

        # Get the date information from the grib file
        parts = grib_file.split('.')
        year = int(parts[1][:4])
        month = int(parts[1][4:6])
        day = int(parts[1][6:8])
        hour = int(parts[1][8:])

        # Figure out the filenames to create
        hdr_name = (Config.get('archive_name_format')
                    .format(variable, year, month, day, hour*100, 'hdr'))
        grb_name = (Config.get('archive_name_format')
                    .format(variable, year, month, day, hour*100, 'grb'))

        # Create inventory/header file to extract the variable data
        cmd = ['wgrib', grib_file, '|', 'grep', variable, '>', hdr_name]
        cmd = ' '.join(cmd)
        self.logger.info('Executing [{0}]'.format(cmd))
        output = System.execute_cmd(cmd)
        if output is not None and len(output) > 0:
            self.logger.info(output)

        # Create grib files for each variable
        cmd = ['cat', hdr_name, '|',
               'wgrib', grib_file, '-i', '-grib', '-o', grb_name]
        cmd = ' '.join(cmd)
        output = ''
        self.logger.info('Executing [{0}]'.format(cmd))
        output = System.execute_cmd(cmd)
        if output is not None and len(output) > 0:
            self.logger.info(output)

        # Create new inventory/header file for the variable
        cmd = ['wgrib', grb_name, '|', 'grep', variable, '>', hdr_name]
        cmd = ' '.join(cmd)
        self.logger.info('Executing [{0}]'.format(cmd))
        output = System.execute_cmd(cmd)
        if output is not None and len(output) > 0:
            self.logger.info(output)

        # Determine the directory to place the data and create it if it does
        # not exist
        dest_path = (Config.get('archive_directory_format')
                     .format(self.base_aux_dir, year, month, day))
        System.create_directory(dest_path)

        # Archive the files
        self.logger.info('Archiving into [{0}]'.format(dest_path))
        # GRIB
        dest_file = os.path.join(dest_path, grb_name)
        shutil.copyfile(grb_name, dest_file)
        # HEADER
        dest_file = os.path.join(dest_path, hdr_name)
        shutil.copyfile(hdr_name, dest_file)

        # Cleanup the working directory
        if os.path.exists(grb_name):
            os.unlink(grb_name)
        if os.path.exists(hdr_name):
            os.unlink(hdr_name)
예제 #4
0
    def process_grib_for_variable(self, variable, grib_file):
        '''
        Description:
            Extract the specified variable from the grib file and archive it.
        '''

        self.logger.info("Processing [{0}]".format(grib_file))

        # Get the date information from the grib file
        parts = grib_file.split('.')
        year = int(parts[1][:4])
        month = int(parts[1][4:6])
        day = int(parts[1][6:8])
        hour = int(parts[1][8:])

        # Figure out the filenames to create
        hdr_name = (Config.get('archive_name_format').format(
            variable, year, month, day, hour * 100, 'hdr'))
        grb_name = (Config.get('archive_name_format').format(
            variable, year, month, day, hour * 100, 'grb'))

        # Create inventory/header file to extract the variable data
        cmd = ['wgrib', grib_file, '|', 'grep', variable, '>', hdr_name]
        cmd = ' '.join(cmd)
        self.logger.info('Executing [{0}]'.format(cmd))
        output = System.execute_cmd(cmd)
        if output is not None and len(output) > 0:
            self.logger.info(output)

        # Create grib files for each variable
        cmd = [
            'cat', hdr_name, '|', 'wgrib', grib_file, '-i', '-grib', '-o',
            grb_name
        ]
        cmd = ' '.join(cmd)
        output = ''
        self.logger.info('Executing [{0}]'.format(cmd))
        output = System.execute_cmd(cmd)
        if output is not None and len(output) > 0:
            self.logger.info(output)

        # Create new inventory/header file for the variable
        cmd = ['wgrib', grb_name, '|', 'grep', variable, '>', hdr_name]
        cmd = ' '.join(cmd)
        self.logger.info('Executing [{0}]'.format(cmd))
        output = System.execute_cmd(cmd)
        if output is not None and len(output) > 0:
            self.logger.info(output)

        # Determine the directory to place the data and create it if it does
        # not exist
        dest_path = (Config.get('archive_directory_format').format(
            self.base_aux_dir, year, month, day))
        System.create_directory(dest_path)

        # Archive the files
        self.logger.info('Archiving into [{0}]'.format(dest_path))
        # GRIB
        dest_file = os.path.join(dest_path, grb_name)
        shutil.copyfile(grb_name, dest_file)
        # HEADER
        dest_file = os.path.join(dest_path, hdr_name)
        shutil.copyfile(hdr_name, dest_file)

        # Cleanup the working directory
        if os.path.exists(grb_name):
            os.unlink(grb_name)
        if os.path.exists(hdr_name):
            os.unlink(hdr_name)