Subversion Repositories lagranto.icon

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
% ------------------------------------------------------------------------
2
% Preparations
3
% ------------------------------------------------------------------------
4
 
5
% Add some matlab paths
6
addpath('/usr/local/matlabtools/mexnc/');
7
addpath('/usr/local/matlabtools/snctools/');
8
addpath('/usr/local/matlabtools/cdf_io/');
9
addpath('/home/sprenger/lagranto/matlab/');
10
 
11
% Set the datadirectories
12
cdfdir   = '/lhome/sprenger/lagranto/test/cdf/';
13
tradir   = '/home/sprenger/lagranto/matlab/';
14
 
15
% Read a trajectory file
16
tra = read_trajectory( [ tradir '/trajectory2.lsl' ]);
17
 
18
% Set the tabel for input files
19
filelist.n = 4;
20
filelist.date(1) = cellstr('19891020_00'); filelist.time(1) =  0;
21
filelist.date(2) = cellstr('19891020_06'); filelist.time(2) =  6;
22
filelist.date(3) = cellstr('19891020_12'); filelist.time(3) = 12;
23
filelist.date(4) = cellstr('19891020_18'); filelist.time(4) = 18; 
24
 
25
% Get the grid parameters from first data file
26
inp                     = ncget([ cdfdir 'P' char(filelist.date(1)) ],'U','PS');
27
grid.nx                 = size(inp.U.data,3);
28
grid.ny                 = size(inp.U.data,2);
29
grid.nz                 = size(inp.U.data,1);
30
grid.xmin               = inp.cstfile.lonmin;
31
grid.ymin               = inp.cstfile.latmin;
32
grid.dx                 = inp.cstfile.dellon;
33
grid.dy                 = inp.cstfile.dellat;
34
grid.xmax               = grid.xmin + double(grid.nx-1) * grid.dx;
35
grid.ymax               = grid.ymin + double(grid.ny-1) * grid.dy;
36
grid.mdv                = -999.
37
grid.lon1               = grid.xmin + double(0:grid.nx-1) * grid.dx;
38
grid.lat1               = grid.ymin + double(0:grid.ny-1) * grid.dy;
39
[ grid.lon2 grid.lat2 ] = meshgrid(grid.lon1,grid.lat1);
40
grid.aklay              = inp.cstfile.aklay;
41
grid.aklev              = inp.cstfile.aklev;
42
grid.bklay              = inp.cstfile.bklay;
43
grid.bklev              = inp.cstfile.bklev;
44
 
45
% ------------------------------------------------------------------------
46
% Loop over al trajectory times
47
% ------------------------------------------------------------------------
48
 
49
% Set flags for load manager
50
load0 = -1;
51
load1 = -1;
52
 
53
% No trajectories are selected
54
select = zeros(tra.ntra,1);
55
 
56
% Init the textlines
57
for j=1:tra.nfield
58
    textline(j) = cellstr(' ');
59
end
60
first = 1;
61
 
62
% Loop over all trajectory times
63
i     = 1;
64
stat  = 1;
65
while ( stat ~= 0 )
66
 
67
    % Get the time 
68
    time    = tra.time(i);
69
 
70
    % Decide which data files are needed
71
    need0 = find( time <= filelist.time , 1 );
72
    need1 = need0 + 1;
73
    if ( need1 > filelist.n ) 
74
       need1 = need0;
75
    end
76
 
77
    % Load files if not yet done
78
    if ( need0 == load1 )
79
        inp0  = inp1;
80
        load0 = need0;
81
    end
82
    if ( need1 == load0 )
83
        inp1  = inp0;
84
        load1 = need1;
85
    end
86
    if ( need0 ~= load0 ) 
87
        inps        = ncget([ cdfdir 'S' char(filelist.date(need0)) ],'TH','PV','RH');
88
        inpp        = ncget([ cdfdir 'P' char(filelist.date(need0)) ],'Q','U','V','T','PS');
89
        inp0        = inpp;
90
        inp0.TH     = inps.TH;
91
        inp0.PV     = inps.PV;
92
        inp0.RH     = inps.RH;
93
        inp0.P      = inp.PS;
