# Path: get_path.py
# def get_path(srcDir, sDTime, eDTime):
#     '''
#     select GPM(hdf5) and TRMM(hdf4) files and return their paths)
#     '''
# 
#     prjName, prdLv, prdVer  = srcDir.split(os.path.sep)[-3:]
# 
#     parse_fname     = {'TRMM': parse_fname_trmm,
#                        'GPM' : parse_fname_gpm}[ prjName.split('.')[0] ]
# 
# 
#     if sDTime == eDTime:
#         raise ValueError, '%s == %s'%(sDTime, eDTime)
# 
# 
# 
#     # do not know the reason of implementation ++++++++++++++++++++++
#     # consider to use trange
#     #srcDIR  = [os.path.join(srcDir, '%i/%02d'%(y,m))
#     srcDIR  = [os.path.join(srcDir, str(y), '%02d'%m)
#                             for y in range(sDTime.year,eDTime.year+1)
#                                 for m in range(1,13)]
# 
#     srcDIR  = srcDIR[sDTime.month-1 : eDTime.month-12 if eDTime.month != 12 else 12]
#     # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 
#     srcPATH = []
# 
#     for srcDir in srcDIR:
#         if not os.path.exists(srcDir):
#             print 'Warning [%s] directory does not exists!'%srcDir
#             continue
# 
#         for srcFName in sorted( os.listdir(srcDir) ):
# 
#             sdt_gtrk, edt_gtrk  = parse_fname( srcFName, ['sDTime','eDTime'] )
# 
#             if sDTime <= edt_gtrk and eDTime >= sdt_gtrk:
#                 srcPATH.append( os.path.join(srcDir, srcFName) )
#             else:
#                 continue
# 
#     return srcPATH
# 
# Path: get_gtrack_dim.py
# def get_gtrack_dim(srcPath, fn_read, cache=False, cache_dir=None):
#     '''
#     scan granules and return dimension (T,Y,X) or ground tracks
# 
#     cache  : mode of cf.devel.collection.cached
#               ['cached', 'cached-verbose', 'skip', 'update']
#     '''
# 
#     verbose     = False if 'verbose' in cache   \
#              else True
#     verbose     = True
# 
#     prjName, prdLv, prdVer, yyyy, mm, srcFName  = srcPath.split(os.path.sep)[-6:]
# 
#     get_dtime, get_location = {'TRMM': [get_dtime_trmm, get_location_trmm],
#                                'GPM' : [get_dtime_gpm,  get_location_gpm ],
#                    }[ prjName.split('.')[0] ]
# 
# 
#     print '+ Get Groundtrack Dimension: {}'.format( srcPath )
# 
#     cache_dir           = os.path.join( cache_dir, prjName, prdLv, prdVer, yyyy, mm )
# 
#     Lat, Lon    = cached( srcFName + '.latlon',
#                           cache_dir,
#                           mode=cache,
#                           verbose=verbose )(get_location)(srcPath, fn_read)#, cache, cache_dir)
# 
#     Timetuple   = cached( srcFName + '.timetuple',
#                           cache_dir,
#                           mode=cache,
#                           verbose=verbose )(get_dtime   )(srcPath, fn_read)#, cache, cache_dir)
# 
# 
#     # exception handling for us 1000000 instead of 0 ------------------------------------
#     DTime   = []
#     for y,m,d,H,M,S,uS in Timetuple:
# 
#         if uS == 1000000:
#             DTime.append( datetime(y,m,d,H,M,S,0)+timedelta(seconds=1) )
#             print 'Warning [NS/ScanTime/Millisecond] == 1000 : %i %i %i %i %i %i %i'    \
#                   %(y,m,d,H,M,S,uS/1000)
# 
#         else:
#             DTime.append( datetime(y,m,d,H,M,S,uS) )
#     # -----------------------------------------------------------------------------------
# 
#     DTime       = array( DTime )
# 
#     return DTime, Lat, Lon


import  os,sys
from    optparse        import OptionParser
from    numpy           import arange, ma
from    get_path        import get_path
from    get_gtrack_dim  import get_gtrack_dim
#! /usr/bin/python
#--------------------------------------------------------------------
# PROGRAM    : search_granules.py
# CREATED BY : hjkim @IIS.2015-07-13 12:59:51.759752
# MODIFED BY :
#
# USAGE      : $ ./search_granules.py
#
# DESCRIPTION:
#------------------------------------------------------cf0.2@20120401






class SearchGranules( object ):
    def search_granules(self, srcDir, sDTime, eDTime, BBox=[[-90,-180],[90,180]], thresh=0.001):
        '''
        BBox    : [[lllat,lllon], [urlat,urlon]]    /* lat: -90 ~ 90 */
                                                    /* lon: -180 ~ 180 */
        '''

        srcPATH = get_path(srcDir, sDTime, eDTime)


