src/realizer_mod.f90
author Forrest Hoffman <forrest@climatemodeling.org>
Wed, 26 Sep 2007 17:50:53 -0400
changeset 0 5fda18b64dcb
permissions -rw-r--r--
Initial commit of the Fortran-90 version of the Fractal Realizer.
forrest@0
     1
module realizer_mod
forrest@0
     2
forrest@0
     3
! Realizer module written for the MidPtFM2d program (now called realizer) by
forrest@0
     4
! Forrest Hoffman on Fri May  4 16:56:34 EDT 2007 to eliminate duplicated
forrest@0
     5
! parameters and common blocks.  Parts of the code were converted to
forrest@0
     6
! Fortran-90 style and intrinsics.  Compiled and tested with gfortran
forrest@0
     7
! version 4.1.1 under Fedora Core 6 Linux.
forrest@0
     8
forrest@0
     9
   implicit none
forrest@0
    10
   save
forrest@0
    11
forrest@0
    12
   ! Parameters
forrest@0
    13
   integer, parameter :: r8 = selected_real_kind(12) ! 8 byte real
forrest@0
    14
   integer, parameter :: r4 = selected_real_kind( 6) ! 4 byte real
forrest@0
    15
   integer, parameter :: nr = kind(1.0)              ! native real
forrest@0
    16
   integer, parameter :: i8 = selected_int_kind(13)  ! 8 byte integer
forrest@0
    17
   integer, parameter :: i4 = selected_int_kind( 6)  ! 4 byte integer
forrest@0
    18
   integer, parameter :: ni = kind(1)                ! native integer
forrest@0
    19
   integer(i4), parameter :: maxN=515
forrest@0
    20
   integer(i4), parameter :: maxsp=10
forrest@0
    21
   real(r8),    parameter :: rad_per_deg=(3.14159265358979323846/180.0)
forrest@0
    22
   real(r8),    parameter :: deg_per_rad=(180.0/3.14159265358979323846)
forrest@0
    23
forrest@0
    24
   ! Global Variables
forrest@0
    25
   integer(i4) :: numcells
forrest@0
    26
   integer(i4) :: itiedsplit
forrest@0
    27
   integer(i4) :: irowcols
forrest@0
    28
   integer(i4) :: mtsplit
forrest@0
    29
   integer(i4) :: mtresolved
forrest@0
    30
   integer(i4) :: isupply
forrest@0
    31
   integer(i4) :: ihabs
forrest@0
    32
   integer(i4) :: maxgray
forrest@0
    33
   real(r4)    :: northeast
forrest@0
    34
   real(r4)    :: southeast
forrest@0
    35
   real(r4)    :: southwest
forrest@0
    36
   real(r4)    :: northwest
forrest@0
    37
forrest@0
    38
   ! Private Member Functions
forrest@0
    39
   public  dump_map
forrest@0
    40
   public  mpfm2d
forrest@0
    41
   private StackSort ! unused?
forrest@0
    42
   private sort      ! unused?
forrest@0
    43
   private SwapDown
forrest@0
    44
   public  Interchange
forrest@0
    45
   public  HeapSort
forrest@0
    46
   private SD2
forrest@0
    47
   private I2
forrest@0
    48
   public  HS2
forrest@0
    49
   private Gauss
forrest@0
    50
   private cosd
forrest@0
    51
   private sind
forrest@0
    52
   private atand
forrest@0
    53
   private atan2d
forrest@0
    54
forrest@0
    55
contains
forrest@0
    56
!
forrest@0
    57
!------------------------------------------------------------------------------
forrest@0
    58
!
forrest@0
    59
   subroutine dump_map(P)
forrest@0
    60
      implicit none
forrest@0
    61
      integer(i4), intent(in) :: P(maxN*maxN,0:maxsp+3)
forrest@0
    62
      ! local variables
forrest@0
    63
      integer(i4) :: i, j ! loop indices
forrest@0
    64
      integer(i4) :: temp(maxsp+3)
forrest@0
    65
      character (len=40) :: fmt
forrest@0
    66
!
forrest@0
    67
      write(fmt,'("(i5, 1x, ",I0,"(i5, 1x), 1x, 3(i5, 1x))")') ihabs+1
forrest@0
    68
      do i=1,numcells
forrest@0
    69
         do j = 0,ihabs+3
forrest@0
    70
           temp(j) = P(i,j)
forrest@0
    71
         end do
forrest@0
    72
         print fmt, i, (temp(j), j=0,ihabs+3)
forrest@0
    73
      end do
forrest@0
    74
!
forrest@0
    75
      return
forrest@0
    76
   end subroutine dump_map
forrest@0
    77
!
forrest@0
    78
!------------------------------------------------------------------------------
forrest@0
    79
!
forrest@0
    80
   real(r4) function f3(delta, x0, x1, x2, sigma)
forrest@0
    81
      implicit none
forrest@0
    82
      real(r4), intent(in) :: delta
forrest@0
    83
      real(r4), intent(in) :: x0, x1, x2
forrest@0
    84
      real(r4), intent(in) :: sigma
forrest@0
    85
forrest@0
    86
      f3 = (x0+x1+x2) / 3.0 + delta * Gauss(0.,sigma)
forrest@0
    87
forrest@0
    88
      return
forrest@0
    89
   end function f3
forrest@0
    90
!
forrest@0
    91
!------------------------------------------------------------------------------
forrest@0
    92
!
forrest@0
    93
   real(r4) function f4(delta, x0, x1, x2, x3, sigma)
forrest@0
    94
      implicit none
forrest@0
    95
      real(r4), intent(in) :: delta
forrest@0
    96
      real(r4), intent(in) :: x0, x1, x2, x3
forrest@0
    97
      real(r4), intent(in) :: sigma
forrest@0
    98
forrest@0
    99
      f4 = (x0+x1+x2+x3) / 4.0 + delta * Gauss(0.,sigma)
forrest@0
   100
forrest@0
   101
      return
forrest@0
   102
   end function f4
forrest@0
   103
!
forrest@0
   104
!------------------------------------------------------------------------------
forrest@0
   105
!
forrest@0
   106
   subroutine mpfm2d(X, maxlevel, sigma, H, addition, wrap, gradient, iz)
forrest@0
   107
      implicit none
forrest@0
   108
      real(r4), intent(inout) :: X(0:maxN,0:maxN)
forrest@0
   109
      integer(i4), intent(in) :: maxlevel
forrest@0
   110
      real(r4), intent(in)    :: sigma
forrest@0
   111
      real(r4), intent(in)    :: H(maxN,maxsp)
forrest@0
   112
      logical, intent(in)     :: addition
forrest@0
   113
      logical, intent(in)     :: wrap
forrest@0
   114
      logical, intent(in)     :: gradient
forrest@0
   115
      integer(i4), intent(in) :: iz
forrest@0
   116
      ! local variables
forrest@0
   117
      integer(i4) :: N
forrest@0
   118
      real(r4)    :: delta
forrest@0
   119
      integer(i4) :: DD
forrest@0
   120
      integer(i4) :: d
forrest@0
   121
      integer(i4) :: is, ix, iy ! loop indices
forrest@0
   122
!
forrest@0
   123
       print *, "Generating fractal"
forrest@0
   124
       N = 2**maxlevel
forrest@0
   125
       delta = sigma
forrest@0
   126
       X(0,0) = delta * Gauss(0.,sigma)
forrest@0
   127
       X(0,N) = delta * Gauss(0.,sigma)
forrest@0
   128
       X(N,0) = delta * Gauss(0.,sigma)
forrest@0
   129
       X(N,N) = delta * Gauss(0.,sigma)
forrest@0
   130
!
forrest@0
   131
       if (gradient) then
forrest@0
   132
         X(0,0) = southwest
forrest@0
   133
         X(0,N) = southeast
forrest@0
   134
         X(N,0) = northwest
forrest@0
   135
         X(N,N) = northeast
forrest@0
   136
       end if
forrest@0
   137
!
forrest@0
   138
       DD = N
forrest@0
   139
       d = N / 2
forrest@0
   140
!
forrest@0
   141
       do is = 1, maxlevel
forrest@0
   142
!  going from grid type I to type II
forrest@0
   143
         delta = delta * 0.5**(0.5 * H(is,iz))
forrest@0
   144
         do ix = d, N-d, DD
forrest@0
   145
           do iy = d, N-d, DD
forrest@0
   146
             X(ix,iy) = f4(delta, X(ix+d,iy+d), X(ix+d,iy-d), &
forrest@0
   147
                           X(ix-d,iy+d), X(ix-d,iy-d), sigma)
forrest@0
   148
           end do
forrest@0
   149
         end do
forrest@0
   150
!  displace other points also if needed
forrest@0
   151
         if (addition) then
forrest@0
   152
           do ix = 0, N, DD
forrest@0
   153
             do iy = 0, N, DD
forrest@0
   154
               X(ix,iy) = X(ix,iy) + delta * Gauss(0.,sigma)
forrest@0
   155
             end do
forrest@0
   156
           end do
forrest@0
   157
         end if
forrest@0
   158
!  going from grid type II to type I
forrest@0
   159
         delta = delta * 0.5**(0.5*H(is,iz))
forrest@0
   160
