[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] | 45 | import os |
---|
| 46 | import glob |
---|
| 47 | |
---|
[ab3dbdd] | 48 | # ------------------------------------------------------------------------------ |
---|
| 49 | # Class |
---|
| 50 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 51 | class 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 |
---|
[02c8c50] | 71 | The types of files which should be manipulated such as |
---|
[64cf353] | 72 | ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2'] |
---|
[507d47f] | 73 | |
---|
| 74 | @Return: |
---|
| 75 | <nothing> |
---|
| 76 | ''' |
---|
[02c8c50] | 77 | |
---|
[507d47f] | 78 | self.suffix = suffix |
---|
[02c8c50] | 79 | |
---|
[507d47f] | 80 | return |
---|
| 81 | |
---|
[02c8c50] | 82 | def listFiles(self, path, pattern): |
---|
[507d47f] | 83 | ''' |
---|
| 84 | @Description: |
---|
| 85 | Lists all files in the directory with the matching |
---|
| 86 | regular expression pattern. The suffixes are already stored |
---|
| 87 | in a list attribute "suffix". |
---|
| 88 | |
---|
| 89 | @Input: |
---|
| 90 | self: instance of UIOFiles |
---|
| 91 | Description see class documentation. |
---|
| 92 | |
---|
[02c8c50] | 93 | path: string |
---|
[507d47f] | 94 | Directory where to list the files. |
---|
| 95 | |
---|
| 96 | pattern: string |
---|
| 97 | Regular expression pattern. For example: |
---|
| 98 | '*OG_acc_SL*.'+c.ppid+'.*' |
---|
| 99 | |
---|
| 100 | @Return: |
---|
| 101 | <nothing> |
---|
| 102 | ''' |
---|
[02c8c50] | 103 | |
---|
| 104 | # Get the absolute path |
---|
| 105 | path = os.path.abspath(path) |
---|
[507d47f] | 106 | |
---|
| 107 | # Get a list of files in pathname |
---|
[02c8c50] | 108 | filesInCurDir0 = glob.glob(path + '/' + pattern) |
---|
[507d47f] | 109 | filesInCurDir = [] |
---|
[d69b677] | 110 | for f in filesInCurDir0: |
---|
| 111 | filesInCurDir.append(f.split('/')[-1]) |
---|
[02c8c50] | 112 | |
---|
[d69b677] | 113 | self.counter = 0 |
---|
| 114 | self.files = [] |
---|
[507d47f] | 115 | # Traverse through all files |
---|
[d69b677] | 116 | for file in filesInCurDir: |
---|
[02c8c50] | 117 | curFile = os.path.join(path, file) |
---|
[507d47f] | 118 | |
---|
| 119 | # Check if it's a normal file or directory |
---|
[d69b677] | 120 | if os.path.isfile(curFile): |
---|
| 121 | # Get the file extension |
---|
[507d47f] | 122 | fileNoExt, curFileExtension = os.path.splitext(curFile) |
---|
[d69b677] | 123 | # Check if the file has an extension of typical video files |
---|
| 124 | if curFileExtension in self.suffix: |
---|
| 125 | # We have got a file file! Increment the counter |
---|
| 126 | self.counter += 1 |
---|
| 127 | # add this filename in the list |
---|
| 128 | self.files.append(curFile) |
---|
| 129 | else: |
---|
| 130 | # We got a directory, enter into it for further processing |
---|
| 131 | self.listFiles(curFile) |
---|
| 132 | |
---|
| 133 | return |
---|