wrf-fire /wrfv2_fire/share/landread.c

Language C Lines 816
MD5 Hash 2306794e3fd676941d1d4a1e66e9fbea Estimated Cost $17,659 (why?)
Repository git://github.com/jbeezley/wrf-fire.git View Raw File View Project SPDX
  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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#ifndef CRAY
# ifdef NOUNDERSCORE
#      define GET_TERRAIN get_terrain
# else
#   ifdef F2CSTYLE
#      define GET_TERRAIN get_terrain__
#   else
#      define GET_TERRAIN get_terrain_
#   endif
# endif
#endif

#ifdef LANDREAD_STUB
#ifndef MS_SUA
# include <stdio.h>
#endif

int GET_TERRAIN (        float *adx,
                         float *xlat,
                         float *xlon,
                         float       *terrain,
                         int   *mix,
                         int   *mjx,
                         int   *iyyn,
                         int   *jxxn,
                         int   *ipath , int * ipathlen)  /* integer coded ASCII string from Funtran and len */

{
#ifndef MS_SUA
 fprintf(stderr, "***************************************************************\n" ) ;
 fprintf(stderr, "Access to RSMAS Topo Ingest Code is by Special Arrangement Only\n" ) ;
 fprintf(stderr, "in WRF 2.1 .  Please contact wrfhelp@ucar.edu .                \n" ) ;
 fprintf(stderr, "***************************************************************\n" ) ;
#endif
 return(0) ;
}

#else

#ifdef FSEEKO_OK
#  define _FILE_OFFSET_BITS 64
#endif
#ifndef MS_SUA
# include <stdio.h>
#endif
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <math.h>
#ifndef MACOS
# include <malloc.h>
#else
# include <malloc/malloc.h>
#endif
#include <string.h>
#include "landread.h"
#define MAXTOPOFILES  100
#define MAXLEN        4096


typedef struct
{
  /* Filenames. */
  char  fn[MAXTOPOFILES][MAXLEN];

  /* Grid spacings in km. */
  float dx[MAXTOPOFILES];

  /* Number of entries. */
  int num;
} TsFileInfo;

static float vmiss;

static int    numHeaderBytes;
static int    globalNx;
static int    globalNy;
static int    tileNx;
static int    tileNy;
static int    extraNx;
static int    extraNy;
static int    numTilesX;
static int    numTilesY;
static double dlat;
static double dlon;
static double lat0;
static double lon0;
static int    ntiles;
static int    wrapx;
static int    wrapy;

/* File information. */
static XDR  *xdrs;
static FILE *fp;

#if 0
 int nint(const double x)
{
  if ( x > 0.0 ) { return( (int)(x + 0.5) ) ; }
  return((int)(x - 0.5));
}
#endif

double aint(const double x)
{
  int ix = (int)(x);
  return((double)(ix));
}

double anint(const double x)
{
  if (x > 0.0) return((double)((int)(x + 0.5)));
  return((double)((int)(x - 0.5)));
}

static double normalizeAngle(double ang)
{
  for (;;)
    {
      if (ang >= 360.0)
        {
          ang -= 360.0;
        }
      else if (ang < 0.0)
        {
          ang += 360.0;
        }
      else
        {
          break;
        }
    }
  
  return(ang);
}

static double lonDistNowrap(double lon1, double lon2)
{
  double lon11 = normalizeAngle(lon1);
  double lon22 = normalizeAngle(lon2);
  if (lon22 < lon11) lon22 += 360.0;
  return(fabs(lon22 - lon11));
}

int tsLatLonToGridpoint(const double  lat,
			const double  lon,
			double       *ix,
			double       *iy)
{
  *ix = lonDistNowrap(lon0, lon) / dlon;
  *iy = (lat - lat0) / dlat;
  return(1);
}

static int areEqual(const double v1, const double v2)
{
  if (fabs(v1-v2) < 0.001) return(1);
  return(0);
}

static int setWrapAroundFlags(void)
{
  /* Compute the end gridpoint location in x. */
  double lon1  = lon0 + dlon*(globalNx);
  double lon2  = lon0 + dlon*(globalNx-1);
  double lat1  = lat0 + dlat*(globalNy);
  double lon0n = normalizeAngle(lon0);
  double lon1n = normalizeAngle(lon1);
  double lon2n = normalizeAngle(lon2);

  wrapx = 0;
  if (areEqual(lon0n, lon1n))
    {
      /* Here the first and last indices in x are one grid interval
         apart. */
      wrapx = 1;
    }
  else if (areEqual(lon0n, lon2n))
    {
      /* Here the first and last indices in x are coincident. */
      wrapx = 2;
    }

  wrapy = 0;
  if (areEqual(lat0, -90.0))
    {
      /* Here the first and last indices in x are one grid interval
         apart. */
      wrapy += 1;
    }
  if (areEqual(lat1, 90.0))
    {
      /* Here the first and last indices in x are coincident. */
      wrapy += 2;
    }

  return(1);
}

