function strregret = stratigraphicregions();
%stratregions = stratigraphicregions();
%
%This function defines a systems of stratigraphic regions primarily for use
%by the helper function georegion.  The single output is a structure array
%defining different geographic regions.
%
%The currently defined regions are: 'North America', 'Europe', 'China',
%'Australia', 'Japan', 'South Africa', 'New Zealand', 'Russian-Kazakhian',
%and 'California'.
%
%The helper function geologicstages.m version 1.1.0+ must be available for 
%this function to operate.  

%Created by Robert A. Rohde (geowhen@robertrohde.com)
%Last Modified January 18, 2005

%stratigraphicregions version 1.1.0

%load geologic stages and make global for access in helper functions.
global GL_STAGES;
GL_STAGES = geologicstages;

%define stratigraphic system.
global GL_STRATREG;
GL_STRATREG = {};

AddSystem('ICS General');
AddAlias('ICS Periods');
AddMember('Hadean');
AddMember('Archean');
AddMember('Paleoproterozoic');
AddMember('Mesoproterozoic');
AddMember('Neoproterozoic');
AddMember('Cambrian');
AddMember('Ordovician');
AddMember('Silurian');
AddMember('Carboniferous');
AddMember('Devonian');
AddMember('Permian');
AddMember('Triassic');
AddMember('Jurassic');
AddMember('Cretaceous');
AddMember('Paleogene');
AddMember('Neogene');

AddSystem('ICS Specific');
AddAlias('ICS');
AddMember('Hadean');
AddMember('Eoarchean');
AddMember('Mesoarchean');
AddMember('Neoarchean');
AddMember('Paleoarchean');
AddMember('Siderian');
AddMember('Rhyacian');
AddMember('Orosirian');
AddMember('Statherian');
AddMember('Calymmian');
AddMember('Ectasian');
AddMember('Stenian');
AddMember('Tonian');
AddMember('Cryogenian');
AddMember('Ediacaran');
AddMember('Paibian');
AddMember('Tremadocian');
AddMember('Arenig');
AddMember('Ordovician III');
AddMember('Darriwilian');
AddMember('Ordovician V');
AddMember('Ordovician VI');
AddMember('Hirnantian');
AddMember('Rhuddanian');
AddMember('Aeronian');
AddMember('Telychian');
AddMember('Sheinwoodian');
AddMember('Homerian');
AddMember('Gorstian');
AddMember('Ludfordian');
AddMember('Pridoli');
AddMember('Lochkovian');
AddMember('Praghian');
AddMember('Emsian');
AddMember('Eifelian');
AddMember('Givetian');
AddMember('Frasnian');
AddMember('Famennian');
AddMember('Tournaisian');
AddMember('Visean');
AddMember('Serpukhovian');
AddMember('Bashkirian');
AddMember('Kasimovian');
AddMember('Moscovian');
AddMember('Gzhelian');
AddMember('Asselian');
AddMember('Sakmarian');
AddMember('Artinskian');
AddMember('Kungurian');
AddMember('Roadian');
AddMember('Wordian');
AddMember('Capitanian');
AddMember('Wuchiapingian');
AddMember('Changhsingian');
AddMember('Induan');
AddMember('Olenekian');
AddMember('Anisian');
AddMember('Ladinian');
AddMember('Carnian');
AddMember('Norian');
AddMember('Rhaetian');
AddMember('Hettangian');
AddMember('Sinemurian');
AddMember('Pliensbachian');
AddMember('Toarcian');
AddMember('Aalenian');
AddMember('Bajocian');
AddMember('Bathonian');
AddMember('Callovian');
AddMember('Oxfordian');
AddMember('Kimmeridgian');
AddMember('Tithonian');
AddMember('Berriasian');
AddMember('Valanginian');
AddMember('Hauterivian');
AddMember('Barremian');
AddMember('Aptian');
AddMember('Albian');
AddMember('Cenomanian');
AddMember('Turonian');
AddMember('Coniacian');
AddMember('Santonian');
AddMember('Campanian');
AddMember('Maastrichtian');
AddMember('Danian');
AddMember('Selandian');
AddMember('Thanetian');
AddMember('Ypresian');
AddMember('Lutetian');
AddMember('Bartonian');
AddMember('Priabonian');
AddMember('Rupelian');
AddMember('Chattian');
AddMember('Aquitanian');
AddMember('Burdigalian');
AddMember('Langhian');
AddMember('Serravallian');
AddMember('Tortonian');
AddMember('Messinian');
AddMember('Zanclean');
AddMember('Piacenzian');
AddMember('Gelasian');
AddMember('Early Pleistocene');
AddMember('Middle Pleistocene');
AddMember('Late Pleistocene');
AddMember('Holocene');

