#!/usr/bin/env python
#encoding:utf-8

# Created by V. Noel [LMD/CNRS] on 2014-09-25


import glob

masks = {
        'SR_histo':['SR_histo330m_*.nc', 'SR_histo_Phase330m_*.nc'],
        '3D_CloudFraction':['3D_CloudFraction330m_*.nc', '3D_CloudFraction_Phase330m_*.nc'],
        'MapLowMidHigh':['MapLowMidHigh330m_*.nc', 'MapLowMidHigh_Phase330m_*.nc']
        }


def count_files(path, mask):
    
    if 'daily' in path:
        filetype = 'daily/'
    else:
        filetype = ''
    files = glob.glob(path + '/' + mask)
    if len(files) > 0:
        print '\t\t\t' + filetype + mask, ' %d files' % len(files)
    else:
        print '\t\t\t' + filetype + mask, ' 0 FILES - PROBLEM'
    

def check_variable(year, variable_name, check):
    
    grids = 'grid_1x1xL40', 'grid_2x2xL40'
    
    print variable_name
    
    for grid in grids:
        print '\t' + grid
        for dayflag in 'day', 'night':
            print '\t\t' + dayflag
            path = variable_name + '/' + grid + '/' + year + '/' + dayflag
            
            for mask in masks[variable_name]:
                if 'monthly' in check:
                    count_files(path, mask)
                if 'daily' in check:
                    count_files(path + '/daily', mask)
    
            
            


def main(year='2007', check='daily+monthly'):
    for variable in masks:
        check_variable(year, variable, check)
    

if __name__ == '__main__':
    import plac
    plac.call(main)