!  interpolate and offset boundary grid points
forrest@0
   161
         do ix = d, N-d, DD
forrest@0
   162
           X(ix,0) = f3(delta, X(ix+d,0), X(ix-d,0), X(ix,d), sigma)
forrest@0
   163
           X(ix,N) = f3(delta, X(ix+d,N), X(ix-d,N), X(ix,N-d), sigma)
forrest@0
   164
           X(0,ix) = f3(delta, X(0,ix+d), X(0,ix-d), X(d,ix), sigma)
forrest@0
   165
           X(N,ix) = f3(delta, X(N,ix+d), X(N,ix-d), X(N-d,ix), sigma)
forrest@0
   166
           if (wrap) then
forrest@0
   167
             X(ix,N) = X(ix,0)
forrest@0
   168
             X(N,ix) = X(0,ix)
forrest@0
   169
           end if
forrest@0
   170
         end do
forrest@0
   171
!  interpolate and offset inter grid points
forrest@0
   172
         do ix = d, N-d, DD
forrest@0
   173
           do iy = DD, N-d, DD
forrest@0
   174
              X(ix,iy) = f4(delta, X(ix,iy+d), X(ix,iy-d), &
forrest@0
   175
                            X(ix+d,iy), X(ix-d,iy), sigma)
forrest@0
   176
           end do
forrest@0
   177
         end do
forrest@0
   178
         do ix = DD, N-d, DD
forrest@0
   179
           do iy = d, N-d, DD
forrest@0
   180
             X(ix,iy) = f4(delta, X(ix,iy+d), X(ix,iy-d), &
forrest@0
   181
                           X(ix+d,iy), X(ix-d,iy), sigma)
forrest@0
   182
           end do
forrest@0
   183
         end do
forrest@0
   184
!  displace other points also if needed
forrest@0
   185
         if (addition) then
forrest@0
   186
           do ix = 0, N, DD
forrest@0
   187
             do iy = 0, N, DD
forrest@0
   188
               X(ix,iy) = X(ix,iy) + delta * Gauss(0.,sigma)
forrest@0
   189
             end do
forrest@0
   190
           end do
forrest@0
   191
           do ix = d, N-d, DD
forrest@0
   192
             do iy = d, N-d, DD
forrest@0
   193
               X(ix,iy) = X(ix,iy) + delta * Gauss(0.,sigma)
forrest@0
   194
             end do
forrest@0
   195
           end do
forrest@0
   196
         end if
forrest@0
   197
!
forrest@0
   198
         DD = DD / 2
forrest@0
   199
         d = d / 2
forrest@0
   200
!
forrest@0
   201
      end do ! for all levels
forrest@0
   202
      return
forrest@0
   203
   end subroutine mpfm2d
forrest@0
   204
!
forrest@0
   205
!------------------------------------------------------------------------------
forrest@0
   206
!
forrest@0
   207
   real(r4) function Gauss(xm, xs)
forrest@0
   208
      implicit none
forrest@0
   209
      real(r4), intent(in) :: xm
forrest@0
   210
      real(r4), intent(in) :: xs
forrest@0
   211
      ! local variables
forrest@0
   212
      real(r4)    :: total
forrest@0
   213
      real(r4)    :: p
forrest@0
   214
      integer(i4) :: i
forrest@0
   215
!
forrest@0
   216
! create a normal number by summing 12 uniform random numbers
forrest@0
   217
      total = 0.0
forrest@0
   218
      do i = 1,12
forrest@0
   219
          call random_number(p)
forrest@0
   220
          total = total + p
forrest@0
   221
      end do
forrest@0
   222
      Gauss = (total - 6.0) * xs + xm
forrest@0
   223
!
forrest@0
   224
      return
forrest@0
   225
   end function Gauss
forrest@0
   226
!
forrest@0
   227
!------------------------------------------------------------------------------
forrest@0
   228
!
forrest@0
   229
   subroutine StackSort(n,arr)
forrest@0
   230
      implicit none
forrest@0
   231
      integer(i4), intent(in) :: n
forrest@0
   232
      real(r4), intent(inout) :: arr(n)
forrest@0
   233
      ! local variables
forrest@0
   234
      integer(i4) :: i, j ! loop indices
forrest@0
   235
      real(r4)    :: a
forrest@0
   236
!
forrest@0
   237
!  pick out each element in turn
forrest@0
   238
      do j = 2,n
forrest@0
   239
         a = arr(j)
forrest@0
   240
!  look for the place to insert it
forrest@0
   241
         do i = j-1,1,-1
forrest@0
   242
           if (arr(i) <= a) go to 10
forrest@0
   243
           arr(i+1) = arr(i)
forrest@0
   244
         end do
forrest@0
   245
         i = 0
forrest@0
   246
!  insert the number here
forrest@0
   247
10       arr(i+1) = a
forrest@0
   248
      end do
forrest@0
   249
      return
forrest@0
   250
   end subroutine StackSort
forrest@0
   251
!
forrest@0
   252
!------------------------------------------------------------------------------
forrest@0
   253
!
forrest@0
   254
!  sort routine from Numerical Recipes
forrest@0
   255
   subroutine sort (n, ra)
forrest@0
   256
      implicit none
forrest@0
   257
      integer(i4), intent(in) :: n
forrest@0
   258
      real(r4), intent(inout) :: ra(n)
forrest@0
   259
      ! local variables
forrest@0
   260
      integer(i4) :: i, j, l
forrest@0
   261
      integer(i4) :: ir
forrest@0
   262
      real(r4)    :: rra
forrest@0
   263
forrest@0
   264
       l = n/2 + 1
forrest@0
   265
       ir = n
forrest@0
   266
!
forrest@0
   267
10     continue
forrest@0
   268
          if (l .gt. 1) then
forrest@0
   269
             l = l-1
forrest@0
   270
             rra = ra(l)
forrest@0
   271
          else
forrest@0
   272
             rra = ra(ir)
forrest@0
   273
             ra(ir) = ra(1)
forrest@0
   274
             ir = ir-1
forrest@0
   275
             if (ir .eq. 1) then
forrest@0
   276
                ra(1) = rra
forrest@0
   277
                return
forrest@0
   278
             end if
forrest@0
   279
          end if
forrest@0
   280
          i = l
forrest@0
   281
          j = l+l
forrest@0
   282
!
forrest@0
   283
20     if (j .le. ir) then
forrest@0
   284
           if (j .lt. ir) then
forrest@0
   285
              if (ra(j) .lt. ra(j+1)) j = j+1
forrest@0
   286
           end if
forrest@0
   287
           if (rra .lt. ra(j)) then
forrest@0
   288
               ra(i) = ra(j)
forrest@0
   289
               i = j
forrest@0
   290
               j = j+j
forrest@0
   291
           else
forrest@0
   292
               j = ir+1
forrest@0
   293
           end if
forrest@0
   294
           goto 20
forrest@0
   295
       end if
forrest@0
   296
!
forrest@0
   297
       ra(i) = rra
forrest@0
   298
       goto 10
forrest@0
   299
!
forrest@0
   300
   end subroutine sort
forrest@0
   301
!
forrest@0
   302
!------------------------------------------------------------------------------
forrest@0
   303
!
forrest@0
   304
   subroutine SwapDown(Heap, r, isize, istart, keyfield)
forrest@0
   305
      implicit none
forrest@0
   306
      integer(i4), intent(inout) :: Heap(maxN*maxN,0:maxsp+3)
forrest@0
   307
      integer(i4), intent(inout) :: r
forrest@0
   308
      integer(i4), intent(in)    :: isize
forrest@0
   309
      integer(i4), intent(in)    :: istart
forrest@0
   310
      integer(i4), intent(in)    :: keyfield
forrest@0
   311
      ! local variables
forrest@0
   312
      integer(i4) :: child
forrest@0
   313
      integer(i4) :: ipos
forrest@0
   314
      logical :: done
forrest@0
   315
!
forrest@0
   316
!      print *, 'entering SwapDown'
forrest@0
   317
!      print *, 'params are ', r, isize, istart, keyfield
forrest@0
   318
      ipos = istart - 1
forrest@0
   319
      done = .false.
forrest@0
   320
      child = 2 * r
forrest@0
   321
      do while (.not. done .and. child .le. isize)
forrest@0
   322
! find the largest child
forrest@0
   323
         if (child .lt. isize .and. Heap(child+ipos,keyfield) .lt. &
forrest@0
   324
            Heap(child+ipos+1,keyfield)) child = child + 1
forrest@0
   325
! interchange node and largest child if necessary
forrest@0
   326
! and move down to the next subtree
forrest@0
   327
         if (Heap(r+ipos,keyfield) .lt. Heap(child+ipos,keyfield)) then
forrest@0
   328
            Call Interchange(Heap,r+ipos,child+ipos)
forrest@0
   329
            r = child
forrest@0
   330
            child = 2 * child
forrest@0
   331
         else
forrest@0
   332
            done = .true.
forrest@0
   333
         end if
forrest@0
   334
!
forrest@0
   335
      end do
forrest@0
   336
!
forrest@0
   337
      return
forrest@0
   338
   end subroutine SwapDown
forrest@0
   339
!
forrest@0
   340
!------------------------------------------------------------------------------
forrest@0
   341
