source:
issm/oecreview/Archive/12678-13393/ISSM-13011-13012.diff@
14312
Last change on this file since 14312 was 13394, checked in by , 13 years ago | |
---|---|
File size: 71.7 KB |
-
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/multiplequeue/LaunchMultipleQueueJob.m
1 function LaunchMultipleQueueJob(cluster,name,executionpath)2 %LAUNCHMULTIPLEQUEUEJOB - ...3 %4 % Usage:5 % LaunchMultipleQueueJob(executionpath)6 7 %First try and figure out if there is a special script for thie particular cluster8 function_name=['LaunchMultipleQueueJob' cluster];9 10 %some specific treatment of identical cluster, gemini, castor and pollux11 if strcmpi(cluster,'castor') || strcmpi(cluster,'pollux'),12 function_name='LaunchMultipleQueueJobgemini';13 end14 15 if exist(function_name,'file'),16 %Call this function:17 eval([function_name '(cluster,name,executionpath);']);18 else19 %Call the generic LaunchMultipleQueueJob:20 LaunchMultipleQueueJobGeneric(cluster,name,executionpath);21 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/multiplequeue/BuildMultipleQueueingScriptgemini.m
1 function BuildQueueingScriptgemini(name,executionpath,codepath)2 %BUILDQUEUEINGSCRIPTGEMINI - ...3 %4 % Usage:5 % BuildQueueingScriptgemini(md,executionpath,codepath)6 7 scriptname=[name '.queue'];8 9 fid=fopen(scriptname,'w');10 if fid==-1,11 error(['BuildQueueingScriptgeminierror message: could not open ' scriptname ' file for ascii writing']);12 end13 14 fprintf(fid,'#!/bin/sh\n');15 fprintf(fid,'cd %s\n',executionpath);16 fprintf(fid,'mkdir %s\n',name);17 fprintf(fid,'cd %s\n',name);18 fprintf(fid,'mv ../ModelList.tar.gz ./\n');19 fprintf(fid,'tar -zxvf ModelList.tar.gz\n');20 fprintf(fid,'foreach i (%s-*vs*.queue)\n',name);21 fprintf(fid,'qsub $i\n');22 fprintf(fid,'end\n');23 fclose(fid); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/multiplequeue/LaunchMultipleQueueJobgemini.m
1 function md=LaunchMultipleQueueJobgemini(cluster,name,executionpath)2 %LAUNCHMULTIPLEQUEUEJOBGEMINI - Launch multiple queueing script on Gemini cluster3 %4 % Usage:5 % LaunchMultipleQueueJobgemini(cluster,name,executionpath)6 7 8 %first, check we have the binary file and the queueing script9 if ~exist([ name '.queue'],'file'),10 error('LaunchMultipleQueueJobgemini error message: queueing script issing, cannot go forward');11 end12 13 if ~exist('ModelList.tar.gz','file'),14 error('LaunchMultipleQueueJobgemini error message: inputs models file missing, cannot go forward');15 end16 17 %upload both files to cluster18 disp('uploading input file, queueing script and variables script');19 eval(['!scp ModelList.tar.gz ' name '.queue ' cluster ':' executionpath]);20 21 disp('launching solution sequence on remote cluster');22 issmssh(cluster,login,['"cd ' executionpath ' && source ' name '.queue "']); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/multiplequeue/BuildMultipleQueueingScriptGeneric.m
1 function BuildMultipleQueueingScriptGeneric(name,executionpath,codepath)2 %BUILDMULTIPLEQUEUEINGSCRIPTGENERIC - ...3 %4 % Usage:5 % BuildMultipleQueueingScriptGeneric(executionpath,codepath)6 7 %not done yet8 error('BuildMultipleQueueingScriptGenericerror message: not supported yet!');9 -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/multiplequeue/BuildMultipleQueueingScript.m
1 function BuildMultipleQueueingScript(cluster,name,executionpath,codepath)2 %BUILDMULTIPLEQUEUEINGSCRIPT -3 %4 % Usage:5 % BuildMultipleQueueingScript(executionpath,codepath)6 7 disp('building queueing script');8 9 %First try and figure out if there is a special script for this particular cluster10 function_name=['BuildMultipleQueueingScript' cluster];11 12 %some specific treatment of identical cluster, gemini, castor and pollux13 if strcmpi(cluster,'castor') || strcmpi(cluster,'pollux'),14 function_name='BuildMultipleQueueingScriptgemini';15 end16 17 if exist(function_name,'file'),18 %Call this function:19 eval([function_name '(name,executionpath,codepath);']);20 else21 %Call the generic BuildQueueingScript:22 BuildMultipleQueueingScriptGeneric(name,executionpath,codepath);23 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/multiplequeue/LaunchMultipleQueueJobGeneric.m
1 function LaunchMultipleQueueJobGeneric(cluster,name,executionpath)2 %LAUNCHMULTIPLEQUEUEJOBGENERIC - Generic routine to launch multiple queueing job3 %4 % Usage:5 % LaunchMultipleQueueJobGeneric(cluster,name,executionpath)6 7 error('LaunchMultipleQueueJobGeneric error message: not supported yet!'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/geometry/GetAreas.m
1 function areas=GetAreas(index,x,y,varargin) 2 %GETAREAS - compute areas or volumes of elements 3 % 4 % compute areas of triangular elements or volumes 5 % of pentahedrons 6 % 7 % Usage: 8 % areas =GetAreas(index,x,y); 9 % volumes=GetAreas(index,x,y,z); 10 % 11 % Examples: 12 % areas =GetAreas(md.mesh.elements,md.mesh.x,md.mesh.y); 13 % volumes=GetAreas(md.mesh.elements,md.mesh.x,md.mesh.y,md.z); 14 15 %get number of elements and number of nodes 16 nels=size(index,1); 17 nods=length(x); 18 if nargin==4, z=varargin{1}; end 19 20 %some checks 21 if nargout~=1 | (nargin~=3 & nargin~=4), 22 help GetAreas 23 error('GetAreas error message: bad usage') 24 end 25 if ((length(y)~=nods) | (nargin==4 & length(z)~=nods)), 26 error('GetAreas error message: x,y and z do not have the same length') 27 end 28 if max(index(:))>nods, 29 error(['GetAreas error message: index should not have values above ' num2str(nods) ]) 30 end 31 if (nargin==3 & size(index,2)~=3), 32 error('GetAreas error message: index should have 3 columns for 2d meshes.') 33 end 34 if (nargin==4 & size(index,2)~=6), 35 error('GetAreas error message: index should have 6 columns for 3d meshes.') 36 end 37 38 %initialization 39 areas=zeros(nels,1); 40 x1=x(index(:,1)); x2=x(index(:,2)); x3=x(index(:,3)); 41 y1=y(index(:,1)); y2=y(index(:,2)); y3=y(index(:,3)); 42 43 %compute the volume of each element 44 if nargin==3, 45 %compute the surface of the triangle 46 areas=(0.5*((x2-x1).*(y3-y1)-(y2-y1).*(x3-x1))); 47 else 48 %V=area(triangle)*1/3(z1+z2+z3) 49 thickness=mean(z(index(:,4:6)),2)-mean(z(index(:,1:3)),2); 50 areas=(0.5*((x2-x1).*(y3-y1)-(y2-y1).*(x3-x1))).*thickness; 51 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/meshread.m
1 function Struct=meshread(filename);2 3 %some checks4 if ~exist(filename),5 error(['meshread error message: file ' filename ' not found!']);6 end7 8 fid=fopen(filename,'r');9 10 while (~feof(fid)),11 12 A=fscanf(fid,'%s',1);13 14 if strcmp(A,'MeshVersionFormatted');15 Struct.Version=fscanf(fid,'%s',1);16 17 elseif strcmp(A,'Dimension'),18 Struct.Dimension=fscanf(fid,'%i',1);19 20 elseif strcmp(A,'Vertices'),21 Struct.nods=fscanf(fid,'%i',1);22 A=fscanf(fid,'%f %f %f',[3 Struct.nods]);23 Struct.x=A(1,:)';24 Struct.y=A(2,:)';25 26 elseif strcmp(A,'Triangles'),27 Struct.nels=fscanf(fid,'%i',1);28 A=fscanf(fid,'%i %i %i',[4 Struct.nels]);29 Struct.index=A(1:3,:)';30 31 elseif strcmp(A,'Quadrilaterals'),32 Struct.nels=fscanf(fid,'%i',1);33 A=fscanf(fid,'%i %i %i %i',[5 Struct.nels]);34 Struct.index=A(1:4,:)';35 else36 %do nothing37 38 end39 end40 41 fclose(fid); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/BamgCallFromMetric.m
1 function md=BamgCallFromMetric(md,metric,gradation),2 %BAMGCALL - call bam3 %4 % call Bamg and the output mesh is plugged onto the model5 % -gradation = maximum edge length gradation between 2 elements6 %7 % Usage:8 % md=BamgCallFromMetric(md,metric,gradation);9 %10 % Example:11 % md=BamgCall(md,metric,1500,10^8,1.3,0.9);12 13 %2d geometric parameter (do not change)14 scale=2/9;15 16 %write files17 t1=clock; fprintf('%s',' writing initial mesh files...');18 fid=fopen('carre0.met','w');19 fprintf(fid,'%i %i\n',md.mesh.numberofvertices,3);20 fprintf(fid,'%i %i %i\n',metric');21 fclose(fid);22 23 fid=fopen('carre0.mesh','w');24 25 %initialiation26 fprintf(fid,'%s %i\n','MeshVersionFormatted',0);27 28 %dimension29 fprintf(fid,'\n%s\n%i\n','Dimension',2);30 31 %Vertices32 fprintf(fid,'\n%s\n%i\n\n','Vertices',md.mesh.numberofvertices);33 fprintf(fid,'%8g %8g %i\n',[md.mesh.x md.mesh.y ones(md.mesh.numberofvertices,1)]');34 35 %Triangles36 fprintf(fid,'\n\n%s\n%i\n\n','Triangles',md.mesh.numberofelements);37 fprintf(fid,'%i %i %i %i\n',[md.mesh.elements ones(md.mesh.numberofelements,1)]');38 numberofelements1=md.mesh.numberofelements;39 40 %close41 fclose(fid);42 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);43 44 %call bamg45 fprintf('%s\n',' call Bamg...');46 system(['bamg -ratio ' num2str(gradation) ' -splitpbedge -nbv 1000000 -M carre0.met -b carre0.mesh -o carre1.mesh']);47 48 %plug new mesh49 t1=clock; fprintf('\n%s',' reading final mesh files...');50 A=meshread('carre1.mesh');51 md.mesh.x=A.x;52 md.mesh.y=A.y;53 md.z=zeros(A.nods,1);54 md.mesh.elements=A.index;55 md.mesh.numberofvertices=A.nods;56 md.mesh.numberofelements=A.nels;57 numberofelements2=md.mesh.numberofelements;58 t2=clock;fprintf('%s\n\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);59 60 %display number of elements61 fprintf('\n%s %i',' inital number of elements:',numberofelements1);62 fprintf('\n%s %i\n\n',' new number of elements:',numberofelements2);63 64 %clean up:65 system('rm carre0.mesh carre0.met carre1.mesh carre1.mesh.gmsh'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/GetCharacteristicLength.m
1 function length=GetCharacteristicLength(index,x,y,varargin)2 %GETCHARACTERISTICLENGTH - compute characteristic length for a mesh3 %4 % compute characteristic lengths of every element of a mesh.5 %6 % Usage:7 % length =GetCharacteristicLength(index,x,y);8 % length =GetCharacteristicLength(index,x,y,z);9 %10 % Examples:11 % length =GetCharacteristicLength(md.mesh.elements,md.mesh.x,md.mesh.y);12 % length =GetCharacteristicLength(md.mesh.elements,md.mesh.x,md.mesh.y,md.z);13 14 15 %get number of elements and number of nodes16 nels=size(index,1);17 nods=numel(x);18 19 %some checks20 if nargout~=1 | (nargin~=3 & nargin~=4),21 help GetCharacteristicLength22 error('GetCharacteristicLength error message: bad usage')23 end24 if ((numel(y)~=nods) | (nargin==4 & numel(z)~=nods)),25 error('GetCharacteristicLength error message: x,y and z do not have the same length')26 end27 if max(index(:))>nods,28 error(['GetCharacteristicLength error message: index should not have values above ' num2str(nods) ])29 end30 if (nargin==3 & size(index,2)~=3),31 error('GetCharacteristicLength error message: index should have 3 columns for 2d meshes.')32 end33 if (nargin==4 & size(index,2)~=6),34 error('GetCharacteristicLength error message: index should have 6 columns for 3d meshes.')35 end36 37 %get areas or volumes.38 areas=GetAreas(index,x,y,varargin{:});39 40 %for a 2d mesh:41 if nargin==3,42 length=sqrt(2*areas);43 else44 error('not supported yet');45 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/GetAreas.m
1 function areas=GetAreas(index,x,y,varargin)2 %GETAREAS - compute areas or volumes of elements3 %4 % compute areas of triangular elements or volumes5 % of pentahedrons6 %7 % Usage:8 % areas =GetAreas(index,x,y);9 % volumes=GetAreas(index,x,y,z);10 %11 % Examples:12 % areas =GetAreas(md.mesh.elements,md.mesh.x,md.mesh.y);13 % volumes=GetAreas(md.mesh.elements,md.mesh.x,md.mesh.y,md.z);14 15 %get number of elements and number of nodes16 nels=size(index,1);17 nods=length(x);18 if nargin==4, z=varargin{1}; end19 20 %some checks21 if nargout~=1 | (nargin~=3 & nargin~=4),22 help GetAreas23 error('GetAreas error message: bad usage')24 end25 if ((length(y)~=nods) | (nargin==4 & length(z)~=nods)),26 error('GetAreas error message: x,y and z do not have the same length')27 end28 if max(index(:))>nods,29 error(['GetAreas error message: index should not have values above ' num2str(nods) ])30 end31 if (nargin==3 & size(index,2)~=3),32 error('GetAreas error message: index should have 3 columns for 2d meshes.')33 end34 if (nargin==4 & size(index,2)~=6),35 error('GetAreas error message: index should have 6 columns for 3d meshes.')36 end37 38 %initialization39 areas=zeros(nels,1);40 x1=x(index(:,1)); x2=x(index(:,2)); x3=x(index(:,3));41 y1=y(index(:,1)); y2=y(index(:,2)); y3=y(index(:,3));42 43 %compute the volume of each element44 if nargin==3,45 %compute the surface of the triangle46 areas=(0.5*((x2-x1).*(y3-y1)-(y2-y1).*(x3-x1)));47 else48 %V=area(triangle)*1/3(z1+z2+z3)49 thickness=mean(z(index(:,4:6)),2)-mean(z(index(:,1:3)),2);50 areas=(0.5*((x2-x1).*(y3-y1)-(y2-y1).*(x3-x1))).*thickness;51 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/isconnected.m
1 function flag=isconnected(elements,A,B)2 %ISCONNECTED: are two nodes connected by a triangulation?3 %4 % Usage: flag=isconnected(elements,A,B)5 %6 %7 8 elements=ElementsFromEdge(elements,A,B);9 if isempty(elements),10 flag=0;11 else12 flag=1;13 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/BamgCall.m
1 function md=BamgCall(md,field,hmin,hmax,gradation,epsilon),2 %BAMGCALL - call bam3 %4 % build a metric using the Hessian of the given field5 % call Bamg and the output mesh is plugged onto the model6 % -hmin = minimum edge length (m)7 % -hmax = maximum edge length (m)8 % -gradation = maximum edge length gradation between 2 elements9 % -epsilon = average error on each element (m/yr)10 %11 % Usage:12 % md=BamgCall(md,field,hmin,hmax,gradation,epsilon);13 %14 % Example:15 % md=BamgCall(md,md.inversion.vel_obs,1500,10^8,1.3,0.9);16 17 %2d geometric parameter (do not change)18 scale=2/9;19 20 %Compute Hessian21 t1=clock; fprintf('%s',' computing Hessian...');22 hessian=ComputeHessian(md.mesh.elements,md.mesh.x,md.mesh.y,field,'node');23 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);24 25 %Compute metric26 t1=clock; fprintf('%s',' computing metric...');27 if length(md.nodeonwater)==md.mesh.numberofvertices,28 pos=find(md.nodeonwater);29 else30 pos=[];31 end32 metric=ComputeMetric(hessian,scale,epsilon,hmin,hmax,pos);33 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);34 35 %write files36 t1=clock; fprintf('%s',' writing initial mesh files...');37 fid=fopen('carre0.met','w');38 fprintf(fid,'%i %i\n',md.mesh.numberofvertices,3);39 fprintf(fid,'%i %i %i\n',metric');40 fclose(fid);41 42 fid=fopen('carre0.mesh','w');43 44 %initialiation45 fprintf(fid,'%s %i\n','MeshVersionFormatted',0);46 47 %dimension48 fprintf(fid,'\n%s\n%i\n','Dimension',2);49 50 %Vertices51 fprintf(fid,'\n%s\n%i\n\n','Vertices',md.mesh.numberofvertices);52 fprintf(fid,'%8g %8g %i\n',[md.mesh.x md.mesh.y ones(md.mesh.numberofvertices,1)]');53 54 %Triangles55 fprintf(fid,'\n\n%s\n%i\n\n','Triangles',md.mesh.numberofelements);56 fprintf(fid,'%i %i %i %i\n',[md.mesh.elements ones(md.mesh.numberofelements,1)]');57 numberofelements1=md.mesh.numberofelements;58 59 %close60 fclose(fid);61 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);62 63 %call bamg64 fprintf('%s\n',' call Bamg...');65 system(['bamg -ratio ' num2str(gradation) ' -splitpbedge -nbv 1000000 -M carre0.met -b carre0.mesh -o carre1.mesh']);66 67 %plug new mesh68 t1=clock; fprintf('\n%s',' reading final mesh files...');69 A=meshread('carre1.mesh');70 md.mesh.x=A.x;71 md.mesh.y=A.y;72 md.z=zeros(A.nods,1);73 md.mesh.elements=A.index;74 md.mesh.numberofvertices=A.nods;75 md.mesh.numberofelements=A.nels;76 numberofelements2=md.mesh.numberofelements;77 t2=clock;fprintf('%s\n\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);78 79 %display number of elements80 fprintf('\n%s %i',' inital number of elements:',numberofelements1);81 fprintf('\n%s %i\n\n',' new number of elements:',numberofelements2);82 83 %clean up:84 system('rm carre0.mesh carre0.met carre1.mesh carre1.mesh.gmsh'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/YamsCall.m
1 function md=YamsCall(md,field,hmin,hmax,gradation,epsilon),2 %YAMSCALL - call yams3 %4 % build a metric using the Hessian of the given field5 % call Yams and the output mesh is plugged onto the model6 % -hmin = minimum edge length (m)7 % -hmax = maximum edge length (m)8 % -gradation = maximum edge length gradation between 2 elements9 % -epsilon = average error on each element (m/yr)10 %11 % Usage:12 % md=YamsCall(md,field,hmin,hmax,gradation,epsilon);13 %14 % Example:15 % md=YamsCall(md,md.inversion.vel_obs,1500,10^8,1.3,0.9);16 17 %2d geometric parameter (do not change)18 scale=2/9;19 20 %Compute Hessian21 t1=clock; fprintf('%s',' computing Hessian...');22 hessian=ComputeHessian(md.mesh.elements,md.mesh.x,md.mesh.y,field,'node');23 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);24 25 %Compute metric26 t1=clock; fprintf('%s',' computing metric...');27 if length(md.mask.vertexonwater)==md.mesh.numberofvertices,28 pos=find(md.mask.vertexonwater);29 else30 pos=[];31 end32 metric=ComputeMetric(hessian,scale,epsilon,hmin,hmax,pos);33 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);34 35 %write files36 t1=clock; fprintf('%s',' writing initial mesh files...');37 save -ascii carre0.met metric38 39 fid=fopen('carre0.mesh','w');40 41 %initialiation42 fprintf(fid,'\n%s\n%i\n','MeshVersionFormatted',1);43 44 %dimension45 fprintf(fid,'\n%s\n%i\n','Dimension',2);46 47 %Vertices48 fprintf(fid,'\n%s\n%i\n\n','Vertices',md.mesh.numberofvertices);49 fprintf(fid,'%8g %8g %i\n',[md.mesh.x md.mesh.y zeros(md.mesh.numberofvertices,1)]');50 51 %Triangles52 fprintf(fid,'\n\n%s\n%i\n\n','Triangles',md.mesh.numberofelements);53 fprintf(fid,'%i %i %i %i\n',[md.mesh.elements zeros(md.mesh.numberofelements,1)]');54 numberofelements1=md.mesh.numberofelements;55 56 %Deal with rifts57 if ~isnan(md.rifts.riftstruct),58 59 %we have the list of triangles that make up the rift. keep those triangles around during refinement.60 triangles=[];61 for i=1:size(md.rifts.riftstruct,1),62 triangles=[triangles md.rifts(i).segments(:,3)'];63 end64 65 fprintf(fid,'\n\n%s\n%i\n\n','RequiredTriangles',length(triangles));66 fprintf(fid,'%i\n',triangles);67 end68 69 %close70 fclose(fid);71 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);72 73 %call yams74 fprintf('%s\n',' call Yams...');75 if ispc76 %windows77 system(['yams2-win -O 1 -v -0 -ecp -hgrad ' num2str(gradation) ' carre0 carre1']);78 elseif ismac79 %Macosx80 system(['yams2-osx -O 1 -v -0 -ecp -hgrad ' num2str(gradation) ' carre0 carre1']);81 else82 %Linux83 system(['yams2-linux -O 1 -v -0 -ecp -hgrad ' num2str(gradation) ' carre0 carre1']);84 end85 86 %plug new mesh87 t1=clock; fprintf('\n%s',' reading final mesh files...');88 Tria=load('carre1.tria');89 Coor=load('carre1.coor');90 md.mesh.x=Coor(:,1);91 md.mesh.y=Coor(:,2);92 md.mesh.z=zeros(size(Coor,1),1);93 md.mesh.elements=Tria;94 md.mesh.numberofvertices=size(Coor,1);95 md.mesh.numberofelements=size(Tria,1);96 numberofelements2=md.mesh.numberofelements;97 t2=clock;fprintf('%s\n\n',[' done (' num2str(etime(t2,t1)) ' seconds)']);98 99 %display number of elements100 fprintf('\n%s %i',' inital number of elements:',numberofelements1);101 fprintf('\n%s %i\n\n',' new number of elements:',numberofelements2);102 103 %clean up:104 system('rm carre0.mesh carre0.met carre1.tria carre1.coor carre1.meshb'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/yams.m
1 function md=yams(md,varargin);2 %MESHYAMS - Build model of Antarctica by refining according to observed velocity error estimator3 %4 % Usage:5 % md=yams(md,varargin);6 % where varargin is a lit of paired arguments.7 % arguments can be: 'domainoutline': Argus file containing the outline of the domain to be meshed8 % arguments can be: 'velocities': matlab file containing the velocities [m/yr]9 % optional arguments: 'groundeddomain': Argus file containing the outline of the grounded ice10 % this option is used to minimize the metric on water (no refinement)11 % optional arguments: 'resolution': initial mesh resolution [m]12 % optional arguments: 'nsteps': number of steps of mesh adaptation13 % optional arguments: 'epsilon': average interpolation error wished [m/yr]14 % optional arguments: 'hmin': minimum edge length15 % optional arguments: 'hmanx': maximum edge16 % optional arguments: 'riftoutline': if rifts are present, specifies rift outline file.17 %18 %19 % Examples:20 % md=yams(md,'domainoutline','Domain.exp','velocities','vel.mat');21 % md=yams(md,'domainoutline','Domain.exp','velocities','vel.mat','groundeddomain','ground.exp');22 % md=yams(md,'domainoutline','Domain.exp','velocities','vel.mat','groundeddomain','ground.exp','nsteps',6,'epsilon',2,'hmin',500,'hmax',30000);23 24 %recover options25 options=pairoptions(varargin{:});26 options=deleteduplicates(options,1);27 28 %recover some fields29 disp('MeshYams Options:')30 domainoutline=getfieldvalue(options,'domainoutline');31 disp(sprintf(' %-15s: ''%s''','DomainOutline',domainoutline));32 riftoutline=getfieldvalue(options,'riftoutline','N/A');33 disp(sprintf(' %-15s: ''%s''','riftoutline',riftoutline));34 groundeddomain=getfieldvalue(options,'groundeddomain','N/A');35 disp(sprintf(' %-15s: ''%s''','GroundedDomain',groundeddomain));36 velocities=getfieldvalue(options,'velocities');37 disp(sprintf(' %-15s: ''%s''','Velocities',velocities));38 resolution=getfieldvalue(options,'resolution',5000);39 disp(sprintf(' %-15s: %f','Resolution',resolution));40 nsteps=getfieldvalue(options,'nsteps',6);41 disp(sprintf(' %-15s: %i','nsteps',nsteps));42 gradation=getfieldvalue(options,'gradation',2*ones(nsteps,1));43 disp(sprintf(' %-15s: %g','gradation',gradation(1)));44 epsilon=getfieldvalue(options,'epsilon',3);45 disp(sprintf(' %-15s: %f','epsilon',epsilon));46 hmin=getfieldvalue(options,'hmin',500);47 disp(sprintf(' %-15s: %f','hmin',hmin));48 hmax=getfieldvalue(options,'hmax',150*10^3);49 disp(sprintf(' %-15s: %f\n','hmax',hmax));50 51 %mesh with initial resolution52 disp('Initial mesh generation...');53 if strcmpi(riftoutline,'N/A');54 md=setmesh(md,domainoutline,resolution);55 else56 md=setmesh(md,domainoutline,riftoutline,resolution);57 md=meshprocessrifts(md,domainoutline);58 end59 disp(['Initial mesh, number of elements: ' num2str(md.mesh.numberofelements)]);60 61 %load velocities62 disp('loading velocities...');63 Names=VelFindVarNames(velocities);64 Vel=load(velocities);65 66 %start mesh adaptation67 for i=1:nsteps,68 disp(['Iteration #' num2str(i) '/' num2str(nsteps)]);69 70 %interpolate velocities onto mesh71 disp(' interpolating velocities...');72 if strcmpi(Names.interp,'node'),73 vx_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0);74 vy_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0);75 else76 vx_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0);77 vy_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0);78 end79 field=sqrt(vx_obs.^2+vy_obs.^2);80 81 %set mask.vertexonwater field82 if ~strcmp(groundeddomain,'N/A'),83 nodeground=ContourToMesh(md.mesh.elements,md.mesh.x,md.mesh.y,groundeddomain,'node',2);84 md.mask.vertexonwater=ones(md.mesh.numberofvertices,1);85 md.mask.vertexonwater(find(nodeground))=0;86 else87 md.mask.vertexonwater=zeros(md.mesh.numberofvertices,1);88 end89 90 %adapt according to velocities91 disp(' adapting...');92 md=YamsCall(md,field,hmin,hmax,gradation(i),epsilon);93 94 %if we have rifts, we just messed them up, we need to recreate the segments that constitute those95 %rifts, because the segments are used in YamsCall to freeze the rifts elements during refinement.96 if md.rifts.numrifts,97 md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices);98 md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity);99 md.mesh.segments=findsegments(md);100 md=yamsrecreateriftsegments(md);101 end102 103 end104 105 disp(['Final mesh, number of elements: ' num2str(md.mesh.numberofelements)]);106 107 %Now, build the connectivity tables for this mesh.108 md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices);109 md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity);110 111 %recreate segments112 md.mesh.segments=findsegments(md);113 md.mesh.vertexonboundary=zeros(md.mesh.numberofvertices,1); md.mesh.vertexonboundary(md.mesh.segments(:,1:2))=1;114 115 %Fill in rest of fields:116 md.mesh.z=zeros(md.mesh.numberofvertices,1);117 md.mesh.vertexonbed=ones(md.mesh.numberofvertices,1);118 md.mesh.vertexonsurface=ones(md.mesh.numberofvertices,1);119 md.mesh.elementonbed=ones(md.mesh.numberofelements,1);120 md.mesh.elementonsurface=ones(md.mesh.numberofelements,1);121 if ~strcmp(groundeddomain,'N/A'),122 nodeground=ContourToMesh(md.mesh.elements,md.mesh.x,md.mesh.y,groundeddomain,'node',2);123 md.mask.vertexonwater=ones(md.mesh.numberofvertices,1);124 md.mask.vertexonwater(find(nodeground))=0;125 else126 md.mask.vertexonwater=zeros(md.mesh.numberofvertices,1);127 end128 if strcmpi(Names.interp,'node'),129 md.inversion.vx_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0);130 md.inversion.vy_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0);131 else132 md.inversion.vx_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0);133 md.inversion.vy_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0);134 end135 md.inversion.vel_obs=sqrt(md.inversion.vx_obs.^2+md.inversion.vy_obs.^2);136 137 %deal with rifts138 if md.rifts.numrifts,139 %first, recreate rift segments140 md=meshyamsrecreateriftsegments(md);141 142 %using the segments, recreate the penaltypairs143 for j=1:md.rifts.numrifts,144 rift=md.rifts.riftstruct(j);145 146 %build normals and lengths of segments:147 lengths=sqrt((md.mesh.x(rift.segments(:,1))-md.mesh.x(rift.segments(:,2))).^2 + (md.mesh.y(rift.segments(:,1))-md.mesh.y(rift.segments(:,2))).^2 );148 normalsx=cos(atan2((md.mesh.x(rift.segments(:,1))-md.mesh.x(rift.segments(:,2))) , (md.mesh.y(rift.segments(:,2))-md.mesh.y(rift.segments(:,1)))));149 normalsy=sin(atan2((md.mesh.x(rift.segments(:,1))-md.mesh.x(rift.segments(:,2))) , (md.mesh.y(rift.segments(:,2))-md.mesh.y(rift.segments(:,1)))));150 151 %ok, build penaltypairs:152 numpenaltypairs=length(rift.segments)/2-1;153 rift.penaltypairs=zeros(numpenaltypairs,7);154 155 for i=1:numpenaltypairs,156 rift.penaltypairs(i,1)=rift.segments(i,2);157 rift.penaltypairs(i,2)=rift.segments(end-i,2);158 rift.penaltypairs(i,3)=rift.segments(i,3);159 rift.penaltypairs(i,4)=rift.segments(end-i,3);160 rift.penaltypairs(i,5)=normalsx(i)+normalsx(i+1);161 rift.penaltypairs(i,6)=normalsy(i)+normalsy(i+1);162 rift.penaltypairs(i,7)=(lengths(i)+lengths(i+1))/2;163 end164 %renormalize norms:165 norms=sqrt(rift.penaltypairs(:,5).^2+rift.penaltypairs(:,6).^2);166 rift.penaltypairs(:,5)=rift.penaltypairs(:,5)./norms;167 rift.penaltypairs(:,6)=rift.penaltypairs(:,6)./norms;168 169 md.rifts.riftstruct(j)=rift;170 end171 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/rifttipsonmesh.m
1 function tips=rifttipsonmesh(md,riftoutline)2 %RIFTTIPSONMESH: identify, using a rift outline, the nodes that are tips of3 % rifts.4 5 %read rifts from outline file6 rifts=expread(riftoutline,1);7 8 tips=[];9 10 for i=1:length(rifts),11 rift=rifts(i);12 13 x_tip=rift.x(1);14 y_tip=rift.y(1);15 16 index=find_point(md.mesh.x,md.mesh.y,x_tip,y_tip);17 tips(end+1)=index;18 19 x_tip=rift.x(end);20 y_tip=rift.y(end);21 22 index=find_point(md.mesh.x,md.mesh.y,x_tip,y_tip);23 tips(end+1)=index;24 25 end26 -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/NodeInElement.m
1 1 function node_in_element=NodeInElement(newx,newy,elements,x,y,nodeconnectivity); 2 % NODEINELEMENT:find for a list of nodes (in newx,newy), which elements in the mesh (elements,x,y) they belong to.2 % NODEINELEMENT - find for a list of nodes (in newx,newy), which elements in the mesh (elements,x,y) they belong to. 3 3 % 4 % Usage: node_in_element=NodeInElement(newx,newy,elements,x,y,md.mesh.vertexconnectivity); 4 % Usage: 5 % node_in_element=NodeInElement(newx,newy,elements,x,y,md.mesh.vertexconnectivity); 5 6 % 6 7 % See also Nodeconnectivity 7 8 % -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/rifts/rifttipsonmesh.m
1 function tips=rifttipsonmesh(md,riftoutline) 2 %RIFTTIPSONMESH: identify, using a rift outline, the nodes that are tips of 3 % rifts. 4 5 %read rifts from outline file 6 rifts=expread(riftoutline,1); 7 8 tips=[]; 9 10 for i=1:length(rifts), 11 rift=rifts(i); 12 13 x_tip=rift.x(1); 14 y_tip=rift.y(1); 15 16 index=find_point(md.mesh.x,md.mesh.y,x_tip,y_tip); 17 tips(end+1)=index; 18 19 x_tip=rift.x(end); 20 y_tip=rift.y(end); 21 22 index=find_point(md.mesh.x,md.mesh.y,x_tip,y_tip); 23 tips(end+1)=index; 24 25 end 26 -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/rifts/meshprocessoutsiderifts.m
92 92 md.mesh.vertexonsurface=ones(md.mesh.numberofvertices,1); 93 93 md.mesh.elementonbed=ones(md.mesh.numberofelements,1); 94 94 md.mesh.elementonsurface=ones(md.mesh.numberofelements,1); 95 end 96 97 function flag=isconnected(elements,A,B)% {{{ 98 %ISCONNECTED: are two nodes connected by a triangulation? 99 % 100 % Usage: flag=isconnected(elements,A,B) 101 % 102 % 103 104 elements=ElementsFromEdge(elements,A,B); 105 if isempty(elements), 106 flag=0; 107 else 108 flag=1; 109 end 110 end % }}} -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/mesh/FixMesh.m
1 1 function [index2 x2 y2 value2]=FixMesh(index,x,y,value) 2 % FixMesh fix mesh with broken triangles, orphan vertices, etc ...2 % FIXMESH - FixMesh fix mesh with broken triangles, orphan vertices, etc ... 3 3 % 4 % Usage:5 % 6 % 7 % 8 % 4 % Usage: 5 % [index2 x2 y2 value2]=FixMesh(index,x,y,value) 6 % where index,x,y is a delaunay triangulation, 7 % value is a field on the input triangulation, with values at the vertices 8 % index2,x2,y2,value2 is the repaired triangulation, with new values on new vertices 9 9 % 10 10 % 11 11 -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/classes/modellist.m
291 291 end % }}} 292 292 end 293 293 end 294 295 function BuildMultipleQueueingScript(cluster,name,executionpath,codepath)% {{{ 296 %BUILDMULTIPLEQUEUEINGSCRIPT - 297 % 298 % Usage: 299 % BuildMultipleQueueingScript(executionpath,codepath) 300 301 disp('building queueing script'); 302 303 %First try and figure out if there is a special script for this particular cluster 304 function_name=['BuildMultipleQueueingScript' cluster]; 305 306 %some specific treatment of identical cluster, gemini, castor and pollux 307 if strcmpi(cluster,'castor') || strcmpi(cluster,'pollux'), 308 function_name='BuildMultipleQueueingScriptgemini'; 309 end 310 311 if exist(function_name,'file'), 312 %Call this function: 313 eval([function_name '(name,executionpath,codepath);']); 314 else 315 %Call the generic BuildQueueingScript: 316 BuildMultipleQueueingScriptGeneric(name,executionpath,codepath); 317 end 318 end % }}} 319 function BuildQueueingScriptgemini(name,executionpath,codepath)% {{{ 320 %BUILDQUEUEINGSCRIPTGEMINI - ... 321 % 322 % Usage: 323 % BuildQueueingScriptgemini(md,executionpath,codepath) 324 325 scriptname=[name '.queue']; 326 327 fid=fopen(scriptname,'w'); 328 if fid==-1, 329 error(['BuildQueueingScriptgeminierror message: could not open ' scriptname ' file for ascii writing']); 330 end 331 332 fprintf(fid,'#!/bin/sh\n'); 333 fprintf(fid,'cd %s\n',executionpath); 334 fprintf(fid,'mkdir %s\n',name); 335 fprintf(fid,'cd %s\n',name); 336 fprintf(fid,'mv ../ModelList.tar.gz ./\n'); 337 fprintf(fid,'tar -zxvf ModelList.tar.gz\n'); 338 fprintf(fid,'foreach i (%s-*vs*.queue)\n',name); 339 fprintf(fid,'qsub $i\n'); 340 fprintf(fid,'end\n'); 341 fclose(fid); 342 end% }}} 343 function LaunchMultipleQueueJob(cluster,name,executionpath)% {{{ 344 %LAUNCHMULTIPLEQUEUEJOB - ... 345 % 346 % Usage: 347 % LaunchMultipleQueueJob(executionpath) 348 349 %First try and figure out if there is a special script for thie particular cluster 350 function_name=['LaunchMultipleQueueJob' cluster]; 351 352 %some specific treatment of identical cluster, gemini, castor and pollux 353 if strcmpi(cluster,'castor') || strcmpi(cluster,'pollux'), 354 function_name='LaunchMultipleQueueJobgemini'; 355 end 356 357 if exist(function_name,'file'), 358 %Call this function: 359 eval([function_name '(cluster,name,executionpath);']); 360 else 361 %Call the generic LaunchMultipleQueueJob: 362 LaunchMultipleQueueJobGeneric(cluster,name,executionpath); 363 end 364 end% }}} 365 function md=LaunchMultipleQueueJobgemini(cluster,name,executionpath)% {{{ 366 %LAUNCHMULTIPLEQUEUEJOBGEMINI - Launch multiple queueing script on Gemini cluster 367 % 368 % Usage: 369 % LaunchMultipleQueueJobgemini(cluster,name,executionpath) 370 371 372 %first, check we have the binary file and the queueing script 373 if ~exist([ name '.queue'],'file'), 374 error('LaunchMultipleQueueJobgemini error message: queueing script issing, cannot go forward'); 375 end 376 377 if ~exist('ModelList.tar.gz','file'), 378 error('LaunchMultipleQueueJobgemini error message: inputs models file missing, cannot go forward'); 379 end 380 381 %upload both files to cluster 382 disp('uploading input file, queueing script and variables script'); 383 eval(['!scp ModelList.tar.gz ' name '.queue ' cluster ':' executionpath]); 384 385 disp('launching solution sequence on remote cluster'); 386 issmssh(cluster,login,['"cd ' executionpath ' && source ' name '.queue "']); 387 end% }}} -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/contrib/bamg/YamsCall.m
1 function md=YamsCall(md,field,hmin,hmax,gradation,epsilon), 2 %YAMSCALL - call yams 3 % 4 % build a metric using the Hessian of the given field 5 % call Yams and the output mesh is plugged onto the model 6 % -hmin = minimum edge length (m) 7 % -hmax = maximum edge length (m) 8 % -gradation = maximum edge length gradation between 2 elements 9 % -epsilon = average error on each element (m/yr) 10 % 11 % Usage: 12 % md=YamsCall(md,field,hmin,hmax,gradation,epsilon); 13 % 14 % Example: 15 % md=YamsCall(md,md.inversion.vel_obs,1500,10^8,1.3,0.9); 16 17 %2d geometric parameter (do not change) 18 scale=2/9; 19 20 %Compute Hessian 21 t1=clock; fprintf('%s',' computing Hessian...'); 22 hessian=ComputeHessian(md.mesh.elements,md.mesh.x,md.mesh.y,field,'node'); 23 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 24 25 %Compute metric 26 t1=clock; fprintf('%s',' computing metric...'); 27 if length(md.mask.vertexonwater)==md.mesh.numberofvertices, 28 pos=find(md.mask.vertexonwater); 29 else 30 pos=[]; 31 end 32 metric=ComputeMetric(hessian,scale,epsilon,hmin,hmax,pos); 33 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 34 35 %write files 36 t1=clock; fprintf('%s',' writing initial mesh files...'); 37 save -ascii carre0.met metric 38 39 fid=fopen('carre0.mesh','w'); 40 41 %initialiation 42 fprintf(fid,'\n%s\n%i\n','MeshVersionFormatted',1); 43 44 %dimension 45 fprintf(fid,'\n%s\n%i\n','Dimension',2); 46 47 %Vertices 48 fprintf(fid,'\n%s\n%i\n\n','Vertices',md.mesh.numberofvertices); 49 fprintf(fid,'%8g %8g %i\n',[md.mesh.x md.mesh.y zeros(md.mesh.numberofvertices,1)]'); 50 51 %Triangles 52 fprintf(fid,'\n\n%s\n%i\n\n','Triangles',md.mesh.numberofelements); 53 fprintf(fid,'%i %i %i %i\n',[md.mesh.elements zeros(md.mesh.numberofelements,1)]'); 54 numberofelements1=md.mesh.numberofelements; 55 56 %Deal with rifts 57 if ~isnan(md.rifts.riftstruct), 58 59 %we have the list of triangles that make up the rift. keep those triangles around during refinement. 60 triangles=[]; 61 for i=1:size(md.rifts.riftstruct,1), 62 triangles=[triangles md.rifts(i).segments(:,3)']; 63 end 64 65 fprintf(fid,'\n\n%s\n%i\n\n','RequiredTriangles',length(triangles)); 66 fprintf(fid,'%i\n',triangles); 67 end 68 69 %close 70 fclose(fid); 71 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 72 73 %call yams 74 fprintf('%s\n',' call Yams...'); 75 if ispc 76 %windows 77 system(['yams2-win -O 1 -v -0 -ecp -hgrad ' num2str(gradation) ' carre0 carre1']); 78 elseif ismac 79 %Macosx 80 system(['yams2-osx -O 1 -v -0 -ecp -hgrad ' num2str(gradation) ' carre0 carre1']); 81 else 82 %Linux 83 system(['yams2-linux -O 1 -v -0 -ecp -hgrad ' num2str(gradation) ' carre0 carre1']); 84 end 85 86 %plug new mesh 87 t1=clock; fprintf('\n%s',' reading final mesh files...'); 88 Tria=load('carre1.tria'); 89 Coor=load('carre1.coor'); 90 md.mesh.x=Coor(:,1); 91 md.mesh.y=Coor(:,2); 92 md.mesh.z=zeros(size(Coor,1),1); 93 md.mesh.elements=Tria; 94 md.mesh.numberofvertices=size(Coor,1); 95 md.mesh.numberofelements=size(Tria,1); 96 numberofelements2=md.mesh.numberofelements; 97 t2=clock;fprintf('%s\n\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 98 99 %display number of elements 100 fprintf('\n%s %i',' inital number of elements:',numberofelements1); 101 fprintf('\n%s %i\n\n',' new number of elements:',numberofelements2); 102 103 %clean up: 104 system('rm carre0.mesh carre0.met carre1.tria carre1.coor carre1.meshb'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/contrib/bamg/meshread.m
1 function Struct=meshread(filename); 2 3 %some checks 4 if ~exist(filename), 5 error(['meshread error message: file ' filename ' not found!']); 6 end 7 8 fid=fopen(filename,'r'); 9 10 while (~feof(fid)), 11 12 A=fscanf(fid,'%s',1); 13 14 if strcmp(A,'MeshVersionFormatted'); 15 Struct.Version=fscanf(fid,'%s',1); 16 17 elseif strcmp(A,'Dimension'), 18 Struct.Dimension=fscanf(fid,'%i',1); 19 20 elseif strcmp(A,'Vertices'), 21 Struct.nods=fscanf(fid,'%i',1); 22 A=fscanf(fid,'%f %f %f',[3 Struct.nods]); 23 Struct.x=A(1,:)'; 24 Struct.y=A(2,:)'; 25 26 elseif strcmp(A,'Triangles'), 27 Struct.nels=fscanf(fid,'%i',1); 28 A=fscanf(fid,'%i %i %i',[4 Struct.nels]); 29 Struct.index=A(1:3,:)'; 30 31 elseif strcmp(A,'Quadrilaterals'), 32 Struct.nels=fscanf(fid,'%i',1); 33 A=fscanf(fid,'%i %i %i %i',[5 Struct.nels]); 34 Struct.index=A(1:4,:)'; 35 else 36 %do nothing 37 38 end 39 end 40 41 fclose(fid); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/contrib/bamg/yams.m
1 function md=yams(md,varargin); 2 %MESHYAMS - Build model of Antarctica by refining according to observed velocity error estimator 3 % 4 % Usage: 5 % md=yams(md,varargin); 6 % where varargin is a lit of paired arguments. 7 % arguments can be: 'domainoutline': Argus file containing the outline of the domain to be meshed 8 % arguments can be: 'velocities': matlab file containing the velocities [m/yr] 9 % optional arguments: 'groundeddomain': Argus file containing the outline of the grounded ice 10 % this option is used to minimize the metric on water (no refinement) 11 % optional arguments: 'resolution': initial mesh resolution [m] 12 % optional arguments: 'nsteps': number of steps of mesh adaptation 13 % optional arguments: 'epsilon': average interpolation error wished [m/yr] 14 % optional arguments: 'hmin': minimum edge length 15 % optional arguments: 'hmanx': maximum edge 16 % optional arguments: 'riftoutline': if rifts are present, specifies rift outline file. 17 % 18 % 19 % Examples: 20 % md=yams(md,'domainoutline','Domain.exp','velocities','vel.mat'); 21 % md=yams(md,'domainoutline','Domain.exp','velocities','vel.mat','groundeddomain','ground.exp'); 22 % md=yams(md,'domainoutline','Domain.exp','velocities','vel.mat','groundeddomain','ground.exp','nsteps',6,'epsilon',2,'hmin',500,'hmax',30000); 23 24 %recover options 25 options=pairoptions(varargin{:}); 26 options=deleteduplicates(options,1); 27 28 %recover some fields 29 disp('MeshYams Options:') 30 domainoutline=getfieldvalue(options,'domainoutline'); 31 disp(sprintf(' %-15s: ''%s''','DomainOutline',domainoutline)); 32 riftoutline=getfieldvalue(options,'riftoutline','N/A'); 33 disp(sprintf(' %-15s: ''%s''','riftoutline',riftoutline)); 34 groundeddomain=getfieldvalue(options,'groundeddomain','N/A'); 35 disp(sprintf(' %-15s: ''%s''','GroundedDomain',groundeddomain)); 36 velocities=getfieldvalue(options,'velocities'); 37 disp(sprintf(' %-15s: ''%s''','Velocities',velocities)); 38 resolution=getfieldvalue(options,'resolution',5000); 39 disp(sprintf(' %-15s: %f','Resolution',resolution)); 40 nsteps=getfieldvalue(options,'nsteps',6); 41 disp(sprintf(' %-15s: %i','nsteps',nsteps)); 42 gradation=getfieldvalue(options,'gradation',2*ones(nsteps,1)); 43 disp(sprintf(' %-15s: %g','gradation',gradation(1))); 44 epsilon=getfieldvalue(options,'epsilon',3); 45 disp(sprintf(' %-15s: %f','epsilon',epsilon)); 46 hmin=getfieldvalue(options,'hmin',500); 47 disp(sprintf(' %-15s: %f','hmin',hmin)); 48 hmax=getfieldvalue(options,'hmax',150*10^3); 49 disp(sprintf(' %-15s: %f\n','hmax',hmax)); 50 51 %mesh with initial resolution 52 disp('Initial mesh generation...'); 53 if strcmpi(riftoutline,'N/A'); 54 md=setmesh(md,domainoutline,resolution); 55 else 56 md=setmesh(md,domainoutline,riftoutline,resolution); 57 md=meshprocessrifts(md,domainoutline); 58 end 59 disp(['Initial mesh, number of elements: ' num2str(md.mesh.numberofelements)]); 60 61 %load velocities 62 disp('loading velocities...'); 63 Names=VelFindVarNames(velocities); 64 Vel=load(velocities); 65 66 %start mesh adaptation 67 for i=1:nsteps, 68 disp(['Iteration #' num2str(i) '/' num2str(nsteps)]); 69 70 %interpolate velocities onto mesh 71 disp(' interpolating velocities...'); 72 if strcmpi(Names.interp,'node'), 73 vx_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0); 74 vy_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0); 75 else 76 vx_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0); 77 vy_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0); 78 end 79 field=sqrt(vx_obs.^2+vy_obs.^2); 80 81 %set mask.vertexonwater field 82 if ~strcmp(groundeddomain,'N/A'), 83 nodeground=ContourToMesh(md.mesh.elements,md.mesh.x,md.mesh.y,groundeddomain,'node',2); 84 md.mask.vertexonwater=ones(md.mesh.numberofvertices,1); 85 md.mask.vertexonwater(find(nodeground))=0; 86 else 87 md.mask.vertexonwater=zeros(md.mesh.numberofvertices,1); 88 end 89 90 %adapt according to velocities 91 disp(' adapting...'); 92 md=YamsCall(md,field,hmin,hmax,gradation(i),epsilon); 93 94 %if we have rifts, we just messed them up, we need to recreate the segments that constitute those 95 %rifts, because the segments are used in YamsCall to freeze the rifts elements during refinement. 96 if md.rifts.numrifts, 97 md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices); 98 md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity); 99 md.mesh.segments=findsegments(md); 100 md=yamsrecreateriftsegments(md); 101 end 102 103 end 104 105 disp(['Final mesh, number of elements: ' num2str(md.mesh.numberofelements)]); 106 107 %Now, build the connectivity tables for this mesh. 108 md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices); 109 md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity); 110 111 %recreate segments 112 md.mesh.segments=findsegments(md); 113 md.mesh.vertexonboundary=zeros(md.mesh.numberofvertices,1); md.mesh.vertexonboundary(md.mesh.segments(:,1:2))=1; 114 115 %Fill in rest of fields: 116 md.mesh.z=zeros(md.mesh.numberofvertices,1); 117 md.mesh.vertexonbed=ones(md.mesh.numberofvertices,1); 118 md.mesh.vertexonsurface=ones(md.mesh.numberofvertices,1); 119 md.mesh.elementonbed=ones(md.mesh.numberofelements,1); 120 md.mesh.elementonsurface=ones(md.mesh.numberofelements,1); 121 if ~strcmp(groundeddomain,'N/A'), 122 nodeground=ContourToMesh(md.mesh.elements,md.mesh.x,md.mesh.y,groundeddomain,'node',2); 123 md.mask.vertexonwater=ones(md.mesh.numberofvertices,1); 124 md.mask.vertexonwater(find(nodeground))=0; 125 else 126 md.mask.vertexonwater=zeros(md.mesh.numberofvertices,1); 127 end 128 if strcmpi(Names.interp,'node'), 129 md.inversion.vx_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0); 130 md.inversion.vy_obs=InterpFromGridToMesh(Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0); 131 else 132 md.inversion.vx_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vxname),md.mesh.x,md.mesh.y,0); 133 md.inversion.vy_obs=InterpFromMeshToMesh2d(Vel.(Names.indexname),Vel.(Names.xname),Vel.(Names.yname),Vel.(Names.vyname),md.mesh.x,md.mesh.y,0); 134 end 135 md.inversion.vel_obs=sqrt(md.inversion.vx_obs.^2+md.inversion.vy_obs.^2); 136 137 %deal with rifts 138 if md.rifts.numrifts, 139 %first, recreate rift segments 140 md=meshyamsrecreateriftsegments(md); 141 142 %using the segments, recreate the penaltypairs 143 for j=1:md.rifts.numrifts, 144 rift=md.rifts.riftstruct(j); 145 146 %build normals and lengths of segments: 147 lengths=sqrt((md.mesh.x(rift.segments(:,1))-md.mesh.x(rift.segments(:,2))).^2 + (md.mesh.y(rift.segments(:,1))-md.mesh.y(rift.segments(:,2))).^2 ); 148 normalsx=cos(atan2((md.mesh.x(rift.segments(:,1))-md.mesh.x(rift.segments(:,2))) , (md.mesh.y(rift.segments(:,2))-md.mesh.y(rift.segments(:,1))))); 149 normalsy=sin(atan2((md.mesh.x(rift.segments(:,1))-md.mesh.x(rift.segments(:,2))) , (md.mesh.y(rift.segments(:,2))-md.mesh.y(rift.segments(:,1))))); 150 151 %ok, build penaltypairs: 152 numpenaltypairs=length(rift.segments)/2-1; 153 rift.penaltypairs=zeros(numpenaltypairs,7); 154 155 for i=1:numpenaltypairs, 156 rift.penaltypairs(i,1)=rift.segments(i,2); 157 rift.penaltypairs(i,2)=rift.segments(end-i,2); 158 rift.penaltypairs(i,3)=rift.segments(i,3); 159 rift.penaltypairs(i,4)=rift.segments(end-i,3); 160 rift.penaltypairs(i,5)=normalsx(i)+normalsx(i+1); 161 rift.penaltypairs(i,6)=normalsy(i)+normalsy(i+1); 162 rift.penaltypairs(i,7)=(lengths(i)+lengths(i+1))/2; 163 end 164 %renormalize norms: 165 norms=sqrt(rift.penaltypairs(:,5).^2+rift.penaltypairs(:,6).^2); 166 rift.penaltypairs(:,5)=rift.penaltypairs(:,5)./norms; 167 rift.penaltypairs(:,6)=rift.penaltypairs(:,6)./norms; 168 169 md.rifts.riftstruct(j)=rift; 170 end 171 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/contrib/bamg/BamgCallFromMetric.m
1 function md=BamgCallFromMetric(md,metric,gradation), 2 %BAMGCALL - call bam 3 % 4 % call Bamg and the output mesh is plugged onto the model 5 % -gradation = maximum edge length gradation between 2 elements 6 % 7 % Usage: 8 % md=BamgCallFromMetric(md,metric,gradation); 9 % 10 % Example: 11 % md=BamgCall(md,metric,1500,10^8,1.3,0.9); 12 13 %2d geometric parameter (do not change) 14 scale=2/9; 15 16 %write files 17 t1=clock; fprintf('%s',' writing initial mesh files...'); 18 fid=fopen('carre0.met','w'); 19 fprintf(fid,'%i %i\n',md.mesh.numberofvertices,3); 20 fprintf(fid,'%i %i %i\n',metric'); 21 fclose(fid); 22 23 fid=fopen('carre0.mesh','w'); 24 25 %initialiation 26 fprintf(fid,'%s %i\n','MeshVersionFormatted',0); 27 28 %dimension 29 fprintf(fid,'\n%s\n%i\n','Dimension',2); 30 31 %Vertices 32 fprintf(fid,'\n%s\n%i\n\n','Vertices',md.mesh.numberofvertices); 33 fprintf(fid,'%8g %8g %i\n',[md.mesh.x md.mesh.y ones(md.mesh.numberofvertices,1)]'); 34 35 %Triangles 36 fprintf(fid,'\n\n%s\n%i\n\n','Triangles',md.mesh.numberofelements); 37 fprintf(fid,'%i %i %i %i\n',[md.mesh.elements ones(md.mesh.numberofelements,1)]'); 38 numberofelements1=md.mesh.numberofelements; 39 40 %close 41 fclose(fid); 42 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 43 44 %call bamg 45 fprintf('%s\n',' call Bamg...'); 46 system(['bamg -ratio ' num2str(gradation) ' -splitpbedge -nbv 1000000 -M carre0.met -b carre0.mesh -o carre1.mesh']); 47 48 %plug new mesh 49 t1=clock; fprintf('\n%s',' reading final mesh files...'); 50 A=meshread('carre1.mesh'); 51 md.mesh.x=A.x; 52 md.mesh.y=A.y; 53 md.z=zeros(A.nods,1); 54 md.mesh.elements=A.index; 55 md.mesh.numberofvertices=A.nods; 56 md.mesh.numberofelements=A.nels; 57 numberofelements2=md.mesh.numberofelements; 58 t2=clock;fprintf('%s\n\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 59 60 %display number of elements 61 fprintf('\n%s %i',' inital number of elements:',numberofelements1); 62 fprintf('\n%s %i\n\n',' new number of elements:',numberofelements2); 63 64 %clean up: 65 system('rm carre0.mesh carre0.met carre1.mesh carre1.mesh.gmsh'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/contrib/bamg/BamgCall.m
1 function md=BamgCall(md,field,hmin,hmax,gradation,epsilon), 2 %BAMGCALL - call bam 3 % 4 % build a metric using the Hessian of the given field 5 % call Bamg and the output mesh is plugged onto the model 6 % -hmin = minimum edge length (m) 7 % -hmax = maximum edge length (m) 8 % -gradation = maximum edge length gradation between 2 elements 9 % -epsilon = average error on each element (m/yr) 10 % 11 % Usage: 12 % md=BamgCall(md,field,hmin,hmax,gradation,epsilon); 13 % 14 % Example: 15 % md=BamgCall(md,md.inversion.vel_obs,1500,10^8,1.3,0.9); 16 17 %2d geometric parameter (do not change) 18 scale=2/9; 19 20 %Compute Hessian 21 t1=clock; fprintf('%s',' computing Hessian...'); 22 hessian=ComputeHessian(md.mesh.elements,md.mesh.x,md.mesh.y,field,'node'); 23 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 24 25 %Compute metric 26 t1=clock; fprintf('%s',' computing metric...'); 27 if length(md.nodeonwater)==md.mesh.numberofvertices, 28 pos=find(md.nodeonwater); 29 else 30 pos=[]; 31 end 32 metric=ComputeMetric(hessian,scale,epsilon,hmin,hmax,pos); 33 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 34 35 %write files 36 t1=clock; fprintf('%s',' writing initial mesh files...'); 37 fid=fopen('carre0.met','w'); 38 fprintf(fid,'%i %i\n',md.mesh.numberofvertices,3); 39 fprintf(fid,'%i %i %i\n',metric'); 40 fclose(fid); 41 42 fid=fopen('carre0.mesh','w'); 43 44 %initialiation 45 fprintf(fid,'%s %i\n','MeshVersionFormatted',0); 46 47 %dimension 48 fprintf(fid,'\n%s\n%i\n','Dimension',2); 49 50 %Vertices 51 fprintf(fid,'\n%s\n%i\n\n','Vertices',md.mesh.numberofvertices); 52 fprintf(fid,'%8g %8g %i\n',[md.mesh.x md.mesh.y ones(md.mesh.numberofvertices,1)]'); 53 54 %Triangles 55 fprintf(fid,'\n\n%s\n%i\n\n','Triangles',md.mesh.numberofelements); 56 fprintf(fid,'%i %i %i %i\n',[md.mesh.elements ones(md.mesh.numberofelements,1)]'); 57 numberofelements1=md.mesh.numberofelements; 58 59 %close 60 fclose(fid); 61 t2=clock;fprintf('%s\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 62 63 %call bamg 64 fprintf('%s\n',' call Bamg...'); 65 system(['bamg -ratio ' num2str(gradation) ' -splitpbedge -nbv 1000000 -M carre0.met -b carre0.mesh -o carre1.mesh']); 66 67 %plug new mesh 68 t1=clock; fprintf('\n%s',' reading final mesh files...'); 69 A=meshread('carre1.mesh'); 70 md.mesh.x=A.x; 71 md.mesh.y=A.y; 72 md.z=zeros(A.nods,1); 73 md.mesh.elements=A.index; 74 md.mesh.numberofvertices=A.nods; 75 md.mesh.numberofelements=A.nels; 76 numberofelements2=md.mesh.numberofelements; 77 t2=clock;fprintf('%s\n\n',[' done (' num2str(etime(t2,t1)) ' seconds)']); 78 79 %display number of elements 80 fprintf('\n%s %i',' inital number of elements:',numberofelements1); 81 fprintf('\n%s %i\n\n',' new number of elements:',numberofelements2); 82 83 %clean up: 84 system('rm carre0.mesh carre0.met carre1.mesh carre1.mesh.gmsh'); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/miscellaneous/create_region.m
1 function create_region(name)2 %CREATE_REGION - create region ????3 %4 % very temporary function.5 %6 % Usage:7 % create_region(name)8 9 eval(['mkdir ' name]);10 eval(['cd ' name ]);11 !mkdir Delivery Exp_Par Results12 cd Exp_Par13 !cp ../../RonneShelf/Exp_Par/* ./14 !rm -rf Hole*15 eval(['!mv Ronne.par ' name '.par']); -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/miscellaneous/findarg.m
1 function vals=findarg(arglist,field)2 %FINDARG - find argument associated to a field in a list3 %4 % This function parses through an argument list (typically varargin in a routine)5 % looking for a character array equal to field. Once this is found, we return the6 % next value in the varargin (if possible).7 % Because field might appear several times in the argument list, we return a structure8 % holding all these values.9 % Note that all comparisons to field value are case independent.10 %11 % Usage:12 % vals=findarg(arglist,field)13 %14 % Example:15 % routine foobar calls vals=findarg('Data',varargin)16 % with varargin='Data',1,'Data','foo','Plot','velocity','Arrow',417 % findarg would return the following structure: vals(1).value=1, vals(2).value='foo';18 19 %some argument checking:20 if ((nargin==0) | (nargout==0)),21 help findarg;22 error('findarg error message');23 end24 25 if ~ischar(field),26 error('findarg error message: field should be a string');27 end28 29 if ~iscell(arglist),30 error('findarg error message: argument list should be a cell array.');31 end32 33 %Recover data to plot34 founddata=0;35 36 for i=1:(length(arglist)-1), %data in arglist comes in pairs, hence the -1.37 if ischar(arglist{i}),38 if (strcmpi(arglist{i},field)),39 founddata=founddata+1;40 if founddata==1,41 vals.value=arglist{i+1};42 else43 vals(end+1).value=arglist{i+1};44 end45 end46 end47 end48 49 if founddata==0,50 vals=[];51 end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/miscellaneous/netcdf.m
1 function S = netcdf(File,varargin)2 % Function to read NetCDF files3 % S = netcdf(File)4 % Input Arguments5 % File = NetCDF file to read6 % Optional Input Arguments:7 % 'Var',Var - Read data for VarArray(Var), default [1:length(S.VarArray)]8 % 'Rec',Rec - Read data for Record(Rec), default [1:S.NumRecs]9 % Output Arguments:10 % S = Structure of NetCDF data organised as per NetCDF definition11 % Notes:12 % Only version 1, classic 32bit, NetCDF files are supported. By default13 % data are extracted into the S.VarArray().Data field for all variables.14 % To read the header only call S = netcdf(File,'Var',[]);15 %16 % SEE ALSO17 % ---------------------------------------------------------------------------18 S = [];19 20 try21 if exist(File,'file') fp = fopen(File,'r','b');22 else fp = []; error('File not found'); end23 if fp == -1 error('Unable to open file'); end24 25 % Read header26 Magic = fread(fp,4,'uint8=>char');27 if strcmp(Magic(1:3),'CDF') error('Not a NetCDF file'); end28 if uint8(Magic(4))~=1 error('Version not supported'); end29 S.NumRecs = fread(fp,1,'uint32=>uint32');30 S.DimArray = DimArray(fp);31 S.AttArray = AttArray(fp);32 S.VarArray = VarArray(fp);33 34 % Setup indexing to arrays and records35 Var = ones(1,length(S.VarArray));36 Rec = ones(1,S.NumRecs);37 for i = 1:2:length(varargin)38 if strcmp(upper(varargin{i}),'VAR') Var=Var*0; Var(varargin{i+1})=1;39 elseif strcmp(upper(varargin{i}),'REC') Rec=Rec*0; Rec(varargin{i+1})=1;40 else error('Optional input argument not recognised'); end41 end42 if sum(Var)==0 fclose(fp); return; end43 44 % Read non-record variables45 Dim = double(cat(2,S.DimArray.Dim));46 ID = double(cat(2,S.VarArray.Type));47 48 for i = 1:length(S.VarArray)49 D = Dim(S.VarArray(i).DimID+1); N = prod(D); RecID{i}=find(D==0);50 if isempty(RecID{i})51 if length(D)==0 D = [1,1]; N = 1; elseif length(D)==1 D=[D,1]; end52 if Var(i)53 S.VarArray(i).Data = ReOrder(fread(fp,N,[Type(ID(i)),'=>',Type(ID(i))]),D);54 fread(fp,(Pad(N,ID(i))-N)*Size(ID(i)),'uint8=>uint8');55 else fseek(fp,Pad(N,ID(i))*Size(ID(i)),'cof'); end56 else S.VarArray(i).Data = []; end57 end58 59 % Read record variables60 for k = 1:S.NumRecs61 for i = 1:length(S.VarArray)62 if ~isempty(RecID{i})63 D = Dim(S.VarArray(i).DimID+1); D(RecID{i}) = 1; N = prod(D);64 if length(D)==1 D=[D,1]; end65 if Var(i) & Rec(k)66 S.VarArray(i).Data = cat(RecID{i},S.VarArray(i).Data,...67 ReOrder(fread(fp,N,[Type(ID(i)),'=>',Type(ID(i))]),D));68 if N > 1 fread(fp,(Pad(N,ID(i))-N)*Size(ID(i)),'uint8=>uint8'); end69 else fseek(fp,Pad(N,ID(i))*Size(ID(i)),'cof'); end70 end71 end72 end73 74 fclose(fp);75 catch76 Err = lasterror; fprintf('%s\n',Err.message);77 if ~isempty(fp) && fp ~= -1 fclose(fp); end78 end79 80 % ---------------------------------------------------------------------------------------81 % Utility functions82 83 function S = Size(ID)84 % Size of NetCDF data type, ID, in bytes85 S = subsref([1,1,2,4,4,8],struct('type','()','subs',{{ID}}));86 87 function T = Type(ID)88 % Matlab string for CDF data type, ID89 T = subsref({'int8','char','int16','int32','single','double'},...90 struct('type','{}','subs',{{ID}}));91 92 function N = Pad(Num,ID)93 % Number of elements to read after padding to 4 bytes for type ID94 N = (double(Num) + mod(4-double(Num)*Size(ID),4)/Size(ID)).*(Num~=0);95 96 function S = String(fp)97 % Read a CDF string; Size,[String,[Padding]]98 S = fread(fp,Pad(fread(fp,1,'uint32=>uint32'),1),'uint8=>char').';99 100 function A = ReOrder(A,S)101 % Rearrange CDF array A to size S with matlab ordering102 A = permute(reshape(A,fliplr(S)),fliplr(1:length(S)));103 104 function S = DimArray(fp)105 % Read DimArray into structure106 if fread(fp,1,'uint32=>uint32') == 10 % NC_DIMENSION107 for i = 1:fread(fp,1,'uint32=>uint32')108 S(i).Str = String(fp);109 S(i).Dim = fread(fp,1,'uint32=>uint32');110 end111 else fread(fp,1,'uint32=>uint32'); S = []; end112 113 function S = AttArray(fp)114 % Read AttArray into structure115 if fread(fp,1,'uint32=>uint32') == 12 % NC_ATTRIBUTE116 for i = 1:fread(fp,1,'uint32=>uint32')117 S(i).Str = String(fp);118 ID = fread(fp,1,'uint32=>uint32');119 Num = fread(fp,1,'uint32=>uint32');120 S(i).Val = fread(fp,Pad(Num,ID),[Type(ID),'=>',Type(ID)]).';121 end122 else fread(fp,1,'uint32=>uint32'); S = []; end123 124 function S = VarArray(fp)125 % Read VarArray into structure126 if fread(fp,1,'uint32=>uint32') == 11 % NC_VARIABLE127 for i = 1:fread(fp,1,'uint32=>uint32')128 S(i).Str = String(fp);129 Num = double(fread(fp,1,'uint32=>uint32'));130 S(i).DimID = double(fread(fp,Num,'uint32=>uint32'));131 S(i).AttArray = AttArray(fp);132 S(i).Type = fread(fp,1,'uint32=>uint32');133 S(i).VSize = fread(fp,1,'uint32=>uint32');134 S(i).Begin = fread(fp,1,'uint32=>uint32'); % Classic 32 bit format only135 end136 else fread(fp,1,'uint32=>uint32'); S = []; end -
u/astrid-r1b/morlighe/issmuci/trunk-jpl/../trunk-jpl/src/m/miscellaneous/netcdf2struct.m
5 5 % S=netcdf2struct(File); 6 6 7 7 %Read netcdf file 8 data= netcdf(File);8 data=readnetcdf(File); 9 9 10 10 %initialize output 11 11 S=struct(); … … 25 25 fieldvalue=double(variables(i).Val); 26 26 S.(fieldname)=fieldvalue; 27 27 end 28 end 29 30 function S = readnetcdf(File,varargin) 31 % Function to read NetCDF files 32 % S = netcdf(File) 33 % Input Arguments 34 % File = NetCDF file to read 35 % Optional Input Arguments: 36 % 'Var',Var - Read data for VarArray(Var), default [1:length(S.VarArray)] 37 % 'Rec',Rec - Read data for Record(Rec), default [1:S.NumRecs] 38 % Output Arguments: 39 % S = Structure of NetCDF data organised as per NetCDF definition 40 % Notes: 41 % Only version 1, classic 32bit, NetCDF files are supported. By default 42 % data are extracted into the S.VarArray().Data field for all variables. 43 % To read the header only call S = netcdf(File,'Var',[]); 44 % 45 % SEE ALSO 46 % --------------------------------------------------------------------------- 47 S = []; 48 49 try 50 if exist(File,'file') fp = fopen(File,'r','b'); 51 else fp = []; error('File not found'); end 52 if fp == -1 error('Unable to open file'); end 53 54 % Read header 55 Magic = fread(fp,4,'uint8=>char'); 56 if strcmp(Magic(1:3),'CDF') error('Not a NetCDF file'); end 57 if uint8(Magic(4))~=1 error('Version not supported'); end 58 S.NumRecs = fread(fp,1,'uint32=>uint32'); 59 S.DimArray = DimArray(fp); 60 S.AttArray = AttArray(fp); 61 S.VarArray = VarArray(fp); 62 63 % Setup indexing to arrays and records 64 Var = ones(1,length(S.VarArray)); 65 Rec = ones(1,S.NumRecs); 66 for i = 1:2:length(varargin) 67 if strcmp(upper(varargin{i}),'VAR') Var=Var*0; Var(varargin{i+1})=1; 68 elseif strcmp(upper(varargin{i}),'REC') Rec=Rec*0; Rec(varargin{i+1})=1; 69 else error('Optional input argument not recognised'); end 70 end 71 if sum(Var)==0 fclose(fp); return; end 72 73 % Read non-record variables 74 Dim = double(cat(2,S.DimArray.Dim)); 75 ID = double(cat(2,S.VarArray.Type)); 76 77 for i = 1:length(S.VarArray) 78 D = Dim(S.VarArray(i).DimID+1); N = prod(D); RecID{i}=find(D==0); 79 if isempty(RecID{i}) 80 if length(D)==0 D = [1,1]; N = 1; elseif length(D)==1 D=[D,1]; end 81 if Var(i) 82 S.VarArray(i).Data = ReOrder(fread(fp,N,[Type(ID(i)),'=>',Type(ID(i))]),D); 83 fread(fp,(Pad(N,ID(i))-N)*Size(ID(i)),'uint8=>uint8'); 84 else fseek(fp,Pad(N,ID(i))*Size(ID(i)),'cof'); end 85 else S.VarArray(i).Data = []; end 86 end 87 88 % Read record variables 89 for k = 1:S.NumRecs 90 for i = 1:length(S.VarArray) 91 if ~isempty(RecID{i}) 92 D = Dim(S.VarArray(i).DimID+1); D(RecID{i}) = 1; N = prod(D); 93 if length(D)==1 D=[D,1]; end 94 if Var(i) & Rec(k) 95 S.VarArray(i).Data = cat(RecID{i},S.VarArray(i).Data,... 96 ReOrder(fread(fp,N,[Type(ID(i)),'=>',Type(ID(i))]),D)); 97 if N > 1 fread(fp,(Pad(N,ID(i))-N)*Size(ID(i)),'uint8=>uint8'); end 98 else fseek(fp,Pad(N,ID(i))*Size(ID(i)),'cof'); end 99 end 100 end 101 end 102 103 fclose(fp); 104 catch 105 Err = lasterror; fprintf('%s\n',Err.message); 106 if ~isempty(fp) && fp ~= -1 fclose(fp); end 107 end 108 109 % --------------------------------------------------------------------------------------- 110 % Utility functions 111 112 function S = Size(ID) 113 % Size of NetCDF data type, ID, in bytes 114 S = subsref([1,1,2,4,4,8],struct('type','()','subs',{{ID}})); 115 116 function T = Type(ID) 117 % Matlab string for CDF data type, ID 118 T = subsref({'int8','char','int16','int32','single','double'},... 119 struct('type','{}','subs',{{ID}})); 120 121 function N = Pad(Num,ID) 122 % Number of elements to read after padding to 4 bytes for type ID 123 N = (double(Num) + mod(4-double(Num)*Size(ID),4)/Size(ID)).*(Num~=0); 124 125 function S = String(fp) 126 % Read a CDF string; Size,[String,[Padding]] 127 S = fread(fp,Pad(fread(fp,1,'uint32=>uint32'),1),'uint8=>char').'; 128 129 function A = ReOrder(A,S) 130 % Rearrange CDF array A to size S with matlab ordering 131 A = permute(reshape(A,fliplr(S)),fliplr(1:length(S))); 132 133 function S = DimArray(fp) 134 % Read DimArray into structure 135 if fread(fp,1,'uint32=>uint32') == 10 % NC_DIMENSION 136 for i = 1:fread(fp,1,'uint32=>uint32') 137 S(i).Str = String(fp); 138 S(i).Dim = fread(fp,1,'uint32=>uint32'); 139 end 140 else fread(fp,1,'uint32=>uint32'); S = []; end 141 142 function S = AttArray(fp) 143 % Read AttArray into structure 144 if fread(fp,1,'uint32=>uint32') == 12 % NC_ATTRIBUTE 145 for i = 1:fread(fp,1,'uint32=>uint32') 146 S(i).Str = String(fp); 147 ID = fread(fp,1,'uint32=>uint32'); 148 Num = fread(fp,1,'uint32=>uint32'); 149 S(i).Val = fread(fp,Pad(Num,ID),[Type(ID),'=>',Type(ID)]).'; 150 end 151 else fread(fp,1,'uint32=>uint32'); S = []; end 152 153 function S = VarArray(fp) 154 % Read VarArray into structure 155 if fread(fp,1,'uint32=>uint32') == 11 % NC_VARIABLE 156 for i = 1:fread(fp,1,'uint32=>uint32') 157 S(i).Str = String(fp); 158 Num = double(fread(fp,1,'uint32=>uint32')); 159 S(i).DimID = double(fread(fp,Num,'uint32=>uint32')); 160 S(i).AttArray = AttArray(fp); 161 S(i).Type = fread(fp,1,'uint32=>uint32'); 162 S(i).VSize = fread(fp,1,'uint32=>uint32'); 163 S(i).Begin = fread(fp,1,'uint32=>uint32'); % Classic 32 bit format only 164 end 165 else fread(fp,1,'uint32=>uint32'); S = []; end 166 end
Note:
See TracBrowser
for help on using the repository browser.