def hatanaka_decompress(local_file): """Hatanaka decompresses a local file using the CRX2RNX program Outputs data to new file with correct name under same directory as input Input: local_file path to Hatanaka compressed RINEX file Returns: new_name name of created decompressed RINEX file """ # Check if CRX2RNX is in /tmp - where Lambda instances are throttled if os.path.isfile('/tmp/CRX2RNX'): CRX2RNX = Executable('/tmp/CRX2RNX', True) else: CRX2RNX = Executable('lib/executables/CRX2RNX') rinex_data = CRX2RNX.run('{} -'.format(local_file)) if CRX2RNX.returncode > 0: raise Exception('CRX2RNX failed with error code {}: {}'.format( CRX2RNX.returncode, CRX2RNX.stderr)) # RINEX 3 file extension changes from crx to rnx when decompressed new_name = local_file.replace('.crx', '.rnx') # Hatanaka compressed RINEX 2 files are suffixed with d, replace with o if new_name == local_file: new_name = local_file[:-1] + 'o' with open(new_name, 'w') as out_file: out_file.write(rinex_data) return new_name
def lambda_handler(event, context): user_str = 'aaa' exe_file = 'hello' try: # API Gateway GET method if event['httpMethod'] == 'GET': user_str = event['queryStringParameters']['str'] exe_file = event['queryStringParameters']['bin'] # API Gateway POST method elif event['httpMethod'] == 'POST': data = json.loads(event['body']) user_str = data['str'] exe_file = data['bin'] except KeyError: # direct invocation user_str = event['str'] exe_file = event['bin'] user_str = '\"' + user_str + '\"' exe = Executable('executables/' + exe_file) result = exe.run(user_str) print('OUT: {}\nERR: {}\nRET: {}'.format(exe.stdout, exe.stderr, exe.returncode)) out = { "headers": { "content-type": "application/json", "Access-Control-Allow-Origin": "*" }, "body": exe.stdout, "statusCode": 200 } return out
def main(): current_app.logger.info("Received request") msg = '---HEADERS---\n{}\n--BODY--\n{}\n-----\n'.format( request.headers, request.get_data()) current_app.logger.info(msg) # Get request body reqStr = request.get_data().decode("utf-8") current_app.logger.info(reqStr) # Call executable binary exe = Executable(os.path.join(MYPATH, 'hello')) result = exe.run(reqStr) current_app.logger.info('OUT: {}\nERR: {}\nRET: {}'.format( exe.stdout, exe.stderr, exe.returncode)) return result, 200
MYPATH = os.path.dirname(os.path.realpath(__file__)) def main(): current_app.logger.info("Received request") msg = '---HEADERS---\n{}\n--BODY--\n{}\n-----\n'.format( request.headers, request.get_data()) current_app.logger.info(msg) # Get request body reqStr = request.get_data().decode("utf-8") current_app.logger.info(reqStr) # Call executable binary exe = Executable(os.path.join(MYPATH, 'hello')) result = exe.run(reqStr) current_app.logger.info('OUT: {}\nERR: {}\nRET: {}'.format( exe.stdout, exe.stderr, exe.returncode)) return result, 200 if __name__ == "__main__": reqStr = "haha" exe = Executable('./hello') result = exe.run(reqStr) print('OUT: {}\nERR: {}\nRET: {}'.format(exe.stdout, exe.stderr, exe.returncode)) print(result)
def lambda_handler(event, context): # Get the file object and bucket names from the event bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus( event['Records'][0]['s3']['object']['key']).decode('utf8') print('Quality Check: Key {}'.format(key)) status, file_type, data_type, year, day = key.split('/')[:5] if data_type == 'nav': nav_file = os.path.basename(key) if nav_file[:4].lower() == 'brdc' and nav_file[-4:] == 'n.gz': triggerQCFromNav(year, day, context.function_name, bucket) else: print('Do not Quality Check using non-Broadcast Navigation data') return # Use AWS request ID from context object for unique directory session_id = context.aws_request_id local_path = '/tmp/{}'.format(session_id) if not os.path.exists(local_path): os.makedirs(local_path) try: response = S3.get_object(Bucket=bucket, Key=key) except Exception as err: # This should only fail more than once if permissions are incorrect print('Error: Failed to get object {} from bucket {}.'.format( key, bucket)) raise err # Decompress Observation file and store locally filename, extension = os.path.splitext(os.path.basename(key)) local_file = os.path.join(local_path, filename) file_data = zlib.decompress(response['Body'].read(), 15+32) with open(local_file, 'wb') as out_file: out_file.write(file_data) # Parse RINEX file rinex_obs = RINEXData(local_file) # Attempt to get Broadcast Navigation file from archive nav_file = getBRDCNavFile(bucket, rinex_obs.start_time, local_path) if nav_file == None: print('Daily BRDC file does not yet exist for {}/{}'.format( year, day)) return # Hatanaka decompress RINEX file if needed if rinex_obs.compressed == True: rinex_obs.local_file = hatanaka_decompress(rinex_obs.local_file) # Generate an Anubis XML config file anubis_config, result_file = generateQCConfig( rinex_obs, nav_file, local_path) # Run Anubis with the generated config file as input anubis = Executable('lib/executables/anubis-2.0.1') anubis_log = anubis.run('-x {}'.format(anubis_config)) if anubis.returncode > 0: print('Anubis errored with return code {}: {}\n{}'.format( anubis.returncode, anubis.stderr, anubis.stdout)) return # Parse results of Anubis parseQCResult(result_file, key) # Delete tmp working space and Anubis copy to resolve Lambda disk # space allocation issue shutil.rmtree(local_path) return