!
forrest@0
   342
   subroutine Interchange(X,m,n)
forrest@0
   343
      implicit none
forrest@0
   344
      integer(i4), intent(inout) :: X(maxN*maxN,0:maxsp+3)
forrest@0
   345
      integer(i4), intent(in) :: m
forrest@0
   346
      integer(i4), intent(in) :: n
forrest@0
   347
      ! local variables
forrest@0
   348
      integer(i4) :: i
forrest@0
   349
      integer(i4) :: temp
forrest@0
   350
!
forrest@0
   351
      do i = 0, ihabs+3
forrest@0
   352
         temp = X(m,i)
forrest@0
   353
         X(m,i) = X(n,i)
forrest@0
   354
         X(n,i) = temp
forrest@0
   355
      end do
forrest@0
   356
!
forrest@0
   357
      return
forrest@0
   358
   end subroutine Interchange
forrest@0
   359
!
forrest@0
   360
!------------------------------------------------------------------------------
forrest@0
   361
!
forrest@0
   362
   subroutine HeapSort(X,isize,istart,keyfield)
forrest@0
   363
      implicit none
forrest@0
   364
      integer(i4), intent(inout) :: X(maxN*maxN,0:maxsp+3)
forrest@0
   365
      integer(i4), intent(in) :: isize
forrest@0
   366
      integer(i4), intent(in) :: istart
forrest@0
   367
      integer(i4), intent(in) :: keyfield
forrest@0
   368
      ! local variables
forrest@0
   369
      integer(i4) :: i ! loop indices
forrest@0
   370
      integer(i4) :: r
forrest@0
   371
      integer(i4) :: tempr
forrest@0
   372
!
forrest@0
   373
!      print *, "isize in HeapSort is ", isize
forrest@0
   374
!      print *, "istart in HeapSort is ", istart
forrest@0
   375
!      print *, "keyfield in HeapSort is ", keyfield
forrest@0
   376
! heapify
forrest@0
   377
      do r = isize/2, 1, -1
forrest@0
   378
         tempr = r
forrest@0
   379
         !Call SwapDown(X,r,isize,istart,keyfield)
forrest@0
   380
         Call SwapDown(X,tempr,isize,istart,keyfield)
forrest@0
   381
         !r = tempr
forrest@0
   382
      end do
forrest@0
   383
!
forrest@0
   384
!      print *, "P after heapify is "
forrest@0
   385
!      do i=istart,isize
forrest@0
   386
!         print *, i, X(i,keyfield)
forrest@0
   387
!      end do
forrest@0
   388
!
forrest@0
   389
! repeatedly put largest item in root at end of list,
forrest@0
   390
! prune it from the tree
forrest@0
   391
! and apply SwapDown to the rest of the tree
forrest@0
   392
!
forrest@0
   393
      do i = isize, 2, -1
forrest@0
   394
         Call Interchange(X,istart,(i+istart-1))
forrest@0
   395
!         r = isize
forrest@0
   396
         r = 1
forrest@0
   397
         Call SwapDown(X,r,i-1,istart,keyfield)
forrest@0
   398
!
forrest@0
   399
!         print *, "P after Swapdown is  "
forrest@0
   400
!         do j=istart,isize
forrest@0
   401
!           print *, j, X(j,keyfield)
forrest@0
   402
!         end do
forrest@0
   403
      end do
forrest@0
   404
!
forrest@0
   405
      return
forrest@0
   406
   end subroutine HeapSort
forrest@0
   407
!
forrest@0
   408
!------------------------------------------------------------------------------
forrest@0
   409
!
forrest@0
   410
   subroutine XPMMap(N,II)
forrest@0
   411
!
forrest@0
   412
!  output routine for xpm X-window display tool
forrest@0
   413
!
forrest@0
   414
!
forrest@0
   415
      implicit none
forrest@0
   416
      integer(i4), intent(in) :: N
forrest@0
   417
      integer(i4), intent(in) :: II(0:maxN,0:maxN)
forrest@0
   418
      ! local variables
forrest@0
   419
      integer(i4) :: itemp(0:maxN)
forrest@0
   420
      integer(i4) :: i, j ! loop indices
forrest@0
   421
      character (len=40) :: fmt
forrest@0
   422
!
forrest@0
   423
!
forrest@0
   424
!  output map
forrest@0
   425
      print *, "writing final map to xpm format file landscape.xpm"
forrest@0
   426
!
forrest@0
   427
      open (20,file='landscape.xpm',status='unknown')
forrest@0
   428
!
forrest@0
   429
      write (20, 100)
forrest@0
   430
100   format ('/* XPM */',/'static char *realizer[]', &
forrest@0
   431
              ' = {',/'/* cols rows ncolors chars_per_pixel', &
forrest@0
   432
              ' */')
forrest@0
   433
      write (20, 102) N+1, N+1
forrest@0
   434
102   format ('"',i6,1x,i6,1x,'11 1 0 0",',/'/* colors */')
forrest@0
   435
      write (20, 104)
forrest@0
   436
104   format ('"0  c white    m white', &
forrest@0
   437
              '",',/'"1  s Decid  c BurlyWood    m black', &
forrest@0
   438
              '",',/'"2  s Mixed  c DarkGoldenrod3       m gray40', &
forrest@0
   439
              '",',/'"3  s Everg  c MistyRose3         m gray50', &
forrest@0
   440
              '",',/'"4  s Trans  c LightGoldenrod4         m gray60', &
forrest@0
   441
              '",',/'"5  s Urban  c DarkGoldenrod1       m gray70', &
forrest@0
   442
              '",',/'"6  s Water  c gold    m gray80', &
forrest@0
   443
              '",',/'"7  s Maint  c LightGoldenrod3       m white', &
forrest@0
   444
              '",',/'"8  s Cropl  c SaddleBrown    m lightgray', &
forrest@0
   445
              '",',/'"9  s Lawng  c RosyBrown    m gray', &
forrest@0
   446
              '",',/'"*  c chocolate     m black",',/'/* pixels */')
forrest@0
   447
!
forrest@0
   448
      write(fmt,'("(",I0,"(i1)$)")') N+1
forrest@0
   449
      do i= 0, N
forrest@0
   450
         do j = 0, N
forrest@0
   451
!            itemp(j) = II(i,j)
forrest@0
   452
            itemp(j) = II(j,i)
forrest@0
   453
         end do
forrest@0
   454
         write (20,'(1A$)') '"'
forrest@0
   455
!         write (20,108) (itemp(j), j=0,N)
forrest@0
   456
         write (20,fmt) (itemp(j), j=0,N)
forrest@0
   457
         write (20,'(2A)') '",'
forrest@0
   458
!108      format (' "',<N+1>(i1),'",')
forrest@0
   459
      end do
forrest@0
   460
!
forrest@0
   461
!
forrest@0
   462
      write (20,112)
forrest@0
   463
112   format ('};')
forrest@0
   464
!
forrest@0
   465
      return
forrest@0
   466
   end subroutine XPMMap
forrest@0
   467
!
forrest@0
   468
!------------------------------------------------------------------------------
forrest@0
   469
!
forrest@0
   470
   subroutine InputMap(mapfile,P,N,k,invert)
forrest@0
   471
!
forrest@0
   472
!   input map from a data file
forrest@0
   473
!
forrest@0
   474
      implicit none
forrest@0
   475
      character (len=60), intent(in) :: mapfile
forrest@0
   476
      integer(i4), intent(inout)     :: P(maxN*maxN,0:maxsp+3)
forrest@0
   477
      integer(i4), intent(in)        :: N
forrest@0
   478
      integer(i4), intent(in)        :: k
forrest@0
   479
      logical, intent(in)            :: invert
forrest@0
   480
      ! local variables
forrest@0
   481
      integer(i4) :: i, j, iv ! loop indices
forrest@0
   482
      real(r4)    :: X(0:maxN,0:maxN)
forrest@0
   483
      real(r4)    :: xmin
forrest@0
   484
      real(r4)    :: xmax
forrest@0
   485
      real(r4)    :: xrange
forrest@0
   486
      real(r4)    :: temp
forrest@0
   487
!
forrest@0
   488
      open (14, file=mapfile, status='old')
forrest@0
   489
!
forrest@0
   490
      xmin = 1e30
forrest@0
   491
      xmax = -1e30
forrest@0
   492
      do j = 0, N
forrest@0
   493
         read (14,*) ( X(i,j), i = 0, N)
forrest@0
   494
!         read (14,118) X(i,j)
forrest@0
   495
!118      format (<(N+1)*(N+1)>i5, 1x)
forrest@0
   496
         do i = 0, N
forrest@0
   497
            if (X(i,j) .lt. xmin) xmin = X(i,j)
forrest@0
   498
            if (X(i,j) .gt. xmax) xmax = X(i,j)
forrest@0
   499
         end do
forrest@0
   500
      end do
forrest@0
   501
!
forrest@0
   502
!      invert if necessary and scale between 0 and 32767
forrest@0
   503
       xrange = xmax - xmin
forrest@0
   504
       do j = 0, N
forrest@0
   505
          do i = 0, N
forrest@0
   506
             if (invert) then
forrest@0
   507
                temp = ((-X(i,j) + xmax) / xrange) * 32767
forrest@0
   508
              else
