/[MITgcm]/MITgcm_contrib/gmaze_pv/eg_write_UVbin2cdf_csgrid_subdomain.m
ViewVC logotype

Annotation of /MITgcm_contrib/gmaze_pv/eg_write_UVbin2cdf_csgrid_subdomain.m

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


Revision 1.1 - (hide annotations) (download)
Tue Jan 30 22:10:10 2007 UTC (17 years, 3 months ago) by gmaze
Branch: MAIN
Add eg for bin2cdf conversion for both lat-lon cs grid

1 gmaze 1.1 % Script to extract a subdomain from a CS510 simulation
2     % and write in netCDF format on a regular lat/lon grid (1/4)
3     % SPECIAL U V FIELDS !!
4     %
5     clear
6     global sla
7     pv_checkpath
8    
9    
10     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Global setup:
11     % Restrict global domain to:
12     subdomain = 3; % North Atlantic
13    
14     % Path to find input binary Cube sphere files:
15     pathi = './bin_cube49';
16    
17     % Path where the netcdf outputs will be stored:
18     patho = './ecco2_cube49_netcdf';
19     patho = strcat(patho,sla,'monthly');
20     %patho = strcat(patho,sla,'six_hourly');
21    
22     % Time step (for date conversion):
23     dt = 1200;
24    
25     % Variables to analyse (index into otab):
26     otab = cs510grid_outputs_table; % from the 1/8 latlon definition
27     wvar = [];
28     dimen = 3;
29     switch dimen
30     case 3 % 3D fields:
31     wvar = [wvar 39]; % UVEL
32     wvar = [wvar 47]; % VVEL
33     case 2 % 2D fields:
34     end %switch number of dimensions
35    
36     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Pre-process
37     % Get the grid:
38     path_grid = './grid';
39     XC = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'XC')),1,'float32');
40     YC = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'YC')),1,'float32');
41     XG = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'XG')),1,'float32');
42     YG = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'YG')),1,'float32');
43     dxG = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'DXG')),1,'float32');
44     dyG = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'DYG')),1,'float32');
45     RAC = readrec_cs510(sprintf('%s.data',strcat(path_grid,sla,'RAC')),1,'float32');
46     GRID_125;
47     ZC = - [0 cumsum(thk125(1:length(thk125)-1))];
48     clear dpt125 lat125 lon125 thk125
49    
50     % How to move to a lat/lon grid:
51     % CS510 is about 22km average resolution, ie: 1/4 degree
52     XI = -180 : 1/4 : 180;
53     YI = -90 : 1/4 : 90;
54     ZI = ZC;
55     if ~exist('CS510_to_LATLON025.mat','file')
56     del = cube2latlon_preprocess(XC,YC,XI,YI);
57     save('CS510_to_LATLON025.mat','XI','YI','XC','YC','del','-v6');
58     else
59     load('CS510_to_LATLON025.mat')
60     end
61    
62     % Set subrange - Longitude given as degrees east
63     % (exact values come from the 1/8 lat-lon grid)
64     switch subdomain
65     case 3
66     sub_name = 'north_atlantic';
67     lonmin = 276.0625;
68     lonmax = 359.9375;
69     latmin = 12.0975;
70     latmax = 53.2011;
71     depmin = 1; % !!! indices
72     depmax = 29; % !!! indices
73     if dimen == 3, depmax = 29;
74     else, depmax = 1;end
75     LIMITS = [lonmin lonmax latmin latmax depmin depmax]
76     if 0
77     m_proj('mercator','long',[270 365],'lat',[0 60]);
78     clf;hold on;m_coast;m_grid;
79     m_line(LIMITS([1 2 2 1 1]),LIMITS([3 3 4 4 3]),'color','k','linewidth',2);
80     title(sub_name);
81     end %if 1/0
82     end
83    
84     % Get subdomain horizontal axis:
85     xi = XI(max(find(XI<=LIMITS(1))):max(find(XI<=LIMITS(2))));
86     yi = YI(max(find(YI<=LIMITS(3))):max(find(YI<=LIMITS(4))));
87     zi = ZI(LIMITS(5):LIMITS(6));
88    
89     filU = otab{39,1}; ifield = 39;
90     filV = otab{47,1};
91    
92     % %%%%%%%%%%%%%%%%%%%%%%%%%%%%
93     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Get info over the time loop
94     % Get the file list:
95     fildU = filU; % Insert condition here for special files:
96     lU = dir(strcat(pathi,sla,fildU));
97     fildV = filV; % Insert condition here for special files:
98     lV = dir(strcat(pathi,sla,fildV));
99    
100     % Get the U files list:
101     it = 0;
102     clear ll
103     for il = 1 : size(lU,1)
104     if ~lU(il).isdir & ...
105     findstr(lU(il).name,strcat(filU,'.')) % is'it the file type we want ?
106     it = it + 1;
107     ll(it).name = lU(il).name;
108     end %if
109     end %for il
110     % Create the timetable of U:
111     for il = 1 : size(ll,2)
112     filin = ll(il).name;
113     % Now extract the stepnum from : %s.%10.10d.data
114     ic = findstr(filin,filU)+length(filU)+1; i = 1; clear stepnum
115     while filin(ic) ~= '.'
116     stepnum(i) = filin(ic); i = i + 1; ic = ic + 1;
117     end
118     ID = str2num(stepnum);
119     TIME(il,:) = datestr(datenum(1992,1,1)+ID*dt/60/60/24,'yyyymmddHHMM');
120     end
121     nt = size(TIME,1);
122    
123     % Then we check if we have V when we have U:
124     for it = 1 : nt
125     snapshot = TIME(it,:);
126     filUs = ll(it).name;
127     filVs = strcat(filV,filUs(findstr(filUs,filU)+length(filU):end));
128     if ~exist(strcat(pathi,sla,fildV,sla,filVs),'file')
129     TIME(it,:) = NaN;
130     end
131     end
132     itt = 0;
133     for it = 1 : nt
134     if find(isnan(TIME(it,:))==0)
135     itt = itt + 1;
136     TI(itt,:) = TIME(it,:);
137     end
138     end
139     TIME = TI; clear TI
140     nt = size(TIME,1);
141    
142    
143     % %%%%%%%%%%%%%%%
144     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Loop over time
145     for it = 1 : nt
146     snapshot = TIME(it,:);
147     ID = 60*60*24/dt*( datenum(snapshot,'yyyymmddHHMM') - datenum(1992,1,1) );
148     filin = ll(it).name;
149     disp('')
150     disp(strcat('Processing: ',num2str(ID),'//',snapshot))
151     dirout = strcat(patho,sla,TIME(it,:),sla);
152     filUout = sprintf('%s.%s.nc',filU,sub_name);
153     filVout = sprintf('%s.%s.nc',filV,sub_name);
154    
155     if ~exist(strcat(dirout,sla,filUout),'file') % File already exists ?
156    
157     %%%% READ FILES U AND V
158     switch otab{ifield,6}
159     case 4, flt = 'float32';
160     case 8, flt = 'float64';
161     end
162     t0 = clock;
163     for iC = 1 : 2
164     if iC == 1, fild = fildU; end
165     if iC == 2, fild = fildV; filin = strcat(filV,filin(findstr(filin,filU)+length(filU):end)); end
166     if findstr(filin,'.gz') % Gunzip file, special care !
167     disp('|----> Find a file with gz extension, work on uncompressed file ...')
168    
169     % 1: copy the filename with it path into gunzip_1_file.txt
170     fid1 = fopen('gunzip_1_file.txt','w');
171     fprintf(fid1,'%s',strcat(pathi,sla,fild,sla,filin));fclose(fid1);
172    
173     % 2: uncompress the file into a temporary folder:
174     disp('|------> uncompressing the file ...')
175     ! ./gunzip_1_file.bat
176     disp(strcat('|--------: ',num2str(etime(t0,clock))))
177    
178     % 3: Read the uncompress file:
179     disp('|--> reading it ...')
180     C = readrec_cs510(strcat('gunzip_1_file',sla,'tempo.data'),LIMITS(6),flt);
181     disp(strcat('|----: ',num2str(etime(t0,clock))))
182    
183     % 4: Suppress it
184     ! \rm ./gunzip_1_file/tempo.data
185    
186     else % Simply read the file:
187     disp('|--> reading it ...')
188     C = readrec_cs510(strcat(pathi,sla,fild,sla,filin),LIMITS(6),flt);
189     disp(strcat('|----: ',num2str(etime(t0,clock))))
190     end
191     if iC == 1, CU = C; end
192     if iC == 2, CV = C; end
193     end %for iC
194     clear C
195    
196     %%%% RESTRICT TO SUBDOMAIN
197     disp('|--> get subdomain ...')
198     % Restrict vertical to subdomain:
199     if LIMITS(5) ~= 1
200     disp('|----> vertical ...');
201     CU = CU(:,:,LIMITS(5):end);
202     CV = CV(:,:,LIMITS(5):end);
203     end
204     % Clean the field:
205     CU(find(CU==0)) = NaN;
206     CV(find(CV==0)) = NaN;
207     % Move the field into lat/lon grid:
208     disp('|----> Move to lat/lon grid ...');
209     [CU CV] = uvcube2latlon_fast3(del,CU,CV,XG,YG,RAC,dxG,dyG);
210    
211     % And then restrict horizontal to subdomain:
212     disp('|----> horizontal ...');
213     CU = CU(max(find(XI<=LIMITS(1))):max(find(XI<=LIMITS(2))),...
214     max(find(YI<=LIMITS(3))):max(find(YI<=LIMITS(4))),:);
215     CV = CV(max(find(XI<=LIMITS(1))):max(find(XI<=LIMITS(2))),...
216     max(find(YI<=LIMITS(3))):max(find(YI<=LIMITS(4))),:);
217    
218    
219     %%%% RECORD
220     disp('|--> record netcdf file ...')
221    
222     if 1 % Realy want to record ?
223    
224     if ~exist(dirout,'dir')
225     mkdir(dirout);
226     end
227    
228     for iC = 1 : 2
229     if iC==1, ifield=39; C = CU; fil = filU; filout = filUout; end
230     if iC==2, ifield=47; C = CV; fil = filV; filout = filVout; end
231     fid1 = fopen('inprogress.txt','w');
232     fprintf(fid1,'%s',strcat(dirout,sla,filout));fclose(fid1);
233    
234     nc = netcdf('inprogress.nc','clobber');
235    
236     nc('X') = length(xi);
237     nc('Y') = length(yi);
238     nc('Z') = length(zi);
239    
240     nc{'X'}='X';
241     nc{'Y'}='Y';
242     nc{'Z'}='Z';
243    
244     nc{'X'}.uniquename='X';
245     nc{'X'}.long_name='longitude';
246     nc{'X'}.gridtype=ncint(0);
247     nc{'X'}.units='degrees_east';
248     nc{'X'}(:) = xi;
249    
250     nc{'Y'}.uniquename='Y';
251     nc{'Y'}.long_name='latitude';
252     nc{'Y'}.gridtype=ncint(0);
253     nc{'Y'}.units='degrees_north';
254     nc{'Y'}(:) = yi;
255    
256     nc{'Z'}.uniquename='Z';
257     nc{'Z'}.long_name='depth';
258     nc{'Z'}.gridtype=ncint(0);
259     nc{'Z'}.units='m';
260     nc{'Z'}(:) = zi;
261    
262     ncid = fil;
263     nc{ncid}={'Z' 'Y' 'X'};
264     nc{ncid}.missing_value = ncdouble(NaN);
265     nc{ncid}.FillValue_ = ncdouble(0.0);
266     nc{ncid}(:,:,:) = permute(C,[3 2 1]);
267     nc{ncid}.units = otab{ifield,5};
268    
269     close(nc);
270     ! ./inprogress.bat
271    
272     end %if 1/0 want to record ?
273     disp(strcat('|--: ',num2str(etime(t0,clock))))
274    
275     else
276     disp(strcat('|--> Skip file (already done):',dirout,sla,filout))
277     end %if %file exist
278    
279    
280     end %for iC
281    
282     end %for it
283     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END Loop over time
284     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285    

  ViewVC Help
Powered by ViewVC 1.1.22