static int isMissing(const float v)
{
  if (fabs(vmiss - v) < 0.1) return(1);
  return(0);
}

float tsGetValueInt(const int aix, const int aiy)
{
  float f = vmiss;
  
  int iy = aiy;
  int ix = aix;

  /* Perform bounds checking. */
  if (iy < 0)
    {
      return(f);
    }
  else if (iy > globalNy - 1)
    {
      return(f);
    }

  if (aix < 0)
    {
      if (wrapx == 1)
	{
	  int n  = -(aix - (globalNx - 1)) / globalNx;
	  ix += n*globalNx;
	}
      else if (wrapx == 2)
	{
	  int nx = globalNx - 1;
	  int n  = -(aix - (nx - 1)) / nx;
	  ix += n*nx;
	}
      else
	{
	  return(f);
	}
    }

  if (ix > globalNx-1)
    {
      if (wrapx == 1)
	{
	  int n  = aix / globalNx;
	  ix -= n*globalNx;
	}
      else if (wrapx == 2)
	{
	  int nx = globalNx - 1;
	  int n  = aix / nx;
	  ix -= n*nx;
	}
      else
	{
	  return(f);
	}
    }

  int tx  = ix / tileNx;
  int ty  = iy / tileNy;
  int tn  = tx + ty*numTilesX;
  int txg = ix - tx*tileNx;
  int tyg = iy - ty*tileNy;
  int gn  = txg + tyg*tileNx;

  long long ll_gn = gn;
  long long ll_numHeaderBytes  = numHeaderBytes;
  long long ll_tileNx = tileNx;
  long long ll_tileNy = tileNy;

#ifdef FSEEKO64_OK 
  /* This is used on machines that support fseeko64. Tested for in ./configure script */
  long long loc = ll_numHeaderBytes + ll_tileNx*ll_tileNy*sizeof(float)*tn +
    ll_gn*sizeof(float);

  /* Seek to the proper location in the file and get the data value. */
  fseeko64(fp, loc, SEEK_SET);
#else
#  ifdef FSEEKO_OK
  /* This is used on machines that support _FILE_OFFSET_BITS=64 which makes
     off_t be 64 bits, and for which fseeko can handle 64 bit offsets.  This
     is tested in the ./configure script */
  off_t loc = ll_numHeaderBytes + ll_tileNx*ll_tileNy*sizeof(float)*tn +
    ll_gn*sizeof(float);

  fseeko(fp, loc, SEEK_SET);
#  else
  /* Note, this will not work correctly for very high resolution terrain input
     because the offset is only 32 bits.   */
  off_t loc = ll_numHeaderBytes + ll_tileNx*ll_tileNy*sizeof(float)*tn +
    ll_gn*sizeof(float);

  fseek(fp, loc, SEEK_SET);
#  endif
#endif
  xdr_float(xdrs, (float *) &f);

  return(f);
}

float tsGetValue(const double ix, const double iy)
{
  int i0 = (int)(floor(ix));
  int j0 = (int)(floor(iy));
  int i1 = (int)(ceil(ix));
  int j1 = (int)(ceil(iy));
  
  /* Interpolate linearly to (oiloc, ojloc). */
  float v0 = tsGetValueInt(i0,j0);
  float v1 = tsGetValueInt(i0,j1);
  float v2 = tsGetValueInt(i1,j0);
  float v3 = tsGetValueInt(i1,j1);
  
  if (isMissing(v0)) return(vmiss);
  if (isMissing(v1)) return(vmiss);
  if (isMissing(v2)) return(vmiss);
  if (isMissing(v3)) return(vmiss);

  double w0 = ix - i0;
  double w1 = iy - j0;

  float v4 = v2*w0 + v0*(1.0-w0);
  float v5 = v3*w0 + v1*(1.0-w0);
  float v6 = w1*v5 + (1.0-w1)*v4;
  float val = v6;

  return(val);
}

float tsGetValueLatLon(const double lat, const double lon)
{
  double ix, iy;
  tsLatLonToGridpoint(lat,lon,&ix,&iy);
  return(tsGetValue(ix,iy));
}