forrest@0
   509
                temp = ((X(i,j) - xmin) / xrange) * 32767
forrest@0
   510
              end if
forrest@0
   511
!
forrest@0
   512
              iv = (i*(N+1))+j+1
forrest@0
   513
              P(iv,k) = nint(temp)
forrest@0
   514
              P(iv,ihabs+1) = i
forrest@0
   515
              P(iv,ihabs+2) = j
forrest@0
   516
          end do
forrest@0
   517
      end do
forrest@0
   518
!
forrest@0
   519
      close(14)
forrest@0
   520
      rewind(14)
forrest@0
   521
!
forrest@0
   522
      return
forrest@0
   523
   end subroutine InputMap
forrest@0
   524
!
forrest@0
   525
!------------------------------------------------------------------------------
forrest@0
   526
!
forrest@0
   527
   subroutine SD2(Heap,r,isize,istart)
forrest@0
   528
      implicit none
forrest@0
   529
      integer(i4), intent(inout) :: Heap(maxN*maxN)
forrest@0
   530
      integer(i4), intent(inout) :: r
forrest@0
   531
      integer(i4), intent(in)    :: isize
forrest@0
   532
      integer(i4), intent(in)    :: istart
forrest@0
   533
      ! local variables
forrest@0
   534
      integer(i4) :: ipos
forrest@0
   535
      integer(i4) :: child
forrest@0
   536
      logical     :: done
forrest@0
   537
!
forrest@0
   538
!      print *, 'entering SwapDown'
forrest@0
   539
!      print *, 'params are ', r, isize, istart
forrest@0
   540
      ipos = istart - 1
forrest@0
   541
      done = .false.
forrest@0
   542
      child = 2 * r
forrest@0
   543
      do while (.not. done .and. child .le. isize)
forrest@0
   544
!  find the largest child
forrest@0
   545
         if (child .lt. isize .and. Heap(child+ipos) .lt. &
forrest@0
   546
             Heap(child+ipos+1)) child = child + 1
forrest@0
   547
!  interchange node and largest child if necessary
forrest@0
   548
!   and move down to the next subtree
forrest@0
   549
         if (Heap(r+ipos) .lt. Heap(child+ipos)) then
forrest@0
   550
            Call I2(Heap,r+ipos,child+ipos)
forrest@0
   551
            r = child
forrest@0
   552
            child = 2 * child
forrest@0
   553
         else
forrest@0
   554
            done = .true.
forrest@0
   555
         end if
forrest@0
   556
!
forrest@0
   557
      end do
forrest@0
   558
!
forrest@0
   559
      return
forrest@0
   560
   end subroutine SD2
forrest@0
   561
!
forrest@0
   562
!------------------------------------------------------------------------------
forrest@0
   563
!
forrest@0
   564
   subroutine I2(X,m,n)
forrest@0
   565
      implicit none
forrest@0
   566
      integer(i4), intent(inout) :: X(maxN*maxN)
forrest@0
   567
      integer(i4), intent(in)    :: m
forrest@0
   568
      integer(i4), intent(in)    :: n
forrest@0
   569
      ! local variables
forrest@0
   570
      integer(i4) :: temp
forrest@0
   571
!
forrest@0
   572
      temp = X(m)
forrest@0
   573
      X(m) = X(n)
forrest@0
   574
      X(n) = temp
forrest@0
   575
!
forrest@0
   576
      return
forrest@0
   577
   end subroutine I2
forrest@0
   578
!
forrest@0
   579
!------------------------------------------------------------------------------
forrest@0
   580
!
forrest@0
   581
   subroutine HS2(X,isize,istart)
forrest@0
   582
      implicit none
forrest@0
   583
      integer(i4), intent(inout) :: X(maxN*maxN)
forrest@0
   584
      integer(i4), intent(in)    :: isize
forrest@0
   585
      integer(i4), intent(in)    :: istart
forrest@0
   586
      ! local variables
forrest@0
   587
      integer(i4) :: i, j ! loop indices
forrest@0
   588
      integer(i4) :: r
forrest@0
   589
      integer(i4) :: tempr
forrest@0
   590
!
forrest@0
   591
!      print *, "isize in HeapSort is ", isize
forrest@0
   592
!      print *, "istart in HeapSort is ", istart
forrest@0
   593
!     heapify
forrest@0
   594
      do r = isize/2, 1, -1
forrest@0
   595
         tempr = r
forrest@0
   596
         Call SD2(X,tempr,isize,istart)
forrest@0
   597
         !Call SD2(X,r,isize,istart)
forrest@0
   598
         !r = tempr
forrest@0
   599
      end do
forrest@0
   600
!
forrest@0
   601
!      print*, "P after heapify is "
forrest@0
   602
!      do i=istart,isize
forrest@0
   603
!         print *, i, X(i)
forrest@0
   604
!      end do
forrest@0
   605
!
forrest@0
   606
!   repeatedly put largest item in root at end of list,
forrest@0
   607
!   prune it from the tree
forrest@0
   608
!   and apply SwapDown to the rest of the tree
forrest@0
   609
!
forrest@0
   610
      do i = isize, 2, -1
forrest@0
   611
         Call I2(X,istart,(i+istart-1))
forrest@0
   612
!         r = isize
forrest@0
   613
         r = 1
forrest@0
   614
         Call SD2(X,r,i-1,istart)
forrest@0
   615
!
forrest@0
   616
!         print*, "P after Swapdown is  "
forrest@0
   617
!         do j=istart,isize
forrest@0
   618
!           print *, j, X(j)
forrest@0
   619
!         end do
forrest@0
   620
forrest@0
   621
      end do
forrest@0
   622
!
forrest@0
   623
      return
forrest@0
   624
   end subroutine HS2
forrest@0
   625
!
forrest@0
   626
!------------------------------------------------------------------------------
forrest@0
   627
!
forrest@0
   628
   subroutine XPMTies(P)
forrest@0
   629
!
forrest@0
   630
!  output routine for xpm X-window display tool
forrest@0
   631
!
forrest@0
   632
!  write xpm format map with grayshades
forrest@0
   633
!
forrest@0
   634
      implicit none
forrest@0
   635
      integer(i4), intent(in) :: P(maxN*maxN,0:maxsp+3)
forrest@0
   636
      ! local variables
forrest@0
   637
      integer(i4) :: itemp(0:maxN,0:maxN)
forrest@0
   638
      integer(i4) :: i, j, m ! loop indices
forrest@0
   639
      real(r4)    :: scalelog
forrest@0
   640
      character (len=40) :: fmt
forrest@0
   641
!
forrest@0
   642
      open (40,file='ties.xpm',status='unknown')
forrest@0
   643
!
forrest@0
   644
      print 99
forrest@0
   645
99    format ('XPM map file ties.xpm being written')
forrest@0
   646
      write (40, 100)
forrest@0
   647
100   format ('/* XPM */',/'static char *tiesmap[]', &
forrest@0
   648
              ' = {',/'/* cols rows ncolors chars_per_pixel', &
forrest@0
   649
              ' */')
forrest@0
   650
      write (40, 102) irowcols+1, irowcols+1
forrest@0
   651
102   format (' "',i6,1x,i6,1x,'108 3 0 0",',/'/* colors */')
forrest@0
   652
      write (40, 104)
forrest@0
   653
