#!/usr/bin/env python

import os
import glob
import numpy as np
import calendar

year = '2008'
month = '07'
caliop_version = 4

if int(year) % 400 == 0:
	day_month = [31,29,31,30,31,30,31,31,30,31,30,31]
elif int(year) % 4 == 0 and int(year) % 100 != 0:
	day_month = [31,29,31,30,31,30,31,31,30,31,30,31]
else:	
	day_month = [31,28,31,30,31,30,31,31,30,31,30,31]

'''---------- step 1 ----------'''	

myd021km_path = '/Volumes/Data/MYD021KM/'
myd03_path = '/Volumes/Data/Myd03/'
myd35_path = '/Volumes/DATA_03/modis/Myd35/'
myd06_c5_path = '/Volumes/DATA_03/modis/Myd06_c5/' 
myd06_c6_path = '/Volumes/DATA_03/modis/Myd06_c6/'
myd06_baseline_c6_path = '/Volumes/DATA_03/modis/Myd06_baseline_c6/'
myd06_research_c6_path = '/Volumes/DATA_03/modis/Myd06_research_c6/'

if caliop_version == 3:
	caliop_01km_path = '/Volumes/Data/Caliop/01km_CLay/'
	caliop_05km_path = '/Volumes/Data/Caliop/05km_CLay/'

if caliop_version == 4:
	caliop_01km_path = '/Volumes/Data/Caliop/v4/01km_CLay/'
	caliop_05km_path = '/Volumes/Data/Caliop/v4/05km_CLay/'

#dest_path = '/Users/marchant/Research/Colocated_data/Caliop_modis/Scripts/Caliop_modis/01km_CLay/'
dest_path = '/Volumes/Data/Colocated_data/Caliop_modis/01kmCLay/'

