Earth's Utilities¶
Detailed documentation on the earthstat.utils
module, a collection of functions designed usually used through building the EarthStat library.
Utilities Overview¶
convertDate(date)
¶
Converts a date string from 'YYYYMMDD' format to a date object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date |
str |
The date string in 'YYYYMMDD' format. |
required |
Returns:
Type | Description |
---|---|
datetime.date |
The corresponding date object. |
Source code in earthstat/utils.py
def convertDate(date):
"""
Converts a date string from 'YYYYMMDD' format to a date object.
Args:
date (str): The date string in 'YYYYMMDD' format.
Returns:
datetime.date: The corresponding date object.
"""
return datetime.strptime(date, '%Y%m%d').date()
extractDateFromFilename(filename)
¶
Extracts a date string in 'YYYYMMDD' format from a filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str |
The name of the file containing a date. |
required |
Returns:
Type | Description |
---|---|
str |
The extracted date string. |
Source code in earthstat/utils.py
def extractDateFromFilename(filename):
"""
Extracts a date string in 'YYYYMMDD' format from a filename.
Args:
filename (str): The name of the file containing a date.
Returns:
str: The extracted date string.
"""
pattern = r'\d{8}'
match = re.search(pattern, filename)
date_str = match.group()
return date_str
loadTiff(directory)
¶
Loads and returns paths to all TIFF files in a specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory |
str |
The directory to search for TIFF files. |
required |
Returns:
Type | Description |
---|---|
list |
A list of paths to the TIFF files found in the directory. |
Source code in earthstat/utils.py
def loadTiff(directory):
"""
Loads and returns paths to all TIFF files in a specified directory.
Args:
directory (str): The directory to search for TIFF files.
Returns:
list: A list of paths to the TIFF files found in the directory.
"""
paths = glob.glob(directory+'/*.tif')
return paths
savedFilePath(file_path)
¶
Extracts and returns the directory and name of a file from its path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str |
The full path to the file. |
required |
Returns:
Type | Description |
---|---|
tuple |
A tuple containing the file's directory and name. |
Source code in earthstat/utils.py
def savedFilePath(file_path):
"""
Extracts and returns the directory and name of a file from its path.
Args:
file_path (str): The full path to the file.
Returns:
tuple: A tuple containing the file's directory and name.
"""
file_dir = os.path.dirname(file_path)
file_name = os.path.basename(file_path)
return file_dir, file_name