% make surface forcing files for global model configuration

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% zonal surface wind forcing

% load and plot windx file
% file created by Alistair for integrating a 4-degree global configuration
clear all, close all, cd /dm1/dimitri/mitgcm/matlab
figure(1), plot_2D('/dm1/dimitri/mitgcm/uv/exe/windx',90,40)

% load and average taux.c
% there are 365 records, one for each day of the year
load grid/cgrid
fid=fopen('/if7/medea/tlee/fluxes/taux.c','r','b');
taux=zeros(length(lat),length(lon));
for i=1:365, disp(i)
  if fread(fid,1,'integer*8')~=length(lat)*length(lon)*4, error, end
  taux=taux+fread(fid,[length(lon),length(lat)],'real*4')';
  if fread(fid,1,'integer*8')~=length(lat)*length(lon)*4, error, end
end
taux=taux/365;                       % (dyn/cm^2)
taux=taux/10;                        % (N/m^2)

% save taux file in binary format
fid=fopen('windx','w','b');
tmp=fwrite(fid,length(taux(:))*4,'integer*8'); % fortran record length
tmp=fwrite(fid,taux','real*4');
tmp=fwrite(fid,length(taux(:))*4,'integer*8'); % fortran record length
tmp=fclose(fid);
figure(2), plot_2D('/dm1/dimitri/mitgcm/global/exe/windx',lon,lat)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% meridional surface wind forcing

% load and plot windy file
% file created by Alistair for integrating a 4-degree global configuration
clear all, clf reset, cd /dm1/dimitri/mitgcm/matlab
figure(1), plot_2D('/dm1/dimitri/mitgcm/uv/exe/windy',90,40)

% load and average tauy.c
% there are 365 records, one for each day of the year
load cgrid
fid=fopen('/if7/medea/tlee/fluxes/tauy.c','r','b');
tauy=zeros(length(lat),length(lon));
for i=1:365, disp(i)
  if fread(fid,1,'integer*8')~=length(lat)*length(lon)*4, error, end
  tauy=tauy+fread(fid,[length(lon),length(lat)],'real*4')';
  if fread(fid,1,'integer*8')~=length(lat)*length(lon)*4, error, end
end
tauy=tauy/365;                       % (dyn/cm^2)
tauy=tauy/10;                        % (N/m^2)

% save tauy file in binary format
fid=fopen('windy','w','b');
tmp=fwrite(fid,length(tauy(:))*4,'integer*8'); % fortran record length
tmp=fwrite(fid,tauy','real*4');
tmp=fwrite(fid,length(tauy(:))*4,'integer*8'); % fortran record length
tmp=fclose(fid);
figure(2), plot_2D('windy',lon,lat)