int tsCloseTileSet(void)
{
  if (xdrs)
    {
      xdr_destroy(xdrs);
      free(xdrs);
      xdrs = 0;
    }
  
  if (fp)
    {
      fclose(fp);
      fp = 0;
    }

  return(1);
}

int tsInitTileSet(const char *fn)
{
  vmiss = -100000000.00;

  xdrs = 0;
  fp   = 0;

  /* fp = (FILE *) fopen64(fn, "r"); */
  if (( fp = (FILE *) fopen(fn, "r")) == NULL ) {
#ifndef MS_SUA
    fprintf(stderr,"tsInitTileSet: cannot open %s\n",fn) ;
#endif
    exit(2) ;
  }
  xdrs = (XDR *) malloc(sizeof(XDR));
  xdrstdio_create(xdrs, fp, XDR_DECODE);

  numHeaderBytes = 5000;

  xdr_int(xdrs,    (int *)    &globalNx);
  xdr_int(xdrs,    (int *)    &globalNy);
  xdr_int(xdrs,    (int *)    &tileNx);
  xdr_int(xdrs,    (int *)    &tileNy);
  xdr_int(xdrs,    (int *)    &extraNx);
  xdr_int(xdrs,    (int *)    &extraNy);
  xdr_int(xdrs,    (int *)    &numTilesX);
  xdr_int(xdrs,    (int *)    &numTilesY);
  xdr_double(xdrs, (double *) &dlat);
  xdr_double(xdrs, (double *) &dlon);
  xdr_double(xdrs, (double *) &lat0);
  xdr_double(xdrs, (double *) &lon0);
  xdr_int(xdrs,    (int *)    &ntiles);

  setWrapAroundFlags();

  return(1);
}

int tsPrintTileSetInto(void)
{
  return(1);
}

#ifdef TERRAIN_AND_LANDUSE
int get_terrain_landuse_(const float &adx,
			 const float *xlat,
			 const float *xlon,
			 float       *terrain,
			 float       *landuse,
			 const int   &mix,
			 const int   &mjx,
			 const int   &iyyn,
			 const int   &jxxn)
#else

int GET_TERRAIN (        float *adx,
                         float *xlat,
                         float *xlon,
                         float       *terrain,
                         int   *mix,
                         int   *mjx,
                         int   *iyyn,
                         int   *jxxn, 
                         int   *ipath , int * ipathlen)  /* integer coded ASCII string from Funtran and len */
#endif
{
  TsFileInfo tsfTopo;
  TsFileInfo tsfOcean;
  TsFileInfo tsfLU;
  int i, j ;
  char path[1024] ;

  tsfTopo.num  = 0;
  tsfOcean.num = 0;
  tsfLU.num    = 0;

#if 0
  /* Read in the list of topography/land use filenames. */
  {
    FILE *fp = fopen("landFilenames", "r");

    for (;;)
      {
	char type[MAXLEN];
	char res[MAXLEN];
	char fn[MAXLEN];

	if (fscanf(fp, "%s %s %s", type, res, fn) == EOF) break;

	float dx;
	sscanf(res, "%f", &dx);

	if (strcmp(type, "landuse") == 0)
	  {
	    tsfLU.dx[tsfLU.num] = dx;
	    strcpy(tsfLU.fn[tsfLU.num], fn);
	    tsfLU.num++;
	  }
	else if (strcmp(type, "topography") == 0)
	  {
	    tsfTopo.dx[tsfTopo.num] = dx;
	    strcpy(tsfTopo.fn[tsfTopo.num], fn);
	    tsfTopo.num++;
	  }
	else if (strcmp(type, "bathymetry") == 0)
	  {
	    tsfOcean.dx[tsfOcean.num] = dx;
	    strcpy(tsfOcean.fn[tsfOcean.num], fn);
	    tsfOcean.num++;
	  }
      }
    fclose(fp);
  }
#else
  for (i = 0 ; i < *ipathlen ; i++ ) {
    path[i] = ipath[i] ;
  }
  path[*ipathlen] = '\0' ;

# if 0
  fprintf(stderr,"path: %s\n",path) ;
# endif
tsfTopo.num  = 0;
tsfTopo.dx[tsfTopo.num] =  1; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  1); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  2; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  2); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  3; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  3); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  4; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  4); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  5; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  5); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  6; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  6); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  7; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  7); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  8; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  8); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] =  9; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path,  9); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] = 10; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path, 10); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] = 20; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path, 20); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] = 30; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path, 30); tsfTopo.num++ ;
tsfTopo.dx[tsfTopo.num] = 40; sprintf(tsfTopo.fn[tsfTopo.num], "%s/topo.%02dkm.ts", path, 40); tsfTopo.num++ ;

