source: flex_extract.git/python/UIOTools.py @ 64cf353

ctbtodev
Last change on this file since 64cf353 was 64cf353, checked in by Anne Philipp <bscannephilipp@…>, 6 years ago

pep8 changes + documentation added + minor code style changes

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[64cf353]1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
[507d47f]3#************************************************************************
4# TODO AP
[64cf353]5#AP
[507d47f]6# - File name und Klassenname gleichsetzen?
[64cf353]7# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
[507d47f]8# und dann hier implementieren
[64cf353]9# - löschen?
[507d47f]10#************************************************************************
[9ac56ea]11"""
12@Author: Anne Fouilloux (University of Oslo)
13
14@Date: October 2014
15
16@ChangeHistory:
[64cf353]17   February 2018 - Anne Philipp (University of Vienna):
18        - applied PEP8 style guide
19        - added documentation
[9ac56ea]20
21@License:
22    (C) Copyright 2014 UIO.
23
24    This software is licensed under the terms of the Apache Licence Version 2.0
25    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
[507d47f]26
[64cf353]27@Requirements:
28    - A standard python 2.6 or 2.7 installation
29    - dateutils
30    - matplotlib (optional, for debugging)
31    - ECMWF specific packages, all available from https://software.ecmwf.int/
32        ECMWF WebMARS, gribAPI with python enabled, emoslib and
33        ecaccess web toolkit
34
35@Description:
36    Further documentation may be obtained from www.flexpart.eu.
[ab3dbdd]37
[64cf353]38    Functionality provided:
39        Prepare input 3D-wind fields in hybrid coordinates +
40        surface fields for FLEXPART runs
41"""
[ab3dbdd]42# ------------------------------------------------------------------------------
43# MODULES
44# ------------------------------------------------------------------------------
[d69b677]45import os
46import glob
47
[ab3dbdd]48# ------------------------------------------------------------------------------
49# Class
50# ------------------------------------------------------------------------------
[d69b677]51class UIOFiles:
[507d47f]52    '''
53    Class to manipulate files. At initialisation it has the attribute
54    suffix which stores a list of suffixes of the files associated
55    with the instance of the class.
56    '''
[ab3dbdd]57    # --------------------------------------------------------------------------
58    # FUNCTIONS
59    # --------------------------------------------------------------------------
[507d47f]60    def __init__(self, suffix):
61        '''
62        @Description:
63            Assignes the suffixes of the files which should be
64            associated with the instance of the class.
65
66        @Input:
67            self: instance of UIOFiles
68                Description see class documentation.
69
70            suffix: list of strings
71                Types of files to manipulate such as
[64cf353]72                ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2']
[507d47f]73
74        @Return:
75            <nothing>
76        '''
77        self.suffix = suffix
78        return
79
80    def listFiles(self, pathname, pattern):
81        '''
82        @Description:
83            Lists all files in the directory with the matching
84            regular expression pattern. The suffixes are already stored
85            in a list attribute "suffix".
86
87        @Input:
88            self: instance of UIOFiles
89                Description see class documentation.
90
91            pathname: string
92                Directory where to list the files.
93
94            pattern: string
95                Regular expression pattern. For example:
96                '*OG_acc_SL*.'+c.ppid+'.*'
97
98        @Return:
99            <nothing>
100        '''
[64cf353]101#AP pathname zu path ändern
102#AP is it possible for each possible file extension ? mabye regexx?
[507d47f]103        # Get the absolute path of the pathname parameter
[d69b677]104        pathname = os.path.abspath(pathname)
[507d47f]105
106        # Get a list of files in pathname
107        filesInCurDir0 = glob.glob(pathname + '/' + pattern)
108        filesInCurDir = []
[d69b677]109        for f in filesInCurDir0:
110            filesInCurDir.append(f.split('/')[-1])
111        self.counter = 0
112        self.files = []
[507d47f]113        # Traverse through all files
[d69b677]114        for file in filesInCurDir:
115            curFile = os.path.join(pathname, file)
[507d47f]116
117            # Check if it's a normal file or directory
[d69b677]118            if os.path.isfile(curFile):
119                # Get the file extension
[507d47f]120                fileNoExt, curFileExtension = os.path.splitext(curFile)
[d69b677]121                # Check if the file has an extension of typical video files
122                if curFileExtension in self.suffix:
123                    # We have got a file file! Increment the counter
124                    self.counter += 1
125                    # add this filename in the list
126                    self.files.append(curFile)
127            else:
128                # We got a directory, enter into it for further processing
129                self.listFiles(curFile)
130
131        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG