ftpopar.py 2.04 KB
Newer Older
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
1 2 3 4
import os
import configobj
import datetime

5 6 7
import wimcollect.common.logger as logger
import wimcollect.common.ftp as ftp
import wimcollect.common.utils as utils
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
8 9


10
class Collector(utils.LogConfig):
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
11

12
    def __init__(self, config_parser: configobj.ConfigObj = None, log: logger = None):
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
13
        self.object_id = "FTPOPAR"
14 15
        utils.LogConfig.__init__(self, self.object_id, config_parser, log)
        self.distant_base_dir = self.config[self.object_id]["distant_base_dir"]
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
16

17
    def download_maido_ftir(self, day: datetime.date):
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
18 19 20 21 22 23
        """Download all Picarro files from FTP server.

        The distant files will be deleted from the server if the transfer is successful.

        Parameters
        ----------
24 25
        day: datetime.date
            Date of the data which should be downloaded.
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
26 27 28
        """
        self.logger.write(self.object_id, "Download Maido's FTIR meteo data.")

29 30
        date_str = day.strftime("%Y%m%d")
        # Build source and destination file paths
31
        source_filepath = self.distant_base_dir + "/" + date_str + "Meteo125HR.xls"
32
        dest_filepath = self.__get_dest_filepath__(day)
33 34

        # Download
35 36
        success = ftp.ftp_download_file(source_filepath, dest_filepath, self.logger, self.object_id,
                                        ftp_config=self.config[self.object_id])
37 38

        # Compress
39
        if success:
40 41 42
            self.logger.write(self.object_id, date_str + ": Compressing ...")
            utils.compress_file(dest_filepath)
            self.logger.write(self.object_id, date_str + ": Done. Archive file: " + dest_filepath)
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56

    def __get_dest_filepath__(self, day: datetime.date) -> str:
        # Build destination directory
        dest_dir = os.path.join(self.config["LOCAL"]["base_dir"], "REU", "meteo", "maidoftir")
        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)

        # Build destination filename
        filename = "REU_maidoftir_" + day.strftime("%Y%m%d") + ".tsv"

        # Build full file path
        dest_filepath = os.path.join(dest_dir, filename)

        return dest_filepath