#!/usr/bin/env python # -*- coding: utf-8 -*- #************************************************************************ # TODO AP #AP # - File name und Klassenname gleichsetzen? # - checken welche regelmässigen methoden auf diese Files noch angewendet werden # und dann hier implementieren # - löschen? #************************************************************************ """ @Author: Anne Fouilloux (University of Oslo) @Date: October 2014 @ChangeHistory: February 2018 - Anne Philipp (University of Vienna): - applied PEP8 style guide - added documentation @License: (C) Copyright 2014 UIO. This software is licensed under the terms of the Apache Licence Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. @Requirements: - A standard python 2.6 or 2.7 installation - dateutils - matplotlib (optional, for debugging) - ECMWF specific packages, all available from https://software.ecmwf.int/ ECMWF WebMARS, gribAPI with python enabled, emoslib and ecaccess web toolkit @Description: Further documentation may be obtained from www.flexpart.eu. Functionality provided: Prepare input 3D-wind fields in hybrid coordinates + surface fields for FLEXPART runs """ # ------------------------------------------------------------------------------ # MODULES # ------------------------------------------------------------------------------ import os import glob # ------------------------------------------------------------------------------ # Class # ------------------------------------------------------------------------------ class UIOFiles: ''' Class to manipulate files. At initialisation it has the attribute suffix which stores a list of suffixes of the files associated with the instance of the class. ''' # -------------------------------------------------------------------------- # FUNCTIONS # -------------------------------------------------------------------------- def __init__(self, suffix): ''' @Description: Assignes the suffixes of the files which should be associated with the instance of the class. @Input: self: instance of UIOFiles Description see class documentation. suffix: list of strings The types of files which should be manipulated such as ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2'] @Return: ''' self.suffix = suffix return def listFiles(self, path, pattern): ''' @Description: Lists all files in the directory with the matching regular expression pattern. The suffixes are already stored in a list attribute "suffix". @Input: self: instance of UIOFiles Description see class documentation. path: string Directory where to list the files. pattern: string Regular expression pattern. For example: '*OG_acc_SL*.'+c.ppid+'.*' @Return: ''' # Get the absolute path path = os.path.abspath(path) # Get a list of files in pathname filesInCurDir0 = glob.glob(path + '/' + pattern) filesInCurDir = [] for f in filesInCurDir0: filesInCurDir.append(f.split('/')[-1]) self.counter = 0 self.files = [] # Traverse through all files for file in filesInCurDir: curFile = os.path.join(path, file) # Check if it's a normal file or directory if os.path.isfile(curFile): # Get the file extension fileNoExt, curFileExtension = os.path.splitext(curFile) # Check if the file has an extension of typical video files if curFileExtension in self.suffix: # We have got a file file! Increment the counter self.counter += 1 # add this filename in the list self.files.append(curFile) else: # We got a directory, enter into it for further processing self.listFiles(curFile) return