Module: file_utils#

A collection of utility functions for working with files in KBMOD.

class kbmod.file_utils.FileUtils[source]#

A class of static methods for working with KBMOD files.

Examples

  • Load an external file of visit ID to timestamp mappings.

time_dict = FileUtils.load_time_dictionary("kbmod/data/demo_times.dat")

  • Load the results of a KBMOD run as trajectory objects.

FileUtils.load_results_file_as_trajectories("results_DEMO.txt")

  • Make a filename safe.

FileUtils.make_safe_filename("my string, is here")

Methods

format_result_mpc(coords, t[, observatory])

This method will take a single result in and return a corresponding MPC formatted string.

load_csv_to_list(file_name[, use_dtype, ...])

Load a CSV file to a list of numpy arrays.

load_psf_dictionary(psf_file)

Load a OrderedDict mapping visit_id to PSF.

load_results_file(filename)

Load the result trajectories.

load_results_file_as_trajectories(filename)

Load the result trajectories.

load_time_dictionary(time_file)

Load a OrderedDict mapping visit_id to time stamp.

make_safe_filename(s)

Makes a safe file name out of an arbitrary string.

mpc_reader(filename)

Read in a file with observations in MPC format and return the coordinates.

save_csv_from_list(file_name, data[, overwrite])

Save a CSV file from a list of lists.

save_results_file(filename, results)

Save the result trajectories to a file.

save_results_mpc(file_out, coords, times[, ...])

Save the MPC-formatted observations to file.

save_time_dictionary(time_file_name, ...)

Save the mapping of visit_id -> time stamp to a file.

trajectory_from_np_object(result)

Transform a numpy object holding trajectory information into a trajectory object.

visit_from_file_name(filename)

Automatically extract the visit ID from the file name.

static format_result_mpc(coords, t, observatory='X05')[source]#

This method will take a single result in and return a corresponding MPC formatted string.

Parameters:
coordsSkyCoord

The sky coordinates of the observation.

tTime

The time of the observation as an astropy Time object.

observatorystring

The three digit observatory code to use.

Returns:
mpc_line: string

An MPC-formatted string of the observation

static load_csv_to_list(file_name, use_dtype=None, none_if_missing=False)[source]#

Load a CSV file to a list of numpy arrays.

Parameters:
file_namestr

The full path and file name of the data.

use_dtypetype

The numpy array dtype to use.

none_if_missingbool

Return None if the file is missing. The default is to raise an exception if the file is missing.

Returns:
datalist of numpy arrays

The data loaded.

static load_psf_dictionary(psf_file)[source]#

Load a OrderedDict mapping visit_id to PSF.

Parameters:
psf_filestr

The path and name of the PSF file.

Returns:
psf_dictOrderedDict

A mapping of visit ID to psf value.

static load_results_file(filename)[source]#

Load the result trajectories.

Parameters:
filenamestr

The filename of the results.

Returns:
resultsnp array

A np array with the result trajectories.

static load_results_file_as_trajectories(filename)[source]#

Load the result trajectories.

Parameters:
filenamestr

The full path and filename of the results.

Returns:
resultslist

A list of trajectory objects

static load_time_dictionary(time_file)[source]#

Load a OrderedDict mapping visit_id to time stamp.

Parameters:
time_filestr

The path and name of the time file.

Returns:
image_time_dictOrderedDict

A mapping of visit ID to time stamp.

static make_safe_filename(s)[source]#

Makes a safe file name out of an arbitrary string.

Preserves the separators (spaces, commas, tabs, etc.) with underscores and removes all other non-alphanumeric characters.

Parameters:
sstring

The input string

Returns:
resstring

The output string

static mpc_reader(filename)[source]#

Read in a file with observations in MPC format and return the coordinates.

Parameters:
filename: str

The name of the file with the MPC-formatted observations.

Returns:
coords: astropy SkyCoord object

A SkyCoord object with the ra, dec of the observations.

times: astropy Time object

Times of the observations

static save_csv_from_list(file_name, data, overwrite=False)[source]#

Save a CSV file from a list of lists.

Parameters:
file_namestr

The full path and file name for the result.

datalist

The data to save.

overwritebool

A Boolean indicating whether to overwrite the files or raise an exception on file existance.

static save_results_file(filename, results)[source]#

Save the result trajectories to a file.

Parameters:
filenamestr

The filename of the results.

resultslist

list of trajectory objects.

static save_results_mpc(file_out, coords, times, observatory='X05')[source]#

Save the MPC-formatted observations to file.

Parameters:
file_out: str

The output filename with the MPC-formatted observations of the KBMOD search result.

coordslist of SkyCoord

A list of sky coordinates (SkyCoord objects) of the observation.

tlist of Time

A list of times for each observation.

observatorystring

The three digit observatory code to use.

static save_time_dictionary(time_file_name, time_mapping)[source]#

Save the mapping of visit_id -> time stamp to a file.

Parameters:
time_file_namestr

The path and name of the time file.

time_mappingdict or OrderedDict

The mapping of visit ID to time stamp.

static trajectory_from_np_object(result)[source]#

Transform a numpy object holding trajectory information into a trajectory object.

Parameters:
resultnp object

The result object loaded by numpy.

Returns:
trjtrajectory

The corresponding trajectory object.

static visit_from_file_name(filename)[source]#

Automatically extract the visit ID from the file name.

Uses the heuristic that the visit ID is the first numeric string of at least length 5 digits in the file name.

Parameters:
filenamestr

The file name

Returns:
resultstr

The visit ID string or None if there is no match.