104   format ('"  0 s 0.0prob c white g4 white g white m white', &
forrest@0
   654
              '",',/'"  1 s 0.01prob c gray99 g4 gray99 g gray99 m gray99', &
forrest@0
   655
              '",',/'"  2 s 0.02prob c gray98 g4 gray98 g gray98 m gray98', &
forrest@0
   656
              '",',/'"  3 s 0.03prob c gray97 g4 gray97 g gray97 m gray97', &
forrest@0
   657
              '",',/'"  4 s 0.04prob c gray96 g4 gray96 g gray96 m gray96', &
forrest@0
   658
              '",',/'"  5 s 0.05prob c gray95 g4 gray95 g gray95 m gray95', &
forrest@0
   659
              '",',/'"  6 s 0.06prob c gray94 g4 gray94 g gray94 m gray94', &
forrest@0
   660
              '",',/'"  7 s 0.07prob c gray93 g4 gray93 g gray93 m gray93', &
forrest@0
   661
              '",',/'"  8 s 0.08prob c gray92 g4 gray92 g gray92 m gray92', &
forrest@0
   662
              '",',/'"  9 s 0.09prob c gray91 g4 gray91 g gray91 m gray91', &
forrest@0
   663
              '",',/'" 10 s 0.10prob c gray90 g4 gray90 g gray90 m gray90', &
forrest@0
   664
              '",',/'" 11 s 0.11prob c gray89 g4 gray89 g gray89 m gray89', &
forrest@0
   665
              '",',/'" 12 s 0.12prob c gray88 g4 gray88 g gray88 m gray88', &
forrest@0
   666
              '",',/'" 13 s 0.13prob c gray87 g4 gray87 g gray87 m gray87', &
forrest@0
   667
              '",',/'" 14 s 0.14prob c gray86 g4 gray86 g gray86 m gray86', &
forrest@0
   668
              '",',/'" 15 s 0.15prob c gray85 g4 gray85 g gray85 m gray85', &
forrest@0
   669
              '",',/'" 16 s 0.16prob c gray84 g4 gray84 g gray84 m gray84', &
forrest@0
   670
              '",',/'" 17 s 0.17prob c gray83 g4 gray83 g gray83 m gray83', &
forrest@0
   671
              '",',/'" 18 s 0.18prob c gray82 g4 gray82 g gray82 m gray82', &
forrest@0
   672
              '",',/'" 19 s 0.19prob c gray81 g4 gray81 g gray81 m gray81', &
forrest@0
   673
              '",',/'" 20 s 0.20prob c gray80 g4 gray80 g gray80 m gray80', &
forrest@0
   674
              '",',/'" 21 s 0.21prob c gray79 g4 gray79 g gray79 m gray79', &
forrest@0
   675
              '",',/'" 22 s 0.22prob c gray78 g4 gray78 g gray78 m gray78', &
forrest@0
   676
              '",',/'" 23 s 0.23prob c gray77 g4 gray77 g gray77 m gray77', &
forrest@0
   677
              '",',/'" 24 s 0.24prob c gray76 g4 gray76 g gray76 m gray76', &
forrest@0
   678
              '",',/'" 25 s 0.25prob c gray75 g4 gray75 g gray75 m gray75', &
forrest@0
   679
              '",',/'" 26 s 0.26prob c gray74 g4 gray74 g gray74 m gray74', &
forrest@0
   680
              '",',/'" 27 s 0.27prob c gray73 g4 gray73 g gray73 m gray73', &
forrest@0
   681
              '",',/'" 28 s 0.28prob c gray72 g4 gray72 g gray72 m gray72', &
forrest@0
   682
              '",',/'" 29 s 0.29prob c gray71 g4 gray71 g gray71 m gray71', &
forrest@0
   683
              '",',/'" 30 s 0.30prob c gray70 g4 gray70 g gray70 m gray70', &
forrest@0
   684
              '",',/'" 31 s 0.31prob c gray69 g4 gray69 g gray69 m gray69', &
forrest@0
   685
              '",',/'" 32 s 0.32prob c gray68 g4 gray68 g gray68 m gray68', &
forrest@0
   686
              '",',/'" 33 s 0.33prob c gray67 g4 gray67 g gray67 m gray67', &
forrest@0
   687
              '",',/'" 34 s 0.34prob c gray66 g4 gray66 g gray66 m gray66', &
forrest@0
   688
              '",',/'" 35 s 0.35prob c gray65 g4 gray65 g gray65 m gray65', &
forrest@0
   689
              '",',/'" 36 s 0.36prob c gray64 g4 gray64 g gray64 m gray64', &
forrest@0
   690
              '",',/'" 37 s 0.37prob c gray63 g4 gray63 g gray63 m gray63', &
forrest@0
   691
              '",',/'" 38 s 0.38prob c gray62 g4 gray62 g gray62 m gray62', &
forrest@0
   692
              '",',/'" 39 s 0.39prob c gray61 g4 gray61 g gray61 m gray61', &
forrest@0
   693
              '",',/'" 40 s 0.40prob c gray60 g4 gray60 g gray60 m gray60', &
forrest@0
   694
              '",',/'" 41 s 0.41prob c gray59 g4 gray59 g gray59 m gray59', &
forrest@0
   695
              '",',/'" 42 s 0.42prob c gray58 g4 gray58 g gray58 m gray58', &
forrest@0
   696
              '",',/'" 43 s 0.43prob c gray57 g4 gray57 g gray57 m gray57', &
forrest@0
   697
              '",',/'" 44 s 0.44prob c gray56 g4 gray56 g gray56 m gray56', &
forrest@0
   698
              '",',/'" 45 s 0.45prob c gray55 g4 gray55 g gray55 m gray55', &
forrest@0
   699
              '",',/'" 46 s 0.46prob c gray54 g4 gray54 g gray54 m gray54', &
forrest@0
   700
              '",',/'" 47 s 0.47prob c gray53 g4 gray53 g gray53 m gray53', &
forrest@0
   701
              '",',/'" 48 s 0.48prob c gray52 g4 gray52 g gray52 m gray52', &
forrest@0
   702
              '",',/'" 49 s 0.49prob c gray51 g4 gray51 g gray51 m gray51', &
forrest@0
   703
              '",',/'" 50 s 0.50prob c gray50 g4 gray50 g gray50 m gray50', &
forrest@0
   704
              '",',/'" 51 s 0.51prob c gray49 g4 gray49 g gray49 m gray49', &
forrest@0
   705
              '",',/'" 52 s 0.52prob c gray48 g4 gray48 g gray48 m gray48', &
forrest@0
   706
              '",',/'" 53 s 0.53prob c gray47 g4 gray47 g gray47 m gray47', &
forrest@0
   707
              '",',/'" 54 s 0.54prob c gray46 g4 gray46 g gray46 m gray46', &
forrest@0
   708
              '",',/'" 55 s 0.55prob c gray45 g4 gray45 g gray45 m gray45', &
forrest@0
   709
              '",',/'" 56 s 0.56prob c gray44 g4 gray44 g gray44 m gray44', &
forrest@0
   710
              '",',/'" 57 s 0.57prob c gray43 g4 gray43 g gray43 m gray43', &
forrest@0
   711
              '",',/'" 58 s 0.58prob c gray42 g4 gray42 g gray42 m gray42', &
forrest@0
   712
              '",',/'" 59 s 0.59prob c gray41 g4 gray41 g gray41 m gray41', &
forrest@0
   713
              '",',/'" 60 s 0.60prob c gray40 g4 gray40 g gray40 m gray40', &
forrest@0
   714
              '",',/'" 61 s 0.61prob c gray39 g4 gray39 g gray39 m gray39', &
forrest@0
   715
              '",',/'" 62 s 0.62prob c gray38 g4 gray38 g gray38 m gray38', &
forrest@0
   716
              '",',/'" 63 s 0.63prob c gray37 g4 gray37 g gray37 m gray37', &
forrest@0
   717
              '",',/'" 64 s 0.64prob c gray36 g4 gray36 g gray36 m gray36', &
forrest@0
   718
              '",',/'" 65 s 0.65prob c gray35 g4 gray35 g gray35 m gray35', &
forrest@0
   719
              '",',/'" 66 s 0.66prob c gray34 g4 gray34 g gray34 m gray34', &
forrest@0
   720
              '",',/'" 67 s 0.67prob c gray33 g4 gray33 g gray33 m gray33', &
forrest@0
   721
              '",',/'" 68 s 0.68prob c gray32 g4 gray32 g gray32 m gray32', &
forrest@0
   722
              '",',/'" 69 s 0.69prob c gray31 g4 gray31 g gray31 m gray31', &
forrest@0
   723
              '",',/'" 70 s 0.70prob c gray30 g4 gray30 g gray30 m gray30', &
forrest@0
   724
              '",',/'" 71 s 0.71prob c gray29 g4 gray29 g gray29 m gray29', &
forrest@0
   725
              '",',/'" 72 s 0.72prob c gray28 g4 gray28 g gray28 m gray28', &
forrest@0
   726
              '",',/'" 73 s 0.73prob c gray27 g4 gray27 g gray27 m gray27', &
forrest@0
   727
              '",',/'" 74 s 0.74prob c gray26 g4 gray26 g gray26 m gray26', &
forrest@0
   728
              '",',/'" 75 s 0.75prob c gray25 g4 gray25 g gray25 m gray25', &
forrest@0
   729
              '",',/'" 76 s 0.76prob c gray24 g4 gray24 g gray24 m gray24', &
forrest@0
   730
              '",',/'" 77 s 0.77prob c gray23 g4 gray23 g gray23 m gray23', &
forrest@0
   731
              '",',/'" 78 s 0.78prob c gray22 g4 gray22 g gray22 m gray22', &
forrest@0
   732
              '",',/'" 79 s 0.79prob c gray21 g4 gray21 g gray21 m gray21', &
forrest@0
   733
              '",',/'" 80 s 0.80prob c gray20 g4 gray20 g gray20 m gray20', &
forrest@0
   734
              '",',/'" 81 s 0.81prob c gray19 g4 gray19 g gray19 m gray19', &
forrest@0
   735
              '",',/'" 82 s 0.82prob c gray18 g4 gray18 g gray18 m gray18', &
forrest@0
   736
              '",',/'" 83 s 0.83prob c gray17 g4 gray17 g gray17 m gray17', &
forrest@0
   737
              '",',/'" 84 s 0.84prob c gray16 g4 gray16 g gray16 m gray16', &
forrest@0
   738
              '",',/'" 85 s 0.85prob c gray15 g4 gray15 g gray15 m gray15', &
forrest@0
   739
              '",',/'" 86 s 0.86prob c gray14 g4 gray14 g gray14 m gray14', &
forrest@0
   740
              '",',/'" 87 s 0.87prob c gray13 g4 gray13 g gray13 m gray13', &
forrest@0
   741
              '",',/'" 88 s 0.88prob c gray12 g4 gray12 g gray12 m gray12', &
forrest@0
   742
              '",',/'" 89 s 0.89prob c gray11 g4 gray11 g gray11 m gray11', &
forrest@0
   743
              '",',/'" 90 s 0.90prob c gray10 g4 gray10 g gray10 m gray10', &
forrest@0
   744
              '",',/'" 91 s 0.91prob c gray9 g4 gray9 g gray9 m gray9', &
forrest@0
   745
              '",',/'" 92 s 0.92prob c gray8 g4 gray8 g gray8 m gray8', &
forrest@0
   746
              '",',/'" 93 s 0.93prob c gray7 g4 gray7 g gray7 m gray7', &
forrest@0
   747
              '",',/'" 94 s 0.94prob c gray6 g4 gray6 g gray6 m gray6', &
forrest@0
   748
              '",',/'" 95 s 0.95prob c gray5 g4 gray5 g gray5 m gray5', &
forrest@0
   749
              '",',/'" 96 s 0.96prob c gray4 g4 gray4 g gray4 m gray4', &
forrest@0
   750
              '",',/'" 97 s 0.97prob c gray3 g4 gray3 g gray3 m gray3', &
forrest@0
   751
              '",',/'" 98 s 0.98prob c gray2 g4 gray2 g gray2 m gray2', &
forrest@0
   752
              '",',/'" 99 s 0.99prob c gray1 g4 gray1 g gray1 m gray1', &
forrest@0
   753
              '",',/'"100 s 1.00prob c black g4 black g black m black', &
forrest@0
   754
              '",',/'"101  c white    m white  s NotFlammable', &
forrest@0
   755
              '",',/'"102  c GreenYellow       m gray70  s LP0', &
forrest@0
   756
              '",',/'"103  c LawnGreen         m gray60 s LP1', &
forrest@0
   757
              '",',/'"104  c LimeGreen         m gray50   s LP2', &
forrest@0
   758
              '",',/'"105  c ForestGreen       m gray40   s LP3', &
forrest@0
   759
              '",',/'"106  c yellow    m gray80  s NF', &
forrest@0
   760
              '",',/'"107  c blue    m black  s water ",', &
forrest@0
   761
              /'/* pixels */')
