%let name=tornado;
filename odsout '.';

goptions xpixels=750 ypixels=700;

%let f1_color=cxfb6a4a;
%let f2_color=cxef3b2c;
%let f3_color=cxcb181d;
%let f4_color=cxa50f15;
%let f5_color=cx67000d;
%let fu_color=graybb;


data my_map; set mapsgfk.us_states (where=(density<=2) drop=resolution);
run;

data my_mapdata; set mapsgfk.us_states;
run;

filename torfile 'tornado.dat';
data tornadoes (drop=dataline);
/* the CRLF allows dos sas to easily read the pc-formatted text */
infile torfile;
input dataline $ 1-80;
yr=0;       yr=      substr(dataline,1,2);
seq=0;      seq=     substr(dataline,3,2);
state=0;    state=   substr(dataline,6,2);
month=0;    month=   substr(dataline,8,2);
day=0;      day=     substr(dataline,10,2);
hr=0;       hr=      substr(dataline,12,2);
mn=0;       mn=      substr(dataline,14,2);
zone=0;     zone=    substr(dataline,16,1);
accuracy=0; accuracy=substr(dataline,17,1);
event=0;    event=   substr(dataline,18,1);  /* tornado='1' */
rmks=0;     rmks=    substr(dataline,19,1);
states=0;   states=  substr(dataline,20,1);
segments=0; segments=substr(dataline,21,1);
seg_num=0;  seg_num= substr(dataline,22,1);
/* begin point, lat/long in degrees & minutes */
blatdeg=0;  blatdeg= substr(dataline,23,2);
blatmin=0;  blatmin= substr(dataline,25,2);
blondeg=0;  blondeg= substr(dataline,27,3);
blonmin=0;  blonmin= substr(dataline,30,2);
/* end point, lat/long in degrees & minutes */
elatdeg=0;  elatdeg= substr(dataline,32,2);
elatmin=0;  elatmin= substr(dataline,34,2);
elondeg=0;  elondeg= substr(dataline,36,3);
elonmin=0;  elonmin= substr(dataline,39,2);
length=0;   length=  substr(dataline,41,4);
on_grd=0;   on_grd=  substr(dataline,45,1);
/* 46-48 are usually blank */
typepath=0; typepath=substr(dataline,46,1);
rotation=0; rotation=substr(dataline,47,1);
typevis=0;  typevis= substr(dataline,48,1);
path=0;     path=    substr(dataline,49,3); 
deaths=0;   deaths=  substr(dataline,52,3);
injuries=0; injuries=substr(dataline,55,3);
damage=0;   damage=  substr(dataline,59,1);
county=0;   county=  substr(dataline,75,2);
f_scale=0;  f_scale= substr(dataline,78,1);
            pl_scale=substr(dataline,80,1);
run;

data tornadoes; set tornadoes;
/* If the ending lat/long is blank, set it same as beginning lat/long */
if elatdeg=. then elatdeg=blatdeg;
if elatmin=. then elatmin=blatmin;
if elondeg=. then elondeg=blondeg;
if elonmin=. then elonmin=blonmin;
run;

/* Output 2 separate obsns for each tornado segment */
data tornadoes; set tornadoes;

point='begin';
latitude=blatdeg+(blatmin/60);
longitude=blondeg+(blonmin/60);
output;

point='end';
latitude=elatdeg+(elatmin/60);
longitude=elondeg+(elonmin/60);
output;

run;

data tornadoes; set tornadoes;
anno_flag=1;
long=-1*longitude;
lat=latitude;
run;

/* project the map */
data combined; set my_map tornadoes; run;
proc gproject data=combined out=combined latlong eastlong degrees dupok project=robinson
  longmin=-90
  longmax=-78
  latmax=43
  latmin=31
  ;
  id state;
run;
data my_map tornadoes; set combined;
  if anno_flag=1 then output tornadoes;
  else if fipstate(state) not in ('LA' 'FL') then output my_map;
run;



