ftpaeris.py 1.9 KB
Newer Older
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
1 2 3
import os
import configobj

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


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

    def __init__(self, config_parser: configobj.ConfigObj = None, log: logger = None):
        self.object_id = "FTPAERIS"
13 14
        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
15 16 17 18 19 20 21 22 23 24 25 26

    def download_maido_mf_1h(self, yyyymm: str):
        """Download hourly monthly-released meteo data of Meteo-France's Maïdo station.

        Parameters
        ----------
        yyyymm: str
            Year and month of the to-be-downloaded data
        """
        self.logger.write(self.object_id, "Download Maido's Meteo-France meteo data.")

        # Build source and destination file paths.
27
        source_filepath = self.distant_base_dir + "/" + "PMAIDO_1h_" + yyyymm + ".csv"
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
28 29 30
        dest_filepath = self.__get_dest_filepath__(yyyymm)

        # Download
31 32
        success = ftp.ftp_download_file(source_filepath, dest_filepath, self.logger, self.object_id,
                                        ftp_config=self.config[self.object_id])
JOSSOUD Olivier's avatar
JOSSOUD Olivier committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

        # Compress
        if success:
            self.logger.write(self.object_id, yyyymm + ": Compressing ...")
            utils.compress_file(dest_filepath)
            self.logger.write(self.object_id, yyyymm + ": Done. Archive file: " + dest_filepath)

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

        # Build destination filename
        filename = "REU_maidomf_" + yyyymm + ".csv"

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

        return dest_filepath