# Path: get_path.py
# def get_path(srcDir, sDTime, eDTime):
#     '''
#     select GPM(hdf5) and TRMM(hdf4) files and return their paths)
#     '''
# 
#     prjName, prdLv, prdVer  = srcDir.split(os.path.sep)[-3:]
# 
#     parse_fname     = {'TRMM': parse_fname_trmm,
#                        'GPM' : parse_fname_gpm}[ prjName.split('.')[0] ]
# 
# 
#     if sDTime == eDTime:
#         raise ValueError, '%s == %s'%(sDTime, eDTime)
# 
# 
# 
#     # do not know the reason of implementation ++++++++++++++++++++++
#     # consider to use trange
#     #srcDIR  = [os.path.join(srcDir, '%i/%02d'%(y,m))
#     srcDIR  = [os.path.join(srcDir, str(y), '%02d'%m)
#                             for y in range(sDTime.year,eDTime.year+1)
#                                 for m in range(1,13)]
# 
#     srcDIR  = srcDIR[sDTime.month-1 : eDTime.month-12 if eDTime.month != 12 else 12]
#     # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 
#     srcPATH = []
# 
#     for srcDir in srcDIR:
#         if not os.path.exists(srcDir):
#             print 'Warning [%s] directory does not exists!'%srcDir
#             continue
# 
#         for srcFName in sorted( os.listdir(srcDir) ):
# 
#             sdt_gtrk, edt_gtrk  = parse_fname( srcFName, ['sDTime','eDTime'] )
# 
#             if sDTime <= edt_gtrk and eDTime >= sdt_gtrk:
#                 srcPATH.append( os.path.join(srcDir, srcFName) )
#             else:
#                 continue
# 
#     return srcPATH
# 
# Path: get_gtrack_dim.py
# def get_gtrack_dim(srcPath, fn_read, cache=False, cache_dir=None):
#     '''
#     scan granules and return dimension (T,Y,X) or ground tracks
# 
#     cache  : mode of cf.devel.collection.cached
#               ['cached', 'cached-verbose', 'skip', 'update']
#     '''
# 
#     verbose     = False if 'verbose' in cache   \
#              else True
#     verbose     = True
# 
#     prjName, prdLv, prdVer, yyyy, mm, srcFName  = srcPath.split(os.path.sep)[-6:]
# 
#     get_dtime, get_location = {'TRMM': [get_dtime_trmm, get_location_trmm],
#                                'GPM' : [get_dtime_gpm,  get_location_gpm ],
#                    }[ prjName.split('.')[0] ]
# 
# 
#     print '+ Get Groundtrack Dimension: {}'.format( srcPath )
# 
#     cache_dir           = os.path.join( cache_dir, prjName, prdLv, prdVer, yyyy, mm )
# 
#     Lat, Lon    = cached( srcFName + '.latlon',
#                           cache_dir,
#                           mode=cache,
#                           verbose=verbose )(get_location)(srcPath, fn_read)#, cache, cache_dir)
# 
#     Timetuple   = cached( srcFName + '.timetuple',
#                           cache_dir,
#                           mode=cache,
#                           verbose=verbose )(get_dtime   )(srcPath, fn_read)#, cache, cache_dir)
# 
# 
#     # exception handling for us 1000000 instead of 0 ------------------------------------
#     DTime   = []
#     for y,m,d,H,M,S,uS in Timetuple:
# 
#         if uS == 1000000:
#             DTime.append( datetime(y,m,d,H,M,S,0)+timedelta(seconds=1) )
#             print 'Warning [NS/ScanTime/Millisecond] == 1000 : %i %i %i %i %i %i %i'    \
#                   %(y,m,d,H,M,S,uS/1000)
# 
#         else:
#             DTime.append( datetime(y,m,d,H,M,S,uS) )
#     # -----------------------------------------------------------------------------------
# 
#     DTime       = array( DTime )
# 
#     return DTime, Lat, Lon


# Path: search_granules.py
import  os,sys
from    optparse        import OptionParser
from    numpy           import arange, ma
from    get_path        import get_path
from    get_gtrack_dim  import get_gtrack_dim

#! /usr/bin/python
#--------------------------------------------------------------------
# PROGRAM    : search_granules.py
# CREATED BY : hjkim @IIS.2015-07-13 12:59:51.759752
# MODIFED BY :
#
# USAGE      : $ ./search_granules.py
#
# DESCRIPTION:
#------------------------------------------------------cf0.2@20120401






class SearchGranules( object ):
    def search_granules(self, srcDir, sDTime, eDTime, BBox=[[-90,-180],[90,180]], thresh=0.001):
        '''
        BBox    : [[lllat,lllon], [urlat,urlon]]    /* lat: -90 ~ 90 */
                                                    /* lon: -180 ~ 180 */
        '''

        srcPATH = get_path(srcDir, sDTime, eDTime)


        gtrkDim = [get_gtrack_dim(path, self.func_read, self.cached, self.cacheDir)