Extract hours and minutes from caliop or modis file name using python

Published: October 11, 2016

DMCA.com Protection Status

CALIOP

Example: how to extract CALIOP granule hours and minutes from file name using python and Regular Expression:

>>> import re
>>> name = 'CAL_LID_L2_01kmCLay-ValStage1-V3-01.2008-01-08T13-55-27ZD.hdf'
>>> x = re.compile("T(.*)-(.*)-(.*)Z")
>>> obj = x.search(name)
>>> obj.group(1)
'13'
>>> obj.group(2)
'55'
>>>
MODIS

For MODIS it is more easy and fast ! just need to use split:

>>> name = 'MYD03.A2008008.1420.006.2012066144951.hdf'
>>> list = name.split('.')
>>> list
['MYD03', 'A2008008', '1420', '006', '2012066144951', 'hdf']
>>> hhmm = list[2]
>>> hh = hhmm[0:2]
>>> mm = hhmm[2:4]
>>> hh
'14'
>>> mm
'20'
>>>

References