# if 0
  for ( i = 0 ; i < tsfTopo.num ; i++ ) {
    fprintf(stderr,"%02d. %s\n",i, tsfTopo.fn[i] ) ;
  }
# endif
#endif


  /* First get the terrain from GTOPO30. */
  {
    /* Use the data with the largest spacing less than the grid
       spacing specified in the argument list. */
    float maxdx = 0.0;
    char fn[MAXLEN];
    int first = 1;
    for (i = 0; i < tsfTopo.num; i++)
      {
# if 0
fprintf(stderr,"%d %d file %f adx %f max %f\n",i,first,tsfTopo.dx[i],*adx , maxdx ) ;
# endif
	if (tsfTopo.dx[i] < maxdx) continue;
	if (first || tsfTopo.dx[i] < *adx)
	  {
	    first = 0;
	    maxdx = tsfTopo.dx[i];
	    strcpy(fn, tsfTopo.fn[i]);
	  }
      }

    if (!tsInitTileSet(fn))
      {
	return(0);
      }

    for ( j = 0; j < *jxxn; j++)
      {
	for ( i = 0; i < *iyyn; i++)
	  {
	    float lat = xlat[*mix*j + i];
	    float lon = xlon[*mix*j + i];
	    
	    double fix;
	    double fiy;
	    tsLatLonToGridpoint(lat,lon,&fix,&fiy);
	    float tv = tsGetValue(fix, fiy);
	    terrain[*mix*j + i] = tv;
	  }
      }

    tsCloseTileSet();
  }

#ifdef TERRAIN_AND_LANDUSE
  /* Next get the terrain from TBASE. */
  {
    /* Use the data with the largest spacing less than the grid
       spacing specified in the argument list. */
    float maxdx = 0.0;
    char fn[MAXLEN];
    int first = 1;
    for ( i = 0; i < tsfOcean.num; i++)
      {
	if (tsfOcean.dx[i] < maxdx) continue;
	if (first || tsfOcean.dx[i] < *adx)
	  {
	    first = 0;
	    maxdx = tsfOcean.dx[i];
	    strcpy(fn, tsfOcean.fn[i]);
	  }
      }

    if (!tsInitTileSet(fn))
      {
	return(0);
      }

    for ( j = 0; j < *jxxn; j++)
      {
	for ( i = 0; i < *iyyn; i++)
	  {
	    float lat = xlat[*mix*j + i];
	    float lon = xlon[*mix*j + i];
	    
	    double fix;
	    double fiy;
	    tsLatLonToGridpoint(lat,lon,fix,fiy);
	    float tv = tsGetValue(fix, fiy);
	    if (isMissing(terrain[*mix*j+i]))
	      {
		if (tv < 0.0) tv = 0.0;
		terrain[*mix*j + i] = tv;
	      }
	  }
      }
    tsCloseTileSet();
  }

  /* Next get the land use. */
  {
    /* Use the data with the largest spacing less than the grid
       spacing specified in the argument list. */
    float maxdx = 0.0;
    char fn[MAXLEN];
    int first = 1;
    for ( i = 0; i < tsfLU.num; i++)
      {
	if (tsfLU.dx[i] < maxdx) continue;
	if (first || tsfLU.dx[i] < *adx)
	  {
	    first = 0;
	    maxdx = tsfLU.dx[i];
	    strcpy(fn, tsfLU.fn[i]);
	  }
      }

    if (!tsInitTileSet(fn))
      {
	return(0);
      }

    for ( j = 0; j < *jxxn; j++)
      {
	for ( i = 0; i < *iyyn; i++)
	  {
	    float lat = xlat[*mix*j + i];
	    float lon = xlon[*mix*j + i];
	    
	    double fix;
	    double fiy;
	    tsLatLonToGridpoint(lat,lon,fix,fiy);
	    int ix = nint(fix);
	    int iy = nint(fiy);
	    float tv = tsGetValueInt(ix, iy);

            /* Set out-of-range values to water. */
            if (tv < 0.9 || tv > 24.1) tv = 16.0;

	    landuse[*mix*j + i] = tv;
	  }
      }
    tsCloseTileSet();
  }
#endif

  return(1);
}

