Launchers are simple web forms that you can use to run templatized scripts. They are especially useful if your script has command line arguments that dynamically change the way the script executes. For heavily customized scripts, those command line arguments can quickly get complicated. Use Launchers to expose all that as a simple web form.
To do this with MATLAB, you will refactor the .m file used in your scheduled job as a function, and give Launcher users the ability to specify the following parameters:
-
The NOAA station IDs are listed here: GHCND Stations
-
A "hot day" temperature threshold
-
Start a new MATLAB Workspace.
-
Open the file you previously created,
predictWeatherReport.m
, and save it aspredictWeatherReportLauncher.m
. -
Copy and paste the following to the top of the script. This wraps the previous code with a
function
statement.NoteThe
function
statement requires anend
statement. You will add this in the last step of this procedure.function result = predictWeatherReport(weatherStationId, hotDayThreshold)
-
Copy and paste the following over lines in the Initial setup section in the existing script to refactor the code:
result = struct; %% Download data file baseUrlString = "https://www.ncei.noaa.gov/data/global-historical-climatology-network-daily/access/"; % compose the URL for the NOAA station ID specified as an input parameter urlString = sprintf("%s%s%s", baseUrlString, weatherStationId, ".csv"); % save the data into the /data subfolder savedFileName = sprintf("%s%s%s%s", "data", filesep, weatherStationId, ".csv"); websave(savedFileName, urlString);
NoteThe next section of code remains unchanged. For reference, the code is as follows:
%% Read the downloaded file opts = detectImportOptions(savedFileName); opts.SelectedVariableNames = {'DATE', 'PRCP', 'TMIN', 'TMAX'}; opts = setvartype(opts, {'DATE','PRCP','TMIN','TMAX'},{'datetime','double', 'double', 'double'}); stationWeatherTbl = readtable(savedFileName, opts); %% [stationWeatherTbl.year, stationWeatherTbl.month, stationWeatherTbl.day] = ymd(stationWeatherTbl.DATE); % MATLAB strength stationWeatherTbl = stationWeatherTbl(stationWeatherTbl.year > 1999 & stationWeatherTbl.year < max(stationWeatherTbl.year), :); stationWeatherTbl.TMAX = stationWeatherTbl.TMAX/10; stationWeatherTbl.TMIN = stationWeatherTbl.TMIN/10; stationWeatherTbl = fillmissing(stationWeatherTbl, 'linear'); %% check if there is enough data for prediction dataRows = size(stationWeatherTbl, 1); if dataRows < 1000 disp('Not enough data for prediction') result.error = 'Not enough data for prediction'; return; end
-
Because those using Launcher will be able to request predictions for any weather station, copy and paste the following code to verify whether a model for the station exists.
NoteIf the training model (
trainRegressionModel.m
) does not exist in your Current Folder, return to the previous step where you trained the model.%% Check if we have a model for this weather station modelFileName = sprintf("%s%s%s%s", "models", filesep, ... weatherStationId, ".mat"); % make sure we have a folder for the models if ~isfolder('models') mkdir('models') end if ~isfile(modelFileName) disp('Training model for weather station...') cv = cvpartition(stationWeatherTbl.year, 'Holdout', 0.3); dataTrain = stationWeatherTbl(cv.training, :); [weatherModel, validationRMSE] = trainRegressionModel(dataTrain); % display prediction precision doneMessage = sprintf('%s%d', "Done. Model RMSE:", validationRMSE); disp(doneMessage); save(modelFileName, 'weatherModel'); else load(modelFileName, 'weatherModel'); end
NoteThe following code already exists to create the data table to drive the prediction model.
%% Create table for future date prediction todayDate = datetime('today'); daysIntoFuture = 365; endDate = todayDate + days(daysIntoFuture); predictedMaxTemps = table('Size', [daysIntoFuture+1 7], 'VariableTypes', ... {'datetime', 'double', 'double', 'double', 'double', 'double', 'double'}, ... 'VariableNames', stationWeatherTbl.Properties.VariableNames); x=1; for i=todayDate:endDate % get the average perception and minimum temps on this date [y, m, d] = ymd(i); minTemps = stationWeatherTbl.TMIN(stationWeatherTbl.month == m & stationWeatherTbl.day == d); prcps = stationWeatherTbl.PRCP(stationWeatherTbl.month == m & stationWeatherTbl.day == d); curMinTemp = NaN; [historicalRowCount z] = size(minTemps); randomRow = randi([1 historicalRowCount]); curMinTemp = minTemps(randomRow); predictedMaxTemps.TMIN(x) = curMinTemp; randomRow = randi([1 historicalRowCount]); predictedMaxTemps.PRCP(x) = prcps(randomRow); predictedMaxTemps.DATE(x) = i; predictedMaxTemps.year(x) = y; predictedMaxTemps.month(x) = m; predictedMaxTemps.day(x) = d; predictedMaxTemps.TMAX(x) = 0; x = x+1; end
NoteLeave the rest of the file as-is. The following is the code for reference:
%% run model with future data yFit = weatherModel.predictFcn(predictedMaxTemps); predResult = table(predictedMaxTemps.DATE, yFit, 'VariableNames', {'Date', 'Predicted TMAX'}); result.predictedTemps = predResult; hotWeatherDaysIdx = predResult(predResult.("Predicted TMAX") > hotDayThreshold, :); result.hotDayCountPrediction = height(hotWeatherDaysIdx); %% save data to file dominoRunId = getenv('DOMINO_RUN_NUMBER'); outputFileName = sprintf('%s%s%s', 'results', filesep, 'predictData_', string(dominoRunId)); save(outputFileName, 'result'); %% publish to report pub_options.format = 'pdf'; pub_options.showCode = false; pub_options.outputDir = sprintf('%s%s%s', 'results', filesep, dominoRunId); doc = publish('predictWeatherReportTemplate.m', pub_options);
-
Add the
end
statement for the function.%% Function end end % This is the end of the function
-
Click the Save and then Sync All Changes to save your work. Stop the workspace.
-
Close the workspace tab in your browser.
-
In the navigation pane, go to Files to create a shell script that will tell the Launcher which MATLAB script to run. The shell script will also identify the parameters to pass to the script from the launcher.
-
Click the new file icon to create a new file. Name the file weather_launcher.sh.
-
Add the following code starting on line 1 of the file:
matlab -nodisplay -nodesktop -nosplash -r " predictWeatherReportLauncher('$1', $2)"
This line of code instructs Domino to run MATLAB from the command line and execute the predictWeatherReportLauncher script with two arguments. MATLAB will look for the function in an .m file with the same name as the function.
-
Click Save.
-
From the navigation pane, click Launchers, then click New Launcher.
-
Type a descriptive title (for example, Weather Predictor), a description, and select a hardware tier. In the Command to run section, enter
weather_launcher.sh
. -
Click Add Parameter. The parameter options open.
-
In the "Command to run" field, replace
parameter1
withstation_id
. You’ll notice the parameter’s name updates in the form. Confirm that the parameter name remains enclosed within${}
, such that the parameter is formatted as such:${name_of_the_parameter}
. -
Give the parameter a default value of
MXM00076680
(for Mexico City) for the station ID. The default value will be used if the user doesn’t enter a value in the Launcher form. In the description, enter "The ID of the station for which to predict weather". -
Click Add Parameter.
-
In the Command to run field, enter
hot_temp
as the parameter’s name between the curly brace4s {}. This parameter represents the hot temperature threshold. -
Click
hot_temp
in the table. Leave the parameter’s type as Text and enter a default value (for example,30
) and a description for the parameter. -
Click Save Launcher, then click Back to Launchers. The new launcher is listed.
-
Click Run. The launcher form opens. Type a title for your run (for example, "First launcher run") and click Run.
-
The Jobs view opens while your launcher executes the job. The job number is listed in the No. column. Double-click the row to see the results as Domino runs the job.
-
The job produces a PDF report. To access the PDF report:
-
Go to the navigation pane and click Files.
-
Open the results folder and open the folder created by your job (in this example, it is folder 36/).
-
-
Click the PDF file to open it.