AddSystem('North America');
AddAlias('North American');
AddAlias('N. Amer.');
AddAlias('N Amer');
AddMember('Aphebian');
AddMember('Helikian');
AddMember('Hadrynian');
AddMember('Waucoban');
AddMember('Albertan');
AddMember('Croixian');
AddMember('Canadian');
AddMember('White Rockian');
AddMember('Mohawkian');
AddMember('Cincinnatian');
AddMember('Medinan');
AddMember('Niagaran');
AddMember('Cayugan');
AddMember('Ulsterian');
AddMember('Erian');
AddMember('Senecan');
AddMember('Chatauquan');
AddMember('Kinderhookian');
AddMember('Osagean');
AddMember('Meramecian');
AddMember('Chesterian');
AddMember('Morrowan');
AddMember('Atokan/Derryan');
AddMember('Desmoinian');
AddMember('Missourian');
AddMember('Virgilian');
AddMember('Wolfcampian');
AddMember('Leonardian');
AddMember('Guadalupian');
AddMember('Ochoan');
AddMember('Griesbachian');
AddMember('Smithian');
AddMember('Spathian');
AddMember('Dienerian');
AddMember('Coahulian');
AddMember('Comanchean');
AddMember('Gulfian');
AddMember('Puercan');
AddMember('Torrejonian');
AddMember('Tiffanian');
AddMember('Clarkforkian');
AddMember('Wasatchian');
AddMember('Bridgerian');
AddMember('Uintan');
AddMember('Duchesnean');
AddMember('Chadronian');
AddMember('Orellan');
AddMember('Whitneyan');
AddMember('Arikareean');
AddMember('Hemingfordian');
AddMember('Barstovian');
AddMember('Clarendonian');
AddMember('Hemphillian');
AddMember('Blancan');
AddMember('Irvingtonian');
AddMember('Rancholabrean');

AddSystem('Europe');
AddAlias('European');
AddMember('Caerfai');
AddMember('St. David''s');
AddMember('Merioneth');
AddMember('Cressagian');
AddMember('Migneintian');
AddMember('Moridunian');
AddMember('Whitlandian');
AddMember('Fennian');
AddMember('Abereiddian');
AddMember('Llandeilian');
AddMember('Aurelucian');
AddMember('Burrellian');
AddMember('Cheneyan');
AddMember('Streffordian');
AddMember('Pusgillian');
AddMember('Cautleyan');
AddMember('Rawtheyan');
AddMember('Namurian');
AddMember('Westphalian');
AddMember('Stephanian');
AddMember('Tatarian');
AddMember('Kazanian');
AddMember('Gallic');
AddMember('Neocomian');
AddMember('Senonian');
AddMember('Calabrian');
AddMember('Sicilian');
AddMember('Tyrrhenian');

