Skip to content

Commit

Permalink
Initial commit with some basic functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jun 1, 2018
1 parent f33aa2e commit 1dee744
Show file tree
Hide file tree
Showing 9 changed files with 84,458 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include bfutils/data/*
10 changes: 10 additions & 0 deletions bfutils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
from bfutils import constants
from bfutils import file

# Read the config file
import yaml
with open( constants.CONFIG_YAML, 'r' ) as f:
constants.CONFIG = yaml.safe_load( f )
del yaml

28 changes: 28 additions & 0 deletions bfutils/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

__author__ = 'Landon T. Clipp'
__email__ = 'clipp2@illinois.edu'

ROOT_DIR = os.path.dirname( os.path.abspath( __file__ ) )
DATA_DIR = os.path.join( ROOT_DIR, 'data' )

ORBIT_INFO_TXT = os.path.join( DATA_DIR, 'Orbit_Path_Time.txt' )
ORBIT_INFO_JSON = os.path.join( DATA_DIR, 'Orbit_Path_Time.json' )
CONFIG_YAML = os.path.join( DATA_DIR, 'config.yml' )

# CONFIG will be initialized with the values in config.yml
CONFIG = None

MOP_re='^MOP01-[0-9]+-L[0-9]+V[0-9]+.[0-9]+.[0-9]+.he5$'
CER_re='^CER_SSF_Terra-FM[0-9]-MODIS_Edition[0-9]+A_[0-9]+.[0-9]+$'
MOD_re='^MOD0((21KM)|(2HKM)|(2QKM)|(3)).A[0-9]+.[0-9]+.[0-9]+.[0-9]+.hdf$'
AST_re='^AST_L1T_[0-9]+_[0-9]+_[0-9]+.hdf$'
MIS_re_GRP='^MISR_AM1_GRP_ELLIPSOID_GM_P[0-9]{3}_O[0-9]+_(AA|AF|AN|BA|BF|CA|CF|DA|DF)_F[0-9]+_[0-9]+.hdf$'
MIS_re_AGP='^MISR_AM1_AGP_P[0-9]{3}_F[0-9]+_[0-9]+.hdf$'
MIS_re_GP='^MISR_AM1_GP_GMP_P[0-9]{3}_O[0-9]+_F[0-9]+_[0-9]+.hdf$'
MIS_re_HRLL='^MISR_HRLL_P[0-9]{3}\.hdf$'

BF_template = 'TERRA_BF_L1B_O{}_{}_F{}_V{}.h5'
BF_re = '^TERRA_BF_L1B_O[0-9]+_[0-9]+_F[0-9]+_V[0-9]+\.h5$'


1 change: 1 addition & 0 deletions bfutils/data/Orbit_Path_Time.json

Large diffs are not rendered by default.

84,303 changes: 84,303 additions & 0 deletions bfutils/data/Orbit_Path_Time.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions bfutils/data/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The basic fusion file format. This number is used to identify the file format
# on the BF filename.
bf_format: '000'

# The BF version. The BF data product may go through versioning. This number
# is used on the BF filenames.
bf_version: '001'
82 changes: 82 additions & 0 deletions bfutils/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Methods for interacting with BF files and filenames, as well as
retrieving metadata.
"""

from bfutils import constants
import os
import re
import json

_orbit_info_dict = None

__all__ = ['orbit_start', 'bf_file_orbit', 'is_bf_file',
'get_bf_filename']

def orbit_start( orbit, orbit_info=constants.ORBIT_INFO_JSON ):
'''
Find the starting time of orbit according to the orbit info json
file.
Returns the starting time in the format of yyyymmddHHMMSS.
'''

global _orbit_info_dict

if _orbit_info_dict is None:
with open( orbit_info, 'r' ) as f:
_orbit_info_dict = json.load( f )

try:
stime = _orbit_info_dict[str(orbit)]['stime']
except KeyError:
raise ValueError("Argument 'orbit' is outside the supported bounds.")

return stime

def bf_file_orbit( file ):
'''
Returns the orbit number of a basic fusion file as an integer.
The argument 'file' can be a BF filename (or path to a BF file).
'''

bn = os.path.basename( file )
if not is_bf_file( file ):
raise ValueError('Argument "file" is not a Basic Fusion file.')

return int( bn.split('_')[3].replace('O', '') )

def is_bf_file( file ):
'''
Returns True if "file" is a proper Basic Fusion filename.
Else, returns False.
'''

bn = os.path.basename(file)
if re.match( constants.BF_re, bn):
return True

return False

def get_bf_filename( orbit ):
'''
Retrieve the filename of a Basic Fusion granule from a specific
orbit. The filename is primarily constructed from a template
(in constants.py) and three pieces of information:
1. Orbit start time (data/Orbit_Path_Start.json)
2. BF format number (data/config.yml)
3. BF version number (data/config.yml)
It is possible that either the format or the version numbers do
not match the BF files you actually have. If so, change the
values in config.yml to the proper value.
'''

o_start = orbit_start( orbit )

filename = constants.BF_template.format( orbit, o_start,
constants.CONFIG['bf_format'],
constants.CONFIG['bf_version'])

return filename
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from setuptools import setup
import os

data = os.path.join( os.path.dirname( __file__ ), 'bfutils', 'data')

setup(
name='bfutils',
version='0.1.0',
author='Landon T. Clipp',
author_email='clipp2@illinois.edu',
packages=['bfutils'],
description='Basic Fusion utilities',
long_description='bfutils provides a few useful utilities for \
interacting with the Basic Fusion data product.',
install_requires=[ 'pyyaml' ],
url = 'https://github.com/TerraFusion/bfutils',
keywords= ['bfutils', 'bf', 'utilities', 'nasa', 'eos', 'earth'],
package_data = {'bfutils':
[ os.path.join( data, 'config.yml'),
os.path.join( data, 'Orbit_Path_Time.json'),
os.path.join( data, 'Orbit_Path_Time.txt')]},
include_package_data = True,
)

0 comments on commit 1dee744

Please sign in to comment.