0001 function gain = calculateGains(d)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 k_b = 1.38e-23;
0015
0016
0017 if(length(find(d.index.noise_event.fast))==0)
0018 display('calculateGains:: FAIL: no noise source in your data.');
0019 display('calculateGains:: Can not calibrate your data');
0020 gainVals = [];
0021 return;
0022 end
0023
0024 if(~issubfield(d, 'correction', 'alpha', 'indices'))
0025
0026 [offStartPos onEndPos offEndPos onStartPos] = calcNoiseIndices(d);
0027 d.correction.alpha.indices = [offStartPos onEndPos offEndPos ...
0028 onStartPos];
0029 end
0030
0031 if(isempty(d.correction.alpha.indices))
0032 display('calculateGains:: FAIL: no noise source in your data.');
0033 display('calculateGains:: Can not calibrate your data');
0034 gainVals = [];
0035 return;
0036 end
0037
0038
0039 if(~isfield(d.correction.tsys, 'tnoise'))
0040 [time tnoise] = getNoiseTemps(d.array.frame.utc(1), ...
0041 d.array.frame.utc(end));
0042 else
0043 time = d.correction.tsys.time;
0044 tnoise = d.correction.tsys.tnoise;
0045 end
0046
0047
0048 indices = d.correction.alpha.indices;
0049 for m=1:size(indices,1)
0050
0051
0052
0053
0054
0055
0056
0057 switch(size(d.antenna0.receiver.data,2))
0058 case 10
0059 dataNoiseOn = d.antenna0.receiver.data((indices(m,4)+5):indices(m,2), ...
0060 [1 9]);
0061 dataNoiseOff= d.antenna0.receiver.data([indices(m,1):(indices(m,4)-5) ...
0062 (indices(m,2)+5):indices(m,3)], [1 9]);
0063 case 8
0064 dataNoiseOn = d.antenna0.receiver.data((indices(m,4)+5):indices(m,2), ...
0065 [1 8]);
0066 dataNoiseOff= d.antenna0.receiver.data([indices(m,1):(indices(m,4)-5) ...
0067 (indices(m,2)+5):indices(m,3)], [1 8]);
0068 end
0069 PnoiseOn = mean(dataNoiseOn);
0070 PnoiseOff = mean(dataNoiseOff);
0071 Ptime = mean(d.antenna0.receiver.utc(indices(m,1):indices(m,3)));
0072
0073 gain.time(m,1) = Ptime;
0074 gain.power(m,:) = PnoiseOn - PnoiseOff;
0075 end
0076
0077 out_of_range_flag=0;
0078
0079
0080
0081
0082 if(d.correction.survey)
0083 preVal = find(time<d.array.frame.utc(1));
0084 if(length(preVal)>1)
0085 tnoiseBin(1,:) = nanmean(tnoise(preVal,:));
0086 timeBin(1) = nanmean(time(preVal));
0087 elseif(length(preVal)==1)
0088 tnoiseBin(1,:) = tnoise(preVal,:);
0089 timeBin(1) = nanmean(time(preVal));
0090 elseif(length(preVal)==0)
0091
0092 gain.tnoise = nanmean(tnoise(:,:));
0093 gain.tnoise = nanmean(gain.tnoise);
0094 out_of_range_flag=1;
0095 end
0096
0097
0098
0099 postVal = find(time>d.array.frame.utc(1));
0100 if(length(postVal)>1)
0101 tnoiseBin(2,:) = nanmean(tnoise(postVal,:));
0102 timeBin(2) = nanmean(time(postVal));
0103 elseif(length(postVal)==1)
0104 tnoiseBin(2,:) = tnoise(postVal,:);
0105 timeBin(2) = nanmean(time(postVal));
0106 elseif(length(postVal)==0)
0107
0108 gain.tnoise = nanmean(tnoise(:,:));
0109 gain.tnoise = nanmean(gain.tnoise);
0110 out_of_range_flag=1;
0111 end
0112
0113 time = timeBin;
0114 tnoise = tnoiseBin;
0115 end
0116
0117
0118
0119
0120
0121 if( out_of_range_flag==0 )
0122 gain.tnoise = interp1(time, tnoise, gain.time);
0123
0124 val = gain.power./(k_b*1e9*gain.tnoise);
0125 else
0126
0127 val = gain.power/(k_b*1e9*gain.tnoise);
0128 end
0129
0130
0131 val = gain.power./(k_b*1e9*gain.tnoise);
0132 gain.val = 10*log10(val);
0133
0134
0135
0136 return;