#!/usr/bin/env python

import os
import glob
import numpy as np
import calendar

#----------------------------------------------------------------------------------------#
# user config.

year = 2008
month = 07

path_to_myd021km = '/Volumes/Data/MYD021KM/'
path_to_myd03 = '/Volumes/Data/Myd03/'
path_to_myd35 = '/Volumes/DATA_03/modis/Myd35/'
path_to_myd06_c5 = '/Volumes/DATA_03/modis/Myd06_c5/' 
path_to_myd06_c6 = '/Volumes/DATA_03/modis/Myd06_c6/'

result_file_name = 'modis_granule_corresponding_files_'+ str(year) + \
                   '_' + "%02d" % month +'.txt'

#----------------------------------------------------------------------------------------#
# create file

f = open(result_file_name, 'w')

day_of_year_0 = 0
for i in range(month-1):
	day_of_year_0 = day_of_year_0 + calendar.monthrange(year,i+1)[1]

for day in np.arange(calendar.monthrange(year,month)[1]):

	day_of_year = day_of_year_0 + day + 1

	#------------------------------------------------------------------------------------# 
	# create paths

	d = str(year) + '/' + str(year) + '_' + "%02d" % month + '_' + "%02d" % (day+1) + '/'

	path_to_myd021km_day = path_to_myd021km + d
	path_to_myd03_day = path_to_myd03 + d
	path_to_myd35_day = path_to_myd35 + d
	path_to_myd06_c5_day = path_to_myd06_c5 + d
	path_to_myd06_c6_day = path_to_myd06_c6 + d

	#------------------------------------------------------------------------------------# 
	# get all file names

	myd021km_day_list = glob.glob(path_to_myd021km_day + '/*hdf' )
	myd03_day_list = glob.glob(path_to_myd03_day + '/*hdf' )
	myd35_day_list = glob.glob(path_to_myd35_day + '/*hdf' )
	myd06_c5_day_list = glob.glob(path_to_myd06_c5_day + '/*hdf' )
	myd06_c6_day_list = glob.glob(path_to_myd06_c6_day + '/*hdf' )

	#------------------------------------------------------------------------------------# 
	# create file

	for modis_granule_name in myd03_day_list:

		offset = len(path_to_myd03_day)
		myd03_name = modis_granule_name[offset:]
		hour = modis_granule_name[offset+15:offset+17]
		minute = modis_granule_name[offset+17:offset+19]
		#print hour, minute

		result = str(year) + ' ' + str(month) + ' ' + str(day+1) + \
		         ' ' + hour + ' ' + minute

		result = result + ' ' + myd03_name

		#--------------------------------------------------------------------------------#
		# MYD021KM
		
		file_name_prefix = 'MYD021KM.A'+ str(year) + "%03d" % day_of_year + \
		                   '.' + hour + minute
		found_file = 0
		for myd021km_granule_name in myd021km_day_list:
			offset = len(path_to_myd021km_day)
			if file_name_prefix == myd021km_granule_name[offset:offset+22]:
				found_file = 1
				myd021km_name = myd021km_granule_name[offset:]
		if found_file == 0: myd021km_name = 'NoFile'

		result = result + ' ' + myd021km_name

		#--------------------------------------------------------------------------------#
		# MYD35

		file_name_prefix = 'MYD35_L2.A'+ str(year) + "%03d" % day_of_year + \
		                   '.' + hour + minute
		found_file = 0
		for myd35_granule_name in myd35_day_list:
			offset = len(path_to_myd35_day)
			if file_name_prefix == myd35_granule_name[offset:offset+22]:
				found_file = 1
				myd35_name = myd35_granule_name[offset:]
		if found_file == 0: myd35_name = 'NoFile'

		result = result + ' ' + myd35_name

		#--------------------------------------------------------------------------------#
		# MYD06_c5

		file_name_prefix = 'MYD06_L2.A'+ str(year) + "%03d" % day_of_year + \
		                   '.' + hour + minute
		found_file = 0		
		for myd06_c5_granule_name in myd06_c5_day_list:
			offset = len(path_to_myd06_c5_day)
			if file_name_prefix == myd06_c5_granule_name[offset:offset+22]:
				found_file = 1			
				myd06_c5_name = myd06_c5_granule_name[offset:]
		if found_file == 0: myd06_c5_name = 'NoFile'		

		result = result + ' ' + myd06_c5_name

		#--------------------------------------------------------------------------------#
		# MYD06_c6

		file_name_prefix = 'MYD06_L2.A'+ str(year) + "%03d" % day_of_year + \
		                   '.' + hour + minute
		found_file = 0		
		for myd06_c6_granule_name in myd06_c6_day_list:
			offset = len(path_to_myd06_c6_day)
			if file_name_prefix == myd06_c6_granule_name[offset:offset+22]:
				found_file = 1			
				myd06_c6_name = myd06_c6_granule_name[offset:]
		if found_file == 0: myd06_c6_name = 'NoFile'	

		result = result + ' ' + myd06_c6_name

		#--------------------------------------------------------------------------------#
		# save result

		f.write(result + '\n')

f.close()

About / Au sujet de

Ben
Created:
14 juin 2016 20:33:42

Updated:
14 juin 2016 20:33:42

License / Licence

MIT License