Create your own file handler¶
File handlers are objects that can read or write files in a specific format.
The formats can be very common like NetCDF4 or very specified ones like the one
from SEVIRI HDF level 1.5 files. File handlers are required by the
FileSet object to load data from files or to
store data back to files. File handlers can provide up to three things:
- Reading of files and returning their content.
- Writing data to files in a specific format.
- Getting information about a file that cannot be retrieved from the file
File handlers for the most common and some specific instrument formats are already included in the typhon package; have a look at Handlers.
However, it might happen that you need to build your own file handler object. This tutorial shows to you how one should do it.
Your file handler needs a read, write and a get_info method even if it does not implement all of them.
|FileHandler.read()||Opens and reads a file.|
|FileHandler.write()||Writes data to a file.|
|FileHandler.get_info()||Gets information (e.g. time coverage) of a file.|
The best start is to inherit from the generic
# Import the file handler base class from typhon.files import FileHandler class YourFileHandler(FileHandler): pass
To provide reading, you need to implement a read method. It should take a
FileInfo object as a first argument and
return an object that holds the read content of the file (e.g. a
# Import the file handler base class from typhon.files import FileHandler class YourFileHandler(FileHandler): def read(self, file_info): """Simple reader that returns the first line of an ASCII file""" with open(file_info) as file: return file.readline()
to be continued