forrest@0
   762
!
forrest@0
   763
!      calculate a log scale for gray levels
forrest@0
   764
!      use this scale log for uniform scaling across realizations
forrest@0
   765
!       scalelog = 10**(log10(100.)/32767.)
forrest@0
   766
!      scalelog for simple summed priority
forrest@0
   767
!       scale max gray to ihabs-isupply-1 full-on categories
forrest@0
   768
!        (= ihabs-isupply-1 x 32767)
forrest@0
   769
!c       scalelog = 10**(log10(100.)/(32767. * (ihabs-isupply-1)))
forrest@0
   770
!      use this scalelog for maximum priority resolution w/in one map
forrest@0
   771
       scalelog = 10**(log10(100.) / real(maxgray,kind(scalelog)))
forrest@0
   772
!       print *, scalelog
forrest@0
   773
!
forrest@0
   774
!     start at bottom of list (highest priority ties)
forrest@0
   775
      do m = numcells, itiedsplit+1, -1 ! over all tied
forrest@0
   776
!         itemp(P(m,ihabs+1),P(m,ihabs+2)) = 
forrest@0
   777
!     &    (real(P(m,ihabs+3),kind(itemp))/32767)*100
forrest@0
   778
!
forrest@0
   779
!         print *, scalelog**P(m,ihabs+3)
forrest@0
   780
!         print *, P(m,ihabs+1),P(m,ihabs+2),P(m,ihabs+3)
forrest@0
   781
         itemp(P(m,ihabs+1),P(m,ihabs+2)) = scalelog**P(m,ihabs+3)
forrest@0
   782
!         itemp(P(m,ihabs+1),P(m,ihabs+2)) = 100
forrest@0
   783
!        all priorities greater than color index 100 set black
forrest@0
   784
         if (itemp(P(m,ihabs+1),P(m,ihabs+2)) .gt. 100) &
forrest@0
   785
            itemp(P(m,ihabs+1),P(m,ihabs+2)) = 100
forrest@0
   786
      end do
forrest@0
   787
!
forrest@0
   788
!      color used blanks yellow
forrest@0
   789
      do m = mtsplit,mtresolved+1,-1
forrest@0
   790
         itemp(P(m,ihabs+1),P(m,ihabs+2)) = 106
forrest@0
   791
      end do
forrest@0
   792
!
forrest@0
   793
      write (fmt,'("(",I0,"(i3)$)")') irowcols+1
forrest@0
   794
      do i = 0, irowcols
forrest@0
   795
         write (40,'(1A$)') '"'
forrest@0
   796
         !write (40,108) (itemp(j,i), j=0, irowcols)
forrest@0
   797
         write (40,fmt) (itemp(j,i), j=0, irowcols)
forrest@0
   798
         write (40,'(2A)') '",'
forrest@0
   799
!108      format ('"',<irowcols+1>(i3),'",')
forrest@0
   800
      end do
forrest@0
   801
!
forrest@0
   802
      write (40,112)
forrest@0
   803
112   format ('};')
forrest@0
   804
      close (40)
forrest@0
   805
!
forrest@0
   806
      return
forrest@0
   807
   end subroutine XPMTies
forrest@0
   808
!
forrest@0
   809
!------------------------------------------------------------------------------
forrest@0
   810
!
forrest@0
   811
   subroutine XPMRelief(P,iz,iflood)
forrest@0
   812
!
forrest@0
   813
!  output routine for xpm X-window display tool
forrest@0
   814
!
forrest@0
   815
!  write xpm format aspect map of painted fractal
forrest@0
   816
!   spatial probability surfaces with grayshades
forrest@0
   817
!
forrest@0
   818
      implicit none
forrest@0
   819
      integer(i4), intent(in) :: P(maxN*maxN,0:maxsp+3)
forrest@0
   820
      integer(i4), intent(in) :: iz
forrest@0
   821
      integer(i4), intent(in) :: iflood
forrest@0
   822
      ! local variables
forrest@0
   823
      integer(i4)        :: itemp(0:maxN,0:maxN)
forrest@0
   824
      integer(i4)        :: idispvec(0:maxN)
forrest@0
   825
      integer(i4)        :: i, j, k, l, m        ! loop indices
forrest@0
   826
      real(r4)           :: alt                  ! altitude
forrest@0
   827
      real(r4)           :: az                   ! azimuth
forrest@0
   828
      real(r4)           :: x, y
forrest@0
   829
      real(r4)           :: slope                ! slope
forrest@0
   830
      real(r4)           :: aspect               ! aspect
forrest@0
   831
      real(r4)           :: c
forrest@0
   832
      character (len=15) :: outfile(0:maxsp+1)
forrest@0
   833
      character (len=40) :: fmt
forrest@0
   834
!
forrest@0
   835
!     set filenames for the first 10 categories
forrest@0
   836
      outfile(0)  = 'probmap0.xpm   '
forrest@0
   837
      outfile(1)  = 'probmap1.xpm   '
forrest@0
   838
      outfile(2)  = 'probmap2.xpm   '
forrest@0
   839
      outfile(3)  = 'probmap3.xpm   '
forrest@0
   840
      outfile(4)  = 'probmap4.xpm   '
forrest@0
   841
      outfile(5)  = 'probmap5.xpm   '
forrest@0
   842
      outfile(6)  = 'probmap6.xpm   '
forrest@0
   843
      outfile(7)  = 'probmap7.xpm   '
forrest@0
   844
      outfile(8)  = 'probmap8.xpm   '
forrest@0
   845
      outfile(9)  = 'probmap9.xpm   '
forrest@0
   846
      outfile(10) = 'probmap10.xpm  '
forrest@0
   847
!
forrest@0
   848
!
forrest@0
   849
      print 99, outfile(iz)
forrest@0
   850
99    format ('XPM map file ', a15 'being written')
forrest@0
   851
!
forrest@0
   852
!     open the right file for xpm output
forrest@0
   853
      open (50,file=outfile(iz),status='unknown')
forrest@0
   854
!
forrest@0
   855
      write (50, 100)
forrest@0
   856
100   format ('/* XPM */',/'static char *fracasp[]', &
forrest@0
   857
              ' = {',/'/* cols rows ncolors chars_per_pixel', ' */')
forrest@0
   858
      write (50, 102) irowcols+1, irowcols+1
forrest@0
   859
102   format ('"',i6,1x,i6,1x,'108 3 0 0",',/'/* colors */')
forrest@0
   860
      write (50, 104)
forrest@0
   861
