#!/usr/bin/env python

'''
This executable performs a recursive stripnetcdf of all
nc-files in the current directory and its subdirectories.

The program assumes that the stripnetcdf command is available
in the shell.

Thomas Bligaard <bligaard@fysik.dtu.dk>
09/22/05
'''

import os,glob

directory = os.path.abspath(".")

def GetAllNetCDFFiles(dirname):
	allncfiles = []
	ncfilesInCurrentDirectory = glob.glob(os.path.join(dirname,"*.nc"))
	allncfiles = allncfiles + ncfilesInCurrentDirectory
	allnamesindirectory = glob.glob(os.path.join(dirname,"*"))
	subdirectories = []
	for name in allnamesindirectory:
		nametest = os.path.join(dirname,name)
		if os.path.isdir(nametest):
			subdirectories.append(nametest)
	for dir in subdirectories:
		subdirfiles = GetAllNetCDFFiles(dir)
		allncfiles = allncfiles + subdirfiles
	return allncfiles

ncfiles = GetAllNetCDFFiles(directory)
i=1
for filename in ncfiles:
	print "Currently stripping file: ",i," : ",filename
	os.system("cd "+os.path.dirname(filename)+" ; stripnetcdf "+os.path.basename(filename))
	i += 1
