Home > reduc > flag > flagMask.m

flagMask

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function d = flagMask(d, antenna)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function flagMask(data, antenna)

    looks at the data (from output of read_arc.m) 
     and sets specific flags for given registers:

    this function should be used in conjunction with flagData.m, where
    you choose which one of the following bits will actually make your data
    bad.  The reason they are not the same functions is that this function
    will use more time (it needs to convert flag arrays of different sizes,
    while the other one does not)

     bit0 - FRAME not received from antenna
     bit1 - RECEIVER hot (above 20K)
     bit2 - not TRACKING. (antenna not tracking)
     bit3 - TRACKER lacking status 
     bit4 - ELEVATION below 10 degrees  
     bit5 - WEATHER station data not received
     bit6 - BACKEND wrong mode (not continuous) // with older versions of
     backend code (before June 1, 2011)
     bit7 - BACKEND data backlog (fifo backlog greater than 10samples)
     bit8 - BACKEND clock not right.
     bit9 - BACKEND 1pps out of sync
     bit10- TIMING issue on Cryocon or Servo
     bit11- TIMING issue on Backend
     bit12- BACKEND saturated integration shortfall.
     bit13- Sun within sunLim degrees.
     bit14- Moon within moonLim degrees
     bit15- CONTROL BUILDING (az between 214.5 adn 226)

 En passant, flagMask creates d.antenna0.servo.solarDist and
 d.flags.dayNight.

