core¶
The core
module houses functions that assist in data formatting, input sanitation,
path manipulations, file naming, logical checks, etc for use in other functions within
dnppy. They are short and sweet, and can be used as examples to start defining your own functions!
Examples¶
Listing files in a directory
Perhaps the function with the greatest utility in the core module is the list_files function, which can crawl through a directory, or entire tree of directories (recursive) and find files that match a certain criteria in a beginner friendly way. The syntax is as follows
from dnppy import core
recursive = True # will find files in directories subfolders
directory = r"C:\mydir" # the directory that we would like to search
contains = ["this","that"] # files must contain these strings
not_contain = ["otherthing"] # files must NOT contain these strings
filelist = core.list_files(recursive, directory, contains, not_contain)
Note that both contains
and not_contain
may be either single strings, or lists of strings. This is a good way to filter down by NASA EOS product type, Landsat band index, or perhaps filter out files by extension. Users wishing to get a little more advanced should check out regular expressions.
Code Help¶
Auto-documentation for functions and classes within this module is generated below!
-
create_outname
(outdir, inname, suffix, ext=False)[source]¶ Quick way to create unique output file names within iterative functions
This function is built to simplify the creation of output file names. Function allows
outdir = False
and will create an outname in the same directory as inname. Function will add a the user input suffix, separated by an underscore “_” to generate an output name. this is useful when performing a small modification to a file and saving new output with a new suffix. Function merely returns an output name, it does not save the file as that name.Parameters: - outdir – either the directory of the desired outname or False to create an outname in the same directory as the inname
- inname – the input file from which to generate the output name “outname”
- suffix – suffix to attach to the end of the filename to mark it as output
- ext – specify the file extension of the output filename. Leave blank or False and the outname will inherit the same extension as inname.
Return outname: the full filepath at which a new file can be created.
-
enf_filelist
(filelist, extension=None)[source]¶ Sanitizes file list inputs
This function checks that the input is a list of files and not a directory. If the input is a directory, then it returns a list of files in the directory which match the desired extension. This is to allow all functions which input filelists to be a little more flexible by accepting directories instead.
Parameters: - filelist – a list of filepath strings
- extension – output list contains only files with this string extension. (txt, tif, etc)
Return new_filelist: sanitized file list
-
enf_list
(item)[source]¶ When a list is expected, this function can be used to ensure non-list data types are placed inside of a single entry list.
Parameters: item – any datatype Return list: a list type
-
exists
(location)[source]¶ Ensures input location is either a file or a folder
Parameters: location – a filepath to a directory or file Return bool: returns true if filepath leads to a real place
-
install_from_wheel
(whl_filepath)[source]¶ This script can be used to add python libraries to the arcmap installation of python from wheel files. A great source of wheel files can be found at [http://www.lfd.uci.edu/~gohlke/pythonlibs/]
Parameters: whl_filepath – the full local filepath to a downloaded wheel file.
-
list_files
(recursive, directory, contains=None, not_contains=None)[source]¶ Simple file listing function with more versatility than python builtins or arcpy.List
This function sifts through a directory and returns a list of filepaths for all files meeting the input criteria. Useful for discriminatory iteration or recursive searches. Could be used to find all tiles with a given datestring such as ‘MOD11A1.A2012’, or perhaps all Band 4 tiles from a directory containing landsat 8 data.
Parameters: - recursive – ‘True’ if search should search subfolders within the directory ‘False’ if search should ignore files in subfolders.
- directory – The directory in which to search for files meeting the criteria
- contains – search criteria to limit returned file list. File names must contain parameters listed here. If no criteria exists use ‘False’
- not_contains – search criteria to limit returned file list. File names must not contain parameters listed here. If no criteria exists use ‘False’
Return filelist: An array of full filepaths meeting the criteria.
-
move
(source, destination)[source]¶ moves a file, ensures destination directory exists
Parameters: - source – the current full location of the file
- destination – the desired full location of the file
-
rename
(filename, replace_this, with_this)[source]¶ renames a file
Parameters: - filename – input file to rename
- replace_this – string to be replaced. such as ” ” (a space)
- with_this – what to replace the string with. such as “_” (an underscore)
Return newfilename: the new name of the file.
-
run_command
(*command)[source]¶ This function formats and runs commands that one would call from the console. Primarily intended for calling gdal commands from other functions in cases where the python bindings are absent. Particularly useful for commands with list type arguments. This function provides simplicity, but users wanting more functionality should use the subprocess module directly.
Parameters: command – command can be virtually any number of string arguments in any configuration of args, lists, and tuples. This function will take all input strings in the order in which they are given and place a space " "
between each argument before passing it to the command line.# all of these are valid syntax core.run_command(arg1) core.run_command(arg1, arg2) core.run_command([arg1, arg2]) core.run_command(arg1, [arg2, arg3]) core.run_command(arg1, [arg2, arg3, [arg4, arg5]], (arg7, arg8))