source: Roms_tools/Forecast_tools/download_GFS.m @ 1

Last change on this file since 1 was 1, checked in by cholod, 13 years ago

import Roms_Agrif

File size: 7.6 KB
Line 
1function download_GFS(today,lonmin,lonmax,latmin,latmax,FRCST_dir,Yorig,it)
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%
4%  download_GFS(today,lonmin,lonmax,latmin,latmax,FRCST_dir,Yorig)
5%
6%
7%  Extract a subgrid from GFS to get a ROMS forcing
8%  Store that into monthly files (to limit the problems
9%  of bandwith...).
10%  Take care of the Greenwitch Meridian.
11%  Transform variables in the ROMS format.
12%
13%
14%  Further Information: 
15%  http://www.brest.ird.fr/Roms_tools/
16
17%  This file is part of ROMSTOOLS
18%
19%  ROMSTOOLS is free software; you can redistribute it and/or modify
20%  it under the terms of the GNU General Public License as published
21%  by the Free Software Foundation; either version 2 of the License,
22%  or (at your option) any later version.
23%
24%  ROMSTOOLS is distributed in the hope that it will be useful, but
25%  WITHOUT ANY WARRANTY; without even the implied warranty of
26%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%  GNU General Public License for more details.
28%
29%  You should have received a copy of the GNU General Public License
30%  along with this program; if not, write to the Free Software
31%  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32%  MA  02111-1307  USA
33%
34%  Copyright (c) 2006 by Pierrick Penven
35%  e-mail:Pierrick.Penven@ird.fr 
36%
37%  Updated    9-Sep-2006 by Pierrick Penven
38%  Updated    20-Aug-2008 by Matthieu Caillaud & P. Marchesiello
39%
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41romstools_param
42
43%
44% Put the date in 'Yorig' time
45%
46rundate=datenum(today)-datenum(Yorig,1,1);
47%
48% GFS output name
49%
50gfs_name=[FRCST_dir,'GFS_',num2str(rundate),'.nc'];
51%
52% start
53%
54disp([' '])
55disp(['Get GFS data for ',datestr(today)])
56disp(['Minimum Longitude: ',num2str(lonmin)])
57disp(['Maximum Longitude: ',num2str(lonmax)])
58disp(['Minimum Latitude: ',num2str(latmin)])
59disp(['Maximum Latitude: ',num2str(latmax)])
60disp([' '])
61%
62% Create the directory
63%
64disp(['Making output data directory ',FRCST_dir])
65eval(['!mkdir ',FRCST_dir])
66%
67% Get the GFS file name (first check if there is an available forecast)
68%
69gfs_run_time0=0;
70gfs_date0=today-0.5;
71gfs_run_time=gfs_run_time0;
72gfs_date=gfs_date0;
73foundfile=0;
74while foundfile==0
75  fname=get_GFS_fname(gfs_date,gfs_run_time,1);
76  warning off
77  try
78    x=loaddap('-A -e +v ', fname);
79    foundfile=1;
80  catch
81    foundfile=0;
82  end
83  if foundfile==1 & ~isempty(x)
84    disp('  File found')
85  else
86    foundfile=0;
87    disp(['  GFS : did not found ',fname])
88    gfs_run_time=gfs_run_time-6;
89    if gfs_run_time<0
90      gfs_date=gfs_date-1;
91      gfs_run_time=18;
92      if gfs_date<gfs_date0-8;
93        error(['  GFS: did not found anything'])
94      end
95    end
96  end
97  warning on
98end
99%
100% Get the grid and the indices of the subgrid for GFS
101%
102gfs_run_time_GFS=gfs_run_time;
103gfs_date_GFS=gfs_date; 
104fname=get_GFS_fname(gfs_date_GFS,gfs_run_time_GFS,1);
105[i1min,i1max,i2min,i2max,i3min,i3max,jrange,lon,lat]=...
106         get_GFS_subgrid(fname,lonmin,lonmax,latmin,latmax);
107mask=getdap('',fname,'landsfc','[1:1]','',jrange,...
108             i1min,i1max,i2min,i2max,i3min,i3max);
109mask(mask==1)=NaN;
110mask(isfinite(mask))=1;
111%
112% Initialisation
113N=70;
114[M,L]=size(mask);
115tx=zeros(N,M,L);
116ty=tx;
117tair=tx;
118rhum=tx;
119prate=tx;
120wspd=tx;
121radlw=tx;
122radsw=tx;
123%
124n=0;
125gfstime=0*(1:N);
126LON=lon;
127LAT=lat;
128%
129%==================================================
130% Get the variables for hindcast
131%==================================================
132%
133% Get GDAS 1 deg grid
134%
135gfs_run_time=12;
136gfs_date=today-(hdays+1);
137fname=get_GFS_fname(gfs_date,gfs_run_time,0);
138[i1min,i1max,i2min,i2max,i3min,i3max,jrange,lon,lat]=...
139         get_GFS_subgrid(fname,lonmin,lonmax,latmin,latmax);
140mask=getdap('',fname,'landsfc','[1:1]','',jrange,...
141             i1min,i1max,i2min,i2max,i3min,i3max);
142mask(mask==1)=NaN;
143mask(isfinite(mask))=1;
144%
145% Loop on GDAS analyses
146% (starts hdays day ago 18Z, 1 analysis every 6h
147%  ends yesterday 06Z).
148%
149gfs_run_time0=12;
150gfs_date0=today-(hdays+1);
151for frcst=1:4*hdays-3             % number of files until yesterday 06Z
152  gfs_run_time0=gfs_run_time0+6;
153  if gfs_run_time0>18
154    gfs_date0=gfs_date0+1;
155    gfs_run_time0=0;
156  end
157%
158% 1.1: check if the GDAS is available.
159%
160  gfs_run_time=gfs_run_time0;
161  gfs_date=gfs_date0;
162  t1=1;
163  fname=get_GFS_fname(gfs_date,gfs_run_time,0);
164  warning off
165  try
166    x=loaddap('-A -e +v ', fname);
167    foundfile=1;
168  catch
169    foundfile=0;
170  end
171  if foundfile==1 & ~isempty(x)
172    disp('  File found')
173    gfs_run_time1=gfs_run_time0;  % Increment Forecast
174    gfs_date1=gfs_date0;          %           start time
175  else
176    foundfile=0;
177    disp(['  GDAS: file not found, try next file'])
178%    error(['  GDAS: file not found'])
179  end
180  warning on
181%
182% 1.2: read file
183%
184  if foundfile,
185   missvalue=x.ugrd10m.missing_value;
186%
187   n=n+1;
188   [gfstime(n),tx0,ty0,tair0,rhum0,...
189           prate0,wspd0,radlw0,radsw0]=...
190   get_GDAS(fname,mask,t1,jrange,i1min,i1max,i2min,i2max,...
191           i3min,i3max,missvalue);
192   TX=interp2(lon,lat',tx0,LON,LAT');
193   TY=interp2(lon,lat',ty0,LON,LAT');
194   TAIR=interp2(lon,lat',tair0,LON,LAT');
195   RHUM=interp2(lon,lat',rhum0,LON,LAT');
196   PRATE=interp2(lon,lat',prate0,LON,LAT');
197   WSPD=interp2(lon,lat',wspd0,LON,LAT');
198   RADLW=interp2(lon,lat',radlw0,LON,LAT');
199   RADSW=interp2(lon,lat',radsw0,LON,LAT');
200   tx(n,:,:)=TX;
201   ty(n,:,:)=TY;
202   tair(n,:,:)=TAIR;
203   rhum(n,:,:)=RHUM;
204   prate(n,:,:)=PRATE;
205   wspd(n,:,:)=WSPD;
206   radlw(n,:,:)=RADLW;
207   radsw(n,:,:)=RADSW;
208%
209  end
210end
211%
212%==================================================================
213% 2: Get the variables for Forecast starting yesterday 12Z
214%==================================================================
215gfs_run_time0=gfs_run_time1+6;
216gfs_date0=gfs_date1;
217  if gfs_run_time0>18
218    gfs_date0=gfs_date0+1;
219    gfs_run_time0=0;
220  end
221gfs_run_time=gfs_run_time0;
222gfs_date=gfs_date0;
223
224% Get the grid and the indices of the subgrid for GFS
225fname=get_GFS_fname(gfs_date,gfs_run_time,1);
226[i1min,i1max,i2min,i2max,i3min,i3max,jrange,lon,lat]=...
227         get_GFS_subgrid(fname,lonmin,lonmax,latmin,latmax);
228
229mask=getdap('',fname,'landsfc','[0:0]','',jrange,...
230             i1min,i1max,i2min,i2max,i3min,i3max);
231mask(mask==1)=NaN;
232mask(isfinite(mask))=1;
233[M,L]=size(mask);
234%
235% 2.1: check if the GFS forecast has been done for this time.
236% if not: take the previous one (but increment time index)
237%
238t1=1;
239foundfile=0;
240while foundfile==0
241  fname=get_GFS_fname(gfs_date,gfs_run_time,1);
242  warning off
243  try
244    x=loaddap('-A -e +v ', fname);
245    foundfile=1;
246  catch
247    foundfile=0;
248  end
249  if foundfile==1 & ~isempty(x)
250    disp('  File found')
251  else
252    foundfile=0;
253    disp(['  GFS : did not found ',fname])
254    t1=t1+it; % increment time index
255    gfs_run_time=gfs_run_time-6;
256    if gfs_run_time<0
257      gfs_date=gfs_date-1;
258      gfs_run_time=18;
259      if gfs_date<gfs_date0-8;
260        error(['  GFS: did not found anything'])
261      end
262    end
263  end
264  warning on
265end
266%
267% 2.2: read 60 time steps (dt = 3h)
268%
269for tndx=t1:it:60
270  n=n+1;
271  [gfstime(n),tx(n,:,:),ty(n,:,:),tair(n,:,:),rhum(n,:,:),...
272   prate(n,:,:),wspd(n,:,:),radlw(n,:,:),radsw(n,:,:)]=...
273   get_GFS(fname,mask,tndx,jrange,i1min,i1max,i2min,i2max,...
274           i3min,i3max,missvalue);
275end
276%
277% Reduce the matrices
278%
279gfstime=gfstime(1:n);
280tx=tx(1:n,:,:);
281ty=ty(1:n,:,:);
282tair=tair(1:n,:,:);
283rhum=rhum(1:n,:,:);
284prate=prate(1:n,:,:);
285wspd=wspd(1:n,:,:);
286radlw=radlw(1:n,:,:);
287radsw=radsw(1:n,:,:);
288%
289% Put the time in Yorig time
290%
291gfstime=gfstime-datenum(Yorig,1,1);
292%
293% Create the GFS output file and write everything down
294%
295mask(isnan(mask))=0;
296write_GFS(gfs_name,Yorig,lon,lat,mask,gfstime,tx,ty,tair,rhum,prate,wspd,radlw,radsw)
297%
298disp('Download GFS: done')
299%
300return
301
302
303
304
Note: See TracBrowser for help on using the repository browser.