104   format ('"  0 s 0.0prob c white g4 white g white m white', &
forrest@0
   862
              '",',/'"  1 s 0.01prob c gray99 g4 gray99 g gray99 m gray99', &
forrest@0
   863
              '",',/'"  2 s 0.02prob c gray98 g4 gray98 g gray98 m gray98', &
forrest@0
   864
              '",',/'"  3 s 0.03prob c gray97 g4 gray97 g gray97 m gray97', &
forrest@0
   865
              '",',/'"  4 s 0.04prob c gray96 g4 gray96 g gray96 m gray96', &
forrest@0
   866
              '",',/'"  5 s 0.05prob c gray95 g4 gray95 g gray95 m gray95', &
forrest@0
   867
              '",',/'"  6 s 0.06prob c gray94 g4 gray94 g gray94 m gray94', &
forrest@0
   868
              '",',/'"  7 s 0.07prob c gray93 g4 gray93 g gray93 m gray93', &
forrest@0
   869
              '",',/'"  8 s 0.08prob c gray92 g4 gray92 g gray92 m gray92', &
forrest@0
   870
              '",',/'"  9 s 0.09prob c gray91 g4 gray91 g gray91 m gray91', &
forrest@0
   871
              '",',/'" 10 s 0.10prob c gray90 g4 gray90 g gray90 m gray90', &
forrest@0
   872
              '",',/'" 11 s 0.11prob c gray89 g4 gray89 g gray89 m gray89', &
forrest@0
   873
              '",',/'" 12 s 0.12prob c gray88 g4 gray88 g gray88 m gray88', &
forrest@0
   874
              '",',/'" 13 s 0.13prob c gray87 g4 gray87 g gray87 m gray87', &
forrest@0
   875
              '",',/'" 14 s 0.14prob c gray86 g4 gray86 g gray86 m gray86', &
forrest@0
   876
              '",',/'" 15 s 0.15prob c gray85 g4 gray85 g gray85 m gray85', &
forrest@0
   877
              '",',/'" 16 s 0.16prob c gray84 g4 gray84 g gray84 m gray84', &
forrest@0
   878
              '",',/'" 17 s 0.17prob c gray83 g4 gray83 g gray83 m gray83', &
forrest@0
   879
              '",',/'" 18 s 0.18prob c gray82 g4 gray82 g gray82 m gray82', &
forrest@0
   880
              '",',/'" 19 s 0.19prob c gray81 g4 gray81 g gray81 m gray81', &
forrest@0
   881
              '",',/'" 20 s 0.20prob c gray80 g4 gray80 g gray80 m gray80', &
forrest@0
   882
              '",',/'" 21 s 0.21prob c gray79 g4 gray79 g gray79 m gray79', &
forrest@0
   883
              '",',/'" 22 s 0.22prob c gray78 g4 gray78 g gray78 m gray78', &
forrest@0
   884
              '",',/'" 23 s 0.23prob c gray77 g4 gray77 g gray77 m gray77', &
forrest@0
   885
              '",',/'" 24 s 0.24prob c gray76 g4 gray76 g gray76 m gray76', &
forrest@0
   886
              '",',/'" 25 s 0.25prob c gray75 g4 gray75 g gray75 m gray75', &
forrest@0
   887
              '",',/'" 26 s 0.26prob c gray74 g4 gray74 g gray74 m gray74', &
forrest@0
   888
              '",',/'" 27 s 0.27prob c gray73 g4 gray73 g gray73 m gray73', &
forrest@0
   889
              '",',/'" 28 s 0.28prob c gray72 g4 gray72 g gray72 m gray72', &
forrest@0
   890
              '",',/'" 29 s 0.29prob c gray71 g4 gray71 g gray71 m gray71', &
forrest@0
   891
              '",',/'" 30 s 0.30prob c gray70 g4 gray70 g gray70 m gray70', &
forrest@0
   892
              '",',/'" 31 s 0.31prob c gray69 g4 gray69 g gray69 m gray69', &
forrest@0
   893
              '",',/'" 32 s 0.32prob c gray68 g4 gray68 g gray68 m gray68', &
forrest@0
   894
              '",',/'" 33 s 0.33prob c gray67 g4 gray67 g gray67 m gray67', &
forrest@0
   895
              '",',/'" 34 s 0.34prob c gray66 g4 gray66 g gray66 m gray66', &
forrest@0
   896
              '",',/'" 35 s 0.35prob c gray65 g4 gray65 g gray65 m gray65', &
forrest@0
   897
              '",',/'" 36 s 0.36prob c gray64 g4 gray64 g gray64 m gray64', &
forrest@0
   898
              '",',/'" 37 s 0.37prob c gray63 g4 gray63 g gray63 m gray63', &
forrest@0
   899
              '",',/'" 38 s 0.38prob c gray62 g4 gray62 g gray62 m gray62', &
forrest@0
   900
              '",',/'" 39 s 0.39prob c gray61 g4 gray61 g gray61 m gray61', &
forrest@0
   901
              '",',/'" 40 s 0.40prob c gray60 g4 gray60 g gray60 m gray60', &
forrest@0
   902
              '",',/'" 41 s 0.41prob c gray59 g4 gray59 g gray59 m gray59', &
forrest@0
   903
              '",',/'" 42 s 0.42prob c gray58 g4 gray58 g gray58 m gray58', &
forrest@0
   904
              '",',/'" 43 s 0.43prob c gray57 g4 gray57 g gray57 m gray57', &
forrest@0
   905
              '",',/'" 44 s 0.44prob c gray56 g4 gray56 g gray56 m gray56', &
forrest@0
   906
              '",',/'" 45 s 0.45prob c gray55 g4 gray55 g gray55 m gray55', &
forrest@0
   907
              '",',/'" 46 s 0.46prob c gray54 g4 gray54 g gray54 m gray54', &
forrest@0
   908
              '",',/'" 47 s 0.47prob c gray53 g4 gray53 g gray53 m gray53', &
forrest@0
   909
              '",',/'" 48 s 0.48prob c gray52 g4 gray52 g gray52 m gray52', &
forrest@0
   910
              '",',/'" 49 s 0.49prob c gray51 g4 gray51 g gray51 m gray51', &
forrest@0
   911
              '",',/'" 50 s 0.50prob c gray50 g4 gray50 g gray50 m gray50', &
forrest@0
   912
              '",',/'" 51 s 0.51prob c gray49 g4 gray49 g gray49 m gray49', &
forrest@0
   913
              '",',/'" 52 s 0.52prob c gray48 g4 gray48 g gray48 m gray48', &
forrest@0
   914
              '",',/'" 53 s 0.53prob c gray47 g4 gray47 g gray47 m gray47', &
forrest@0
   915
              '",',/'" 54 s 0.54prob c gray46 g4 gray46 g gray46 m gray46', &
forrest@0
   916
              '",',/'" 55 s 0.55prob c gray45 g4 gray45 g gray45 m gray45', &
forrest@0
   917
              '",',/'" 56 s 0.56prob c gray44 g4 gray44 g gray44 m gray44', &
forrest@0
   918
              '",',/'" 57 s 0.57prob c gray43 g4 gray43 g gray43 m gray43', &
forrest@0
   919
              '",',/'" 58 s 0.58prob c gray42 g4 gray42 g gray42 m gray42', &
forrest@0
   920
              '",',/'" 59 s 0.59prob c gray41 g4 gray41 g gray41 m gray41', &
forrest@0
   921
              '",',/'" 60 s 0.60prob c gray40 g4 gray40 g gray40 m gray40', &
forrest@0
   922
              '",',/'" 61 s 0.61prob c gray39 g4 gray39 g gray39 m gray39', &
forrest@0
   923
              '",',/'" 62 s 0.62prob c gray38 g4 gray38 g gray38 m gray38', &
forrest@0
   924
              '",',/'" 63 s 0.63prob c gray37 g4 gray37 g gray37 m gray37', &
forrest@0
   925
              '",',/'" 64 s 0.64prob c gray36 g4 gray36 g gray36 m gray36', &
forrest@0
   926
              '",',/'" 65 s 0.65prob c gray35 g4 gray35 g gray35 m gray35', &
forrest@0
   927
              '",',/'" 66 s 0.66prob c gray34 g4 gray34 g gray34 m gray34', &
forrest@0
   928
              '",',/'" 67 s 0.67prob c gray33 g4 gray33 g gray33 m gray33', &
forrest@0
   929
              '",',/'" 68 s 0.68prob c gray32 g4 gray32 g gray32 m gray32', &
forrest@0
   930
              '",',/'" 69 s 0.69prob c gray31 g4 gray31 g gray31 m gray31', &
forrest@0
   931
              '",',/'" 70 s 0.70prob c gray30 g4 gray30 g gray30 m gray30', &
forrest@0
   932
              '",',/'" 71 s 0.71prob c gray29 g4 gray29 g gray29 m gray29', &
forrest@0
   933
              '",',/'" 72 s 0.72prob c gray28 g4 gray28 g gray28 m gray28', &
forrest@0
   934
              '",',/'" 73 s 0.73prob c gray27 g4 gray27 g gray27 m gray27', &
forrest@0
   935
              '",',/'" 74 s 0.74prob c gray26 g4 gray26 g gray26 m gray26', &
forrest@0
   936
              '",',/'" 75 s 0.75prob c gray25 g4 gray25 g gray25 m gray25', &
forrest@0
   937
              '",',/'" 76 s 0.76prob c gray24 g4 gray24 g gray24 m gray24', &
forrest@0
   938
              '",',/'" 77 s 0.77prob c gray23 g4 gray23 g gray23 m gray23', &
forrest@0
   939
              '",',/'" 78 s 0.78prob c gray22 g4 gray22 g gray22 m gray22', &
forrest@0
   940
              '",',/'" 79 s 0.79prob c gray21 g4 gray21 g gray21 m gray21', &
forrest@0
   941
              '",',/'" 80 s 0.80prob c gray20 g4 gray20 g gray20 m gray20', &
forrest@0
   942
              '",',/'" 81 s 0.81prob c gray19 g4 gray19 g gray19 m gray19', &
forrest@0
   943
              '",',/'" 82 s 0.82prob c gray18 g4 gray18 g gray18 m gray18', &
forrest@0
   944
              '",',/'" 83 s 0.83prob c gray17 g4 gray17 g gray17 m gray17', &
forrest@0
   945
              '",',/'" 84 s 0.84prob c gray16 g4 gray16 g gray16 m gray16', &
forrest@0
   946
              '",',/'" 85 s 0.85prob c gray15 g4 gray15 g gray15 m gray15', &
forrest@0
   947
              '",',/'" 86 s 0.86prob c gray14 g4 gray14 g gray14 m gray14', &
forrest@0
   948
              '",',/'" 87 s 0.87prob c gray13 g4 gray13 g gray13 m gray13', &
forrest@0
   949
              '",',/'" 88 s 0.88prob c gray12 g4 gray12 g gray12 m gray12', &
forrest@0
   950
              '",',/'" 89 s 0.89prob c gray11 g4 gray11 g gray11 m gray11', &
forrest@0
   951
              '",',/'" 90 s 0.90prob c gray10 g4 gray10 g gray10 m gray10', &
forrest@0
   952
              '",',/'" 91 s 0.91prob c gray9 g4 gray9 g gray9 m gray9', &
forrest@0
   953
              '",',/'" 92 s 0.92prob c gray8 g4 gray8 g gray8 m gray8', &
forrest@0
   954
              '",',/'" 93 s 0.93prob c gray7 g4 gray7 g gray7 m gray7', &
forrest@0
   955
              '",',/'" 94 s 0.94prob c gray6 g4 gray6 g gray6 m gray6', &
forrest@0
   956
              '",',/'" 95 s 0.95prob c gray5 g4 gray5 g gray5 m gray5', &
forrest@0
   957
              '",',/'" 96 s 0.96prob c gray4 g4 gray4 g gray4 m gray4', &
forrest@0
   958
              '",',/'" 97 s 0.97prob c gray3 g4 gray3 g gray3 m gray3', &
forrest@0
   959
              '",',/'" 98 s 0.98prob c gray2 g4 gray2 g gray2 m gray2', &
forrest@0
   960
              '",',/'" 99 s 0.99prob c gray1 g4 gray1 g gray1 m gray1', &
forrest@0
   961
              '",',/'"100 s 1.00prob c black g4 black g black m black', &
forrest@0
   962
              '",',/'"101  c white    m white  s NotFlammable', &
forrest@0
   963
              '",',/'"102  c GreenYellow       m gray70  s LP0', &
forrest@0
   964
              '",',/'"103  c LawnGreen         m gray60 s LP1', &
forrest@0
   965
              '",',/'"104  c LimeGreen         m gray50   s LP2', &
forrest@0
   966
              '",',/'"105  c ForestGreen       m gray40   s LP3', &
forrest@0
   967
              '",',/'"106  c yellow    m gray80  s NF', &
forrest@0
   968
              '",',/'"107  c blue    m black  s water ",', &
forrest@0
   969
              /'/* pixels */')
