DOWNLOAD.sml

  Download

More scripts: SML Fundamentals

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
# DOWNLOAD.sml
# Sample script for tutorial "Writing Scripts with SML"
# Demonstrates use of the HTTP_CLIENT class to download and process a file from the internet.
# Downloads a CSV file from the US Geological Survey containing global earthquake epicenter locations for the previous 7 days.
# Imports the epicenters as points along with their attributes, and copies points within the desired geographic limits
# to the designated output vector object.
# NOTE: RUNNING THIS SCRIPT REQUIRES AN INTERNET CONNECTION
numeric err; 	# variable for error checking
clear();
#------------------------------  Get information from user  ---------------------------------
# prompt for selection of a new output vector object for the earthquake location points
class STRING prompt$ = "Please select a new vector object to create:";
PopupMessage(prompt$);
class RVC_VECTOR QuakeVec;
GetOutputVector(QuakeVec, "Planar");
# prompt for minimum and maximum latitude and longitude values of the desired area
prompt$ = "Enter minimum latitude of desired area in decimal degrees:";
numeric latmin, latmax, lonmin, lonmax;
latmin = PopupNum(prompt$, -90, -90, 89, 1);
prompt$ = "Enter maximum latitude of desired area:";
latmax = PopupNum(prompt$, 90, latmin, 90, 1);
prompt$ = "Enter minimum longitude of desired area:";
lonmin = PopupNum(prompt$, -180, -180, 179, 1);
prompt$ = "Enter maximum longitude of desired area:";
lonmax = PopupNum(prompt$, 180, lonmin, 180, 1);
#----------------------------  Download the CSV file  -----------------------------
#  host name of the web server to connect to
class STRING address$ = "earthquakes.usgs.gov";
# URL of the file to download		
class STRING url$ = "http://earthquakes.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt";
# local path and name of the file to be saved from the download
class STRING textfile$ = _context.ScriptDir + "/eqs7day-M1.txt";
print(textfile$);
# class for a client connection to an HTTP (web) server
class HTTP_CLIENT http;
# connect to the web server
#http.SetTimeOut(15);						# set time out duration in case can't connect; this method available in TNTmips 2010 and later.
err = http.Connect(address$, 80);
printf("Connect returned %d.\n", err);
# download the text file
http.DownloadFile(url$, textfile$);
#------------------------  Import the CSV file to temporary vector  ---------------------
# create temporary vector object to import the global earthquake data
class RVC_OBJECT tempfile;
tempfile.MakeTempFile(1);
# create object item for new vector object in tempfile
class RVC_OBJITEM fqObjItem;
class RVC_DESCRIPTOR fqDescript;
fqDescript.SetName("GlobalQuakes");
fqDescript.SetDescription("");
fqObjItem.CreateNew(tempfile.GetObjItem(), "VECTOR", fqDescript);
# set Coordinate Reference System for the earthquake data
class SR_COORDREFSYS crs("Geographic2D_WGS84_Deg");
printf("CRS = %s\n", crs.Name);
# set up class for import/export of vector from/to text; the class method to import the object takes an
# RVC_OBJITEM rather than an RVC_VECTOR class instance.
class MieTEXTVECTOR mieTextVec;
mieTextVec.ImportType = "Points";
mieTextVec.ColumnFormat = 0;
mieTextVec.Delimiter = ",";
mieTextVec.FileHeaderLines = 1;
mieTextVec.CoordRefSys = crs;
mieTextVec.LatLonFormat = "decimal_degrees";
mieTextVec.XField = 5;
mieTextVec.YField = 4;
mieTextVec.Encoding = "ASCII";
# set string to define additional attribute fields to import and attach to the points
class STRING additionalAttrib$ = 
'Src,Character,2,0,0,Eqid,Character,8,0,1,Version,Character,1,0,2,Datetime,Character,39,0,3,Lat,Floating-point,8,4,4,Lon,Floating-point,9,4,5,Magnitude,Character,3,1,6,Depth,Floating-point,6,2,7,NST,Integer,3,0,8,Region,Character,45,0,9';
mieTextVec.AdditionalAttributeInfo= additionalAttrib$;
# import the earthquake text file
err = mieTextVec.ImportObject(textfile$, fqObjItem);
printf("mie ImportObject returned %d\n\n", err); 
#---------------------  Copy points in the desired area to the output vector  ---------------
# RVC_VECTOR class instance for the global earthquake vector object, needed for the VectorCopy Elements function
class RVC_VECTOR GlobalQuakes;
# open the global earthquake vector object
GlobalQuakes.OpenByName(fqObjItem.GetFilePath(), fqObjItem.GetObjectPath(), "Read");
printf("Number of earthquake points imported = %d\n", GlobalQuakes.$Info.NumPoints);
# query string to select epicenter points within desired latitude-longitude range using VectorCopyElements()
class STRING ptQry;		
ptQry = sprintf("if (CLASS.Lat > %.2f && CLASS.Lat < %.2f && CLASS.Lon > %.2f && CLASS.Lon < %.2f) return true;", latmin, latmax, lonmin, lonmax);
printf("Point query = %s\n", ptQry);
# copy epicenters within desired area to the output vector object
err = VectorCopyElements(GlobalQuakes, QuakeVec, "RemExRecords", ptQry);
printf("VectorCopyElements returned %d\n\n", err);
printf("%d earthquake points copied to output vector.\n", QuakeVec.$Info.NumPoints);
CloseVector(GlobalQuakes);
CloseVector(QuakeVec);