0001 function [K_best,shift,dorig] = calc_loadcorrect_final(d)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 dcut = cutObs(d, 'blank', 'only');
0027
0028
0029 flags = convertFlagToDataSize(dcut.flags, ...
0030 size(dcut.antenna0.receiver.data,2));
0031
0032 numDims = size(dcut.antenna0.receiver.data,2);
0033
0034
0035 for chan=1:numDims
0036
0037 if(isfield(dcut.antenna0.receiver, 'dataT'))
0038 isT = 1;
0039 temp = [dcut.antenna0.receiver.dataT(:,chan), dcut.antenna0.thermal.coldLoad(:)];
0040 else
0041 isT = 0;
0042 temp = [dcut.antenna0.receiver.data(:,chan), dcut.antenna0.thermal.coldLoad(:)];
0043 end
0044
0045
0046
0047
0048
0049
0050 load_res = temp(:,2)-nanmean(temp(:,2));
0051
0052
0053 sky_res = temp(:,1) - nanmean(temp(:,1));
0054
0055
0056 load_res(isnan(load_res)) = 0;
0057 sky_res(isnan(sky_res)) = 0;
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 ind=0;
0089
0090 if(all(ind)==0)
0091 startInd = 1;
0092 stopInd = length(ind);
0093 else
0094 diffInd = cat(1,0,diff(ind));
0095 startInd = find(diffInd < 0);
0096 stopInd = find(diffInd > 0);
0097 end
0098
0099
0100 length(startInd);
0101 length(stopInd);
0102
0103
0104 if(length(startInd)~=length(stopInd))
0105 if(length(startInd)<length(stopInd))
0106 if(startInd(1)>stopInd(1))
0107 stopInd(1) = [];
0108 end
0109 else
0110 if(startInd(2)<stopInd(1))
0111 startInd(1) = [];
0112 end
0113 end
0114 end
0115
0116 if (length(startInd)>0)
0117
0118
0119
0120 if (length(stopInd)==0)
0121 fstart = startInd(1);
0122 fend = fstart + 99;
0123 else
0124
0125 if startInd(1) > stopInd(1)
0126
0127 startInd = cat(1,1,startInd);
0128 stopInd = cat(1,stopInd,length(ind));
0129 if max(stopInd - startInd) >= 100
0130 firstLong = find((stopInd - startInd) >= 100,1);
0131 else
0132 firstLong = find((stopInd - startInd) >= 50,1);
0133 end
0134 fstart = startInd(firstLong);
0135 fend = fstart + 99;
0136 else
0137 if startInd(end) > stopInd(end)
0138 stopInd = cat(1,startInd(1)-1,stopInd,length(ind));
0139 startInd = cat(1,1,startInd);
0140 if max(stopInd - startInd) >= 100
0141 firstLong = find((stopInd - startInd) >= 100,1);
0142 else
0143 firstLong = find((stopInd - startInd) >= 50,1);
0144 end
0145 fstart = startInd(firstLong);
0146 fend = fstart + 99;
0147 else
0148 if max(stopInd - startInd) >= 100
0149 firstLong = find((stopInd - startInd) >= 100,1);
0150 else
0151 firstLong = find((stopInd - startInd) >= 50,1);
0152 end
0153 fstart = startInd(firstLong);
0154 fend = fstart+99;
0155 end
0156 end
0157 end
0158
0159 shiftIndex = 0;
0160
0161
0162 load_shift = circshift(load_res, shiftIndex);
0163 value(shiftIndex+1) = (dot((sky_res - load_shift), load_shift));
0164 shiftIndex = shiftIndex+1;
0165
0166
0167 [H,I]=max(value);
0168
0169 required_shift = I-1;
0170 shift(chan) = (required_shift);
0171
0172 load_res = circshift(load_res,required_shift);
0173
0174 test_function = @(x) (std((sky_res - x*load_res)));
0175
0176 [x,f_val] = fminsearch(test_function, 1);
0177
0178 K_best(chan) = x;
0179
0180
0181
0182
0183 load_res = circshift(load_res,-required_shift);
0184
0185 else
0186
0187
0188 if (chan ==1)
0189 disp('calc_loadcorrect_final:: All your blank data is flagged')
0190 disp('calc_loadcorrect_final:: Will not apply a correction')
0191 end
0192 K_best(chan) = 0;
0193 shift(chan) = 0;
0194 end
0195 end
0196
0197
0198
0199
0200
0201 for chan = 1:numDims
0202 shifted_load = circshift(load_res,shift(chan));
0203 correction = K_best(chan)*(shifted_load - mean(shifted_load));
0204
0205 if(isT)
0206 dcut.antenna0.receiver.dataT(:,chan) = ...
0207 (dcut.antenna0.receiver.dataT(:,chan))- correction;
0208 else
0209 dcut.antenna0.receiver.data(:,chan) = ...
0210 (dcut.antenna0.receiver.data(:,chan))- correction;
0211 end
0212 end
0213
0214
0215
0216
0217 dorig = d;
0218
0219
0220 return;
0221