classdef omniBox < hgsetget
    %Basically a container to hold omni data and some methods to get it
    %in and out
    
    properties
       numcol
       year
       file
       data
       colinf
    end
    
    methods
        function obj = omniBox(year)
            obj.numcol = 45;
            obj.year = year;
            protype.nickname = 'none';
            protype.label = 'none';
            protype.limits = 'none';
            protype.units = 'none';
            protype.style = 'none';

            for i = 1:obj.numcol
                obj.colinf{i,1} = protype;
            end

        end
        
        function setfile(obj)
            obj.file = sprintf('/hao/aim3/dmsp/omni/omni_5min%d.asc',obj.year)
        end
        
        function readyear(obj)
            
            obj.setfile;
            
            [obj.data, delimiter, nheaderlines] = importdata(obj.file);
            
            obj.setcolinf(1,'year','Year','years',[1995,2008]); 
            obj.setcolinf(2,'doy','Day of Year','days',[1,366]);
            obj.setcolinf(3,'hour','Hour of the Day','hours',[1,24]);
            obj.setcolinf(4,'min','Minute of the Hour','minutes',[0,60]);
            obj.setcolinf(14,'B','Interplanetary Magnetic Field: Magnitude','nT',[0,100]); 
            obj.setcolinf(15,'Bx','Interplanetary Magnetic Field: X Coordinate GSM/GSE','nT',[-60,60]); 
            obj.setcolinf(18,'By','Interplanetary Magnetic Field: Y Coordinate GSM','nT',[-60,60]);
            obj.setcolinf(19,'Bz','Interplanetary Magnetic Field: Z Coordinate GSM','nT',[-60,60]); 
            obj.setcolinf(22,'vsw','Solar Wind Speed','km/s',[200,2000]);
% NOTE:  23,24,25 are Vx(neg),Vy, and Vz Vsw (increased 800 to 2000 km/s)
            obj.setcolinf(23,'Vy','Solar Wind Speed: Y component, GSE','km/s',[200,2000]);
            obj.setcolinf(24,'Vz','Solar Wind Speed: Z component, GSE','km/s',[200,2000]);
            obj.setcolinf(28,'psw','Solar Wind Pressure','nPa',[-5e6,5e6]);
            obj.setcolinf(26,'dsw','Solar Wind Density','#/cc',[0,300]);
            obj.setcolinf(42,'dst','SYM/H index (a.k.a. DST)','nT',[200,-1000]);
            
            
        end
        
        function setcolinf(obj, number, nickname, label, units, limits)
           obj.colinf{number}.nickname = nickname;
           obj.colinf{number}.label = label;
           obj.colinf{number}.units = units;
           obj.colinf{number}.limits = limits;
        end
        
        function num = getcolnum(obj, nickname)
            if ischar(nickname)
                i = 1;
                found = 0;
                while(~found)
                    try
                    col = obj.colinf{i};
                    catch ME1
                        disp(ME1.getReport);
                        error('Failed to find column with nickname %s',nickname);
                    end
                    
                    if strcmp(nickname,col.nickname)
                            found = 1;
                            num = i;
                    end
                    i=i+1;
                end
            end
        end
        
        function [time,val] = inquire(obj, doy, second, datcolname)
            
            datcolnum = obj.getcolnum(datcolname);
            doycol = obj.getcolnum('doy');
            hrcol = obj.getcolnum('hour');
            mincol = obj.getcolnum('min');
            
            hr = floor(second/3600);
            min = floor( (second - hr*3600)/60 );
            
            thisday = obj.data(:,doycol) == doy;
            thishr = thisday & obj.data(:,hrcol) == hr;
            value = thishr &...
                (min.*ones(length(thisday),1) - obj.data(:,mincol)) >= 0 &...
                (min.*ones(length(thisday),1) - obj.data(:,mincol)) < 5;
            
            val = obj.data(value,datcolnum);
            time = hr*3600 + (obj.data(value,mincol))*60;
            
        end
        
  
    end
    
end

