/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_IO/struct2nctiles.m
ViewVC logotype

Contents of /MITgcm_contrib/gael/matlab_class/gcmfaces_IO/struct2nctiles.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Tue Jan 20 12:01:19 2015 UTC (10 years, 5 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint65x, checkpoint65r, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, HEAD
Error occurred while calculating annotation data.
- process2nctiles.m : minor fixes
- struct2nctiles.m : output data structure to nctiles
- prep2nctiles.m : two examples using struct2nctiles.m

1 function []=struct2nctiles(dirModel,fileOut,structIn,tileSize);
2 %process2nctiles(dirModel);
3 %object : convert MITgcm binary output to netcdf files (tiled)
4 %inputs : dirModel is the MITgcm run directory
5 % It is expected to contain binaries in
6 % 'diags/STATE/', 'diags/TRSP/', etc. as well
7 % as the 'available_diagnostics.log' text file.
8 % fileModel the file name base e.g. 'state_2d_set1'
9 % By default : all variables in e.g. 'state_2d_set1*'
10 % files will be processed, and writen individually to
11 % nctiles (tiled netcdf) that will be located in 'nctiles/'
12 % structIn is a structure containing descr, defs and vars
13 % (see prep2nctiles.m for an example)
14 % tileSize (optional) is e.g. [90 90] (by default tiles=faces)
15 %output : (netcdf files)
16
17 gcmfaces_global;
18
19 %directory names
20 filReadme=[dirModel 'README'];
21 dirOut=[dirModel 'tmp_nctiles/'];
22 if ~isdir(dirOut); mkdir(dirOut); end;
23
24 %set list of variables to process
25 listFlds={structIn(:).defs.fldName};
26 listDefs=listFlds;
27
28 %determine map of tile indices (by default tiles=faces)
29 if isempty(whos('tileSize'));
30 tileNo=mygrid.XC;
31 for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end;
32 else;
33 tileNo=gcmfaces_loc_tile(tileSize(1),tileSize(2));
34 end;
35
36 %get description of estimate from README
37 [rdm]=read_readme(filReadme);
38 disp(rdm');
39
40 %add structIn.descr to the beginning of rdm...
41 disp(structIn.descr');
42 descr=structIn.descr{1};
43 for ii=2:length(structIn.descr);
44 descr=[descr ' ' structIn.descr{ii}];
45 end;
46
47 %set output directory/file name
48 myFile=[dirOut fileOut];%first instance is for subdirectory name
49 if ~isdir(myFile); mkdir(myFile); end;
50 myFile=[myFile filesep fileOut];%second instance is for file name base
51
52 %create netcdf file using write2nctiles
53 doCreate=1;
54
55 %sort by number of dimensions
56 ndimDiag=NaN*ones(1,length(listFlds));
57 for ii=1:length(listFlds);
58 nameDiag=listFlds{ii};
59 myDiag=getfield(structIn.vars,nameDiag);
60 if isa(myDiag,'gcmfaces');
61 ndimDiag(ii)=length(size(myDiag{1}));
62 else;
63 ndimDiag(ii)=sum(size(myDiag)~=1);
64 end;
65 end;
66 [ndimDiag,ii]=sort(ndimDiag,'descend');
67 listFlds={listFlds{ii}};
68
69 %write first field
70 nameDiag=listFlds{1}; myDiag=getfield(structIn.vars,nameDiag); jj=find(strcmp(listDefs,nameDiag));
71 dimlist=write2nctiles(myFile,myDiag,doCreate,{'tileNo',tileNo},{'fldName',nameDiag},...
72 {'units',structIn.defs(jj).units},{'longName',structIn.defs(jj).longName},...
73 {'descr',descr},{'rdm',rdm});
74
75 %determine relevant dimensions
76 for ff=1:length(dimlist);
77 dim.threeD{ff}=dimlist{ff};
78 dim.twoD{ff}={dimlist{ff}{end-1:end}};
79 dim.oneD{ff}={dimlist{ff}{1}};
80 end;
81
82 %prepare to add fields
83 doCreate=0;
84
85 %now add the other fields
86 for ii=2:length(listFlds);
87 nameDiag=listFlds{ii};
88 myDiag=getfield(structIn.vars,nameDiag);
89 jj=find(strcmp(listDefs,nameDiag));
90 if ndimDiag(ii)==1; tmpDim=dim.oneD;
91 elseif ndimDiag(ii)==2; tmpDim=dim.twoD;
92 elseif ndimDiag(ii)==3; tmpDim=dim.threeD;
93 end;
94 write2nctiles(myFile,myDiag',doCreate,{'tileNo',tileNo},...
95 {'fldName',nameDiag},{'units',structIn.defs(jj).units},...
96 {'longName',structIn.defs(jj).longName},{'dimIn',tmpDim});
97 end;
98
99 function [meta]=read_meta(fileName);
100
101 %read meta file
102 tmp1=dir([fileName '*.meta']); tmp1=tmp1(1).name;
103 tmp2=strfind(fileName,filesep);
104 if ~isempty(tmp2); tmp2=tmp2(end); else; tmp2=0; end;
105 tmp1=[fileName(1:tmp2) tmp1]; fid=fopen(tmp1);
106 while 1;
107 tline = fgetl(fid);
108 if ~ischar(tline), break, end
109 if isempty(whos('tmp3')); tmp3=tline; else; tmp3=[tmp3 ' ' tline]; end;
110 end
111 fclose(fid);
112
113 %add meta variables to workspace
114 eval(tmp3);
115
116 %reformat to meta structure
117 meta.dataprec=dataprec;
118 meta.nDims=nDims;
119 meta.nFlds=nFlds;
120 meta.nrecords=nrecords;
121 meta.fldList=fldList;
122 meta.dimList=dimList;
123 if ~isempty(who('timeInterval')); meta.timeInterval=timeInterval; end;
124 if ~isempty(who('timeStepNumber')); meta.timeStepNumber=timeStepNumber; end;
125
126 %%
127
128 function [rdm]=read_readme(filReadme);
129
130 gcmfaces_global;
131
132 rdm=[];
133
134 fid=fopen(filReadme,'rt');
135 while ~feof(fid);
136 nn=length(rdm);
137 rdm{nn+1} = fgetl(fid);
138 end;
139 fclose(fid);
140
141 %%

  ViewVC Help
Powered by ViewVC 1.1.22