diff --git a/antarctica_today/compute_mean_climatology.py b/antarctica_today/compute_mean_climatology.py index 53a01f2..5183cd3 100644 --- a/antarctica_today/compute_mean_climatology.py +++ b/antarctica_today/compute_mean_climatology.py @@ -247,9 +247,8 @@ def read_daily_sum_melt_averages_picklefile( ) logger.debug(f"Reading {daily_sum_picklefile}") - f = open(daily_sum_picklefile, "rb") - array, dt_dict = pickle.load(f) - f.close() + with open(daily_sum_picklefile, "rb") as f: + array, dt_dict = pickle.load(f) return array, dt_dict diff --git a/antarctica_today/generate_antarctica_today_map.py b/antarctica_today/generate_antarctica_today_map.py index dceede0..ff98d70 100644 --- a/antarctica_today/generate_antarctica_today_map.py +++ b/antarctica_today/generate_antarctica_today_map.py @@ -548,9 +548,8 @@ def _generate_new_baseline_map_figure( if save_to_picklefile: fname = map_picklefile_dictionary[(map_type_lower, region_number)] - f = open(fname, "wb") - pickle.dump(fig, f) - f.close() + with open(fname, "wb") as f: + pickle.dump(fig, f) logger.debug(f"Wrote {fname}") return fig, ax @@ -571,9 +570,9 @@ def _read_baseline_map_picklefile(self, map_type="daily", region_number=0): logger.debug(f"Reading {fname}") # Read the picklefile - f = open(fname, "rb") - fig = pickle.load(f) - f.close() + with open(fname, "rb") as f: + fig = pickle.load(f) + # Get the axes, should just be one panel here. ax = fig.axes[0] # Set the current axes to ax diff --git a/antarctica_today/update_data.py b/antarctica_today/update_data.py index 3bc3126..a58db22 100644 --- a/antarctica_today/update_data.py +++ b/antarctica_today/update_data.py @@ -310,9 +310,8 @@ def update_everything_to_latest_date( dt_dict[dt] = previous_melt_array.shape[2] + i if overwrite: - f = open(tb_file_data.model_results_picklefile, "wb") - pickle.dump((melt_array_updated, dt_dict), f) - f.close() + with open(tb_file_data.model_results_picklefile, "wb") as f: + pickle.dump((melt_array_updated, dt_dict), f) logger.info(f"Wrote {tb_file_data.model_results_picklefile}") diff --git a/antarctica_today/write_flat_binary.py b/antarctica_today/write_flat_binary.py index b58b711..8bda5ef 100644 --- a/antarctica_today/write_flat_binary.py +++ b/antarctica_today/write_flat_binary.py @@ -36,35 +36,34 @@ def write_array_to_binary( byteorder = "big" # Open the output file name. - f = open(bin_filename, "wb") - - # Convert the number of bytes into the correct numpy array datatype. - if signed: - n_dtype = {1: numpy.int8, 2: numpy.int16, 4: numpy.int32, 8: numpy.int64}[ - int(numbytes) - ] - else: - n_dtype = {1: numpy.uint8, 2: numpy.uint16, 4: numpy.uint32, 8: numpy.uint64}[ - int(numbytes) - ] - - # Converte the array into the appropriate data type, and multiply by the multiplier - out_array = numpy.array(array * multiplier, dtype=n_dtype) - - # Flatten the array. - out_array = out_array.flatten() - - for value in out_array: - f.write( - int.to_bytes( - int(value), length=numbytes, byteorder=byteorder, signed=signed + with open(bin_filename, "wb") as f: + # Convert the number of bytes into the correct numpy array datatype. + if signed: + n_dtype = {1: numpy.int8, 2: numpy.int16, 4: numpy.int32, 8: numpy.int64}[ + int(numbytes) + ] + else: + n_dtype = { + 1: numpy.uint8, + 2: numpy.uint16, + 4: numpy.uint32, + 8: numpy.uint64, + }[int(numbytes)] + + # Converte the array into the appropriate data type, and multiply by the multiplier + out_array = numpy.array(array * multiplier, dtype=n_dtype) + + # Flatten the array. + out_array = out_array.flatten() + + for value in out_array: + f.write( + int.to_bytes( + int(value), length=numbytes, byteorder=byteorder, signed=signed + ) ) - ) - - f.close() logger.debug(f"Wrote {os.path.split(bin_filename)[-1]}") - return bin_filename