0001 function write_vpv(theFilename, t, u, v)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 fclose('all');
0030
0031 if nargin < 1, help(mfilename), return, end
0032
0033 if isempty(theFilename), theFilename = '*'; end
0034
0035 if any(theFilename == '*')
0036 theSuggested = 'vpv.out';
0037 [theFile, thePath] = uiputfile(theSuggested, 'Save VSV Data');
0038 if ~any(theFile), return, end
0039 if thePath(end) ~= filesep, thePath = [thePath filesep]; end
0040 theFilename = [thePath theFile];
0041 end
0042
0043 if nargin == 3
0044 if size(u, 2) == 2
0045 v = u(:, 2);
0046 u = u(:, 1);
0047 else
0048 v = imag(u);
0049 u = real(u);
0050 end
0051 end
0052
0053 u(u == -999999) = nan;
0054 v(v == -999999) = nan;
0055
0056 u(isnan(u)) = 9999.99;
0057 v(isnan(v)) = 9999.99;
0058
0059 startDate = t(1);
0060 stopDate = t(end);
0061
0062 [mEnsembles, nBins] = size(u);
0063
0064 fmt1 = '%10.5f%10.5f%5i%5i%5i';
0065 fmt2 = ' %s\n';
0066 fmt3 = '%6i%10.2f%10.2f\n';
0067
0068 fp = fopen(theFilename, 'w');
0069 if fp < 0, close(f), return, end
0070
0071 fprintf(fp, '%s\n', theFilename);
0072
0073 yd0 = yearday(startDate);
0074 ydn = yearday(stopDate);
0075
0076 fprintf(fp, '%03i%4i%03i%4i\n', fix([yd0(2) yd0(1) ydn(2) ydn(1)]));
0077
0078 lat = [0 0 0];
0079 lon = [0 0 0];
0080
0081 fprintf(fp, '%2i%2i%2i%2i%2i%2i\n', [lat lon]);
0082
0083 dt = t(2) - t(1);
0084 binHeight = 1;
0085
0086 fprintf(fp, '%5i%10i%15.5f%10.5f\n', [1 nBins dt binHeight]);
0087
0088 d0 = rem(datevec(startDate), 100);
0089 dn = rem(datevec(stopDate), 100);
0090
0091 fprintf(fp, '%5i%5i%5i%10i%5i%5i\n', [d0(1:3) dn(1:3)]);
0092
0093 tic
0094 try
0095 for i = 1:mEnsembles
0096 remaining = mEnsembles-i+1;
0097 if i == 2 | (i > 1 & rem(remaining, 100) == 0)
0098 done = mEnsembles - remaining;
0099 est = (remaining * toc / done) / 86400;
0100 disp([' ## Remaining: ' int2str(remaining) ' records; ' ...
0101 dhms(est)]);
0102 end
0103 d = datevec(t(i));
0104 d(1) = rem(d(1), 100);
0105 hm = int2str(100*d(4)+round(d(5)*100/60));
0106 while length(hm) < 4, hm = ['0' hm]; end
0107 dt = t(i)-t(1);
0108 pressure = 0;
0109 ensemble_header = [dt pressure d(1:3)];
0110 fprintf(fp, fmt1, ensemble_header);
0111 fprintf(fp, fmt2, hm);
0112 for j = 1:nBins
0113 ensemble_data = [j u(i, j) v(i, j)];
0114 fprintf(fp, fmt3, ensemble_data);
0115 end
0116 end
0117 catch
0118 disp([' ## Error: ' lasterr])
0119 end
0120
0121 fclose(fp);
0122
0123
0124 function theResult = yearday(theYear, theDay)
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 if nargin < 1, theYear = 'demo'; end
0144
0145 if isequal(theYear, 'demo')
0146 help(mfilename)
0147 a = now;
0148 disp(datestr(a))
0149 b = yearday(yearday(a));
0150 disp(datestr(b))
0151 year_day_round_trip_error = b-a
0152 return
0153 end
0154
0155
0156
0157 if nargin == 2, theYear = [theYear theDay]; end
0158
0159 if length(theYear) == 2
0160 theDay = theYear(2);
0161 theYear = theYear(1);
0162 d = [theYear 1 1 0 0 0];
0163 for i = 1:6
0164 v{i} = d(i);
0165 end
0166 result = datenum(v{:}) + theDay - 1;
0167 if nargout > 0
0168 theResult = result;
0169 else
0170 disp(result)
0171 end
0172 return
0173 end
0174
0175
0176
0177 theDate = theYear;
0178
0179 if ischar(theDate)
0180 theDate = datenum(theDate);
0181 elseif length(theDate) > 1
0182 for i = 1:length(theDate)
0183 v{i} = theDate(i);
0184 end
0185 theDate = datenum(v{:});
0186 end
0187
0188 d = datevec(theDate);
0189 d(2:6) = [1 1 0 0 0];
0190 for i = 1:length(d)
0191 v{i} = d(i);
0192 end
0193 newYearsDay = datenum(v{:});
0194
0195 delta = (theDate-newYearsDay);
0196
0197 result = [d(1) (1+delta)];
0198
0199 if nargout > 0
0200 theResult = result;
0201 else
0202 disp(result)
0203 end
0204
0205
0206 function theResult = dhms(theArg)
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224 if nargin < 1, theArg = 'demo'; end
0225
0226 if isequal(theArg, 'demo')
0227 help(mfilename)
0228 format long
0229 theTimeString = '9d23h59m59s'
0230 theDecimalDays = dhms2d(theTimeString)
0231 theTimeString = d2dhms(theDecimalDays)
0232 format short
0233 return
0234 end
0235
0236 switch class(theArg)
0237 case 'double'
0238 result = d2dhms(theArg);
0239 case 'char'
0240 result = dhms2d(theArg);
0241 otherwise
0242 help(mfilename)
0243 warning([' ## Invalid argument type: ' class(theArg)])
0244 result = [];
0245 end
0246
0247 if nargout > 0
0248 theResult = result;
0249 else
0250 disp(result)
0251 end
0252
0253
0254 function theResult = d2dhms(theDecimalDays)
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271 if nargin < 1
0272 help(mfilename)
0273 theDecimalDays = 'demo';
0274 end
0275
0276 if isequal(theDecimalDays, 'demo')
0277 theDecimalDays = dhms2d('9d23h59m59s')
0278 theTimeString = d2dhms(theDecimalDays)
0279 return
0280 end
0281
0282 t = theDecimalDays;
0283
0284 d = fix(t);
0285 t = rem(t, 1) * 24;
0286 h = fix(t);
0287 t = rem(t, 1) * 60;
0288 m = fix(t);
0289 t = rem(t, 1) * 60;
0290 s = round(t);
0291
0292 result = '';
0293 if any(d), result = [result int2str(d) 'd']; end
0294 if any(h), result = [result int2str(h) 'h']; end
0295 if any(m), result = [result int2str(m) 'm']; end
0296 if any(s), result = [result int2str(s) 's']; end
0297
0298 if isempty(result), result = '0s'; end
0299
0300 if nargout > 0
0301 theResult = result;
0302 else
0303 disp(result)
0304 end
0305
0306
0307 function theResult = dhms2d(theTimeString)
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 if nargin < 1
0325 help(mfilename)
0326 theTimeString = 'demo';
0327 end
0328
0329 if isequal(theTimeString, 'demo')
0330 theTimeString = '9d23h59m60s'
0331 theDecimalDays = dhms2d(theTimeString)
0332 return
0333 end
0334
0335 t = lower(theTimeString);
0336
0337 flag = 0;
0338
0339 if any(t == 's')
0340 t = strrep(t, 's', '*1');
0341 flag = 1;
0342 end
0343
0344 if any(t == 'm')
0345 if flag
0346 t = strrep(t, 'm', '*60+');
0347 else
0348 t = strrep(t, 'm', '*60');
0349 end
0350 flag = 1;
0351 end
0352
0353 if any(t == 'h')
0354 if flag
0355 t = strrep(t, 'h', '*3600+');
0356 else
0357 t = strrep(t, 'h', '*3600');
0358 end
0359 flag = 1;
0360 end
0361
0362 if any(t == 'd')
0363 if flag
0364 t = strrep(t, 'd', '*86400+');
0365 else
0366 t = strrep(t, 'd', '*86400');
0367 end
0368 end
0369
0370 result = eval(t) / 86400;
0371
0372 if nargout > 0
0373 theResult = result;
0374 else
0375 disp(result)
0376 end