#! /usr/bin/python # USAGE: MosaicFileTxFromTowerToDaq.py # PURPOSE: Performs FTP data-acquisition file transfers from MOSAiC tower computer to the ship computer # AUTHOR: Dan Gottas (NOAA/PSD) import ftplib, time, datetime, os, sys #### begin user defined variables #### LogFilePath = 'C:\\Noaa\\Logs\\MosaicFileTxFromTowerToDaq' FtpServer = '192.168.202.4' FtpPort = 21 FtpUserName = 'mosaic_tower' FtpPassword = 'mosaic_tower' # FTP transfer parameter dictionary # SYNTAX: { SessionName : [ FtpInputPath, OutputPath, BeginHourOffset, EndHourOffset, [ FileTxList ] ] } # DESCRIPTION: SessionName = name of the parameter set (no impact on the file transfers) # FtpPath = source path on the remote FTP server where files to be transferred exist # OutputPath = destination path on the local computer where files will be transferred to # BeginHourOffset = hour offset from the execution time that specifies the begin time of the files to transfer # EndHourOffset = hour offset from the execution time that specifies the end time of the files to transfer # FileTxList = list of files to transfer; dynamic time elements in file name are defined by strftime # FtpTxParmDict = { 'Tower Clasp (02m)' : [ '/Clasp02m' , 'd:/tower/Clasp02m' , 6, 0, ['msc0%y%j%H_raw.txt']], 'Tower Ozone' : [ '/Ozone' , 'd:/tower/Ozone' , 6, 0, ['msc0%y%j%H_raw.txt']], 'Tower Picarro' : [ '/Picarro' , 'd:/tower/Picarro' , 6, 0, ['msc0%y%j%H_raw.txt']], 'Tower CR1000X' : [ '/CR1000X/daily_files' , 'd:/tower/CR1000X/daily_files', 6, 0, ['cr1000x_tower_%m%d%Y_%H%M.dat']], 'Tower CR1000X_now' : [ '/CR1000X' , 'd:/tower/CR1000X' , 6, 0, ['cr1000x_tower.dat']], 'Tower Licor (02m)' : [ '/Licor02m' , 'd:/tower/Licor02m' , 6, 0, ['msc0%y%j%H_raw.txt', 'msc0%y%j%H_stats.txt']], 'Tower Metek (02m)' : [ '/Metek02m' , 'd:/tower/Metek02m' , 6, 0, ['msc0%y%j%H_raw.txt', 'msc0%y%j%H_stats.txt']], 'Tower Metek (06m)' : [ '/Metek06m' , 'd:/tower/Metek06m' , 6, 0, ['msc0%y%j%H_raw.txt', 'msc0%y%j%H_stats.txt']], 'Tower Metek (10m)' : [ '/Metek10m' , 'd:/tower/Metek10m' , 6, 0, ['msc0%y%j%H_raw.txt', 'msc0%y%j%H_stats.txt']], 'Tower Metek (30m)' : [ '/Metek30m' , 'd:/tower/Metek30m' , 6, 0, ['msc0%y%j%H_raw.txt', 'msc0%y%j%H_stats.txt']] } #### end user defined variables #### def WriteMessage(LogFilePath, Message): LogTime = datetime.datetime.utcnow() LogFile = LogFilePath + LogTime.strftime('\\MosaicFileTxFromTowerToDaq_%y%j.log') # output to stdout print(Message) # output to log file with open(LogFile, 'a') as fout: fout.write(Message + '\n') ### main #### InputPathIndex = 0 OutputPathIndex = 1 FileTxBeginHourOffsetIndex = 2 FileTxEndHourOffsetIndex = 3 FileListIndex = 4 WriteMessage(LogFilePath, '\nRunning MosaicFileTxFromTowerToDaq.py on ' + time.ctime() + '\n') # create ftp object try: f = ftplib.FTP() except: WriteMessage(LogFilePath, 'Error creating FTP object',) sys.exit() # connect to ftp server try: f.connect(FtpServer, FtpPort) except: WriteMessage(LogFilePath, 'Error connecting to FTP server ' + FtpServer + ' on port ' + str(FtpPort)) sys.exit() # ftp authentication try: f.login(FtpUserName, FtpPassword) except: WriteMessage(LogFilePath, 'Error logging into FTP server as ' + FtpUserName) sys.exit() WriteMessage(LogFilePath, 'FTP connection connection established with ' + FtpServer + ' on port ' + str(FtpPort) + ' as user ' + FtpUserName) # cycle through each ftp file transfer session for FtpTxSession in sorted(FtpTxParmDict): WriteMessage(LogFilePath, '\nLoading FTP transfer parameters for session \'' + FtpTxSession + '\':\n') # assign dictionary elements FtpBasePath = FtpTxParmDict[FtpTxSession][InputPathIndex] OutputBasePath = FtpTxParmDict[FtpTxSession][OutputPathIndex] FtpTxFileList = FtpTxParmDict[FtpTxSession][FileListIndex] FileTxBeginHourOffset = FtpTxParmDict[FtpTxSession][FileTxBeginHourOffsetIndex] FileTxEndHourOffset = FtpTxParmDict[FtpTxSession][FileTxEndHourOffsetIndex] # create time objects for the begin and end time of the file transfer CurrentTime = datetime.datetime.utcnow() BeginTxTime = CurrentTime - datetime.timedelta(hours=FileTxBeginHourOffset) EndTxTime = CurrentTime - datetime.timedelta(hours=FileTxEndHourOffset) FileTxTime = EndTxTime try: # change directory on the FTP server f.cwd(FtpBasePath) except: WriteMessage(LogFilePath, 'FTP directory ' + FtpBasePath + ' does not exist; skipping') continue # cycle through all file transfer hours while(FileTxTime >= BeginTxTime): # cycle through each file transfer expression for FtpTxFile in FtpTxFileList: File = FileTxTime.strftime(FtpTxFile) InputFile = FtpBasePath + '/' + File OutputFile = OutputBasePath + '/' + File try: # get remote file size try: InputFileSize = f.size(InputFile) except: InputFileSize = 0 # get local file size try: statinfo = os.stat(OutputFile) OutputFileSize = statinfo.st_size except: OutputFileSize = 0 # transfer the file if(InputFileSize > 0): if(InputFileSize != OutputFileSize): # create output path if it doesn't exist if(os.path.isdir(os.path.dirname(OutputFile)) == False): os.makedirs(os.path.dirname(OutputFile)) WriteMessage(LogFilePath, 'Transferring ' + InputFile + ' to ' + OutputFile) # transfer file in binary mode f.retrbinary('RETR ' + InputFile, open(OutputFile,'wb').write) else: WriteMessage(LogFilePath, 'Skipping ' + InputFile + '; local and remote files are the same size') else: WriteMessage(LogFilePath, 'Input file ' + InputFile + ' does not exist; skipping') except: WriteMessage(LogFilePath, 'Error transferring ' + InputFile) # decrease the time by one hour FileTxTime -= datetime.timedelta(hours=1) WriteMessage(LogFilePath, '\nMosaicFileTxFromTowerToDaq.py finished on ' + time.ctime())