From 7bdfac90351d4b266e3009a2390d06a54f6888cb Mon Sep 17 00:00:00 2001 From: Minna Win Date: Thu, 5 Dec 2019 23:27:21 +0000 Subject: [PATCH] Github issue #331 part 2 of issue: remove the creation of a tmp subdirectory that is the current process ID and remove existing tmp file before appending to it. This will prevent appending identical information (and headers) to the tmp file for a given storm, when re-running extract tiles with existing tc pairs output. This will result in an error as it is assumed that the tmp file will only have one header. --- ush/extract_tiles_wrapper.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ush/extract_tiles_wrapper.py b/ush/extract_tiles_wrapper.py index 65b280e5b..2724edb6a 100755 --- a/ush/extract_tiles_wrapper.py +++ b/ush/extract_tiles_wrapper.py @@ -72,8 +72,9 @@ def run_at_time(self, input_dict): # get the process id to be used to identify the output # amongst different users and runs. - cur_pid = str(os.getpid()) - tmp_dir = os.path.join(self.config.getdir('TMP_DIR'), cur_pid) + # cur_pid = str(os.getpid()) + #tmp_dir = os.path.join(self.config.getdir('TMP_DIR'), cur_pid) + tmp_dir = self.config.getdir('TMP_DIR') self.logger.info("Begin extract tiles") cur_init = init_time[0:8]+"_"+init_time[8:10] @@ -140,6 +141,13 @@ def run_at_time(self, input_dict): full_tmp_filename = os.path.join(tmp_dir, tmp_filename) storm_match_list = util.grep(cur_storm, filter_name) + + # If this tmp file already exists in the tmp directory, remove + # it first otherwise we end up repeatedly appending the + # same information to this tmp file, which can have dire consequences. + if os.path.exists(full_tmp_filename): + os.remove(full_tmp_filename) + with open(full_tmp_filename, "a+") as tmp_file: # copy over header information tmp_file.write(header) @@ -156,9 +164,10 @@ def run_at_time(self, input_dict): # end of for cur_storm # Remove any empty files and directories in the extract_tiles output - # directory + # directory and clean up any tmp files util.prune_empty(self.filtered_out_dir, self.logger) + # Clean up the tmp directory if it exists if os.path.isdir(tmp_dir): util.rmtree(tmp_dir)