/usr/share/ncarg/hluex/streamlineplot/st04c.c is in libncarg-data 6.3.0-6build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | /*
* $Id: st04c.c,v 1.8 2010-03-15 22:49:24 haley Exp $
*/
/***********************************************************************
* *
* Copyright (C) 1996 *
* University Corporation for Atmospheric Research *
* All Rights Reserved *
* *
***********************************************************************/
/*
* File: st04c.c
*
* Author: David Brown (converted by Mary Haley)
* National Center for Atmospheric Research
* PO 3000, Boulder, Colorado
*
* Date: Thu June 27 9:47:28 MST 1996
*
* Description: This example shows a StreamlinePlot of 500 mb wind
* vector data overlaid on a MapPlot. The streamlines
* are drawn over a VectorPlot of surface winds colored
* by surface pressure that in turn is drawn over a filled
* ContourPlot of surface temperature. Different intervals
* of the "temp1" colormap are used to color the contour
* levels and the vectors.
* The data represents 15 days of weather over North
* America in January, 1996.
* The data is extracted from NMC forcast data produced
* at 12 hour intervals and converted to netcdf format
* by Unidata. Most of the time steps in the files
* extracted from the original data are taken from the
* 0 and 6 hour forecast times. However, because some of
* the original files were lost, certain time steps come
* from longer range forcasts. Also, several steps had
* to be excluded from the frame set because the data is
* defective. The result is that there is an
* apparent discontinuity between some of the frames
* when the output is animated.
*/
#include <math.h>
#include <ncarg/gks.h>
#include <ncarg/ncargC.h>
#include <ncarg/hlu/hlu.h>
#include <ncarg/hlu/App.h>
#include <ncarg/hlu/NcgmWorkstation.h>
#include <ncarg/hlu/PSWorkstation.h>
#include <ncarg/hlu/PDFWorkstation.h>
#include <ncarg/hlu/CairoWorkstation.h>
#include <ncarg/hlu/StreamlinePlot.h>
#include <ncarg/hlu/VectorPlot.h>
#include <ncarg/hlu/ScalarField.h>
#include <ncarg/hlu/MapPlot.h>
#include <ncarg/hlu/ContourPlot.h>
#include <ncarg/hlu/TextItem.h>
#include <netcdf.h>
/*
* Depending on the value of the TIMESTEPS variable declared below,
* this example example can generate up to 61 frames from the 64
* timesteps in the data files. As shipped, only the first 20 frames
* are created. To see the complete plot uncomment the second
* assignment to TIMESTEPS. Some systems may not have enough physical
* memory to allow all frames to be viewed as an animation.
*
* #define TIMESTEPS 64
*/
#define TIMESTEPS 20
/*
* Initialize netCDF filenames
*/
char *cdffiles[6] = {"Ustorm.cdf","Vstorm.cdf","Pstorm.cdf","Tstorm.cdf","U500storm.cdf","V500storm.cdf"};
int main(int argc, char *argv[])
{
int i, j, d, h;
int appid, wid, cnid, vcid, stid, txid, amid, mpid, tmid, stdmid;
long stid_len;
int vfield, vfield2, sfield, sfield2;
int rlist;
ng_size_t len_dims[2];
long strt[1], cnt[1];
long latlen, lonlen;
long timelen;
int *timestep;
int ncid[6], uid, vid, u5id, v5id, pid, tid;
int latid, lonid;
float *lon, *lat;
float *X, *Y;
char filename[256];
char *rftime;
const char *dir = _NGGetNCARGEnv("data");
char hour[3], day[3], mainstring[17];
extern void get_2d_array(float *, long, long, int, int, long);
char const *wks_type = "x11";
/*
* Initialize the high level utility library
*/
NhlInitialize();
/*
* Create an application object.
*/
rlist = NhlRLCreate(NhlSETRL);
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNappUsrDir,"./");
NhlRLSetString(rlist,NhlNappDefaultParent,"True");
NhlCreate(&appid,"st04",NhlappClass,NhlDEFAULT_APP,rlist);
if (!strcmp(wks_type,"ncgm") || !strcmp(wks_type,"NCGM")) {
/*
* Create a meta file workstation.
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNwkMetaName,"./st04c.ncgm");
NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
NhlCreate(&wid,"st04Work",
NhlncgmWorkstationClass,NhlDEFAULT_APP,rlist);
}
else if (!strcmp(wks_type,"x11") || !strcmp(wks_type,"X11")) {
/*
* Create an X workstation.
*/
NhlRLClear(rlist);
NhlRLSetInteger(rlist,NhlNwkPause,True);
NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
NhlCreate(&wid,"st04Work",NhlcairoWindowWorkstationClass,appid,rlist);
}
else if (!strcmp(wks_type,"oldps") || !strcmp(wks_type,"OLDPS")) {
/*
* Create an older-style PostScript workstation.
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNwkPSFileName,"st04c.ps");
NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
NhlCreate(&wid,"st04Work",NhlpsWorkstationClass,appid,rlist);
}
else if (!strcmp(wks_type,"oldpdf") || !strcmp(wks_type,"OLDPDF")) {
/*
* Create an older-style PDF workstation.
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNwkPDFFileName,"st04c.pdf");
NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
NhlCreate(&wid,"st04Work",NhlpdfWorkstationClass,appid,rlist);
}
else if (!strcmp(wks_type,"pdf") || !strcmp(wks_type,"PDF") ||
!strcmp(wks_type,"ps") || !strcmp(wks_type,"PS")) {
/*
* Create a cairo PS/PDF workstation.
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNwkFileName,"st04c");
NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type);
NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
NhlCreate(&wid,"st04Work",NhlcairoDocumentWorkstationClass,appid,rlist);
}
else if (!strcmp(wks_type,"png") || !strcmp(wks_type,"PNG")) {
/*
* Create a cairo PNG workstation.
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNwkFileName,"st04c");
NhlRLSetString(rlist,NhlNwkFormat,(char*)wks_type);
NhlRLSetString(rlist,NhlNwkColorMap,"temp1");
NhlCreate(&wid,"st04Work",NhlcairoImageWorkstationClass,appid,rlist);
}
/*
* Open the netCDF files.
*/
for( i = 0; i <= 5; i++ ) {
sprintf( filename, "%s/cdf/%s", dir, cdffiles[i] );
ncid[i] = ncopen(filename,NC_NOWRITE);
}
/*
* Get the lat/lon dimensions (they happen to be the
* same for all files in this case)
*/
latid = ncdimid(ncid[0],"lat");
lonid = ncdimid(ncid[0],"lon");
ncdiminq(ncid[0],latid,(char *)0,&latlen);
ncdiminq(ncid[0],lonid,(char *)0,&lonlen);
len_dims[0] = latlen;
len_dims[1] = lonlen;
/*
* Get the variable ids
*/
uid = ncvarid(ncid[0],"u");
vid = ncvarid(ncid[1],"v");
pid = ncvarid(ncid[2],"p");
tid = ncvarid(ncid[3],"t");
u5id = ncvarid(ncid[4],"u");
v5id = ncvarid(ncid[5],"v");
latid = ncvarid(ncid[0],"lat");
lonid = ncvarid(ncid[0],"lon");
/*
* allocate space for arrays
*/
X = (float *)malloc(sizeof(float)*latlen*lonlen);
Y = (float *)malloc(sizeof(float)*latlen*lonlen);
lat = (float *)malloc(sizeof(float)*latlen);
lon = (float *)malloc(sizeof(float)*lonlen);
/*
* Get lat/lon values (they are the same for all files)
*/
strt[0] = 0;
cnt[0] = latlen;
ncvarget(ncid[0],latid,(long const *)strt,(long const *)cnt,lat);
cnt[0] = lonlen;
ncvarget(ncid[0],lonid,(long const *)strt,(long const *)cnt,lon);
/*
* Get U and V data values
*/
get_2d_array(X,latlen,lonlen,ncid[0],uid,0);
get_2d_array(Y,latlen,lonlen,ncid[1],vid,0);
/*
* Create a VectorField of the surface wind data
*/
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
NhlRLSetFloat(rlist,NhlNvfXCStartV,lon[0]);
NhlRLSetFloat(rlist,NhlNvfYCStartV,lat[0]);
NhlRLSetFloat(rlist,NhlNvfXCEndV,lon[lonlen-1]);
NhlRLSetFloat(rlist,NhlNvfYCEndV,lat[latlen-1]);
NhlRLSetFloat(rlist,NhlNvfMissingUValueV,-9999.0);
NhlCreate(&vfield,"VectorField",NhlvectorFieldClass,appid,rlist);
/*
* Create a VectorField of 500 millibar wind data
*
* Get U and V values
*/
get_2d_array(X,latlen,lonlen,ncid[4],u5id,0);
get_2d_array(Y,latlen,lonlen,ncid[5],v5id,0);
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
NhlRLSetFloat(rlist,NhlNvfXCStartV,lon[0]);
NhlRLSetFloat(rlist,NhlNvfYCStartV,lat[0]);
NhlRLSetFloat(rlist,NhlNvfXCEndV,lon[lonlen-1]);
NhlRLSetFloat(rlist,NhlNvfYCEndV,lat[latlen-1]);
NhlRLSetFloat(rlist,NhlNvfMissingUValueV,-9999.0);
NhlCreate(&vfield2,"VectorField",NhlvectorFieldClass,appid,rlist);
/*
* Create a ScalarField of surface pressure
*
* Get P data values
*/
get_2d_array(X,latlen,lonlen,ncid[2],pid,0);
for( i = 0; i < latlen*lonlen; i++ ) {
if( X[i] != -9999.0 ) {
X[i] /= 100.;
}
}
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
NhlRLSetFloat(rlist,NhlNsfXCStartV,lon[0]);
NhlRLSetFloat(rlist,NhlNsfYCStartV,lat[0]);
NhlRLSetFloat(rlist,NhlNsfXCEndV,lon[lonlen-1]);
NhlRLSetFloat(rlist,NhlNsfYCEndV,lat[latlen-1]);
NhlRLSetFloat(rlist,NhlNsfMissingValueV,-9999.0);
NhlCreate(&sfield,"ScalarField",NhlscalarFieldClass,appid,rlist);
/*
* Create a ScalarField of surface temperature
* (convert from Kelvin to Farenheit)
*
* Get T data values
*/
get_2d_array(X,latlen,lonlen,ncid[3],tid,0);
/*
* Convert to Fahrenheit
*/
for( i = 0; i < latlen*lonlen; i++ ) {
if( X[i] != -9999.0) {
X[i] = (X[i] - 273.15) * 9.0/5.0 + 32.0;
}
}
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
NhlRLSetFloat(rlist,NhlNsfXCStartV,lon[0]);
NhlRLSetFloat(rlist,NhlNsfYCStartV,lat[0]);
NhlRLSetFloat(rlist,NhlNsfXCEndV,lon[lonlen-1]);
NhlRLSetFloat(rlist,NhlNsfYCEndV,lat[latlen-1]);
NhlRLSetFloat(rlist,NhlNsfMissingValueV,-9999.0);
NhlCreate(&sfield2,"ScalarField2",NhlscalarFieldClass,appid,rlist);
/*
* Create a ContourPlot with surface temperature data
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNcnFillOn,"true");
NhlRLSetString(rlist,NhlNcnLinesOn,"false");
NhlRLSetString(rlist,NhlNcnFillDrawOrder,"predraw");
NhlRLSetInteger(rlist,NhlNcnScalarFieldData,sfield2);
NhlCreate(&cnid,"contourplot",NhlcontourPlotClass,wid,rlist);
/*
* Create a VectorPlot with the surface wind and pressure data
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNvcUseScalarArray,"true");
NhlRLSetInteger(rlist,NhlNvcVectorFieldData,vfield);
NhlRLSetInteger(rlist,NhlNvcScalarFieldData,sfield);
NhlCreate(&vcid,"vectorplot",NhlvectorPlotClass,wid,rlist);
/*
* Create a StreamlinePlot with 500 mb wind data
*/
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNpmTitleDisplayMode,"always");
NhlRLSetString(rlist,NhlNtiMainFuncCode,"~");
NhlRLSetInteger(rlist,NhlNstVectorFieldData,vfield2);
NhlCreate(&stid,"streamlineplot",NhlstreamlinePlotClass,wid,rlist);
/*
* Create an annotation used to explain the streamline data
*/
NhlCreate(&txid,"streamlineplotanno",NhltextItemClass,wid,0);
amid = NhlAddAnnotation(stid,txid);
/*
* Create a map object
*/
NhlRLClear(rlist);
/* NhlRLSetString(rlist,NhlNvpUseSegments,"true"); */
NhlCreate(&mpid,"mapplot",NhlmapPlotClass,wid,rlist);
/*
* Overlay everything on the MapPlot. The last object overlaid will
* appear on top
*/
NhlAddOverlay(mpid,cnid,-1);
NhlAddOverlay(mpid,vcid,-1);
NhlAddOverlay(mpid,stid,-1);
/*
* Variables for manipulating the title string
*/
tmid = ncdimid(ncid[1],"timestep");
ncdiminq(ncid[1],tmid,(char *)0,&timelen);
tmid = ncvarid(ncid[1],"timestep");
timestep = (int *)malloc(sizeof(int)*timelen);
strt[0] = 0;
cnt[0] = timelen;
ncvarget(ncid[1],tmid,(long const *)strt,(long const *)cnt,timestep);
sprintf( hour, "00");
sprintf( day, "05");
stdmid = ncdimid(ncid[1],"timelen");
ncdiminq(ncid[1], stdmid, (char *)0, &stid_len );
tmid = ncvarid(ncid[1],"reftime");
rftime = (char *)malloc((stid_len+1)*sizeof(char));
strt[0] = 0; cnt[0] = stid_len;
ncvarget(ncid[1],tmid,(long const *)strt,(long const *)cnt,rftime);
for( i = 0; i <= TIMESTEPS-1; i++ ) {
if (i != 17 && i != 36 && i != 37) {
/*
* Figure out the hour and day from the timestep, convert to strings
* and build the title string
*/
d = timestep[i] / 24 + 5;
h = timestep[i] % 24;
if (h > 9) {
sprintf( hour, "%d", h );
}
else {
sprintf( hour, "0%d", h );
}
if (d > 9) {
sprintf(day, "%d", d );
}
else {
sprintf(day, "0%d", d );
}
/*
* Set the new title string
*/
strcpy(mainstring, rftime);
sprintf(&mainstring[8], "%2s %2s:00", day, hour);
printf("%s\n",mainstring);
NhlRLClear(rlist);
NhlRLSetString(rlist,NhlNtiMainString,mainstring);
NhlSetValues(stid,rlist);
/*
* Modify the data objects with data for the current time step
*
* Get U and V values
*/
get_2d_array(X,latlen,lonlen,ncid[0],uid,i);
get_2d_array(Y,latlen,lonlen,ncid[1],vid,i);
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
NhlSetValues(vfield,rlist);
/*
* Get U and V values
*/
get_2d_array(X,latlen,lonlen,ncid[4],u5id,i);
get_2d_array(Y,latlen,lonlen,ncid[5],v5id,i);
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNvfUDataArray,X,2,len_dims);
NhlRLSetMDFloatArray(rlist,NhlNvfVDataArray,Y,2,len_dims);
NhlSetValues(vfield2,rlist);
/*
* Get P values
*/
get_2d_array(X,latlen,lonlen,ncid[2],pid,i);
for( j = 0; j < latlen*lonlen; j++ ) {
if( X[j] != -9999.0 ) {
X[j] /= 100.;
}
}
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
NhlSetValues(sfield,rlist);
/*
* Get T values
*/
get_2d_array(X,latlen,lonlen,ncid[3],tid,i);
/*
* Convert to Fahrenheit
*/
for( j = 0; j < latlen*lonlen; j++ ) {
if( X[j] != -9999.0) {
X[j] = (X[j] - 273.15) * 9.0/5.0 + 32.0;
}
}
NhlRLClear(rlist);
NhlRLSetMDFloatArray(rlist,NhlNsfDataArray,X,2,len_dims);
NhlSetValues(sfield2,rlist);
/*
* Draw the plot
*/
NhlDraw(mpid);
NhlFrame(wid);
}
}
/*
* Destroy the workstation object and exit.
*/
NhlDestroy(wid);
NhlClose();
exit(0);
}
/*
* function for reading in 3-d array from netCDF
* file and converting it to a 2-d array.
*/
void get_2d_array(
float *array,
long latlen,
long lonlen,
int fid,
int aid,
long timestep
)
{
long start[3], count[3];
start[0] = timestep;
start[1] = start[2] = 0;
count[0] = 1; count[1] = latlen; count[2] = lonlen;
ncvarget(fid,aid,(long const *)start,(long const *)count,array);
return;
}
|