-----------------------------------------------------------
 Edited 12/4/2013 by Paddy: Check sun or moon above horizon before setting
 bits 13 & 14.
 Edited 15/4/2014 by Paddy: New antenna variable, uses OVRO horizon
 profile to determine sun/Moon rise. Creates solarDist and
 dayNight columns in the data structure.
                             
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function d = flagMask(d, antenna)
0002 
0003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0004 %  function flagMask(data, antenna)
0005 %
0006 %    looks at the data (from output of read_arc.m)
0007 %     and sets specific flags for given registers:
0008 %
0009 %    this function should be used in conjunction with flagData.m, where
0010 %    you choose which one of the following bits will actually make your data
0011 %    bad.  The reason they are not the same functions is that this function
0012 %    will use more time (it needs to convert flag arrays of different sizes,
0013 %    while the other one does not)
0014 %
0015 %     bit0 - FRAME not received from antenna
0016 %     bit1 - RECEIVER hot (above 20K)
0017 %     bit2 - not TRACKING. (antenna not tracking)
0018 %     bit3 - TRACKER lacking status
0019 %     bit4 - ELEVATION below 10 degrees
0020 %     bit5 - WEATHER station data not received
0021 %     bit6 - BACKEND wrong mode (not continuous) // with older versions of
0022 %     backend code (before June 1, 2011)
0023 %     bit7 - BACKEND data backlog (fifo backlog greater than 10samples)
0024 %     bit8 - BACKEND clock not right.
0025 %     bit9 - BACKEND 1pps out of sync
0026 %     bit10- TIMING issue on Cryocon or Servo
0027 %     bit11- TIMING issue on Backend
0028 %     bit12- BACKEND saturated integration shortfall.
0029 %     bit13- Sun within sunLim degrees.
0030 %     bit14- Moon within moonLim degrees
0031 %     bit15- CONTROL BUILDING (az between 214.5 adn 226)
0032 %
0033 % En passant, flagMask creates d.antenna0.servo.solarDist and
0034 % d.flags.dayNight.
0035 %
0036 %-----------------------------------------------------------
0037 % Edited 12/4/2013 by Paddy: Check sun or moon above horizon before setting
0038 % bits 13 & 14.
0039 % Edited 15/4/2014 by Paddy: New antenna variable, uses OVRO horizon
0040 % profile to determine sun/Moon rise. Creates solarDist and
0041 % dayNight columns in the data structure.
0042 %
0043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0044 
0045 rxTempLim = 20;
0046 elevLim   = 10;
0047 sunLim    = 30;
0048 moonLim   =  5;
0049 controlBldg = [214.5 226];
0050 
0051 if ~exist('antenna','var')
0052     antenna = 1;
0053 end
0054 
0055 switch antenna
0056     case 1
0057         antname = 'ovro';
0058     case 2
0059         antname = 'hrao';
0060     case 3
0061         antname = 'karoo';
0062     otherwise
0063         error('Antenna #%d not known to C-BASS',antenna)
0064 end
0065 
0066 % first we check if the data we're getting from the backend is ok.
0067 if(length(unique(d.antenna0.receiver.utc)) ~= ...
0068       length(d.antenna0.receiver.utc))
0069   display('BACKEND IN BAD STATE DURING TRACK');
0070   display('ALL YOUR DATA IS BAD');
0071   error('Pick a different time to analyze');
0072 %  return; Not needed as error function returns anyway.
0073 end
0074 
0075 % we need flags of many different sizes
0076 % interpolating each individual flag onto the other size is much faster than
0077 % writing a for-loop at the end.
0078 
0079 % first we set up the flags
0080 d.flags.mask.slow   = zeros(size(d.array.frame.utc));
0081 d.flags.mask.medium = zeros(size(d.antenna0.servo.utc));
0082 d.flags.mask.fast   = zeros(size(d.antenna0.receiver.utc));
0083 
0084 %%%%% flags that are from data taken at 1Hz %%%%%
0085 % frame received
0086 flagRegTemp = double(d.antenna0.frame.received ~=2);
0087 d = applyFlag(d, flagRegTemp, [],[],0);
0088 
0089 % rx cold - temp sesors set upi differently for cbass N and S
0090 % Also, temp sensor on LNA 3 is broken on cbass-S
0091 antenna
0092 switch antenna
0093     case 1
0094         flagRegTemp = d.antenna0.thermal.lsTemperatureSensors(:,1:4) > rxTempLim; % cbass-n temps
0095     case 2
0096         flagRegTemp = d.antenna0.thermal.lsTemperatureSensors(:,[5,6,8]) > rxTempLim; % cbass-s temps
0097     case 3
0098         flagRegTemp = d.antenna0.thermal.lsTemperatureSensors(:,[5,6,8]) > rxTempLim; % cbass-s temps
0099 end
0100 
0101 flagRegTemp = sum(flagRegTemp,2)>0;
0102 d = applyFlag(d, flagRegTemp, [],[],1);
0103 
0104 % not tracking
0105 flagRegTemp = double(d.antenna0.tracker.offSource);
0106 d = applyFlag(d, flagRegTemp, [],[],2);
0107 
0108 % tracker lacking
0109 flagRegTemp = (double(d.antenna0.tracker.lacking>0 & ...
0110     d.antenna0.tracker.lacking<1025));
0111 d = applyFlag(d, flagRegTemp, [],[],3);
0112 
0113 %weather station
0114 flagRegTemp = double(d.array.weather.status~=0);
0115 d = applyFlag(d, flagRegTemp, [],[],5);
0116 
0117 %%%%% flags at data rate of 5Hz %%%%%
0118 % (raw servo) elevation below elevLim degrees or with 2 deg of zenith
0119 if(isfield(d.antenna0.servo, 'el'))
0120   flagServTemp = double(d.antenna0.servo.el < elevLim  | ...
0121       d.antenna0.servo.el > 88) ;
0122   d = applyFlag(d, [], [], flagServTemp, 4);
0123 else
0124   flagServTemp = double(d.antenna0.servo.fast_el_pos < elevLim | ...
0125       d.antenna0.servo.fast_el_pos > 88) ;
0126   d = applyFlag(d, [], flagServTemp,[],4);
0127 end
0128 
0129 % cryocon or servo 1pps issue
0130 flagServTemp = d.flags.interpFlags.medium;
0131 d = applyFlag(d, [], flagServTemp, [], 10);
0132 
0133 %%%%% flags at 100Hz %%%%%
0134 % if set to high, backend not in continous mode
0135 if(d.array.frame.utc(1) < date2mjd(2011,06, 01))
0136   flagRxTemp   = bitand(d.antenna0.receiver.flags, 2^4)==0;
0137   d = applyFlag(d, [], [],flagRxTemp, 6);
0138 end
0139 
0140 %% Backend flags - need to work out what we use for CBASS-S**
0141 switch antenna
0142     case 1
0143         % backend data backlog (128)
0144         flagRxTemp = d.antenna0.receiver.diagnostics(:,1)>10;
0145         d = applyFlag(d, [], [],flagRxTemp, 7);
0146         
0147         % backend Clock Error (256)
0148         % if it's set to high, the clock is ok.
0149         flagRxTemp = bitand(d.antenna0.receiver.flags, 2^8)==0;
0150         d = applyFlag(d, [], [],flagRxTemp, 8);
0151         
0152         % backend 1PPS (512)
0153         % if it's set to high, 1pps is good.
0154         flagRxTemp = bitand(d.antenna0.receiver.flags, 2^9)==0;
0155         d = applyFlag(d, [], [],flagRxTemp, 9);
0156         
0157         % backend timing issue
0158         flagRxTemp = d.flags.interpFlags.fast;
0159         d = applyFlag(d, [], [], flagRxTemp, 11);
0160         
0161         % backend saturated integration shortfall (4096)
0162         flagRxTemp = d.antenna0.receiver.diagnostics(:,4) == 254;
0163         d = applyFlag(d, [], [], flagRxTemp, 12);
0164 end
0165 
0166 % Get accurate Az El coords for Sun/Moon flagging
0167 d = apparentAzEl(d,0);
0168 
0169 % sun within  sunlim degrees
0170 [dist az el]  = calcSourceDist(d, 'sun');
0171 d.antenna0.servo.solarDist = dist;
0172 az = wrap360(az);
0173 raz=round(60*az);
0174 raz(raz >= 21600) = 0;
0175 % Read horizon profile
0176 bindata=fitsread(strcat('constants/',antname,'_horizon.fits'),'bintable');
0177 horizonInf = bindata{:,2};
0178 
0179 %%%
0180 % ACT - kludge until we get a horizon profile for south africa
0181 if(antenna>1)
0182     horizonInf=horizonInf*0.0;
0183 end
0184 %%%
0185 
0186 horizonEl = horizonInf(raz+1)';
0187 % Create day/Night flag: 0 = night, 1 = Too close to call, 2 = day
0188 d.flags.dayNight = ones(size(az),'uint8');
0189 d.flags.dayNight(el < (horizonEl - 1.)) = 0;
0190 d.flags.dayNight(el > (horizonEl + 1.)) = 2;
0191 
0192 flagRxTemp=(dist < sunLim) & (el > (horizonEl - 0.25)); % Assumes sun radius = 0.25d
0193 
0194 % flagRxTemp = (dist < sunLim) & (el > 7.75); % assumes horizon at 8degrees & radius = 0.25d.
0195 clear dist az el horizonEl;
0196 d = applyFlag(d, [], [], flagRxTemp, 13);
0197 % sun above horizon
0198 %el       = calcSunLoc(d);
0199 %flagRxTemp = el > sunLim;
0200 %d = applyFlag(d, [], [], flagRxTemp, 13);
0201 
0202 % moon within moonLim degrees
0203 [dist az el]      = calcSourceDist(d, 'moon');
0204 az = wrap360(az);
0205 raz = round(60*az);
0206 raz(raz >= 21600) = 0;
0207 horizonEl = horizonInf(raz+1)';
0208 flagRxTemp = (dist < moonLim) & (el > (horizonEl - 0.25));
0209 clear dist az el horizonEl;
0210 d = applyFlag(d, [], [], flagRxTemp, 14);
0211 
0212 % control building within view
0213 %flagServTemp = d.antenna0.servo.fast_az_pos > controlBldg(1) & ...
0214 %    d.antenna0.servo.fast_az_pos < controlBldg(2);
0215 %d = applyFlag(d, [], flagServTemp, [], 15);
0216 
0217 
0218 d.flags = rmfield(d.flags, 'interpFlags');
0219 
0220 
0221 % now we should have a bitMask for these values.
0222 
0223 % initialize all variables to zero
0224 d.flags.bit.slow   = uint32(zeros(size(d.flags.mask.slow)));
0225 d.flags.bit.medium = uint32(zeros(size(d.flags.mask.medium)));
0226 d.flags.bit.fast   = uint32(zeros(length(d.flags.mask.fast), 3));
0227 
0228 d.flags.slow   = false(size(d.flags.mask.slow));
0229 d.flags.medium = false(size(d.flags.mask.medium));
0230 d.flags.fast   = false(length(d.flags.mask.fast), 3);
0231 
0232 return;
0233 
0234 
0235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0236 function d = applyFlag(d, flagRegTemp, flagServTemp, ...
0237     flagRxTemp, bitNum)
0238 
0239 if(~isempty(flagRegTemp))
0240   flagServTemp = repmat(flagRegTemp, [1 5]);
0241   flagServTemp = flagServTemp';
0242   flagServTemp = flagServTemp(:);
0243 
0244   flagRxTemp   = repmat(flagRegTemp, [1 100]);
0245   flagRxTemp   = flagRxTemp';
0246   flagRxTemp   = flagRxTemp(:);
0247   
0248 elseif(~isempty(flagServTemp))
0249   flagRegTemp = reshape(flagServTemp, [5, length(flagServTemp)/5]);
0250   flagRegTemp = mean(flagRegTemp) == 1;
0251   flagRegTemp = flagRegTemp';
0252 
0253   flagRxTemp   = repmat(flagServTemp, [1 20]);
0254   flagRxTemp   = flagRxTemp';
0255   flagRxTemp   = flagRxTemp(:);
0256 elseif(~isempty(flagRxTemp))
0257   flagRegTemp = reshape(flagRxTemp, [100, length(flagRxTemp)/100]);
0258   flagRegTemp = mean(flagRegTemp) == 1;
0259   flagRegTemp = flagRegTemp';
0260   
0261   flagServTemp = reshape(flagRxTemp, [20, length(flagRxTemp)/20]);
0262   flagServTemp = mean(flagServTemp) == 1;
0263   flagServTemp = flagServTemp';
0264   else
0265   error('No flags passed');
0266 end
0267 
0268 d.flags.mask.slow   = d.flags.mask.slow + 2^(bitNum)*double(flagRegTemp);
0269 d.flags.mask.medium = d.flags.mask.medium   + 2^(bitNum)*double(flagServTemp);
0270 d.flags.mask.fast   = d.flags.mask.fast+ 2^(bitNum)*double(flagRxTemp);  
0271 
0272 return;
0273 
0274 
0275 
0276 %-- FOR REFERENCE -- %
0277 %% backend bitmask
0278 %bit0 - switch_alt
0279 %bit1 - switch_en
0280 %bit2 - noise_on
0281 %bit3 - simulate_en
0282 %bit4 - cont_mode
0283 %bit5 - nonlin_en
0284 %bit6 - fifo empty
0285 %bit7 - 1pps rolover
0286 %bit8 - dcm_locked
0287 %bit9 - pps_sync
0288 
0289 %% tracker lacking bitmask
0290 %bit0 - site
0291 %bit1 - atmosphere
0292 %bit2 - ut1utc
0293 %bit3 - eqneqx
0294 %bit4 - enc rev counts
0295 %bit5 - tilts
0296 %bit6 - collimation
0297 %bit7 - enc limits
0298 %bit8 - flexure
0299 %bit9 - enc zeros
0300 %bit10- location
0301 
0302

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005