function Storm(ncycles)
%USAGE Storm(ncycles)
%ncycles is how many timesteps you want to calculate
%Initializes variables
%Main loop
    %Recalculates charges of disks based on charging currents
    %Recalculates charges of disks based on the last lightning flash
    %Recalculates charges of disks based on screening current
    %Calls plotting code
    %prints the figure to a .jpeg named with the current stormtime
    basename='mylittlethunderstormLOL';
    namestring=strcat(basename,num2str(disk.time,'%03i'));
    print_simple(1,namestring);
    %Calls logging code
    %Advances time step
end

function disk=StormInit()
%Defines all the key variables in the structure that will carry storm data
%This makes it easy to pass the data from function to function with the
%variable disk.
    disk.d=1000*[1.5 1.5 1.5 0.5];
    disk.R=1000*[1.5 3 4 4];
    disk.Q=[13 -58 60 -20];
    disk.MSL=1000*[5 6.75 9.75 11];
    ground_altitude=3; %At langmuir, ground is at 3 km.
    disk.ground_altitude=ground_altitude;
    disk.AGL=disk.MSL-ground_altitude*1000;
    disk.name=[{'LP'},{'MN'},{'UP'},{'SC'}];
    disk.time=0;
    disk.Qtoground=0;
    disk.Iscreen=0;
end


function [flash,flash_altitude,Estormtop]=StormTick(disk)
    %StormTick coordinates the analysis as the storm advances or "ticks".
    %For a given set of charges in "disk" the Electric Field and Potential
    %are calculated as a function of altitude.
    %The NextFlash function is called to determine if there will be a
    %lightning flash for the newly calculated vector of electric fields.
    %The field at the top of the storm (where the screening layer is)
    %is calculated so that the screening current can be calculated.
end

function [flash,flash_altitude]=NextFlash(disk,Z,AbsE,EBR)
%Returns a type of flash based on AbsE and EBR
%{'NF'},{'CG'},{'IC'},{'SC'}
%Based on comparing the E-field and EBR
% If AbsE>EBR between MN and UP regions, then IC
% If AbsE>EBR below MN region, then CG.
% EBR must be increased by 15% for CG
%
% disk is structure holding charge information
% Z is a vector of altitudes
% AbsE is the absolute value of E-field vs. Altitude
% EBR is breakdown field vs. altitude.
end


function x=SumE(RHO,Ztarget,Z,R)
%x is the Sum of electric fields created by all charged thin disks at all
%altitudes.
%Ztarget is the altitude (AGL) of the point where you want to calculate
%the local electric field
%Z is a vector of altitudes
%R is a vector of disk radii.
end

function x=SumEimage(RHO,Ztarget,Z,R)
%x is the Sum of electric fields created by the IMAGES of all charged thin disks at all
%altitudes.
%Ztarget is the altitude (AGL) of the point where you want to calculate
%the local electric field
%Z is a vector of altitudes
%R is a vector of disk radii.
end

function E=Edisk(sigma, z, z0, R)
%z is the altitude where you are measuring E
%z0 is the altitude of the center of the charged disk
%The disk is assumed to be thin.
%sigma is the charge of the disk per unit area
%R is the radius of the disk
end

function [RHO, R, Q] = rho_stuffer(RHO,R,Z,LL,UL,Q,r,q)
%Takes in an array of charges and altitudes
%and returns the array of charges with new charges plugged
%in over a specified range of altitudes. The total charge Q
%Will be plugged in uniformly over the range of altitudes
%It is assumed that the charge is in a disk of radius "R"
end

function EBR=Breakdown(Z,h)
%USAGE: EBR=Breakdown(Z,h);  EBR=Breakdown(Z,3)
% Return EBR ... Breakdown E-field (in kV/m) as a function of altitude
% Needs an array of altitudes Z.  (in km).
% h is the altitude of the ground (since the rest of the model works
% in above ground level units and this is the only part where
% absolute altitude matters.
end

function  LogData(fOut,rootstr,disk,flash, flash_altitude,firstcall)
%Example of formatted tabular output
%USAGE: >>LogData(fOut,rootstr,disk,flash, flash_altitude,firstcall)
%fOut is handle to an already opened file (also remember to close it when main program exits)
%rootstr is the name to give the log file.
%disk is the structure that carries lots of the charge data for the storm.
%flash is aa two letter string with the type of the most recent flash
%flash_altitude is the altitude at which the most recent flash triggered
%firstcall is 1 the first time to tell you to write file headers, and 0
%thereafter so that you just add data to the log.

if firstcall==1;
    fprintf('\n\n');

    fprintf(fOut,'STORM SIMULATOR\n');
    fprintf(fOut,'===== =========\n\n');

    %Define the table formats
    formatHeadings='%4s %5s %4s %6s %6s %6s %6s %6s %6s \n';

    fprintf(fOut,formatHeadings,'Time','Flash',' Alt','Q_LP','Q_MN','Q_UP','Q_SC','Q_gnd','I_scrn');
    fprintf(fOut,formatHeadings,'(s)' ,'type' ,'(km)', '(C)',' (C)', '(C)','(C)', '(C)','(Amp)'                  );
    fprintf(fOut,formatHeadings,'----','-----','-----','-----','-----','-----','-----','-----','-----');

    %Matlab's fprintf borrows all the formatting convenctions from C's printf.
    %   See also help http://publications.gbdirect.co.uk/c_book/chapter9/formatted_io.html
    %
end

formatData='%04d %5s %4.1f %6.1f %6.1f %6.2f %6.1f %6.1f %6.3f \n';
fprintf(fOut,formatData,disk.time,flash,flash_altitude/1000,disk.Q,disk.Qtoground,disk.Iscreen);
end

function PlotData(disk,E,Z,R,RHO,Q,PHI, EBR,flash)
%USAGE: PlotData(disk,E,Z,R,RHO,Q,PHI, EBR,flash)
%Plots the data.  Does not do any calculations or modify
%any variables
%disk is a structure of charges in the storm
%Ef is total E-field vs. Z
%Z is altitude above ground level (AGL)
%R is array of charged disk radius vs. altitude
%RHO is array of charge density vs. altitude
%Q  is array of charge vs. altitude
%PHI is potential vs. altitude
%EBR is breakdown voltage vs. altitude (corrected for MSL vs. AGL)
%flash is a two character string identifying the last flash that happened

end

function titstr=flashtitle(flash)
%USAGE: titstr=flashtitle(flash)
%Creates an appropriate title for the next plot based on what type
%of flash just happened.
%Does not do any calculations or modify any variables
%Returns titstr "title string" which contains a title for the next plot
end


function print_simple(target, picname)
%PRINT_SIMPLE Prints the figure numbered "target"
%with the basename "picname"
orient portrait
filetype='.jpeg';
figure(target)
fn=strcat(picname,filetype);

print(target, '-djpeg', '-r300', fn );
%The system call is not needed ...it's only if you want to view the
%jpeg you just created
%system([' feh -r -g 1200x900 ',fn,'&']);
end
Undefined variable "disk" or class "disk.time".

Error in Storm20160224_HW5 (line 12)
    namestring=strcat(basename,num2str(disk.time,'%03i'));