wrf-fire /WPS/metgrid/src/rotate_winds_module.F

Language Fortran 77 Lines 596
MD5 Hash 35175581bc2258903df5d12db8ed1c82 Estimated Cost $10,838 (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
module rotate_winds_module

   use bitarray_module
   use constants_module
   use llxy_module
   use misc_definitions_module
   use module_debug

   integer :: orig_selected_projection

   contains

   !
   ! ARW Wind Rotation Code
   !

   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   ! Name: map_to_met                                                             !
   !                                                                              !
   ! Purpose: Rotate grid-relative winds to Earth-relative winds                  !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   subroutine map_to_met(u, u_mask, v, v_mask, &
                         us1, us2, ue1, ue2, &
                         vs1, vs2, ve1, ve2, &
                         xlon_u, xlon_v, xlat_u, xlat_v)

      implicit none

      ! Arguments
      integer, intent(in) :: us1, us2, ue1, ue2, vs1, vs2, ve1, ve2
      real, pointer, dimension(:,:) :: u, v, xlon_u, xlon_v, xlat_u, xlat_v
      type (bitarray), intent(in) :: u_mask, v_mask

      orig_selected_projection = iget_selected_domain()
      call select_domain(SOURCE_PROJ)
      call metmap_xform(u, u_mask, v, v_mask, &
                        us1, us2, ue1, ue2, &
                        vs1, vs2, ve1, ve2, &
                        xlon_u, xlon_v, xlat_u, xlat_v, 1)
      call select_domain(orig_selected_projection)

   end subroutine map_to_met

   
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   ! Name: met_to_map                                                             !
   !                                                                              !
   ! Purpose: Rotate Earth-relative winds to grid-relative winds                  !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   subroutine met_to_map(u, u_mask, v, v_mask, &
                         us1, us2, ue1, ue2, &
                         vs1, vs2, ve1, ve2, &
                         xlon_u, xlon_v, xlat_u, xlat_v)

      implicit none

      ! Arguments
      integer, intent(in) :: us1, us2, ue1, ue2, vs1, vs2, ve1, ve2
      real, pointer, dimension(:,:) :: u, v, xlon_u, xlon_v, xlat_u, xlat_v
      type (bitarray), intent(in) :: u_mask, v_mask

      orig_selected_projection = iget_selected_domain()
      call select_domain(1)
      call metmap_xform(u, u_mask, v, v_mask, &
                        us1, us2, ue1, ue2, &
                        vs1, vs2, ve1, ve2, &
                        xlon_u, xlon_v, xlat_u, xlat_v, -1)
      call select_domain(orig_selected_projection)

   end subroutine met_to_map

   
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   ! Name: metmap_xform                                                           !
   !                                                                              !
   ! Purpose: Do the actual work of rotating winds for C grid.                    !
   !          If idir= 1, rotate grid-relative winds to Earth-relative winds      !
   !          If idir=-1, rotate Earth-relative winds to grid-relative winds      !
   !                                                                              !
   ! ASSUMPTIONS: 1) MEMORY ORDER IS XY.                                          !
   !              2) U ARRAY HAS ONE MORE COLUMN THAN THE V ARRAY, AND V ARRAY    !
   !                 HAS ONE MORE ROW THAN U ARRAY.                               !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   subroutine metmap_xform(u, u_mask, v, v_mask, &
                           us1, us2, ue1, ue2, vs1, vs2, ve1, ve2, &
                           xlon_u, xlon_v, xlat_u, xlat_v, idir)

      implicit none

      ! Arguments
      integer, intent(in) :: us1, us2, ue1, ue2, vs1, vs2, ve1, ve2, idir
      real, pointer, dimension(:,:) :: u, v, xlon_u, xlon_v, xlat_u, xlat_v
      type (bitarray), intent(in) :: u_mask, v_mask

      ! Local variables
      integer :: i, j
      real :: u_weight, v_weight
      real :: u_map, v_map, alpha, diff
      real, pointer, dimension(:,:) :: u_new, v_new, u_mult, v_mult
      logical :: do_last_col_u, do_last_row_u, do_last_col_v, do_last_row_v

      ! If the proj_info structure has not been initialized, we don't have
      !   information about the projection and standard longitude.
      if (proj_stack(current_nest_number)%init) then

         ! Only rotate winds for Lambert conformal, polar stereographic, or Cassini
         if ((proj_stack(current_nest_number)%code == PROJ_LC) .or. &
             (proj_stack(current_nest_number)%code == PROJ_PS) .or. &
             (proj_stack(current_nest_number)%code == PROJ_CASSINI)) then
            call mprintf((idir ==  1),LOGFILE,'Rotating map winds to earth winds.')
            call mprintf((idir == -1),LOGFILE,'Rotating earth winds to grid winds')

            allocate(u_mult(us1:ue1,us2:ue2))
            allocate(v_mult(vs1:ve1,vs2:ve2))

            do j=us2,ue2
               do i=us1,ue1
                  if (bitarray_test(u_mask, i-us1+1, j-us2+1)) then
                     u_mult(i,j) = 1.
                  else
                     u_mult(i,j) = 0.
                  end if
               end do
            end do

            do j=vs2,ve2
               do i=vs1,ve1
                  if (bitarray_test(v_mask, i-vs1+1, j-vs2+1)) then
                     v_mult(i,j) = 1.
                  else
                     v_mult(i,j) = 0.
                  end if
               end do
            end do

            if (ue1-us1 == ve1-vs1) then
               do_last_col_u = .false.
               do_last_col_v = .true.
            else
               do_last_col_u = .true.
               do_last_col_v = .false.
            end if

            if (ue2-us2 == ve2-vs2) then
               do_last_row_u = .true.
               do_last_row_v = .false.
            else
               do_last_row_u = .false.
               do_last_row_v = .true.
            end if

            ! Create arrays to hold rotated winds
            allocate(u_new(us1:ue1, us2:ue2))
            allocate(v_new(vs1:ve1, vs2:ve2))

            ! Rotate U field
            do j=us2,ue2
               do i=us1,ue1

                  diff = idir * (xlon_u(i,j) - proj_stack(current_nest_number)%stdlon)
                  if (diff > 180.) then
                     diff = diff - 360.
                  else if (diff < -180.) then
                     diff = diff + 360.
                  end if

                  ! Calculate the rotation angle, alpha, in radians
                  if (proj_stack(current_nest_number)%code == PROJ_LC) then
                     alpha = diff * proj_stack(current_nest_number)%cone * rad_per_deg * proj_stack(current_nest_number)%hemi 
                  else if (proj_stack(current_nest_number)%code == PROJ_CASSINI) then
                     if (j == ue2) then
                        diff = xlon_u(i,j)-xlon_u(i,j-1)
                        if (diff > 180.) then
                           diff = diff - 360.
                        else if (diff < -180.) then
                           diff = diff + 360.
                        end if
                        alpha = atan2(   (-cos(xlat_u(i,j)*rad_per_deg) * diff*rad_per_deg),   &
                                                        (xlat_u(i,j)-xlat_u(i,j-1))*rad_per_deg    &
                                     )
                     else if (j == us2) then
                        diff = xlon_u(i,j+1)-xlon_u(i,j)
                        if (diff > 180.) then
                           diff = diff - 360.
                        else if (diff < -180.) then
                           diff = diff + 360.
                        end if
                        alpha = atan2(   (-cos(xlat_u(i,j)*rad_per_deg) * diff*rad_per_deg),   &
                                                        (xlat_u(i,j+1)-xlat_u(i,j))*rad_per_deg    &
                                     )
                     else
                        diff = xlon_u(i,j+1)-xlon_u(i,j-1)
                        if (diff > 180.) then
                           diff = diff - 360.
                        else if (diff < -180.) then
                           diff = diff + 360.
                        end if
                        alpha = atan2(   (-cos(xlat_u(i,j)*rad_per_deg) * diff*rad_per_deg),   &
                                                        (xlat_u(i,j+1)-xlat_u(i,j-1))*rad_per_deg    &
                                     )
                     end if
                  else
                     alpha = diff * rad_per_deg * proj_stack(current_nest_number)%hemi 
                  end if
                 
                  v_weight = 0.

                  ! On C grid, take U_ij, and get V value at the same lat/lon
                  !   by averaging the four surrounding V points
                  if (bitarray_test(u_mask, i-us1+1, j-us2+1)) then
                     u_map = u(i,j)
                     if (i == us1) then
                        if (j == ue2 .and. do_last_row_u) then
                           v_weight = v_mult(i,j)
                           v_map = v(i,j)*v_mult(i,j)
                        else
                           v_weight = v_mult(i,j) + v_mult(i,j+1)
                           v_map = v(i,j)*v_mult(i,j) + v(i,j+1)*v_mult(i,j+1)
                        end if 
                     else if (i == ue1 .and. do_last_col_u) then
                        if (j == ue2 .and. do_last_row_u) then
                           v_weight = v_mult(i-1,j)
                           v_map = v(i-1,j)
                        else
                           v_weight = v_mult(i-1,j) + v_mult(i-1,j+1) 
                           v_map = v(i-1,j)*v_mult(i-1,j) + v(i-1,j+1)*v_mult(i-1,j+1)
                        end if 
                     else if (j == ue2 .and. do_last_row_u) then
                        v_weight = v_mult(i-1,j-1) + v_mult(i,j-1)
                        v_map = v(i-1,j-1)*v_mult(i-1,j-1) + v(i,j-1)*v_mult(i,j-1)
                     else
                        v_weight = v_mult(i-1,j) + v_mult(i-1,j+1) + v_mult(i,j) + v_mult(i,j+1)
                        v_map = v(i-1,j)*v_mult(i-1,j) + v(i-1,j+1)*v_mult(i-1,j+1) + v(i,j)*v_mult(i,j) + v(i,j+1)*v_mult(i,j+1)
                     end if
                     if (v_weight > 0.) then
                        u_new(i,j) = cos(alpha)*u_map + sin(alpha)*v_map/v_weight
                     else
                        u_new(i,j) = u(i,j)
                     end if
                  else
                     u_new(i,j) = u(i,j)
                  end if

               end do
            end do

            ! Rotate V field
            do j=vs2,ve2
               do i=vs1,ve1

                  diff = idir * (xlon_v(i,j) - proj_stack(current_nest_number)%stdlon)
                  if (diff > 180.) then
                     diff = diff - 360.
                  else if (diff < -180.) then
                     diff = diff + 360.
                  end if

                  if (proj_stack(current_nest_number)%code == PROJ_LC) then
                     alpha = diff * proj_stack(current_nest_number)%cone * rad_per_deg * proj_stack(current_nest_number)%hemi 
                  else if (proj_stack(current_nest_number)%code == PROJ_CASSINI) then
                     if (j == ve2) then
                        diff = xlon_v(i,j)-xlon_v(i,j-1)
                        if (diff > 180.) then
                           diff = diff - 360.
                        else if (diff < -180.) then
                           diff = diff + 360.
                        end if
                        alpha = atan2(   (-cos(xlat_v(i,j)*rad_per_deg) * diff*rad_per_deg),   &
                                                        (xlat_v(i,j)-xlat_v(i,j-1))*rad_per_deg    &
                                     )
                     else if (j == vs2) then
                        diff = xlon_v(i,j+1)-xlon_v(i,j)
                        if (diff > 180.) then
                           diff = diff - 360.
                        else if (diff < -180.) then
                           diff = diff + 360.
                        end if
                        alpha = atan2(   (-cos(xlat_v(i,j)*rad_per_deg) * diff*rad_per_deg),   &
                                                        (xlat_v(i,j+1)-xlat_v(i,j))*rad_per_deg    &
                                     )
                     else
                        diff = xlon_v(i,j+1)-xlon_v(i,j-1)
                        if (diff > 180.) then
                           diff = diff - 360.
                        else if (diff < -180.) then
                           diff = diff + 360.
                        end if
                        alpha = atan2(   (-cos(xlat_v(i,j)*rad_per_deg) * diff*rad_per_deg),   &
                                                        (xlat_v(i,j+1)-xlat_v(i,j-1))*rad_per_deg    &
                                     )
                     end if
                  else
                     alpha = diff * rad_per_deg * proj_stack(current_nest_number)%hemi 
                  end if

                  u_weight = 0.

                  if (bitarray_test(v_mask, i-vs1+1, j-vs2+1)) then
                     v_map = v(i,j)
                     if (j == vs2) then
                        if (i == ve1 .and. do_last_col_v) then
                           u_weight = u_mult(i,j)
                           u_map = u(i,j)*u_mult(i,j)
                        else
                           u_weight = u_mult(i,j) + u_mult(i+1,j)
                           u_map = u(i,j)*u_mult(i,j) + u(i+1,j)*u_mult(i+1,j)
                        end if 
                     else if (j == ve2 .and. do_last_row_v) then
                        if (i == ve1 .and. do_last_col_v) then
                           u_weight = u_mult(i,j-1)
                           u_map = u(i,j-1)*u_mult(i,j-1)
                        else
                           u_weight = u_mult(i,j-1) + u_mult(i+1,j-1)
                           u_map = u(i,j-1)*u_mult(i,j-1) + u(i+1,j-1)*u_mult(i+1,j-1)
                        end if 
                     else if (i == ve1 .and. do_last_col_v) then
                        u_weight = u_mult(i,j) + u_mult(i,j-1)
                        u_map = u(i,j)*u_mult(i,j) + u(i,j-1)*u_mult(i,j-1)
                     else
                        u_weight = u_mult(i,j-1) + u_mult(i,j) + u_mult(i+1,j-1) + u_mult(i+1,j)
                        u_map = u(i,j-1)*u_mult(i,j-1) + u(i,j)*u_mult(i,j) + u(i+1,j-1)*u_mult(i+1,j-1) + u(i+1,j)*u_mult(i+1,j)
                     end if
                     if (u_weight > 0.) then
                        v_new(i,j) = -sin(alpha)*u_map/u_weight + cos(alpha)*v_map
                     else
                        v_new(i,j) = v(i,j)
                     end if
                  else
                     v_new(i,j) = v(i,j)
                  end if

               end do
            end do

            ! Copy rotated winds back into argument arrays
            u = u_new 
            v = v_new 

            deallocate(u_new)
            deallocate(v_new)
            deallocate(u_mult)
            deallocate(v_mult)
         end if

      else
         call mprintf(.true.,ERROR,'In metmap_xform(), uninitialized proj_info structure.')
      end if
 
   end subroutine metmap_xform


   !
   ! NMM Wind Rotation Code
   !

   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   ! Name: map_to_met_nmm                                                         !
   !                                                                              !
   ! Purpose: Rotate grid-relative winds to Earth-relative winds                  !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   subroutine map_to_met_nmm(u, u_mask, v, v_mask, &
                             vs1, vs2, ve1, ve2, &
                             xlat_v, xlon_v)

      implicit none

      ! Arguments
      integer, intent(in) :: vs1, vs2, ve1, ve2
      real, pointer, dimension(:,:) :: u, v, xlat_v, xlon_v
      type (bitarray), intent(in) :: u_mask, v_mask

      orig_selected_projection = iget_selected_domain()
      call select_domain(SOURCE_PROJ)
      call metmap_xform_nmm(u, u_mask, v, v_mask, &
                            vs1, vs2, ve1, ve2, &
                            xlat_v, xlon_v, 1)
      call select_domain(orig_selected_projection)

   end subroutine map_to_met_nmm

   
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   ! Name: met_to_map_nmm                                                         !
   !                                                                              !
   ! Purpose: Rotate Earth-relative winds to grid-relative winds                  !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
   subroutine met_to_map_nmm(u, u_mask, v, v_mask, &
                             vs1, vs2, ve1, ve2, &
                             xlat_v, xlon_v)

      implicit none

      ! Arguments
      integer, intent(in) :: vs1, vs2, ve1, ve2
      real, pointer, dimension(:,:) :: u, v, xlat_v, xlon_v
      type (bitarray), intent(in) :: u_mask, v_mask

      orig_selected_projection = iget_selected_domain()
      call select_domain(1)
      call metmap_xform_nmm(u, u_mask, v, v_mask, &
                            vs1, vs2, ve1, ve2, &
                            xlat_v, xlon_v, -1)
      call select_domain(orig_selected_projection)

   end subroutine met_to_map_nmm


   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   ! Name: metmap_xform_nmm                                                       !
   !                                                                              !
   ! Purpose: Do the actual work of rotating winds for E grid.                    !
   !          If idir= 1, rotate grid-relative winds to Earth-relative winds      !
   !          If idir=-1, rotate Earth-relative winds to grid-relative winds      !
   !                                                                              !
   ! ASSUMPTIONS: 1) MEMORY ORDER IS XY.                                          !
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   subroutine metmap_xform_nmm(u, u_mask, v, v_mask, &
                               vs1, vs2, ve1, ve2, &
                               xlat_v, xlon_v, idir)

      implicit none

      ! Arguments
      integer, intent(in) :: vs1, vs2, ve1, ve2, idir
      real, pointer, dimension(:,:) :: u, v, xlat_v, xlon_v
      type (bitarray), intent(in) :: u_mask, v_mask

      ! Local variables
      integer :: i, j
      real :: u_map, v_map, diff, alpha
      real :: phi0, lmbd0, big_denominator, relm, rlat_v,rlon_v, clontemp
      real :: sin_phi0, cos_phi0,  cos_alpha, sin_alpha
      real, pointer, dimension(:,:) :: u_new, v_new


      ! If the proj_info structure has not been initialized, we don't have
      !   information about the projection and standard longitude.
      if (proj_stack(current_nest_number)%init) then

         if (proj_stack(current_nest_number)%code == PROJ_ROTLL) then

            call mprintf((idir ==  1),LOGFILE,'Rotating map winds to earth winds.')
            call mprintf((idir == -1),LOGFILE,'Rotating earth winds to grid winds')
   
            ! Create arrays to hold rotated winds
            allocate(u_new(vs1:ve1, vs2:ve2))
            allocate(v_new(vs1:ve1, vs2:ve2))

            phi0  = proj_stack(current_nest_number)%lat1 * rad_per_deg

            clontemp= proj_stack(current_nest_number)%lon1

            if (clontemp .lt. 0.) then
               lmbd0 = (clontemp + 360.) * rad_per_deg
            else
               lmbd0 = (clontemp) * rad_per_deg
            endif

            sin_phi0  = sin(phi0)
            cos_phi0  = cos(phi0)

            do j=vs2,ve2
               do i=vs1,ve1

                  ! Calculate the sine and cosine of rotation angle
                  rlat_v = xlat_v(i,j) * rad_per_deg
                  rlon_v = xlon_v(i,j) * rad_per_deg
                  relm = rlon_v - lmbd0
                  big_denominator = cos(asin( &
                                       cos_phi0 * sin(rlat_v) - &
                                       sin_phi0 * cos(rlat_v) * cos(relm) &
                                        )   )

                  sin_alpha = sin_phi0 * sin(relm)  /  &
                                         big_denominator

                  cos_alpha = (cos_phi0 * cos(rlat_v) + &
                               sin_phi0 * sin(rlat_v) * cos(relm))  /  &
                                            big_denominator
   
                  ! Rotate U field
                  if (bitarray_test(u_mask, i-vs1+1, j-vs2+1)) then
                     u_map = u(i,j)
                     if (bitarray_test(v_mask, i-vs1+1, j-vs2+1)) then
                        v_map = v(i,j)
                     else
                        v_map = 0.
                     end if
                     
                     u_new(i,j) = cos_alpha*u_map + idir*sin_alpha*v_map
                  else
                     u_new(i,j) = u(i,j)
                  end if
                       
                  ! Rotate V field
                  if (bitarray_test(v_mask, i-vs1+1, j-vs2+1)) then
                     v_map = v(i,j)
                     if (bitarray_test(u_mask, i-vs1+1, j-vs2+1)) then
                        u_map = u(i,j)
                     else
                        u_map = 0.
                     end if
                     
                     v_new(i,j) = -idir*sin_alpha*u_map + cos_alpha*v_map
                  else
                     v_new(i,j) = v(i,j)
                  end if
   
               end do
            end do

            ! Copy rotated winds back into argument arrays
            u = u_new 
            v = v_new 
   
            deallocate(u_new)
            deallocate(v_new)
   
         ! Only rotate winds for Lambert conformal, polar stereographic, or Cassini
         else if ((proj_stack(current_nest_number)%code == PROJ_LC) .or. &
                  (proj_stack(current_nest_number)%code == PROJ_PS) .or. &
                  (proj_stack(current_nest_number)%code == PROJ_CASSINI)) then

            call mprintf((idir ==  1),LOGFILE,'Rotating map winds to earth winds.')
            call mprintf((idir == -1),LOGFILE,'Rotating earth winds to grid winds')

            ! Create arrays to hold rotated winds
            allocate(u_new(vs1:ve1, vs2:ve2))
            allocate(v_new(vs1:ve1, vs2:ve2))

            do j=vs2,ve2
               do i=vs1,ve1

                  diff = idir * (xlon_v(i,j) - proj_stack(current_nest_number)%stdlon)
                  if (diff > 180.) then
                     diff = diff - 360.
                  else if (diff < -180.) then
                     diff = diff + 360.
                  end if

                  ! Calculate the rotation angle, alpha, in radians
                  if (proj_stack(current_nest_number)%code == PROJ_LC) then
                     alpha = diff * proj_stack(current_nest_number)%cone * &
                             rad_per_deg * proj_stack(current_nest_number)%hemi
                  else
                     alpha = diff * rad_per_deg * proj_stack(current_nest_number)%hemi 
                  end if

                  ! Rotate U field
                  if (bitarray_test(u_mask, i-vs1+1, j-vs2+1)) then
                     u_map = u(i,j)
                     if (bitarray_test(v_mask, i-vs1+1, j-vs2+1)) then
                        v_map = v(i,j)
                     else
                        v_map = 0.
                     end if
                     
                     u_new(i,j) = cos(alpha)*u_map + idir*sin(alpha)*v_map
                  else
                     u_new(i,j) = u(i,j)
                  end if
                       
                  ! Rotate V field
                  if (bitarray_test(v_mask, i-vs1+1, j-vs2+1)) then
                     v_map = v(i,j)
                     if (bitarray_test(u_mask, i-vs1+1, j-vs2+1)) then
                        u_map = u(i,j)
                     else
                        u_map = 0.
                     end if
                     
                     v_new(i,j) = -idir*sin(alpha)*u_map + cos(alpha)*v_map
                  else
                     v_new(i,j) = v(i,j)
                  end if

               end do
            end do

            ! Copy rotated winds back into argument arrays
            u = u_new 
            v = v_new 
   
            deallocate(u_new)
            deallocate(v_new)

         end if

      else
         call mprintf(.true.,ERROR,'In metmap_xform_nmm(), uninitialized proj_info structure.')
      end if

   end subroutine metmap_xform_nmm

end module rotate_winds_module
Back to Top