function [CONFIG] = loadconfig(varargin)
%loadconfig()
%loadconfig(DIRECTORY)
%
%Reads MITgcm parameter input files ("data") to create a CONFIG structure
%If DIRECTORY is not specified the current working directory is used.
%
%e.g.
% >> CONFIG=loadconfig;
% >> CONFIG2=loadconfig('/scratch/john/run2/');
%
%Written by adcroft@mit.edu, 2002
%$Header:

if nargin==0
 Dir='./';
elseif nargin==1
 Dir=[varargin{1} '/'];
else
 error('I don''t know what to do with the second argument');
end

% Extract names of forcing files from "data" file
datafile=[Dir 'data'];

fid=fopen(datafile,'r');
if fid==-1
 error(['Could not open file:' datafile ' for reading']);
end

CONFIG.tinitfile=grepparameter(datafile,'hydrogthetafile');
CONFIG.sinitfile=grepparameter(datafile,'hydrogsaltfile');
CONFIG.sstfile=grepparameter(datafile,'thetaclimfile');
CONFIG.sssfile=grepparameter(datafile,'saltclimfile');
CONFIG.tauxfile=grepparameter(datafile,'zonalwindfile');
CONFIG.tauyfile=grepparameter(datafile,'meridwindfile');
CONFIG.qfile=grepparameter(datafile,'surfqfile');
CONFIG.empmrfile=grepparameter(datafile,'empmrfile');

CONFIG.dt=grepparameter(datafile,'deltat');
CONFIG.tau_cd=grepparameter(datafile,'taucd');
CONFIG.tau_t=grepparameter(datafile,'tauthetaclimrelax');
CONFIG.tau_s=grepparameter(datafile,'tausaltclimrelax');

CONFIG.diffkh=grepparameter(datafile,'diffkh');
CONFIG.viscar=grepparameter(datafile,'visca[rz]');
CONFIG.viscah=grepparameter(datafile,'viscah');
CONFIG.visca4=grepparameter(datafile,'visca4');
CONFIG.diffkht=grepparameter(datafile,'diffkht');
CONFIG.diffk4t=grepparameter(datafile,'diffk4t');
CONFIG.diffkrt=grepparameter(datafile,'diffk[rz]t');
CONFIG.diffkhs=grepparameter(datafile,'diffkhs');
CONFIG.diffk4s=grepparameter(datafile,'diffk4s');
CONFIG.diffkrs=grepparameter(datafile,'diffk[rz]s');

CONFIG.rhonil=grepparameter(datafile,'rhonil');
 if isempty(CONFIG.rhonil); CONFIG.rhonil=1000; end

CONFIG.hfacmin=grepparameter(datafile,'hfacmin');
CONFIG.hfacmindr=grepparameter(datafile,'hfacmind[rz]');

CONFIG.usegmredi=grepparameter([datafile '.pkg'],'usegmredi');
CONFIG.useptracers=grepparameter([datafile '.pkg'],'useptracers');
CONFIG.usekpp=grepparameter([datafile '.pkg'],'usekpp');

if ( ~isempty(CONFIG.usegmredi) & CONFIG.usegmredi=='true')
 CONFIG.background_k=grepparameter([datafile '.gmredi'],'gm_background_k');
 CONFIG.taper_scheme=grepparameter([datafile '.gmredi'],'gm_taper_scheme');
 CONFIG.maxslope=grepparameter([datafile '.gmredi'],'gm_maxslope');
 CONFIG.kmin_horiz=grepparameter([datafile '.gmredi'],'gm_kmin_horiz');
end