/* Create a line segment between each segment point */
data line_anno; set tornadoes (keep = x y point f_scale); 
length function color $8;
xsys='2'; ysys='2'; hsys='3'; when='a';
  position='5';
  size=.3;
  if point eq 'begin' then function='move';
  else if point eq 'end' then function='draw';
  color="&fu_color"; /* default/unknown color */
  if (f_scale eq 5) then color="&f5_color";   /* F-5 tornado */
  else if (f_scale eq 4) then color="&f4_color";
  else if (f_scale eq 3) then color="&f3_color";
  else if (f_scale eq 2) then color="&f2_color";
  else if (f_scale eq 1) then color="&f1_color";
run;

/* 
Since lines don't support charttip/drilldown, also annotate some 'invisible' pies
(behind/before the map, for the charttip & drilldown)...
*/
data circle_anno; set tornadoes; 
length function style color $12 html $1024;
xsys='2'; ysys='2'; hsys='3'; when='b';
  function='pie'; style='pempty'; color='white'; position='5'; rotate=360; size=.5; 
  html=
  'title='||quote( 
    'Category: F-'||trim(left(f_scale))||'0d'x||
    'Fatalities: '||trim(left(deaths))||'0d'x||
    'Injuries: '||trim(left(injuries))||'0d'x||
    'Damage: '||trim(left(damage))||'0d'x||
    'State: '||trim(left(fipstate(state)))||'0d'x||
    'HH:MM: '||trim(left(hr))||':'||trim(left(mn))||'0d'x||
    'Longitude: '||trim(left(put(longitude,comma5.1)))||'0d'x||
    'Latitude: '||trim(left(put(latitude,comma5.1))))||
  ' href='||quote('http://maps.google.com/maps?ie=UTF8&ll='||trim(left(latitude))||','||trim(left(longitude))||'&z=14');
run;

data all_anno; set line_anno circle_anno;
run;


/* Now, create a 'centers' dataset with the estimated centers of each 
   visible piece of each state. */
%annomac;
%centroid( my_map, centers, state );
data centers; set centers;
xsys='2'; ysys='2'; hsys='3'; when='a';
function='label'; color=''; position='5'; size=2.5; style='';
text=fipstate(state);
/* relocate 1 label the centroid macro didn't place well */
if trim(left(text)) eq 'NC' then x=x+35;
if trim(left(text)) not in ('AR') then output; 
run;

data all_anno;  
 set all_anno centers;
run;


/* Annotate a legend */
data anno_legend;
length function $8 style color $12;
xsys='3'; ysys='3'; hsys='3'; when='A';

/* text */
 function='LABEL'; size=2.1; 
 style=''; 
 position='4'; 
 color='black';
 text='F-5'; y=15; x=90; output;
 text='F-4'; y=y-2.1; output;
 text='F-3'; y=y-2.1; output;
 text='F-2'; y=y-2.1; output;
 text='F-1'; y=y-2.1; output;
 text='Unknown'; y=y-2.1; output;

/* color dashes */
 style='swissb';
 position='6'; 
 color="&f5_color"; text='-'; y=15.25; x=91; output;
 color="&f4_color"; text='-'; y=y-2.1; output;
 color="&f3_color"; text='-'; y=y-2.1; output;
 color="&f2_color"; text='-'; y=y-2.1; output;
 color="&f1_color"; text='-'; y=y-2.1; output;
 color="&fu_color"; text='-'; y=y-2.1; output;
run;


/* Annotate NOAA tornado logo at top of page ... */
data img;
length function text color $8;
xsys='3'; ysys='3'; hsys='3'; when='A';
 html='title="1974 Tornado Outbreak" href="http://www.publicaffairs.noaa.gov/storms/" ';
 function='move'; x=1; y=1; output;
 function='image'; x=x+23; y=y+91; imgpath='outbreak-banner2r.gif'; style='fit'; output;
run;

data other_anno;
 set anno_legend img;
run;

goptions device=png;
goptions border cback=white;
 
ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm"
 (title="1974 Tornado Outbreak") style=htmlblue ;

goptions gunit=pct htitle=5 htext=4 ftitle="albany amt/bold" ftext="albany amt";

title1 ls=1.5 h=6pct link="http://www.publicaffairs.noaa.gov/storms/" "1974 US Tornado Outbreak";
title2 a=90 h=20pct " ";

pattern1 c=cxc7e9c0 v=s;

proc gmap map=my_map data=my_mapdata anno=all_anno;
id state; 
choro state / levels=1 nolegend 
 coutline=white woutline=2
 anno=other_anno
 des='' name="&name";
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