forrest@0
   970
!
forrest@0
   971
!
forrest@0
   972
!      load fractal probability terrain into itemp array
forrest@0
   973
      do m = 1, numcells       ! over all cells
forrest@0
   974
         itemp(P(m,ihabs+1),P(m,ihabs+2)) = P(m,iz)
forrest@0
   975
      end do
forrest@0
   976
!
forrest@0
   977
!
forrest@0
   978
!  equations for slope and aspect are from:
forrest@0
   979
!  Horn, B.K.P., 1981.  Hill Shading and the Reflectance Map.
forrest@0
   980
!       Proceedings of the I.E.E.E., 69(1):14-47.
forrest@0
   981
!
forrest@0
   982
!  calculate and display aspect map
forrest@0
   983
!  set sun altitude above horizon in degrees (0 to 90)
forrest@0
   984
      alt = 40.
forrest@0
   985
!  set sun azimuth in degrees west of north (-1 to 360)
forrest@0
   986
      az = 105.
forrest@0
   987
      k = 0
forrest@0
   988
      do j = 0,irowcols
forrest@0
   989
         do i = 0,irowcols
forrest@0
   990
!
forrest@0
   991
            x = (itemp(i-1,j-1)+2.*itemp(i,j-1)+itemp(i+1,j-1) &
forrest@0
   992
                -itemp(i-1,j+1)-2.*itemp(i,j+1)-itemp(i+1,j+1)) &
forrest@0
   993
                /(8.0*30.0)
forrest@0
   994
!
forrest@0
   995
            y = (itemp(i-1,j-1)+2.*itemp(i-1,j)+itemp(i-1,j+1) &
forrest@0
   996
                -itemp(i+1,j-1)-2.*itemp(i+1,j)-itemp(i+1,j+1)) &
forrest@0
   997
                /(8.0*30.0)
forrest@0
   998
!
forrest@0
   999
            slope = 90. - atand(sqrt(x*x + y*y))
forrest@0
  1000
            if (x .eq. 0. .and. y .eq. 0.) then
forrest@0
  1001
               aspect = 360.
forrest@0
  1002
            else
forrest@0
  1003
               aspect = atan2d(x,y)
forrest@0
  1004
            end if
forrest@0
  1005
!
forrest@0
  1006
            c = sind(alt)*sind(slope) + cosd(alt)*cosd(slope)*cosd(az-aspect)
forrest@0
  1007
!
forrest@0
  1008
            if (c .le. 0.) then
forrest@0
  1009
               idispvec(k)=0
forrest@0
  1010
            else
forrest@0
  1011
               idispvec(k)=nint(c*100.)
forrest@0
  1012
            end if
forrest@0
  1013
!
forrest@0
  1014
!           flood with water if not painted
forrest@0
  1015
            if (itemp(i,j) .le. iflood) idispvec(k) = 107
forrest@0
  1016
!
forrest@0
  1017
!            print *, i,j,k,idispvec(k)
forrest@0
  1018
            k = k + 1
forrest@0
  1019
         end do
forrest@0
  1020
         write (fmt,'("(",I0,"(i3)$)")') irowcols+1
forrest@0
  1021
         write (50,'(2A$)') ' "'
forrest@0
  1022
         !write (50,108) (idispvec(l), l=0, irowcols)
forrest@0
  1023
         write (50,fmt) (idispvec(l), l=0, irowcols)
forrest@0
  1024
         write (50,'(2A)') '",'
forrest@0
  1025
!108      format (' "',<irowcols+1>(i3),'",')
forrest@0
  1026
         k = 0
forrest@0
  1027
      end do
forrest@0
  1028
!
forrest@0
  1029
      write (50,112)
forrest@0
  1030
112   format ('};')
forrest@0
  1031
      close (50)
forrest@0
  1032
!
forrest@0
  1033
      return
forrest@0
  1034
   end subroutine XPMRelief
forrest@0
  1035
!
forrest@0
  1036
!------------------------------------------------------------------------------
forrest@0
  1037
!
forrest@0
  1038
!**********************************************************
forrest@0
  1039
! The following were added by Forrest Hoffman
forrest@0
  1040
! Sat Apr  7 23:20:48 EDT 2007
forrest@0
  1041
!**********************************************************
forrest@0
  1042
!
forrest@0
  1043
!------------------------------------------------------------------------------
forrest@0
  1044
!
forrest@0
  1045
   real function cosd(angle)
forrest@0
  1046
      implicit none
forrest@0
  1047
      real, intent(in) :: angle
forrest@0
  1048
forrest@0
  1049
      cosd = cos(angle * rad_per_deg)
forrest@0
  1050
forrest@0
  1051
      return
forrest@0
  1052
   end function cosd
forrest@0
  1053
!
forrest@0
  1054
!------------------------------------------------------------------------------
forrest@0
  1055
!
forrest@0
  1056
   real function sind(angle)
forrest@0
  1057
      implicit none
forrest@0
  1058
      real, intent(in) :: angle
forrest@0
  1059
forrest@0
  1060
      sind = sin(angle * rad_per_deg)
forrest@0
  1061
forrest@0
  1062
      return
forrest@0
  1063
   end function sind
forrest@0
  1064
!
forrest@0
  1065
!------------------------------------------------------------------------------
forrest@0
  1066
!
forrest@0
  1067
   real function atand(x)
forrest@0
  1068
      implicit none
forrest@0
  1069
      real, intent(in) :: x
forrest@0
  1070
forrest@0
  1071
      atand = atan(x) * deg_per_rad
forrest@0
  1072
forrest@0
  1073
      return
forrest@0
  1074
   end function atand
forrest@0
  1075
!
forrest@0
  1076
!------------------------------------------------------------------------------
forrest@0
  1077
!
forrest@0
  1078
   real function atan2d(x, y)
forrest@0
  1079
      implicit none
forrest@0
  1080
      real, intent(in) :: x
forrest@0
  1081
      real, intent(in) :: y
forrest@0
  1082
forrest@0
  1083
      atan2d = atan2(x, y) * deg_per_rad
forrest@0
  1084
forrest@0
  1085
      return
forrest@0
  1086
   end function atan2d
forrest@0
  1087
!
forrest@0
  1088
!------------------------------------------------------------------------------
forrest@0
  1089
!
forrest@0
  1090
forrest@0
  1091
end module realizer_mod