%SMBpdd Class definition
%
%   Usage:
%      SMBpdd=SMBpdd();

classdef SMBpdd
	properties (SetAccess=public) 
		precipitation             = NaN;
		monthlytemperatures       = NaN;
		desfac                    = 0;
		s0p                       = 0;
		s0t                       = 0;
		rlaps                     = 0;
		rlapslgm                  = 0;                
		Pfac                      = NaN;
		Tdiff                     = NaN;
		sealev                    = NaN;
		isdelta18o                = 0;
		ismungsm                  = 0;
		delta18o                  = NaN;
		delta18o_surface          = NaN;
		temperatures_presentday   = NaN;
		temperatures_lgm          = NaN;
		precipitations_presentday = NaN;
		precipitations_lgm        = NaN;
	end
	methods
		function obj = SMBpdd(varargin) % {{{
			switch nargin
				case 0
					obj=setdefaultparameters(obj);
				otherwise
					error('constructor not supported');
			end
		end % }}}
		function self = extrude(self,md) % {{{
			if(self.isdelta18o==0 & self.ismungsm==0),self.precipitation=project3d(md,'vector',self.precipitation,'type','node');end
			if(self.isdelta18o==0 & self.ismungsm==0),self.monthlytemperatures=project3d(md,'vector',self.monthlytemperatures,'type','node');end
			if(self.isdelta18o),self.temperatures_lgm=project3d(md,'vector',self.temperatures_lgm,'type','node');end
			if(self.isdelta18o),self.temperatures_presentday=project3d(md,'vector',self.temperatures_presentday,'type','node');end
			if(self.isdelta18o),self.precipitations_presentday=project3d(md,'vector',self.precipitations_presentday,'type','node');end
			if(self.isdelta18o),self.precipitations_lgm=project3d(md,'vector',self.precipitations_lgm,'type','node');end
			if(self.ismungsm),self.temperatures_lgm=project3d(md,'vector',self.temperatures_lgm,'type','node');end
			if(self.ismungsm),self.temperatures_presentday=project3d(md,'vector',self.temperatures_presentday,'type','node');end
			if(self.ismungsm),self.precipitations_presentday=project3d(md,'vector',self.precipitations_presentday,'type','node');end
			if(self.ismungsm),self.precipitations_lgm=project3d(md,'vector',self.precipitations_lgm,'type','node');end

		end % }}}
		function self = initialize(self,md) % {{{
                    
			% if isnan(self.precipitation),
			% 	self.precipitation=zeros(md.mesh.numberofvertices,1);
			% 	disp('      no SMBpdd.precipitation specified: values set as zero');
			% end

		end % }}}
		function obj = setdefaultparameters(obj) % {{{

		  obj.isdelta18o = 0;
		  obj.ismungsm   = 0;
		  obj.desfac     = 0.5;
		  obj.s0p        = 0;
		  obj.s0t        = 0;
		  obj.rlaps      = 6.5;
		  obj.rlapslgm   = 6.5;
                  
		end % }}}
		function md = checkconsistency(obj,md,solution,analyses) % {{{

			if ismember(MasstransportAnalysisEnum(),analyses),
				md = checkfield(md,'fieldname','surfaceforcings.desfac','<=',1,'numel',1);
				md = checkfield(md,'fieldname','surfaceforcings.s0p','>=',0,'numel',1);
				md = checkfield(md,'fieldname','surfaceforcings.s0t','>=',0,'numel',1);
				md = checkfield(md,'fieldname','surfaceforcings.rlaps','>=',0,'numel',1);
				md = checkfield(md,'fieldname','surfaceforcings.rlapslgm','>=',0,'numel',1);
				if(obj.isdelta18o==0 & obj.ismungsm==0)
					md = checkfield(md,'fieldname','surfaceforcings.monthlytemperatures','forcing',1,'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.precipitation','forcing',1,'NaN',1);
				elseif(obj.isdelta18o==1) 
					md = checkfield(md,'fieldname','surfaceforcings.delta18o','NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.delta18o_surface','NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.temperatures_presentday','size',[md.mesh.numberofvertices+1 12],'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.temperatures_lgm','size',[md.mesh.numberofvertices+1 12],'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.precipitations_presentday','size',[md.mesh.numberofvertices+1 12],'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.precipitations_lgm','size',[md.mesh.numberofvertices+1 12],'NaN',1);                                       
					md = checkfield(md,'fieldname','surfaceforcings.Tdiff','NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.sealev','NaN',1);
				elseif(obj.ismungsm==1) 
					md = checkfield(md,'fieldname','surfaceforcings.temperatures_presentday','size',[md.mesh.numberofvertices+1 12],'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.temperatures_lgm','size',[md.mesh.numberofvertices+1 12],'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.precipitations_presentday','size',[md.mesh.numberofvertices+1 12],'NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.precipitations_lgm','size',[md.mesh.numberofvertices+1 12],'NaN',1);                                       
					md = checkfield(md,'fieldname','surfaceforcings.Pfac','NaN',1,'size',[2,NaN]);
					md = checkfield(md,'fieldname','surfaceforcings.Tdiff','NaN',1);
					md = checkfield(md,'fieldname','surfaceforcings.sealev','NaN',1);
				end
			end
		end % }}}
		function disp(obj) % {{{
			disp(sprintf('   surface forcings parameters:'));

			disp(sprintf('\n   PDD and deltaO18 parameters:'));
			fielddisplay(obj,'isdelta18o','is temperature and precipitation delta18o parametrisation activated (0 or 1, default is 0)');
			fielddisplay(obj,'ismungsm','is temperature and precipitation mungsm parametrisation activated (0 or 1, default is 0)');
			fielddisplay(obj,'desfac','desertification elevation factor (between 0 and 1, default is 0.5) [m]');
			fielddisplay(obj,'s0p','should be set to elevation from precip source (between 0 and a few 1000s m, default is 0) [m]');
			fielddisplay(obj,'s0t','should be set to elevation from temperature source (between 0 and a few 1000s m, default is 0) [m]');
			fielddisplay(obj,'rlaps','present day lapse rate [degree/km]');
			fielddisplay(obj,'rlapslgm','LGM lapse rate [degree/km]');
                        if(obj.isdelta18o==0 & obj.ismungsm==0)
                            fielddisplay(obj,'monthlytemperatures',['monthly surface temperatures [K], required if pdd is activated and delta18o not activated']);
                            fielddisplay(obj,'precipitation',['monthly surface precipitation [m/yr water eq], required if pdd is activated and delta18o or mungsm not activated']);
                        elseif(obj.isdelta18o==1)
                            fielddisplay(obj,'delta18o','delta18o, required if pdd is activated and delta18o activated');
                            fielddisplay(obj,'delta18o_surface','surface elevation of the delta18o site, required if pdd is activated and delta18o activated');
                            fielddisplay(obj,'temperatures_presentday','monthly present day surface temperatures [K], required if delta18o/mungsm is activated');
                            fielddisplay(obj,'temperatures_lgm','monthly LGM surface temperatures [K], required if delta18o or mungsm is activated');
                            fielddisplay(obj,'precipitations_presentday','monthly surface precipitation [m/yr water eq], required if delta18o or mungsm is activated');
                            fielddisplay(obj,'precipitations_lgm','monthly surface precipitation [m/yr water eq], required if delta18o or mungsm is activated');
                            fielddisplay(obj,'Tdiff','time interpolation parameter for temperature, 1D(year), required if mungsm is activated');
                            fielddisplay(obj,'sealev','sea level [m], 1D(year), required if mungsm is activated');
                        elseif(obj.ismungsm==1)
                            fielddisplay(obj,'temperatures_presentday','monthly present day surface temperatures [K], required if delta18o/mungsm is activated');
                            fielddisplay(obj,'temperatures_lgm','monthly LGM surface temperatures [K], required if delta18o or mungsm is activated');
                            fielddisplay(obj,'precipitations_presentday','monthly surface precipitation [m/yr water eq], required if delta18o or mungsm is activated');
                            fielddisplay(obj,'precipitations_lgm','monthly surface precipitation [m/yr water eq], required if delta18o or mungsm is activated');
                            fielddisplay(obj,'Pfac','time interpolation parameter for precipitation, 1D(year), required if mungsm is activated');
                            fielddisplay(obj,'Tdiff','time interpolation parameter for temperature, 1D(year), required if mungsm is activated');
                            fielddisplay(obj,'sealev','sea level [m], 1D(year), required if mungsm is activated');
                        end
		end % }}}
		function marshall(obj,md,fid) % {{{

			yts=365.0*24.0*3600.0;

			WriteData(fid,'enum',SurfaceforcingsEnum(),'data',SMBpddEnum(),'format','Integer');

			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','isdelta18o','format','Boolean');
			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','ismungsm','format','Boolean');
			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','desfac','format','Double');
			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','s0p','format','Double');
			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','s0t','format','Double');
			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','rlaps','format','Double');
			WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','rlapslgm','format','Double');

			if(obj.isdelta18o==0 & obj.ismungsm==0)
				%WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','monthlytemperatures','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','monthlytemperatures','format','DoubleMat','mattype',1,'forcinglength',md.mesh.numberofvertices+1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','precipitation','format','DoubleMat','mattype',1,'scale',1./yts,'forcinglength',md.mesh.numberofvertices+1);
			elseif obj.isdelta18o
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','temperatures_presentday','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','temperatures_lgm','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','precipitations_presentday','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','precipitations_lgm','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','delta18o_surface','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','delta18o','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','Tdiff','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','sealev','format','DoubleMat','mattype',1);
			elseif obj.ismungsm
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','temperatures_presentday','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','temperatures_lgm','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','precipitations_presentday','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','precipitations_lgm','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','Pfac','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','Tdiff','format','DoubleMat','mattype',1);
				WriteData(fid,'object',obj,'class','surfaceforcings','fieldname','sealev','format','DoubleMat','mattype',1);
			end
		end % }}}
	end
end