#ifdef TERRAIN_AND_LANDUSE
int get_bathymetry_(const float &tadx,
		    const float *xlat,
		    const float *xlon,
		    float       *depth,
		    const int   &mix,
		    const int   &mjx,
		    const int   &iyyn,
		    const int   &jxxn,
		    const float &mindepth,
		    const float &zlimww3)
{
  /* Set grid resolution to .1 km to get highest resolution data possible. */
  float adx = 0.1;

  TsFileInfo tsfOcean;
  TsFileInfo tsfLU;

  tsfOcean.num = 0;
  tsfLU.num    = 0;

  /* Read in the list of topography/land use filenames. */
  {
    FILE *fp = fopen("landFilenames", "r");

    for (;;)
      {
	char type[MAXLEN];
	char res[MAXLEN];
	char fn[MAXLEN];

	if (fscanf(fp, "%s %s %s", type, res, fn) == EOF) break;

	float dx;
	sscanf(res, "%f", &dx);

	if (strcmp(type, "landuse") == 0)
	  {
	    tsfLU.dx[tsfLU.num] = dx;
	    strcpy(tsfLU.fn[tsfLU.num], fn);
	    tsfLU.num++;
	  }
	else if (strcmp(type, "bathymetry") == 0)
	  {
	    tsfOcean.dx[tsfOcean.num] = dx;
	    strcpy(tsfOcean.fn[tsfOcean.num], fn);
	    tsfOcean.num++;
	  }
      }

    fclose(fp);
  }

  /* Get the water depth from TBASE. */
  {
    /* Use the data with highest resolution possible. */
    float maxdx = 0.0;
    char fn[MAXLEN];
    int first = 1;
    for (int i = 0; i < tsfOcean.num; i++)
      {
	if (tsfOcean.dx[i] < maxdx) continue;
	if (first || tsfOcean.dx[i] < adx)
	  {
	    first = 0;
	    maxdx = tsfOcean.dx[i];
	    strcpy(fn, tsfOcean.fn[i]);
	  }
      }

    if (!tsInitTileSet(fn))
      {
	return(0);
      }

    for (int i = 0; i < mix*mjx; i++)
      {
	depth[i] = vmiss;
      }
    
    for (int j = 0; j < jxxn; j++)
      {
	for (int i = 0; i < iyyn; i++)
	  {
	    float lat = xlat[mix*j + i];
	    float lon = xlon[mix*j + i];
	    
	    double fix;
	    double fiy;
	    tsLatLonToGridpoint(lat,lon,fix,fiy);
	    float tv = tsGetValue(fix, fiy);
	    if (isMissing(depth[mix*j+i]))
	      {
		depth[mix*j + i] = -tv;
	      }
	  }
      }
    tsCloseTileSet();
  }

  /* Next get the land use. */
  {
    /* Use the data with the largest spacing less than the grid
       spacing specified in the argument list. */
    float maxdx = 0.0;
    char fn[MAXLEN];
    int first = 1;
    for (int i = 0; i < tsfLU.num; i++)
      {
	if (tsfLU.dx[i] < maxdx) continue;
	if (first || tsfLU.dx[i] < adx)
	  {
	    first = 0;
	    maxdx = tsfLU.dx[i];
	    strcpy(fn, tsfLU.fn[i]);
	  }
      }

    if (!tsInitTileSet(fn))
      {
	return(0);
      }

    for (int j = 0; j < jxxn; j++)
      {
	for (int i = 0; i < iyyn; i++)
	  {
	    float lat = xlat[mix*j + i];
	    float lon = xlon[mix*j + i];
	    
	    double fix;
	    double fiy;
	    tsLatLonToGridpoint(lat,lon,fix,fiy);
	    int ix = nint(fix);
	    int iy = nint(fiy);
	    float tv = tsGetValueInt(ix, iy);

            /* Set out-of-range values to water. */
            if (tv < 0.9 || tv > 24.1) tv = 16.0;

	    if (fabs(tv - 16.0) < 0.1)
	      {
		/* Water. */
		if (1)
		  {
		    if (depth[mix*j + i] < mindepth) depth[mix*j + i] = mindepth;
		  }
		else
		  {
		    if (depth[mix*j + i] < -zlimww3)
		      {
			/* Water depth below zlimww3, so turn this point 
			   into land. */
			depth[mix*j + i] = -0.1;		    
		      }
		    else if (depth[mix*j + i] < mindepth)
		      {
			depth[mix*j + i] = mindepth;
		      }
		  }
	      }
	    else
	      {
		/* Land. Set depth to 0.0. */
		depth[mix*j + i] = 0.0;
	      }
	  }
      }
    tsCloseTileSet();
  }

  return(1);
}
#endif

#endif 
Back to Top