% ANIMATESONAR.M SCRIPT TO GENERATE MOVIES FROM USGS IMAGENEX FILES % % -- to use this file, put a separate copy of the imagenex data directories (I#####) % containing all the images you want to animate under a single root directory, % then point to that directory when prompted by this program % -- look through the image files and delete files that are 0-5kb, I'm not % sure why these duds appear, but they will hang this movie script % -- You will need to set, manually, in this script, the values of the settings % struct in this script too, where it % says 'get metadata from user' simply set % the correct values in 'def' % -- frame images are written to the local directory and also saved to % Fanmovie or Pencilmovie in the workspace, so if you like MATLAB movies, % save the workspace when this script is done. % -- the best results so far have been by digitizing frames using VideoMach % Microsoft Video 1 codec, 75% compression quality % turn keep duration off % turn automatic off % 5 frames per second % parse imagenex data file names to get the time stamps % analyze time % make movies % % Written by Marinna Martini 2/19/03 % Modified by Chris Sherwood % Last modified 4/29/03 by Marinna Martini % - added time series % U.S. Geological Survey, Woods Hole Field Center close all clear % things you need to set by hand to make this work, maybe I'll make it all % gui some day... % for Eurostrat first deployment 2002-2003 %CurrentsFile = 'E:\EuroStrat\701wh\currents\7011adc-a.nc'; CurrentsFile = 'c:\projects\euro\701wh\7011adc-a.nc'; FirstSonarDay = julian(2002,11,7,0); LastSonarDay = julian(2002,12,8,0); %RootDataDir = 'E:\EuroStrat\701im\test_images_for_movies_single\'; % don't delete these %RootDataDir = 'E:\EuroStrat\701im\test_images_for_movies\'; % don't delete these %RootDataDir = 'E:\EuroStrat\701im\images_for_movies\'; % don't delete these RootDataDir = 'C:\projects\euro\701im\images_for_movies\'; %RootDataDir = 'C:\projects\euro\701im\test_images_for_movies_single\'; %RootDataDir = 'G:\Feb03EuroStrat\701-Particle\701im\'; %RootDataDir = 'C:\crs\data\EuroStrat_0203\701-Particle\701im\'; FanSettings = {'348','0','1','500','0.55','2002'}; PencilSettings = {'132','0','1','500','1.06','2002'}; % pick a bin according to 0.42x total depth % mooring 701 in 9.2 m = 3.864 m depth ADCPbin = 6; % at 3.5448 m loadwaveinfo; % find the indeces to the dates of interest findidx = find(datenum_wave < datenum(gregorian(FirstSonarDay))); if isempty(findidx), wavedateidx(1) = 1; else wavedateidx(1) = max(findidx); end if wavedateidx(1) < 0, wavedateidx(1) = 1; end wavedateidx(2) = min(find(datenum_wave > datenum(gregorian(LastSonarDay)))); % load the currents during the desired time of the ncvarnames = {'u_1205','v_1206','w_1204',}; names = {'u','v','w'}; nc = netcdf(CurrentsFile); timeobj = nc{'time'}; timeobj = autonan(timeobj, 1); time2obj = nc{'time2'}; time2obj = autonan(time2obj, 1); tj=timeobj(:)+time2obj(:)./(3600*1000*24); % find the indeces to the dates of interest findidx = find(tj < FirstSonarDay); if isempty(findidx), dateidx(1) = 1; else dateidx(1) = max(findidx); end if dateidx(1) < 0, dateidx(1) = 1; end dateidx(2) = min(find(tj > LastSonarDay)); % trim time tj=tj(dateidx(1):dateidx(2)); tg=gregorian(tj); currenttm=datenum(tg(:,1),tg(:,2),tg(:,3),tg(:,4),tg(:,5),tg(:,6)); clear tg tj timeobj time2obj % now get the data and reduce to hourly observations for n = 1:length(ncvarnames); ncobj = nc{ncvarnames{n}}; ncobj = autonan(ncobj,1); eval([names{n},' = ncobj(dateidx(1):dateidx(2),:);']) end bindepth = nc{'depth'}(:); waterdepth = nc.WATER_DEPTH(:); close(nc) [hdg, spd] = uv2polar(u, v); clear nc data ncobj % choose one SonartoAnimate = 'Fan'; %SonartoAnimate = 'Pencil'; % ask user to point to a directory of files %RootDataDir = uigetdir('c:\projects\euro\701im','Select the root directory with all the Imagenex files'); %RootDataDir = uigetdir('c:\instment\imagenex','Select the root directory with all the Imagenex files'); SubDataDir = dir(RootDataDir); % get the metadata from the user prompt = {'Enter the sector that was swept:',... 'Enter the center to sweep around:',... 'Enter the Step size used:',... 'Enter the data points setting (points/10):',... 'Enter the sonar''s height off the bottom, m:',... 'Enter the year the instrument was deployed:',... }; if(strcmp(SonartoAnimate,'Fan')), def = FanSettings; elseif(strcmp(SonartoAnimate,'Pencil')), def = PencilSettings; else error('Pencil or Fan?') end dlgtitle = 'Sonar input metadata for the setup'; lineNo = 1; dlgresult = inputdlg(prompt,dlgtitle,lineNo,def); settings.SectorSweep = str2num(dlgresult{1}); settings.AngleSweepAround = str2num(dlgresult{2}); settings.StepSize = str2num(dlgresult{3}); settings.DataPoints = str2num(dlgresult{4}); % number of 'ranges' recorded in each ping settings.Height = str2num(dlgresult{5}); settings.datayear = str2num(dlgresult{6}); % RootMovieDir = uigetdir(pwd,'Select or create a directory to place the image files'); % prompt = {'Enter a root file name:'}; % def = {'frame'}; % dlgtitle = 'Enter a file name for the images'; % lineNo = 1; % dlgresult = inputdlg(prompt,dlgtitle,lineNo,def); % outfile = dlgresult{1}; outfile = 'frame'; outdir = 'frame'; [success,message,messageid] = mkdir(outdir); outfile = fullfile(outdir,outfile); clear FanTime PencilTime Fanidx = 1; Pencilidx = 1; imfig = figure; set(gcf,'Position',[9 37 827 659]) % go through each and parse for d=3:length(SubDataDir), % the first two are always . and .. disp(SubDataDir(d).name) % act only on directories if exist(fullfile(RootDataDir,SubDataDir(d).name)) == 7, DataFiles = dir(fullfile(RootDataDir,SubDataDir(d).name)); for f = 3:length(DataFiles), % disp(DataFiles(f).name) % name convention is S1111020.F50 = S1MMDDHH.?MM % where ? = F for fan or P for pencil a = sscanf(DataFiles(f).name,'S1%c%c%c%c%c%c.%c%c%c') mon = str2num(a(1:2)); day = str2num(a(3:4)); hr = str2num(a(5:6)); min = str2num(a(8:9)); datenum_sonar = datenum(2002,mon,day,hr,min,0); WhichSonar = a(7); if WhichSonar == 'F', % first, display the sonar image FanTime(Fanidx) = julian([settings.datayear mon day hr min 0]); FanDatenum=datenum([settings.datayear mon day hr min 0]); if strcmp(SonartoAnimate,'Fan'), subplot(3,1,1) showfan(fullfile(RootDataDir,SubDataDir(d).name,DataFiles(f).name),... 'polar',settings); ylabel('Fan Beam Sonar, range in meters') hold on [interpdata]=interp1(datenum_wave,[cspd,cdir,do,Dp], ... datenum_sonar) cspdi = interpdata(1); cdiri = interpdata(2); doi = interpdata(3); Dpi = interpdata(4); [xc,yc]=xycoord(cspdi,cdiri); [xw,yw]=xycoord((0.62*doi),Dpi); xw2 = -xw; yw2 = -yw; plot([xw,xw2],[yw,yw2],'-c','linewidth',2) plot([0,xc],[0,yc],'-r','linewidth',2) set(gca,'xtick',[],'ytick',[]) % now, display the time series with the time marked on it subplot(3,1,2) plot(datenum_wave(wavedateidx(1):wavedateidx(2)), ub(wavedateidx(1):wavedateidx(2)).*100) set(gca,'xlim',[currenttm(1) currenttm(end)]) datetick('x','keepticks','keeplimits') text(0.05, 0.8, '\itu_{b}', 'units','normalized') hold on ylims = get(gca,'ylim'); plot([FanDatenum FanDatenum],ylims,'k','linewidth',2); set(gca,'xticklabel',[]) ylabel('cm/s') subplot(3,1,3) plot(currenttm,spd(:,ADCPbin)) set(gca,'xlim',[currenttm(1) currenttm(end)]) datetick('x','keepticks','keeplimits') ylabel('cm/s') hold on text(0.05, 0.8, sprintf('Speed at %2.1f mab',waterdepth-bindepth(ADCPbin)),'units','normalized') ylims = get(gca,'ylim'); plot([FanDatenum FanDatenum],ylims,'k','linewidth',2); % put the plots where we want them for a pretty picture kids = get(gcf,'children'); % get the pointer to the images set(kids(3),'position',[0.13 0.238 0.75 0.75]) set(kids(2),'position',[0.22 0.123 0.58 0.067]) set(kids(1),'position',[0.22 0.032 0.58 0.067]) % it's bizzare that I have to do this, but the y limits % get changed suddenly... not by me, so set them back % so the time line fits set(kids(1),'ylim',ylims); %FanMovie(Fanidx) = getframe(gcf); imfile = sprintf('%s%03d.png',outfile,Fanidx); %print(imfig,'-djpeg90',imfile); % image was a bit muddy, text looked OK %set(gcf,'papersize',[8.5 12]) %set(kids(3),'PlotBoxAspectRatioMode','manual') % don't do this %set(kids(2),'PlotBoxAspectRatioMode','manual') % it crunches the time series plots %set(kids(1),'PlotBoxAspectRatioMode','manual') set(gcf,'paperunits','normalized','renderer','zbuffer') print(imfig,'-dpng',imfile); % best for videomach set(gcf,'paperunits','inches','renderer','painters') end Fanidx = Fanidx+1; elseif WhichSonar == 'P', % first, display the sonar image PencilTime(Pencilidx) = julian([settings.datayear mon day hr min 0]); if strcmp(SonartoAnimate,'Pencil'), subplot(2,1,1) showpencil(fullfile(RootDataDir,SubDataDir(d).name,DataFiles(f).name),... 'polar',settings); %PencilMovie(Pencilidx) = getframe(gcf); imfile = sprintf('%s%03d.png',outfile,Pencilidx); %print(imfig,'-djpeg90',imfile); % image was a bit muddy, text looked OK print(imfig,'-dpng',imfile); % best for videomach end Pencilidx = Pencilidx+1; end end end % clf; end