| 1 | enderton | 1.1 | function CalcOcnForcingFields(cplFiles,gridFiles,prevFocnFiles,period,... | 
| 2 |  |  | outDir,FocnRoot,focnRoot,iitC,fitC,ditC,... | 
| 3 |  |  | dtC,gWeight,FocnOnly,InspectFocn,Inspectfocn) | 
| 4 |  |  |  | 
| 5 |  |  | fields = {'icL','taux','tauy','EmP','Qnet'}; | 
| 6 |  |  | mncfld = {'SIC','TX'  ,'TY'  ,'FW' ,'HF'  }; | 
| 7 |  |  |  | 
| 8 |  |  | gravity = 9.81; | 
| 9 |  |  |  | 
| 10 |  |  | warning off MATLAB:MKDIR:DirectoryExists; | 
| 11 |  |  | mkdir(outDir); | 
| 12 |  |  |  | 
| 13 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 14 |  |  | %                                 Load Data                               % | 
| 15 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 16 |  |  |  | 
| 17 | enderton | 1.2 | datacpl = rdmnc(cplFiles,'iter','HFtave','TXtave',... | 
| 18 |  |  | 'TYtave','FWtave','SICtave'); | 
| 19 |  |  | datagrd = rdmnc(gridFiles,'rA','XG','YG','XC','YC'); | 
| 20 |  |  | RAC = datagrd.rA; | 
| 21 |  |  | XC = datagrd.XC; XG = datagrd.XG(1:size(RAC,1),1:size(RAC,2)); | 
| 22 |  |  | YC = datagrd.YC; YG = datagrd.YG(1:size(RAC,1),1:size(RAC,2)); | 
| 23 |  |  | %save('DataPerCpl.mat','datacpl','RAC','XG','YG','XC','YC'); | 
| 24 |  |  | %load('DataPerCpl.mat'); | 
| 25 | enderton | 1.1 |  | 
| 26 |  |  |  | 
| 27 |  |  | % If a new focn is to be calculated, the old Focn must be loaded. | 
| 28 |  |  | if ~FocnOnly | 
| 29 |  |  | for ifield=1:length(mncfld) | 
| 30 |  |  | filename = [prevFocnFiles,'/',FocnRoot,'.',... | 
| 31 |  |  | fields{ifield},'.bin.',num2str(period-1)]; | 
| 32 |  |  | fid=fopen(filename,'r','b'); | 
| 33 |  |  | dataFocnOld.(mncfld{ifield}) = ... | 
| 34 |  |  | reshape(fread(fid,'real*8'),size(RAC)); | 
| 35 |  |  | fclose(fid); | 
| 36 |  |  | end | 
| 37 |  |  | end | 
| 38 |  |  |  | 
| 39 |  |  |  | 
| 40 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 41 |  |  | %                      Select Coupled Field Data                          % | 
| 42 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 43 |  |  |  | 
| 44 |  |  | test  = ismember([iitC:ditC:fitC],datacpl.iter); | 
| 45 |  |  | index = ismember(datacpl.iter,[iitC:ditC:fitC]); | 
| 46 |  |  | if ~isempty(find(test==0)) | 
| 47 |  |  | error('Specified indecies not present in cpl_tave.* files!!'); | 
| 48 |  |  | end | 
| 49 |  |  | for ifield = 1:length(mncfld) | 
| 50 |  |  | dataCplSelect.([mncfld{ifield},'tave']) = ... | 
| 51 |  |  | datacpl.([mncfld{ifield},'tave'])(:,:,index); | 
| 52 |  |  | end | 
| 53 |  |  |  | 
| 54 |  |  |  | 
| 55 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 56 |  |  | %                            Prepare Focn                                 % | 
| 57 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 58 |  |  |  | 
| 59 |  |  | % What if it is not in year increments?  Need a more sophisticated | 
| 60 |  |  | % treatment of finding Focn.  Should compute monthly mean before.  Test | 
| 61 |  |  | % with various sorts of coupled field outputs. | 
| 62 |  |  | for ifield = 1:length(mncfld) | 
| 63 |  |  | FocnNew = mean(dataCplSelect.([mncfld{ifield},'tave']),3); | 
| 64 |  |  | FocnNew = DataCorrections(FocnNew,mncfld{ifield},gravity,RAC); | 
| 65 |  |  | if FocnOnly | 
| 66 |  |  | Focn = FocnNew; | 
| 67 |  |  | else | 
| 68 | enderton | 1.3 | FocnOld = dataFocnOld.(mncfld{ifield}); | 
| 69 |  |  | if isequal(mncfld{ifield},'FW') | 
| 70 |  |  | FocnOld = DataCorrections(FocnOld,mncfld{ifield},gravity,RAC); | 
| 71 |  |  | end | 
| 72 | enderton | 1.1 | Focn = (gWeight*FocnOld + FocnNew)/(gWeight+1); | 
| 73 |  |  | end | 
| 74 |  |  | fid=fopen([outDir,'/',FocnRoot,'.',fields{ifield},... | 
| 75 |  |  | '.bin.',num2str(period)],'w','b'); | 
| 76 |  |  | fwrite(fid,Focn,'real*8'); | 
| 77 |  |  | fclose(fid); | 
| 78 |  |  | dataFocn.(mncfld{ifield}) = Focn; | 
| 79 |  |  | end | 
| 80 |  |  |  | 
| 81 |  |  |  | 
| 82 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 83 |  |  | %                         Prepare focn as needed                          % | 
| 84 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 85 |  |  |  | 
| 86 |  |  | if ~FocnOnly | 
| 87 |  |  |  | 
| 88 |  |  | years = [iitC:ditC:fitC].*dtC./31104000; | 
| 89 |  |  | yrfrc = rem(years,floor(years)); | 
| 90 |  |  | yrfrc(yrfrc == 0) = 1; | 
| 91 |  |  | uyrfrc = sort(unique(yrfrc)); | 
| 92 |  |  | length(yrfrc) | 
| 93 |  |  | length(uyrfrc) | 
| 94 |  |  |  | 
| 95 |  |  | for ifield = 1:length(mncfld) | 
| 96 |  |  |  | 
| 97 |  |  | % Make yearly forcing file. | 
| 98 |  |  | focnFull = dataCplSelect.([mncfld{ifield},'tave']); | 
| 99 |  |  | focn = NaN.*zeros([size(RAC,1),size(RAC,2),length(uyrfrc)]); | 
| 100 |  |  | for itime = 1:length(uyrfrc) | 
| 101 |  |  | index = find(uyrfrc(itime) == yrfrc); | 
| 102 |  |  | focn(:,:,itime) = mean(focnFull(:,:,index),3); | 
| 103 |  |  | end | 
| 104 |  |  | if ~isempty(find(isnan(focn(:)))) | 
| 105 |  |  | error('NaNs in ocean forcing files!!'); | 
| 106 |  |  | end | 
| 107 |  |  |  | 
| 108 |  |  | % Remove mean, add Focn | 
| 109 |  |  | SubMean = mean(focn,3); | 
| 110 |  |  | AddMean = dataFocn.(mncfld{ifield}); | 
| 111 |  |  | for itime = 1:size(focn,3) | 
| 112 |  |  | focn(:,:,itime) = focn(:,:,itime) - SubMean + AddMean; | 
| 113 |  |  | end | 
| 114 |  |  |  | 
| 115 |  |  | % Where ice, make Tau_x, Tau_y equal zero. | 
| 116 |  |  | if isequal(mncfld{ifield},'SIC') | 
| 117 |  |  | iceMask = (focn==0); | 
| 118 |  |  | elseif ismember(mncfld{ifield},{'TX','TY'}) | 
| 119 |  |  | focn = focn.*iceMask; | 
| 120 |  |  | end | 
| 121 |  |  |  | 
| 122 |  |  |  | 
| 123 |  |  | focnmean = mean(focn,3); | 
| 124 |  |  | focnmean = sum(focnmean(:).*RAC(:))./sum(RAC(:)); | 
| 125 |  |  | disp([mncfld{ifield},' mean:  ',num2str(focnmean)]); | 
| 126 |  |  |  | 
| 127 |  |  | fid=fopen([outDir,'/',focnRoot,'.',fields{ifield},'.bin'],'w','b'); | 
| 128 |  |  | fwrite(fid,focn,'real*8'); | 
| 129 |  |  | fclose(fid); | 
| 130 |  |  | end | 
| 131 |  |  | end | 
| 132 |  |  |  | 
| 133 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 134 |  |  | %                            Inspect Focn                                 % | 
| 135 |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 136 |  |  |  | 
| 137 |  |  | if InspectFocn | 
| 138 |  |  | for ifield = 1:length(mncfld) | 
| 139 |  |  | fid=fopen([outDir,'/',FocnRoot,'.',fields{ifield},... | 
| 140 |  |  | '.bin.',num2str(period)],'r','b'); | 
| 141 |  |  | Focn = reshape(fread(fid,'real*8'),size(RAC)); | 
| 142 |  |  | fclose(fid); | 
| 143 |  |  | figure(ifield); | 
| 144 |  |  | merccube(XG,YG,Focn); colorbar; | 
| 145 |  |  | title(mncfld{ifield}); | 
| 146 |  |  | end | 
| 147 |  |  | end | 
| 148 |  |  |  | 
| 149 |  |  | if Inspectfocn | 
| 150 |  |  | for ifield = 1:length(mncfld) | 
| 151 |  |  | figure(ifield); orient tall; | 
| 152 |  |  | fid=fopen([outDir,'/',focnRoot,'.',fields{ifield},'.bin'],'r','b'); | 
| 153 |  |  | focn = reshape(fread(fid,'real*8'),[size(RAC),12]); | 
| 154 |  |  | fclose(fid); | 
| 155 |  |  | crange = [min(focn(:)),max(focn(:))]; | 
| 156 |  |  | for ii = 1:size(focn,3) | 
| 157 |  |  | subplot(4,3,ii); merccube(XG,YG,focn(:,:,ii)); | 
| 158 |  |  | shading flat; caxis(crange); colorbar; | 
| 159 |  |  | title([mncfld{ifield},' month ',num2str(ii)]); | 
| 160 |  |  | end | 
| 161 |  |  | end | 
| 162 |  |  | end | 
| 163 |  |  |  | 
| 164 |  |  |  | 
| 165 |  |  | % Field dependent data corrections. | 
| 166 |  |  | function CorrectedData = ... | 
| 167 |  |  | DataCorrections(DataToCorrect,CorrectionField,gravity,RAC) | 
| 168 |  |  | if     isequal(CorrectionField,'SIC') | 
| 169 |  |  | CorrectedData = gravity.*DataToCorrect; | 
| 170 |  |  | elseif isequal(CorrectionField,'FW') | 
| 171 |  |  | offset = sum(DataToCorrect(:).*RAC(:))./sum(RAC(:)); | 
| 172 |  |  | CorrectedData = DataToCorrect - offset; | 
| 173 |  |  | else | 
| 174 |  |  | CorrectedData = DataToCorrect; | 
| 175 |  |  | end | 
| 176 | enderton | 1.2 |  |