94
        inp0.P.data = kron(inp0.PS.data,grid.bklay) + kron(ones(grid.ny,grid.nx),grid.aklay);
95
        inp0.P.data = reshape(inp0.P.data,grid.nz,grid.ny,grid.nx);
96
        load0       = need0;
97
    end
98
    if ( need1 ~= load1 ) 
99
        inps        = ncget([ cdfdir 'S' char(filelist.date(need1)) ],'TH','PV','RH');
100
        inpp        = ncget([ cdfdir 'P' char(filelist.date(need1)) ],'Q','U','V','T','PS');
101
        inp1        = inpp;
102
        inp1.TH     = inps.TH;
103
        inp1.PV     = inps.PV;
104
        inp1.RH     = inps.RH;
105
        inp1.P      = inp.PS;
106
        inp1.P.data = kron(inp1.PS.data,grid.bklay) + kron(ones(grid.ny,grid.nx),grid.aklay);
107
        inp1.P.data = reshape(inp1.P.data,grid.nz,grid.ny,grid.nx);
108
        load1       = need1;
109
    end
110
 
111
    % Get the mean pressure of the trajectory sample
112
    if ( any(select) )
113
        mask = ( find(select) - 1) * tra.ntime + i;
114
    else
115
        mask = ( (1:tra.ntra) -1 ) * tra.ntime + i;
116
    end
117
    hori.level = mean ( tra.p ( mask ) );
118
 
119
    % Interpolate fields to a pressure surface
120
    [ xpos  ypos zpos ] = meshgrid( grid.lon1, grid.lat1, hori.level );
121
    ind                 = get_index(inp0.P.data,[ zpos(:) ypos(:) xpos(:) ],grid);
122
    hori0.U             = reshape( int_index(inp0.U.data,ind,grid.mdv), 1,grid.ny,grid.nx);
123
    hori0.V             = reshape( int_index(inp0.V.data,ind,grid.mdv), 1,grid.ny,grid.nx);
124
    hori0.VEL           = sqrt(hori0.U .^ 2 + hori0.V .^ 2);
125
 
126
    [ xpos  ypos zpos ] = meshgrid( grid.lon1, grid.lat1, hori.level );
127
    ind                 = get_index(inp1.P.data,[ zpos(:) ypos(:) xpos(:) ],grid);
128
    hori1.U             = reshape( int_index(inp1.U.data,ind,grid.mdv), 1,grid.ny,grid.nx);
129
    hori1.V             = reshape( int_index(inp1.V.data,ind,grid.mdv), 1,grid.ny,grid.nx);
130
    hori1.VEL           = sqrt(hori1.U .^ 2 + hori1.V .^ 2);
131
 
132
    % Do a time interpolation (and get rid of 1-dimensions) 
133
    if ( need1 ~= need0 )
134
        frac     = ( time - filelist.time(need0) ) / ( filelist.time(need1) - filelist.time(need0) );
135
        hori.U   = squeeze( (1-frac) * hori0.U   + frac * hori1.U   );
136
        hori.V   = squeeze( (1-frac) * hori0.V   + frac * hori1.V   );
137
        hori.VEL = squeeze( (1-frac) * hori0.VEL + frac * hori1.VEL );
138
    else
139
        hori.U   = squeeze( hori0.U   );
140
        hori.V   = squeeze( hori0.V   );
141
        hori.VEL = squeeze( hori0.VEL );
142
    end
143
 
144
    % Open a new figure ans set the geographical projection
145
    figure(1);
146
    clf;
147
    load coast
148
    %h=axesm('MapProjection','stereo','origin',[ 90 0 ]);
149
    h=axesm( 'eqdcylin','MapLatLimit',[ 20 80 ], 'MapLonLimit', [ -100 40 ] );
150
    gridm;
151
    h=plotm(lat,long,'Color','k','LineWidth',1.5);
152
    %axis([-0.6 0.67 -1.2 -0.1]);
153
    axis([ -1.0    1.0    0.4000    1.4000 ]);
154
    daspect([ 1 0.75 1] );
155
    title ( [ 'P = ' num2str(round(hori.level)) ' hPa / T = ' num2str(time) ' h'],...
156
           'FontSize',22)
