if len(argv) != 2:
    printUsage()
    exit(1)

back_date = argv[1]
if len(back_date) != 8:
    print("****************************")
    print("* date parameter malformed *")
    print("****************************")
    printUsage()
    exit(1)

_year = back_date[:4]
_month = back_date[4:6]
_day = back_date[6:]
_doy = getDOY(_year, _month, _day)
print("Processing date " + back_date + " (DOY = " + str(_doy) + ").")

modisL3_TSMPath = ensureTrailingSlash(
    ensureTrailingSlash(
        ensureTrailingSlash(modisL3_TSMBasePath + _year) + _month) + _day)
modisL3_UTMPath = ensureTrailingSlash(
    ensureTrailingSlash(
        ensureTrailingSlash(modisL3_TSM_UTMPath + _year) + _month) + _day)
modisL3_UTM_QLPath = ensureTrailingSlash(modisL3_TSM_UTM_QLPath + _year)

for _path in [modisL3_UTMPath, modisL3_UTM_QLPath]:
    if not exists(_path):
        print("Making directory: ", _path, " ...")
        makedirs(_path)
def printUsage():
    print('Usage: create_MODIS_GEO_L1B.py date')
    print('where date is a string representing the date to process,')
    print('e.g. 20120607 for May 7, 2012.')


argc = len(argv)
if argc < 2:
    printUsage()
    exit(1)

_date  = str(argv[1])
_year  = _date[:4]
_month = _date[4:6]
_day   = _date[6:]
_doy   = getDOY(_year, _month, _day)

print(_year, _month, _day, _doy)

modisL1A_LACPath = ensureTrailingSlash(ensureTrailingSlash(ensureTrailingSlash(modisL1A_LACBasePath + _year) + _month) + _day)
modisL1A_subPath = ensureTrailingSlash(ensureTrailingSlash(ensureTrailingSlash(modisL1A_subBasePath + _year) + _month) + _day)
modisGEOPath     = ensureTrailingSlash(ensureTrailingSlash(ensureTrailingSlash(modisGEOBasePath     + _year) + _month) + _day)
modisGEO_subPath = ensureTrailingSlash(ensureTrailingSlash(ensureTrailingSlash(modisGEO_subBasePath + _year) + _month) + _day)

for _path in [modisL1A_subPath, modisGEOPath, modisGEO_subPath]:
    if not exists(_path):
        print("Making directory: ", _path, " ...")
        makedirs(_path)

try:
    srcList = listdir(modisL1A_LACPath)
    printUsage()
    exit(1)

date = argv[1]
if not len(date)==10:
    print("Wrong parameter: ", argv[1])
    printUsage()
    exit(1)

_year  = date[0:4]
_month = date[5:7]
_day   = date[8:10]
_backdate = _year + _month + _day

# get the backday value needed for get_MODIS_LAC.py
DOY_backday = getDOY(_year,_month, _day)
todate = datetime.date.today()
DOY_today = getDOY(todate.year, todate.month, todate.day)
_backday = DOY_today - DOY_backday



print("Processing products of ", _backdate, "...")

dwnldCmd     = 'python /home/uwe/cronjobs/nasa/modis/get_MODIS_LAC.py AQUA' + ' ' + str(_backday) # download L1A_LAC from nasa
l1aCmd       = 'python ' + l1aScript          + ' ' + _backdate     # L1A_LAC => L1A_sub, GEO_sub
l1bCmd       = 'python ' + l1bScript          + ' ' + _backdate     # L1A_sub, GEO_sub => L1B_sub
qlGenCmd     = 'python ' + quicklookScript    + ' ' + _backdate     # L1B_sub => L1B_RGB
l2Cmd        = 'python ' + l2Script           + ' ' + _backdate     # L1B_sub => L2_sub
gptTSMCmd    = 'python ' + gptTSMScript       + ' ' + _backdate     # L2_sub => L2_TSM
l3binningCmd = 'python ' + l3binningScript    + ' ' + _backdate     # L2_TSM => L3_TSM