AddSystem('China');
AddAlias('Chinese');
AddMember('Wutaian');
AddMember('Fupingan');
AddMember('Qianxin');
AddMember('Sinian');
AddMember('Jinningian');
AddMember('Luliangian');
AddMember('Meishuchuan');
AddMember('Qungzusian');
AddMember('Changlangpuan');
AddMember('Longwangmioan');
AddMember('Zhungxian');
AddMember('Zuzhuangian');
AddMember('Maozhangian');
AddMember('Gushanian');
AddMember('Changshanian');
AddMember('Fengshanian');
AddMember('Lianghekouan');
AddMember('Honghuayuanian');
AddMember('Dawanian');
AddMember('Guniutanian');
AddMember('Miaopoan');
AddMember('Wufengian');
AddMember('Baotan');
AddMember('Linxiangian');
AddMember('Longmaxian');
AddMember('Shinulanian');
AddMember('Baishaean');
AddMember('Xiushanian');
AddMember('Guandian');
AddMember('Miaogoalingian');
AddMember('Yulongsian');
AddMember('Lianhuashanian');
AddMember('Nagaolingian');
AddMember('Dongganglingian');
AddMember('Yingtangian');
AddMember('Sipaian');
AddMember('Yujianian');
AddMember('Xikuangshanian');
AddMember('Shetianqiaoan');
AddMember('Shaodongian');
AddMember('Yanguan');
AddMember('Datangian');
AddMember('Dewuan');
AddMember('Huashibanian');
AddMember('Dalanian');
AddMember('Mapingian');
AddMember('Xixianian');
AddMember('Maokovian');
AddMember('Wujiapingian');
AddMember('Changhsingian');
AddMember('Feixianguanian');
AddMember('Yongningzhenian');
AddMember('Guanlingian');
AddMember('Falangian');
AddMember('Bananian');
AddMember('Huobachengian');
AddMember('Erqiaoan');
AddMember('Ziliujingian');
AddMember('Xintiangouan');
AddMember('Shaximioan');
AddMember('Suiningian');
AddMember('Penglaizhenian');

AddSystem('Australia');
AddAlias('Australian');
AddMember('Nullaginian');
AddMember('Carpentarian');
AddMember('Adelaidean');
AddMember('Ordian');
AddMember('Templetonian');
AddMember('Florian');
AddMember('Undillian');
AddMember('Boomerangian');
AddMember('Mindyallan');
AddMember('Idamean');
AddMember('Payntonian');
AddMember('Datsonian');
AddMember('Warendian/Lancefieldian');
AddMember('Bendigonian');
AddMember('Chewtonian');
AddMember('Castlemanian');
AddMember('Darriwilian');
AddMember('Ypeenian');
AddMember('Gisbornian');
AddMember('Eastonian');
AddMember('Bolindian');
AddMember('Keiloran');
AddMember('Eildonian');
AddMember('Melbournian');
AddMember('Wangerripian');
AddMember('Johannian');
AddMember('Aldingian');
AddMember('Janjukian');
AddMember('Longfordian');
AddMember('Batesfordian');
AddMember('Balcombian');
AddMember('Bairnsdalian');
AddMember('Mitchellian');
AddMember('Cheltenhamian');
AddMember('Kalimnan');
AddMember('Yatalan');
AddMember('Werrikooian');

AddSystem('Russian-Kazakhian');
AddAlias('Russia');
AddAlias('Russian');
AddAlias('Kazakhian');
AddMember('Nemakit-Daldynian');
AddMember('Tommotian');
AddMember('Atdabanian');
AddMember('Botomian');
AddMember('Toyonian');
AddMember('Amgan');
AddMember('Mayan');
AddMember('Ayusokkanian');
AddMember('Sakian');
AddMember('Aksayan');
AddMember('Batyrbayan');
AddMember('Noginskian');
AddMember('Klazminskian');
AddMember('Dorogomilovksian');
AddMember('Chamovnicheskian');
AddMember('Krevyakinskian');
AddMember('Myachkovskian');
AddMember('Podolskian');
AddMember('Kashirskian');
AddMember('Vereiskian');
AddMember('Melekesskian');
AddMember('Cheremshanskian');

AddSystem('Japan');
AddAlias('Japanese');
AddMember('Yuzanjian');
AddMember('Suchian');
AddMember('Totomian');
AddMember('Yuian');
AddMember('Fujian');
AddMember('Kaburan');
AddMember('Tozawan');
AddMember('Haranoyan');
AddMember('Hetonian');
AddMember('Urakawan');
AddMember('Gyliakian');
AddMember('Miyakoan');
AddMember('Aritan');
AddMember('Kochian');
AddMember('Kechienjian');

