Lecture 12 Program Improvement Jeff Henrikson jhenriksumd edu

  • Slides: 11
Download presentation
Lecture 12 Program Improvement Jeff Henrikson (jhenriks@umd. edu) http: //www. atmos. umd. edu/~jeff/aosc 347/

Lecture 12 Program Improvement Jeff Henrikson (jhenriks@umd. edu) http: //www. atmos. umd. edu/~jeff/aosc 347/ 1

Import/Export ASCII File with Known Data Format: Import Export n csvread n csvwrite n

Import/Export ASCII File with Known Data Format: Import Export n csvread n csvwrite n dlmread n dlmwrite n textscan n textread n fscanf Cell Array {. . } – access cellplot – visualize celldisp – display File with Unknown Data Format: n uiimport Import Wizard 2 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

Atmospheric Sounding Open Explorer & go to http: //weather. uwyo. edu n Navigate to

Atmospheric Sounding Open Explorer & go to http: //weather. uwyo. edu n Navigate to Upper Air Observations Soundings n n From the menu select Region: North America Type of plot: Text List Year: 2012 Month: Jul From: 15/12 Z To: 15/12 Z n Find Sterling VA (IAD) on the map and click 3 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

urlread Copy & Paste urlname here Type in the Command Window >> a =

urlread Copy & Paste urlname here Type in the Command Window >> a = urlread ('http: //weather. uwyo. edu/cgi. . . M=72403') Do not forget the quotes 4 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

urlreads all characters in the source file Source file contains data and formatting text

urlreads all characters in the source file Source file contains data and formatting text We need only the data not the formatting text 5 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

urlread a is an array of thousands of characters! We have to locate the

urlread a is an array of thousands of characters! We have to locate the actual data within a 6 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

Locate Data Start & End Data are bound by delimiters Scroll down Search for

Locate Data Start & End Data are bound by delimiters Scroll down Search for delimiters: strfind, regexp, strtok, etc. 7 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

String Find / Length Position=533 Position=610 Linefeed >>L 1 = ' 1010. 0 93

String Find / Length Position=533 Position=610 Linefeed >>L 1 = ' 1010. 0 93 23. 2 22. 4 95 17. 21 205 2 295. 5 >>Id = length(L 1)+1; % length of 1 st data line >>Ib = strfind(a, L 1); % position of 1 st char >>Ie = Ib + Id-1; % position of last char =610 345. 1 298. 6'; =78 =533 8 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

Get Data Structure Line 1 Line 2 Start 533 611 End 610 687 >>

Get Data Structure Line 1 Line 2 Start 533 611 End 610 687 >> Je = strfind (a, '</PRE>'); % data bound >> Ix = Je(1)-1; % last data char = 9892 >> Ln = (Ix-Ib+1)/Id; % total num of data lines = 120 for l = 1: Ln i 1 = Ib+(l-1)*Id; i 2 = i 1+Id; disp (a(i 1: i 2)) end 9 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

Convert String to Data We can read multiple lines and convert each line into

Convert String to Data We can read multiple lines and convert each line into a 1 -d array >> b = str 2 num ( a (i 1: i 2) ) We need to convert the whole table into a 2 -d array # of rows = num of data lines to read # of columns = num of data fields (=11) >> if length ( b ) < 11 indicate missing data => ignore the line Missing data 10 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang

Save this code into an M-File called L 12 E 1. m Final Code

Save this code into an M-File called L 12 E 1. m Final Code close; clear; clc a = urlread('http: //weather. uwyo. edu/cgi-bin/sounding…M=74560'); L 1 =' 1010. 0 93 23. 2 22. 4 95 17. 21 205 2 295. 5 345. 1 298. 6'; Id = length(L 1)+1; % length of 1 st data line Ib = strfind(a, L 1); % position of 1 st char Ie = Ib + Id-1; % position of last char Je = strfind (a, '</PRE>'); % data bound Ix = Je(1)-1; % last data char Ln = (Ix-Ib+1)/Id; % total no of data lines Fn = 11; % no of fields Rn = 0; % no of valid data records for l = 1: Ln i 1 = Ib+(l-1)*Id; i 2 = i 1+Id; b = str 2 num (a(i 1: i 2)); if (length(b)==Fn) % add the data if no missing Rn = Rn+1; z(Rn, : ) = b(: ); end disp([z(: , 1), z(: , Fn)]); % for visual validation plot( z(: , 3), z(: , 1) ); set(gca, 'ydir', 'rev'); title('Sounding at Sterling'); xlabel('Temperature (C)'); ylabel('Pressures (h. Pa)'); 11 Copyright © 2015 University of Maryland. This material may not be reproduced or redistributed in whole or in part without written permission from Xin-Zhong Liang