157
 
158
    % Plot velocity and overlay wind vectors
159
    c_map = col_scale([0:2.5:50 ],hori.VEL);
160
    [C,h] = contourfm(grid.lat1,grid.lon1,c_map.data,c_map.xtick);
161
    colormap('default');
162
    col_load( [ pwd '/col_table.txt' ],c_map.xtick,44); 
163
    caxis(c_map.caxis)
164
    q=colorbar('vert');
165
    set(q,'ytick',c_map.xtick,'YTickLabel',c_map.label,'FontSize',15); 
166
    [ c_map.x c_map.y ] = meshgrid(grid.lon1,grid.lat1);
167
    c_map.u             = squeeze(hori.U);
168
    c_map.v             = squeeze(hori.V);
169
    maskx               = 1:2:size(grid.lon2,2);        
170
    masky               = 1:2:size(grid.lon2,1);
171
    hold on
172
    q=quiverm(grid.lat2(masky,maskx),grid.lon2(masky,maskx),...
173
              hori.V   (masky,maskx),hori.U   (masky,maskx),'k');
174
    set(q,'Linewidth',1,'Color','k')
175
    hold off  
176
 
177
    % Handle user commands
178
    stat = 2;
179
    while ( stat == 2 )
180
 
181
        % Show the positions of the trajectory points
182
        hold on
183
        for j=1:tra.ntra
184
            if select(j) 
185
                mask = ( tra.time == time ) & ( tra.label == j );
186
                plotm(tra.lat(mask),tra.lon(mask),'o','markersize',10,'color','r',...
187
                      'MarkerEdgeColor','k','MarkerFaceColor','r');
188
            else
189
                mask = ( tra.time == time ) & ( tra.label == j );
190
                plotm(tra.lat(mask),tra.lon(mask),'o','markersize',10,'color','k',...
191
                      'MarkerEdgeColor','k','MarkerFaceColor','k');
192
            end
193
        end
194
        hold off
195
 
196
        % Get the cursor position
197
        [ x y ] = ginput(1)
198
        plotdom = axis;
199
 
200
       % Step backward
201
       if ( x < plotdom(1) ) 
202
           i = i - 1;
203
           if ( i < 1)
204
               i = tra.ntime;
205
           end
206
           stat = 1;
207
 
208
       % Step forward
209
       elseif ( x > plotdom(2) ) 
210
           i = i + 1;
211
           if ( i > tra.ntime )
212
               i = 1;
213
           end
214
           stat = 1;
215
 
216
       % Exit
217
       elseif ( y > plotdom(4) )
218
           stat = 0;
219
 
220
       % Write trajectory info
221
       elseif ( (y < plotdom(3)) & any(select) )
222
           for j=1:tra.nfield
223
              mask = ( find(select) - 1) * tra.ntime + i;
224
              val  = tra.(char(tra.field(j)));
225
              val  = mean( val(mask) );
226
              if ( first == 0 ) 
227
                 text(plotdom(1),plotdom(3)-0.04*j,char(textline(j)),...
228
                      'FontSize',15,'Color','g' );
229
              end
230
              textline(j) = cellstr([ char(tra.field(j)) ' = ' num2str(val)]);
231
              text(plotdom(1),plotdom(3)-0.04*j,char(textline(j)),...
232
                   'FontSize',15,'Color','b' );
233
              first = 0;
234
           end
235
 
236
       % Select trajectory points
237
       else
238
           [ lat0, lon0 ] = minvtran(x,y);
239
           mask           = (tra.time == time );
240
           lon1           = tra.lon(mask);
241
           lat1           = tra.lat(mask);
242
           dist           = distance(lat0,lon0,lat1,lon1);
243
           label          = find( min(dist) == dist );
244
           if  ( dist(label) < 5 ) 
245
              if ( select(label) == 0 ) 
246
                 select(label) = 1;
247
              elseif ( select(label) == 1 )
248
                 select(label) = 0;
249
              end
250
           else
251
               stat = 1;
252
           end
253
       end
254
 
255
    end
256
 
257
% End loop 
258
end
259