for i in np.arange(calendar.monthrange(int(year),int(month))[1]):
	'''---------- ----------'''	
	if i+1 < 10:
		day = '0' + str(i+1)
	else:
		day = str(i+1)
	'''---------- ----------'''	
	sum_days = 0
	for d in np.arange(int(month)-1):
		sum_days = sum_days + day_month[d]
	if int(month)-1 == 0:
		Julian_day = i+1 
	else:
		Julian_day = sum_days + i+1 	
	if Julian_day < 10:	
		Julian_day_str = '00' + str(Julian_day)
	elif Julian_day < 100:	
		Julian_day_str = '0' + str(Julian_day)
	else:
		Julian_day_str = str(Julian_day)
	'''---------- step 2 ----------'''
	myd021km_full_path = myd021km_path + year + '/' + year + '_' + month + '_' + day + '/'
	myd03_full_path = myd03_path + year + '/' + year + '_' + month + '_' + day + '/'
	myd35_full_path = myd35_path + year + '/' + year + '_' + month + '_' + day + '/'
	myd06_c5_full_path = myd06_c5_path + year + '/' + year + '_' + month + '_' + day + '/'
	myd06_c6_full_path = myd06_c6_path + year + '/' + year + '_' + month + '_' + day + '/'
	myd06_baseline_c6_full_path = myd06_baseline_c6_path + year + '/' + year + '_' + month + '_' + day + '/'
	myd06_research_c6_full_path = myd06_research_c6_path + year + '/' + year + '_' + month + '_' + day + '/'
	caliop_01km_full_path = caliop_01km_path + year + '/' + year + '_' + month + '_' + day + '/'
	caliop_05km_full_path = caliop_05km_path + year + '/' + year + '_' + month + '_' + day + '/'
	'''---------- step 3 ----------'''	
	myd021km_day_list = glob.glob(myd021km_full_path + '/*hdf' )
	myd03_day_list = glob.glob(myd03_full_path + '/*hdf' )
	myd35_day_list = glob.glob(myd35_full_path + '/*hdf' )
	myd06_c5_day_list = glob.glob(myd06_c5_full_path + '/*hdf' )
	myd06_c6_day_list = glob.glob(myd06_c6_full_path + '/*hdf' )
	myd06_baseline_c6_day_list = glob.glob(myd06_baseline_c6_full_path + '/*hdf' )
	myd06_research_c6_day_list = glob.glob(myd06_research_c6_full_path + '/*hdf' )
	caliop_01km_day_list = glob.glob(caliop_01km_full_path + '/*hdf' )
	caliop_05km_day_list = glob.glob(caliop_05km_full_path + '/*hdf' )
	'''---------- step 4 ----------'''	
	for modis_granule_name in myd03_day_list:
		offset = len(myd03_full_path)
		myd03_name = modis_granule_name[offset:]
		#print 'offset', offset
		#print modis_granule_name[offset+15:offset+17]
		modis_granule_hour_str = modis_granule_name[offset+15:offset+17] 
		modis_granule_hour = int( modis_granule_hour_str )
		#print modis_granule_name[offset+17:offset+19]
		modis_granule_minute_str = modis_granule_name[offset+17:offset+19] 
		modis_granule_minute = int( modis_granule_minute_str )
		#print modis_granule_hour, modis_granule_minute
		missing_file = 0 
		'''---------- MYD021KM ----------'''	
		file_name_prefix = 'MYD021KM.A'+ year + Julian_day_str + '.' \
		                   + modis_granule_hour_str + modis_granule_minute_str
		found_file = 0
		for myd021km_granule_name in myd021km_day_list:
			offset = len(myd021km_full_path)
			if file_name_prefix == myd021km_granule_name[offset:offset+22]:
				found_file = 1
				myd021km_name = myd021km_granule_name[offset:]
				#print myd35_granule_name[offset:]
		if found_file == 0:
			#missing_file = missing_file + 1
			myd021km_name = 'NoFile'
		'''---------- MYD35 ----------'''	
		file_name_prefix = 'MYD35_L2.A'+ year + Julian_day_str + '.' \
		                   + modis_granule_hour_str + modis_granule_minute_str
		found_file = 0
		for myd35_granule_name in myd35_day_list:
			offset = len(myd35_full_path)
			if file_name_prefix == myd35_granule_name[offset:offset+22]:
				found_file = 1
				myd35_name = myd35_granule_name[offset:]
				#print myd35_granule_name[offset:]
		if found_file == 0:
			#missing_file = missing_file + 1
			myd35_name = 'NoFile'
		'''---------- MYD06_c5 ----------'''				
		file_name_prefix = 'MYD06_L2.A'+ year + Julian_day_str + '.' \
		                   + modis_granule_hour_str + modis_granule_minute_str
		found_file = 0		
		for myd06_c5_granule_name in myd06_c5_day_list:
			offset = len(myd06_c5_full_path)
			if file_name_prefix == myd06_c5_granule_name[offset:offset+22]:
				found_file = 1			
				myd06_c5_name = myd06_c5_granule_name[offset:]
				#print myd06_c5_granule_name[offset:]
		if found_file == 0:
			#missing_file = missing_file + 1
			#print "MISSING MYD06_c5", len(myd06_c5_day_list)
			#print myd06_c5_full_path
			myd06_c5_name = 'NoFile'		
		'''---------- MYD06_c6 ----------'''				
		file_name_prefix = 'MYD06_L2.A'+ year + Julian_day_str + '.' \
		                   + modis_granule_hour_str + modis_granule_minute_str
		found_file = 0		
		for myd06_c6_granule_name in myd06_c6_day_list:
			offset = len(myd06_c6_full_path)
			if file_name_prefix == myd06_c6_granule_name[offset:offset+22]:
				found_file = 1			
				myd06_c6_name = myd06_c6_granule_name[offset:]
				#print myd06_c6_granule_name[offset:]
		if found_file == 0:
			#missing_file = missing_file + 1	
			myd06_c6_name = 'NoFile'		
		'''---------- MYD06_baseline_c6 ----------'''				
		file_name_prefix = 'MYD06_L2.A'+ year + Julian_day_str + '.' \
		                   + modis_granule_hour_str + modis_granule_minute_str
		found_file = 0		
		for myd06_baseline_c6_granule_name in myd06_baseline_c6_day_list:
			offset = len(myd06_baseline_c6_full_path)
			if file_name_prefix == myd06_baseline_c6_granule_name[offset:offset+22]:
				found_file = 1			
				myd06_baseline_c6_name = myd06_baseline_c6_granule_name[offset:]
				#print myd06_baseline_c6_granule_name[offset:]
		if found_file == 0:
			#missing_file = missing_file + 1		
			myd06_baseline_c6_name = 'NoFile'		
		'''---------- MYD06_research_c6 ----------'''				
		file_name_prefix = 'MYD06_L2.A'+ year + Julian_day_str + '.' \
		                   + modis_granule_hour_str + modis_granule_minute_str
		found_file = 0		
		for myd06_research_c6_granule_name in myd06_research_c6_day_list:
			offset = len(myd06_research_c6_full_path)
			if file_name_prefix == myd06_research_c6_granule_name[offset:offset+22]:
				found_file = 1			
				myd06_research_c6_name = myd06_research_c6_granule_name[offset:]
				#print myd06_research_c6_granule_name[offset:]
		if found_file == 0:
			#missing_file = missing_file + 1			
		    myd06_research_c6_name = 'NoFile'
		'''---------- CAL 01km ----------'''					
		minimum = 1000
		find_one = 0
		for caliop_01km_granule_name in caliop_01km_day_list:
			offset = len(caliop_01km_full_path)
			
			if caliop_version == 3:
				caliop_granule_hour = int( caliop_01km_granule_name[offset+47:offset+49] )
				caliop_granule_minute = int( caliop_01km_granule_name[offset+50:offset+52]   )

			if caliop_version == 4:
				caliop_granule_hour = int( caliop_01km_granule_name[offset+46:offset+48] )
				caliop_granule_minute = int( caliop_01km_granule_name[offset+49:offset+51]   )

			#print caliop_granule_hour, caliop_granule_minute
			difference = modis_granule_hour * 60 + modis_granule_minute \
			             - ( caliop_granule_hour * 60 + caliop_granule_minute )
			if (difference < minimum ) and ( difference > 0 ):
				minimum = difference
				tmp_02 = caliop_01km_granule_name
				find_one = 1
		if find_one == 1:
			caliop_01km_name = tmp_02[offset:]
			#print tmp_02[offset:]
		else:
			missing_file = missing_file + 1	
			#print 'CAL 01 km missing'			
		'''---------- CAL 05km ----------'''				
		minimum = 1000
		find_one = 0
		for caliop_05km_granule_name in caliop_05km_day_list:
			offset = len(caliop_05km_full_path)
			
			if caliop_version == 3:
				caliop_granule_hour = int( caliop_05km_granule_name[offset+42:offset+44] )
				caliop_granule_minute = int( caliop_05km_granule_name[offset+45:offset+47] )

			if caliop_version == 4:
				caliop_granule_hour = int( caliop_05km_granule_name[offset+46:offset+48] )
				caliop_granule_minute = int( caliop_05km_granule_name[offset+49:offset+51] )

			#print caliop_05km_granule_name, caliop_granule_hour, caliop_granule_minute
			difference = modis_granule_hour * 60 + modis_granule_minute \
			             - ( caliop_granule_hour * 60 + caliop_granule_minute )
			if (difference < minimum ) and ( difference > 0 ):
				minimum = difference
				tmp_03 = caliop_05km_granule_name
				find_one = 1
		if find_one == 1:
			caliop_05km_name = tmp_03[offset:]		
			#print tmp_03[offset:]
		else:
			missing_file = missing_file + 1	
			#print 'CAL 05 km missing'				
		'''---------- step 7 ----------'''
		#print "missing_file", missing_file
		if ( missing_file == 0 ):
			#print 'NO MISSING FILES FOUND'
			filename = 'calmod_file_name_' + modis_granule_hour_str + modis_granule_minute_str
			f = open(filename, 'w')
			f.write(myd03_name + ' \n')	
			f.write(myd35_name + ' \n')				
			f.write(myd06_c5_name + ' \n')			
			f.write(myd06_c6_name + ' \n')			
			f.write(myd06_baseline_c6_name + ' \n')
			f.write(myd06_research_c6_name + ' \n')			
			f.write(caliop_01km_name + ' \n')
			f.write(caliop_05km_name + ' \n')
			f.write(myd021km_name + ' \n')	
			f.closed
			command = 'mv ' + filename + ' ' + dest_path + year + '/' + year + '_' + month + '_' + day + '/'
			os.system(command)			
		'''----------  ----------'''			
		#print 'modis granule done', modis_granule_hour, modis_granule_minute
	print day + ' done'

About / Au sujet de

Ben
Created:
24 avril 2017 20:19:13

Updated:
24 avril 2017 20:19:13

License / Licence

MIT License