AddSystem('South Africa');
AddAlias('South African');
AddMember('Swazian');
AddMember('Randian');
AddMember('Vaalian');
AddMember('Mokolian');
AddMember('Namibian');

AddSystem('California');
AddAlias('Californian');
AddMember('Hallian');
AddMember('Wheelerian');
AddMember('Venturian');
AddMember('Repettian');
AddMember('Delmontian');
AddMember('Mohnian');
AddMember('Luisian');
AddMember('Relizian');
AddMember('Saucesian');
AddMember('Zemorrian');
AddMember('Refugian');
AddMember('Narizian');
AddMember('Ulatisian');
AddMember('Penutian');
AddMember('Bulitian');
AddMember('Ynezian');

AddSystem('New Zealand');
AddMember('Telfordian');
AddMember('Mangapirian');
AddMember('Braxtonian');
AddMember('Waiitian');
AddMember('Makabewan');
AddMember('Malakovian');
AddMember('Etalian');
AddMember('Kaihikuan');
AddMember('Oretian');
AddMember('Otamitan');
AddMember('Warepan');
AddMember('Otapirian');
AddMember('Aratauran');
AddMember('Uruoan');
AddMember('Temaikan');
AddMember('Heterian');
AddMember('Ohauan');
AddMember('Puaroan');
AddMember('Mokoiwian');
AddMember('Korangian');
AddMember('Urutawan');
AddMember('Motuan');
AddMember('Ngaterian');
AddMember('Arowhanan');
AddMember('Mangaotanean');
AddMember('Teratan');
AddMember('Piripauan');
AddMember('Haumurian');
AddMember('Teurian');
AddMember('Waipawan');
AddMember('Mangaorapan');
AddMember('Heretaungan');
AddMember('Porangan');
AddMember('Bortonian');
AddMember('Kiaitan');
AddMember('Runangan');
AddMember('Whaingaroan');
AddMember('Duntroonian');
AddMember('Waitakian');
AddMember('Otaian');
AddMember('Hutchinsonian');
AddMember('Awamoan');
AddMember('Altonian');
AddMember('Cliffdenian');
AddMember('Lillburnian');
AddMember('Waiauan');
AddMember('Tongaporutuan');
AddMember('Kapitean');
AddMember('Opoitian');
AddMember('Waipipian');
AddMember('Mangapanian');
AddMember('Nukumaruan');
AddMember('Okehuan');
AddMember('Putikian');
AddMember('Hawera');

%Sort Data
SortMembers;

%Return array
strregret = GL_STRATREG;

%This helper function adds the start of a new system to the structure array.
function AddSystem(nm);
global GL_STRATREG;
GL_STRATREG(end+1).name = nm;
GL_STRATREG(end).member= {};
GL_STRATREG(end).dates = [];
GL_STRATREG(end).error = {};
GL_STRATREG(end).alias = {};

%This helper function adds a new stage to the current stratigraphic system.
function AddMember(mem);
global GL_STRATREG GL_STAGES;
GL_STRATREG(end).member{end+1} = mem;
[st,ed,rec] = geowhen(mem,GL_STAGES);
GL_STRATREG(end).dates(end+1,:) = [st,ed];
GL_STRATREG(end).error{end+1,1} = rec.dStart;
GL_STRATREG(end).error{end,2} = rec.dEnd;

%This helper function adds an alias to the current stratigraphic system.
function AddAlias(al)
global GL_STRATREG GL_STAGES;
GL_STRATREG(end).alias{end+1} = al;

%This helper function sorts the stages in all systems.
function SortMembers();
global GL_STRATREG;
for a = 1:length(GL_STRATREG)
   t =[];
   for b = 1:length(GL_STRATREG(a).dates)
      t(b) = GL_STRATREG(a).dates(b,1) + GL_STRATREG(a).dates(b,2)/10^6;
   end
   [s,ind] = sort(t);
   ind = ind(end:-1:1);
   [GL_STRATREG(a).member{:}] = deal(GL_STRATREG(a).member{ind});
   GL_STRATREG(a).dates = GL_STRATREG(a).dates(ind,:);
   GL_STRATREG(a).error = GL_STRATREG(a).error(ind,:);   
end
