% get_nc_file.m
%
% Edit addpath for our site.  May 25, 2006.  rcg
%
% open an unknown NetCDF and/or epic file and get the contents
%
% 041220 return meta structure with all the attributes
% 001101 nrg driver for NetCDF and/or epic interface
% if file is NetCDF/EPIC, also gets yearday
%
% REQUIRED:
% set the filename nc_in_file before calling this routine
% ie:  nc_in_file ='scat884g.epic';
% uses netcdf toolbox (required) available at
%      http://sourceforge.net/projects/mexcdf/
%      gregorian, julian, s2hms (required if NetCDF/EPIC)
%
% Set path
% Assume netcdf is in toolbox/netcdf, not local/netcdf:

%%  addpath ([matlabroot '/usr/local/matlab/toolbox/netcdf']);
  addpath ([matlabroot '/toolbox/netcdf']);
%%  addpath ([matlabroot '/toolbox/local/netcdf']);
%%  addpath ([matlabroot '/toolbox/netcdf/ncutility']);  
    

  % Open NetCDF file.
  nc = netcdf(nc_in_file, 'nowrite');          

  % Get global attributes
  attributes = att(nc);  
  for ii=1:length(attributes),
     theRawName = name(attributes{ii});
     theName= strrep(theRawName,' ','_');
     theName= strrep(theName,':','_');
     theName= strrep(theName,';','_');
     theType = datatype(attributes{ii});
     theVal  = attributes{ii}(:);
      switch(theType)
       case 'char'
           % this blows up on multiline strings
	       cmd = sprintf( ...
               'meta.global.%s = ''%s'';', lower(theName),theVal);
       case 'long'
           cmd = sprintf( 'meta.global.%s = %d;', ...
              lower(theName),theVal);
       otherwise 
           cmd=sprintf( 'meta.global.%s = %f;', ...
              lower(theName),theVal);
     end
     fprintf('%d %s %s \n', ii,theType, cmd);
     eval(cmd)
  end
  % Get variables 
  vars = var(nc);                         
  nvars=length(vars);
  isepic = 0; 

  for ii = 1:nvars,
     vname=name(vars{ii});
     % Get data
     cmd=sprintf('%s = vars{%d}(:);', vname,ii);
     eval (cmd);
     if  strncmp( 'time2', vname,5) == 1,
         isepic = 1;
     end
     % Get variables' attributes (added 041220)
     atts=att(vars{ii});
     natts = length(atts);
     for jj = 1:natts,
         attname=name(atts{jj});
         attname= strrep(attname,' ','_');
         if strcmp(attname, '_FillValue'),
             attname = 'FillValue_';   end
         atttype = datatype(atts{jj});
         attvalue = atts{jj}(:);
         cmd=sprintf('meta.variable.%s.%s =',  vname, attname);
         switch(atttype)
             case 'char'
                 cmd2 = sprintf( '''%s'';',attvalue);
             case 'long'
                 cmd2 = sprintf( '%d;',  attvalue);
             otherwise
                 cmd2=sprintf( '%f;', attvalue);
         end
         cmd=[cmd cmd2];
         eval(cmd)
     end
  end
   
% If NetCDF/EPIC, translate dates to yearday
  if  isepic > 0,
     jjday = time + time2/3600/1000/24;
     ymdhms = gregorian(jjday(1));
     year = ymdhms(1,1);
     yday =  jjday - julian(year,1,0);
   end
  
   nc = close(nc);                                 % Close the file.
   clear vars
   


  
  