function [ ] = transformXLAI( srcDir, outDir, targYear )
%SPOOFXLAI Summary of this function goes here
%   Detailed explanation goes here

inParse = inputParser;
inParse.addRequired('srcDir',@(x)validateattributes(x,{'char'},{'vector'}));
inParse.addRequired('outDir',@(x)validateattributes(x,{'char'},{'vector'}));
inParse.addRequired('targYear',@(x)validateattributes(x,{'numeric'},...
    {'vector','integer','increasing'}));
inParse.parse(srcDir,outDir,targYear);
assert(exist(srcDir,'dir')==7,'spoofXLAI:noSrcDir','Could not find source directory');
assert(exist(outDir,'dir')==7,'spoofXLAI:noOutDir','Could not find output directory');

% 0.25x0.25
gSpec = genGridSpec('SQR_0.25','geos5');
nLon = gSpec.nLon;
nLat = gSpec.nLat;

% 46 time slices per month
nSamplesFile = 46;

% 73 entries
nXLAI = 73;

% Exclude the last day given
lastMsg = [];
fprintf('Beginning write procedure.\n');
tTick = 5;
tLast = 0;
tTimer = tic;
nYear = length(targYear);
for xYear = 1:nYear
    iYear = targYear(xYear);
    yrStr = sprintf('%04i',iYear);
    yrSrcDir = fullfile(srcDir);
    yrOutDir = fullfile(outDir);
    assert(exist(yrSrcDir,'dir')==7,'spoofXLAI:missingSrcYear','Could not find source directory for %04i',iYear);
    if ~(exist(yrOutDir,'dir')==7)
        mkdir(yrOutDir);
    end
    for iMonth = 1:1
        % New file
        fileDate = datestr(datenum(iYear,1,1),'yyyy');
        inFile = fullfile(yrSrcDir,sprintf('MODIS_XLAI_BNUv6.025x025.%s.nc',fileDate));
        outFile = fullfile(yrOutDir,sprintf('Condensed_MODIS_XLAI_BNUv6.025x025.%s.nc',fileDate));
        sprintf('inFile  : %s', inFile)
	sprintf('outFile : %s', outFile)

	% Get time data from the input file
	timeVec = ncread(inFile,'time')

        % Set up the file
        dataName = {'XLAIMULTI'};
        chunkSize = [nLon,nLat,1,1];
        dimCell = {'lon',nLon,'lat',nLat,'lev',nXLAI,'time',nSamplesFile};
        % Delete existing files (!)
        if fastExist(outFile)
            delete(outFile);
        end
        % Start with dimensions
        nccreate(outFile,'lon','Format','netcdf4_classic',...
            'FillValue',1e14,'DataType','single',...
            'Dimensions',{'lon',nLon});
        nccreate(outFile,'lat','Format','netcdf4_classic',...
            'FillValue',1e14,'DataType','single',...
            'Dimensions',{'lat',nLat});
        nccreate(outFile,'lev','Format','netcdf4_classic',...
            'FillValue',1e14,'DataType','single',...
            'Dimensions',{'lev',nXLAI});
        nccreate(outFile,'time','Format','netcdf4_classic',...
            'FillValue',-100,'DataType','int32',...
            'Dimensions',{'time',nSamplesFile});

        % Data
        for iField = 1:length(dataName)
            nccreate(outFile,dataName{iField},'Format','netcdf4_classic',...
                'FillValue',1e14,'chunksize',chunkSize,...
                'DeflateLevel',1,'Datatype','single',...
                'Dimensions',dimCell);
        end

        % Write attributes - global first
        [~,shortName,shortExt] = fileparts(outFile);
        ncwriteatt(outFile,'/','Title',sprintf('Condensed MODIS XLAI for %04i',iYear));
        ncwriteatt(outFile,'/','Contact','Sebastian David Eastham (seastham@fas.harvard.edu)');
        ncwriteatt(outFile,'/','Conventions','COARDS');
        ncwriteatt(outFile,'/','Filename',[shortName,shortExt]);
        ncwriteatt(outFile,'/','History',sprintf('Created %s',datestr(now,'yyyy-mm-dd HH:MM')));

        % Now the dimensions
        ncwriteatt(outFile,'lon','short_name','lon');
        ncwriteatt(outFile,'lon','long_name','Longitude');
        ncwriteatt(outFile,'lon','standard_name','Longitude');
        ncwriteatt(outFile,'lon','units','degrees_east');
        ncwriteatt(outFile,'lon','axis','X');

        ncwriteatt(outFile,'lat','short_name','lat');
        ncwriteatt(outFile,'lat','long_name','latitude');
        ncwriteatt(outFile,'lat','standard_name','latitude');
        ncwriteatt(outFile,'lat','units','degrees_north');
        ncwriteatt(outFile,'lat','axis','Y');

        % Pretend that the XLAI data is 3D to save time
        ncwriteatt(outFile,'lev','short_name','lev');
        ncwriteatt(outFile,'lev','long_name','level');
        ncwriteatt(outFile,'lev','standard_name','level');
        ncwriteatt(outFile,'lev','units','level');
        ncwriteatt(outFile,'lev','axis','Z');
        % Do not want data to be flipped
        %ncwriteatt(outFile,'lev','positive','up');

        ncwriteatt(outFile,'time','short_name','time');
        ncwriteatt(outFile,'time','long_name','Time');
        ncwriteatt(outFile,'time','standard_name','Time');
        ncwriteatt(outFile,'time','calendar','standard');
        ncwriteatt(outFile,'time','units',sprintf('minutes since %04i-01-01 00:00:00',iYear));
        ncwriteatt(outFile,'time','axis','T');

        % The data
        for iField = 1:length(dataName)
            currField = dataName{iField};
            ncwriteatt(outFile,currField,'short_name',currField);
            ncwriteatt(outFile,currField,'long_name','XLAIMULTI');
            ncwriteatt(outFile,currField,'standard_name','XLAIMULTI');
            ncwriteatt(outFile,currField,'units','cm2 cm-2');
            ncwriteatt(outFile,currField,'add_offset',0);
            ncwriteatt(outFile,currField,'scale_factor',1);
            ncwriteatt(outFile,currField,'missing_value',cast(1e32,'single'));
        end

        % Write dimension data to file
        ncwrite(outFile,'lon',cast(gSpec.lonMid,'single'));
        ncwrite(outFile,'lat',cast(gSpec.latMid,'single'));
        ncwrite(outFile,'lev',cast(0:72,'single'));
        ncwrite(outFile,'time',cast(timeVec,'int32'));

	% Create output array
	dataSliceMulti = zeros(nLon, nLat, nXLAI, nSamplesFile);

        % Read LAI variables and save into levels
	for iXLAI = 1:nXLAI

            % Note: Size of array will be nLon x nLat x nSampleTimes
	    dataSlice = ncread(inFile,sprintf('XLAI%02i',iXLAI-1));

	    % Pad this into an array of nLon x nLat x nXLAI x nSampleTimes
	    dataSliceMulti(:,:,iXLAI,:) = dataSlice;
	end       

	% Write to netCDF
	sprintf('Writing %s', outFile)
	ncwrite(outFile,'XLAIMULTI',dataSliceMulti);

        tNow = toc(tTimer);
    end
end
fprintf(' done.\n');

end

