#! /usr/bin/python
"""
#+
# NAME:
#	sync_ace
# PURPOSE:
# 	Downloads real-time ACE data from ftp.sec.noaa.gov
#	for the SWEPAM and MAG instruments
# CALLING SEQUENCE:
#	usually as cron job:
#	bash --login -c "sync_ace.py [-mirror -force year=<year> <dat=dat>]"
# OPTIONAL INPUT PARAMETERS:
#	year		four-digit year; default: current year
#					yearly ACE file to be updated
#					(currently disabled)
#	dat=<dat>	if specified this directory is used instead of $DAT.
#					(the directory is created if it doesn't exist yet).
#	-mirror		if set then the local ACE data base is updated from
#					the SEC data base.
#	-force		forces update of yearly file from the currently
#					available ACE data.
# RESTRICTIONS:
#	The directories $DAT/insitu and $DAT/insitu/ace must exist. The
#	Perl script mirror is in SolarSoft ($SSW/gen/mirror/mirror)
# CALLS:
#	mirror, tiny.is_there, tiny.start
# PROCEDURE:
#	The ACE data are stored at NOAA in monthly files with filenames
#	200002_swepam_1hr.txt, where the numbers indicate year and month.
#	These monthly files are updated regularly with real-time ACE data.
#
#	Monthly files are stored locally in $DAT/insitu/ace2. This directory
#	maintains copies of all SWEPAM and MAG files from the NOAA site.
#	This is done using the Perl script 'mirror'.
#
#	If no year is specified on the command line then the current yearly
#	file is updated only if new data have been down loaded from NOAA.
#	If a year is specified then the file for that year is regenerated.
# MODIFICATION HISTORY:
#	???-1999, James Chao, Paul Hick
#		Original bash version
#	JAN-2000, Paul Hick (UCSD/CASS)
#		Added a couple of error checks
#	AUG-2002, Paul Hick (UCSD/CASS)
#		Removed explicit calls to bash startup scripts
#		/etc/profile and ~/.bash_profile.
#	OCT-2002, Paul Hick (UCSD/CASS)
#		Rewrite of download section. Now uses Perl script 'mirror'
#	DEC-2003, Paul Hick (UCSD/CASS)
#		Changed solar.sec.noaa.edu to ftp.sec.noaa.edu
#	DEC-2003, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		Converted from bash to Python
#-
"""
import os, sys
#import datetime	only in Python 2.3
import time
from tiny   import is_there, start
from mirror import mirror

if __name__ == '__main__':

	# === Initialization =================

	force = is_there( '-force' , sys.argv )
	mget  = is_there( '-mirror', sys.argv )

	year  = '%d'%((time.localtime( time.time() )).tm_year)

	#year = start( 'year=', sys.argv )
	#if year == '':
		#year = '%d'%(datetime.date.today().year)
		#year = '%d'%((time.localtime( time.time() )).tm_year)

	# Destination directory for yearly ACE files

	insitu_dir = start( 'dat=', sys.argv )

	if insitu_dir == '':
		insitu_dir = os.environ['DAT']
		insitu_dir = os.path.join( insitu_dir, 'insitu' )
		local_dir  = os.path.join( insitu_dir, 'ace2' )

	else:
		if not os.path.exists(insitu_dir):
			os.mkdir( insitu_dir)

		insitu_dir = os.path.join( insitu_dir, 'insitu' )

		if not os.path.exists(insitu_dir):
			os.mkdir( insitu_dir)

		local_dir  = os.path.join( insitu_dir, 'ace2' )

		if not os.path.exists(local_dir):
			os.mkdir( local_dir)

	if mget:			# Run mirror

		args = dict	\
			( [	\
			 ( 'job'	, 'real_time_ace'	),	\
			 ( 'local_dir'	, local_dir		),	\
			 ( 'remote_site', 'ftp.sec.noaa.gov'	),	\
			 ( 'remote_dir'	, '/pub/lists/ace2/'	),	\
			 ( 'recursive'	, 'false' 		),	\
			 ( 'get_patt'	, 'ace_mag_1h.txt$|ace_swepam_1h.txt$' )	\
			] )

		if len( mirror( args ) ) > 0:
			force = 1

	if force:			# Create yearly files

		files = os.listdir( local_dir )
		files.sort()

		#print 'sync_ace, updating year', year

		sw_all = []
		bb_all = []

		for file in files:

			if file[0:4] != year:
				continue

			data = (open( os.path.join( local_dir, file) , 'r')).read()
			data = data.split('\n')

			if file.find( 'swepam' ) != -1:				# Plasma data
				for line in data:
					if len(line) > 0 and line[0] != ':' and line[0] != '#':
						sw_all.append(line)
			else:										# Magnetic data
				for line in data:
					if len(line) > 0 and line[0] != ':' and line[0] != '#':
						bb_all.append(line)

		sw_all = ('\n').join( sw_all )+'\n'
		(open( os.path.join( insitu_dir, 'acesw_'+year+'.hravg'), 'w')).write(sw_all)
 
		bb_all = ('\n').join( bb_all )+'\n'
		(open( os.path.join( insitu_dir, 'aceb_' +year+'.hravg'), 'w')).write(bb_all)

	#else:
	#	print 'sync_ace, no update for year', year

	sys.exit()
