1 |
% |
---|
2 |
% READ_OBS_TRUNC read standard observation files. |
---|
3 |
% the standards are m2d,o2d,m3d,o3d |
---|
4 |
% |
---|
5 |
% Call as: [data,gridname,year,ncol,gmt]=read_obs(fname); |
---|
6 |
% or [data,gridname,year,ncol,gmt]=read_obs; |
---|
7 |
% |
---|
8 |
% Input: |
---|
9 |
% fname - path/name of .pth file |
---|
10 |
% Output: |
---|
11 |
% data - array of column data |
---|
12 |
% year - the year of the data; |
---|
13 |
% ncol - number of columns used; |
---|
14 |
% gmt - the gmt string from the data file (not yet) |
---|
15 |
% |
---|
16 |
% All the data is assumed to be real numbers (not integers). |
---|
17 |
% |
---|
18 |
% Written by: Charles G. Hannah Jan 1997. |
---|
19 |
% |
---|
20 |
function [data,gridname,year,ncol,gmt]=read_obs_trunc(fname) |
---|
21 |
|
---|
22 |
year = []; |
---|
23 |
gridname = []; |
---|
24 |
ncol = -1; |
---|
25 |
gmt = []; |
---|
26 |
|
---|
27 |
%Uses GUI interface is no parameter fame is not sent |
---|
28 |
if ~exist('fname') |
---|
29 |
[fname,fpath]=uigetfile('*.???','Which file?'); |
---|
30 |
if fname==0,return,end |
---|
31 |
else |
---|
32 |
fpath=[]; |
---|
33 |
end |
---|
34 |
|
---|
35 |
% open fname |
---|
36 |
[pfid,message]=fopen([fpath fname]); |
---|
37 |
if pfid==-1 |
---|
38 |
error([fpath fname,' not found. ',message]); |
---|
39 |
end |
---|
40 |
|
---|
41 |
% read until 'XXXX' delimiter |
---|
42 |
test=fscanf(pfid,'%s',1); |
---|
43 |
while ~strcmp(test,'XXXX') |
---|
44 |
[test,count]=fscanf(pfid,'%s',1); |
---|
45 |
% if test== [] |
---|
46 |
% if isempty[test] == 1 |
---|
47 |
if count == 0 |
---|
48 |
disp(['String XXXX not found in file ',fname]); |
---|
49 |
gridname=0; |
---|
50 |
return |
---|
51 |
end |
---|
52 |
end |
---|
53 |
|
---|
54 |
%clear the end of line left from above |
---|
55 |
fline=fgets(pfid); |
---|
56 |
|
---|
57 |
% read grid name from top of file |
---|
58 |
gridname=fgets(pfid); |
---|
59 |
gridname=blank(gridname); |
---|
60 |
%gridname |
---|
61 |
|
---|
62 |
% read header |
---|
63 |
header=fgets(pfid); |
---|
64 |
%header |
---|
65 |
|
---|
66 |
% read year |
---|
67 |
year=fscanf(pfid,'%d',1); |
---|
68 |
%year |
---|
69 |
|
---|
70 |
% read number of columns |
---|
71 |
ncol=fscanf(pfid,'%d',1); |
---|
72 |
%ncol |
---|
73 |
% read entire file |
---|
74 |
data=fscanf(pfid,'%f',[inf])'; |
---|
75 |
fclose(pfid); |
---|
76 |
% discard nonrectangular part |
---|
77 |
nrow=floor(length(data)/ncol); |
---|
78 |
data=data(1:nrow*ncol); |
---|
79 |
% create rectangular result |
---|
80 |
data=reshape(data,ncol,nrow); |
---|
81 |
data=data'; |
---|
82 |
return |
---|