#!/usr/bin/python import os import sys import subprocess import datetime import ast import socket import shutil import glob def main(cmdArgs): # This script copies down data McIDAS (python package) # An AREA file is written to a temporary directory based on a # specified MCPATH plus the pid number of the process. # # Once this is completed, the AREA file is moved to the root MCPATH directory. # The AREA file is renamed. Right now this script is designed to copy down each # individual band. It can be updated to handle a multibanded images with very # little work # This changes the input from amqpfind to a dictionary eventInfo = ast.literal_eval(cmdArgs[2]) print (eventInfo) try: import mcidasx except: print ('Unable to find mcidas-x python modules') return # Create a shortIP address for copying data to a unique location ipAddress = socket.gethostname() shortIP = ipAddress.split('.')[0] # Setup PATH os.environ['PATH'] = os.environ['HOME'] + '/mcidas/bin:/home/mcidas/bin:' + os.environ['PATH'] # Setup MCPATH information rootMCPATH = os.environ['HOME'] + '/mcidas/data/' + shortIP tempMCPATH = rootMCPATH + '/' + str(os.getpid()) os.environ['MCPATH'] = tempMCPATH print (tempMCPATH) if not os.path.isdir(tempMCPATH): try: os.makedirs(tempMCPATH) except Exception, e: print 'cannot make tempMCPATH: ', tempMCPATH, e return # # If the server of McIDAS data is not an SDI, you need to update # the information for dataset and server IP # if os.environ['MCIDAS_DATA_SOURCE'] == 'NFS': eventInfo = get_Dataset_info(eventInfo) addeServer = eventInfo['server_ip'] addeDataset = eventInfo['adde_dataset'] addeGroup = addeDataset.split('/')[0] addeDescriptor = addeDataset.split('/')[1] # # This code would be used to determine the datasets when the # data is mounted to a NFS server. # localDataset = ' ABI/TEMP ' mcenv = mcidasx.mcenv() # setup MCTABLE entry checkMCTABLE = os.path.join(rootMCPATH,'MCTABLE.TXT') if not os.path.isfile(checkMCTABLE): datalocString='ADD ' + addeGroup + ' ' + addeServer datalocOut = mcenv.dataloc(datalocString) if datalocOut.retcode != 0: print ('DATALOC command failed: Return Code ',datalocOut.retcode) print (datalocString) print (datalocOut.stderr) return checkRESOLV = os.path.join(rootMCPATH,'RESOLV.SRV') if not os.path.isfile(checkRESOLV): dsserveString='ADD ' + localDataset + ' AREA 1 1' dsserveOut = mcenv.dsserve(dsserveString) if dsserveOut.retcode != 0: print ('DSSERVE command failed: Return Code ',dsserveOut.retcode) print (dsserveString) print (datalocOut.stderr) return # get information for IMGCOPY command dayKeyword = ' DAY= ' + eventInfo['start_time'].split(' ')[0] timeValue = eventInfo['start_time'].split(' ')[1] timeKeyword = ' TIME= ' + timeValue.split('.')[0] band = str(eventInfo['band']) imgcopyString = addeDataset + ' ' + localDataset + ' BAND=' + band + ' SIZE=SAME ' + dayKeyword + timeKeyword + ' OVERRIDE=NO' imgcopyOut = mcenv.imgcopy(imgcopyString) if imgcopyOut.retcode != 0: print ('IMGCOPY command failed: Return Code ',imgcopyOut.retcode) print (imgcopyString) print (imgcopyOut.stderr) return mvAREA(eventInfo,tempMCPATH,rootMCPATH) return def mvAREA(eventInfo, tempMCPATH, rootMCPATH): # satellite_ID-instrument-coverage-band-start_date-start_date.area satID = eventInfo['satellite_ID'] coverage = eventInfo['coverage'] if 'Full' in coverage: coverage = coverage.replace(' ','') elif 'Meso' in coverage: coverage = coverage.replace('-','') band = 'b' + str(eventInfo['band']).zfill(2) dateTimeObj = datetime.datetime.strptime(eventInfo['start_time'], '%Y-%m-%d %H:%M:%S.%f') startTime = dateTimeObj.strftime('%Y%j-%H%M%S') pid = str(os.getpid()) newFileName = os.path.join(rootMCPATH,'-'.join([satID,coverage,band,startTime])+'.area') originalFile = os.path.join(tempMCPATH,'AREA0001') imgcopyFile = os.path.join(tempMCPATH,'imgcopy.ccc') newcopy = os.path.join(rootMCPATH,'-'.join([pid,satID,coverage,band,startTime])+'.imgcopy') try: shutil.move(originalFile,newFileName) except: pass for f in glob.glob(os.path.join(tempMCPATH,'*')): try: os.remove(f) except: pass os.rmdir(tempMCPATH) return def get_Dataset_info(datasetInfo): # Update the McIDAS server IP and ADDE server datasetInfo['server_ip'] = os.environ['MCIDAS_SERVER_IP'] if 'Mesoscale-1' in datasetInfo['coverage']: datasetInfo['adde_dataset'] = 'NFSG16/M1' elif 'Mesoscale-2' in datasetInfo['coverage']: datasetInfo['adde_dataset'] = 'NFSG16/M2' elif 'CONUS' in datasetInfo['coverage']: datasetInfo['adde_dataset'] = 'NFSG16/CONUS' else: datasetInfo['adde_dataset'] = 'NFSG16/FD' return (datasetInfo) if __name__ == '__main__': main(sys.argv)