NADCON5-ng  0.0.2
NADCON5 Next Generation Documentation
mymedian5.f
Go to the documentation of this file.
1 c> \ingroup doers
2 c> \if MANPAGE
3 c> \page mymedian5
4 c> \endif
5 c>
6 c> Program to filter Map Data for GMT Plotting
7 c>
8 c> 1. Run a customized block-median thinning
9 c> algorithm on coordinate differences (horizontal
10 c> and ellipsoid height)
11 c> 2. Save the thinned data to GMT-ready plottable files
12 c> 3. Save the removed data to GMT-ready plottable files
13 c> 4. Create a GMT batch file (gmtbat02) to grid the thinned data at T=0.4 (which
14 c> becomes the final transformation grid) and also at T=1.0 and
15 c> T=0.0, (whose difference becomes the bases for the
16 c> "method noise" grid)
17 c> 5. If, and only if, we are doing the combination of
18 c> HARN/FBN/CONUS, insert commands into gmtbat02
19 c> to apply a mask to the "04.b" grids (See DRU-12, p. 36-37)
20 c> See DRU-11, p. 127
21 c>
22 c> unlike `mymedian.f`, this program is
23 c> set up to filter/process all data
24 c> at once in one run. Also, significant
25 c> philosophical changes occurred, including:
26 c>
27 c> 1. Median filter on absolute horizontal length, and
28 c> then, when we have our "kept" points, we use
29 c> the lat and lon of those kept points to grid
30 c> lat and lon separately. (mymedian.f actually
31 c> sorted lat medians and lon medians separately,
32 c> raising the very real possibility that separate
33 c> points would go into each grid.)
34 c> 2. Nothing RANDOM! No coin flipping, etc. It was
35 c> viewed, for NADCON 5.0, as scientifically
36 c> improper for the final grids to be reliant upon
37 c> a filtering mechanism that could be different
38 c> each time it was run.
39 c>
40 c> ### Program arguments
41 c> Arguments are newline terminated and read from standard input
42 c>
43 c> They are enumerated here
44 c> \param oldtm Source Datum
45 c> \param newdtm Target Datum,region
46 c> \param region Conversion Region
47 c> \param agridsec Grid Spacing in Arc Seconds
48 c>
49 c> ### Program Inputs:
50 c>
51 c> ## Changelog
52 c>
53 c> ### 2016 09 14
54 c> Bug found when running Hawaii with points outside grid -- some mixup
55 c> between "ikt" and "ipid". Changed this program as follows: It now
56 c> REQUIRES that the incoming data has NO points outside the grid
57 c> boundary. This has been forced by giving such points a "444" reject
58 c> code in "makework" when creating the work file. By going through
59 c> "makeplotfiles01" with a "444" reject, those points won't even make
60 c> it into the "all" file, which is our input for median filtering here...
61 c>
62 c> ### 2016 06 29
63 c> Updated to insert masking commands for the "...04.b" (transformation
64 c> grid) into gmtbat02 when working ONLY in the HARN/FBN/CONUS combination.
65 c>
66 c> ### 2015 10 27
67 c> For use in creating NADCON5
68 c> Built by Dru Smith
69 c> Built from scratch, scrapping all previous
70 c> "mymedian" programs used in making GEOCON
71  program mymedian5
72 
73  implicit real*8(a-h,o-z)
74 
75 c - Updated to allow up to 280,000 points to come in
76 c - which also translates to letting 280,000 grid cells
77 c - to be non-empty.
78  parameter(max=280000)
79 c - And using a little hit or miss logic, the number
80 c - of possible points in one cell can't be more than
81 c - about 100, right?
82  parameter(maxppcell=5000)
83 
84 c - Holding place for the values themselves, in read-order
85  dimension zs(max)
86 
87 c - Pass/nopass flag for all data, in read-order
88  logical lpass(max)
89 
90 c - Holding place for each record, in read-order
91  character*80 cards(max) ! 2016 09 14
92 
93 c - Index of what latititude row and longitude column
94 c - is associated with each non-empty cell, in
95 c - cell-found order.
96 c - 1 <= ilapcell() <= nla
97 c - 1 <= ilopcell() <= nlo
98  integer*4 ilapcell(max),ilopcell(max)
99  integer*4 poscode(max)
100  integer*4 indx(max)
101 c - Distance of a point from its cell's center
102  real*8 dist(max)
103 
104 c - Index telling me which cell a given point is
105 c - associated with, in read-order
106 c - 1 <= whichcell() <= ncells
107  integer*4 whichcell(max)
108 
109 c - ppcell(i) contains the count of how many values fall
110 c - in cell "i", where "i" ratchets up from 1 to max
111 c - every time I find a new non-empty cell.
112  integer*4 ppcell(max)
113 
114  character*10 olddtm,newdtm,region
115  character*200 suffix1,suffix2
116  character*200 suffix2t00,suffix2t04,suffix2t10
117  character*200 suffix2d1,suffix2d2,suffix2d3
118 c - Input GMT-ready files:
119  character*200 gfncvacdlat
120  character*200 gfncvacdlon
121  character*200 gfncvacdeht
122  character*200 gfnvmacdlat
123  character*200 gfnvmacdlon
124  character*200 gfnvmacdeht
125  character*200 gfnvmacdhor
126  character*200 gfnvsacdlat
127  character*200 gfnvsacdlon
128  character*200 gfnvsacdhor
129 c - Output thinned GMT-ready files:
130  character*200 gfncvtcdlat
131  character*200 gfncvtcdlon
132  character*200 gfncvtcdeht
133  character*200 gfnvmtcdlat
134  character*200 gfnvmtcdlon
135  character*200 gfnvmtcdeht
136  character*200 gfnvmtcdhor
137  character*200 gfnvstcdlat
138  character*200 gfnvstcdlon
139  character*200 gfnvstcdhor
140 c - Output dropped GMT-ready files:
141  character*200 gfncvdcdlat
142  character*200 gfncvdcdlon
143  character*200 gfncvdcdeht
144  character*200 gfnvmdcdlat
145  character*200 gfnvmdcdlon
146  character*200 gfnvmdcdeht
147  character*200 gfnvmdcdhor
148  character*200 gfnvsdcdlat
149  character*200 gfnvsdcdlon
150  character*200 gfnvsdcdhor
151 c - Output thinned GMT(surface)-ready files:
152  character*200 gfnsmtcdlat
153  character*200 gfnsmtcdlon
154  character*200 gfnsmtcdeht
155  character*200 gfnsstcdlat
156  character*200 gfnsstcdlon
157 c - To be created ".grd" files by surface
158  character*200 rfnvmtcdlat04
159  character*200 rfnvmtcdlon04
160  character*200 rfnvmtcdeht04
161  character*200 rfnvstcdlat04
162  character*200 rfnvstcdlon04
163 
164  character*200 rfnvmtcdlat00
165  character*200 rfnvmtcdlon00
166  character*200 rfnvmtcdeht00
167  character*200 rfnvstcdlat00
168  character*200 rfnvstcdlon00
169 
170  character*200 rfnvmtcdlat10
171  character*200 rfnvmtcdlon10
172  character*200 rfnvmtcdeht10
173  character*200 rfnvstcdlat10
174  character*200 rfnvstcdlon10
175 
176 c character*200 rfnvmtcdlat
177 c character*200 rfnvmtcdlon
178 c character*200 rfnvmtcdeht
179 c character*200 rfnvstcdlat
180 c character*200 rfnvstcdlon
181 
182 c - To be created ".xyz" files by GMT's grd2xyz program
183  character*200 zfnvmtcdlat04
184  character*200 zfnvmtcdlon04
185  character*200 zfnvmtcdeht04
186  character*200 zfnvstcdlat04
187  character*200 zfnvstcdlon04
188 
189  character*200 zfnvmtcdlat00
190  character*200 zfnvmtcdlon00
191  character*200 zfnvmtcdeht00
192  character*200 zfnvstcdlat00
193  character*200 zfnvstcdlon00
194 
195  character*200 zfnvmtcdlat10
196  character*200 zfnvmtcdlon10
197  character*200 zfnvmtcdeht10
198  character*200 zfnvstcdlat10
199  character*200 zfnvstcdlon10
200 
201 c character*200 zfnvmtcdlat
202 c character*200 zfnvmtcdlon
203 c character*200 zfnvmtcdeht
204 c character*200 zfnvstcdlat
205 c character*200 zfnvstcdlon
206 
207 c - To be created ".b" files by grd2b
208  character*200 bfnvmtcdlat04
209  character*200 bfnvmtcdlon04
210  character*200 bfnvmtcdeht04
211  character*200 bfnvstcdlat04
212  character*200 bfnvstcdlon04
213 
214  character*200 bfnvmtcdlat00
215  character*200 bfnvmtcdlon00
216  character*200 bfnvmtcdeht00
217  character*200 bfnvstcdlat00
218  character*200 bfnvstcdlon00
219 
220  character*200 bfnvmtcdlat10
221  character*200 bfnvmtcdlon10
222  character*200 bfnvmtcdeht10
223  character*200 bfnvstcdlat10
224  character*200 bfnvstcdlon10
225 
226 c - Difference grids to be used in creation of the "method noise" grid
227  character*200 dbfnvmtcdlat1000
228  character*200 dbfnvstcdlat1000
229  character*200 dbfnvmtcdlon1000
230  character*200 dbfnvstcdlon1000
231  character*200 dbfnvmtcdeht1000
232 
233  character*200 adbfnvmtcdlat1000
234  character*200 adbfnvstcdlat1000
235  character*200 adbfnvmtcdlon1000
236  character*200 adbfnvstcdlon1000
237  character*200 adbfnvmtcdeht1000
238 
239  character*200 sadbfnvmtcdlat1000
240  character*200 sadbfnvstcdlat1000
241  character*200 sadbfnvmtcdlon1000
242  character*200 sadbfnvstcdlon1000
243  character*200 sadbfnvmtcdeht1000
244 
245  character*200 sadrfnvmtcdlat1000
246  character*200 sadrfnvstcdlat1000
247  character*200 sadrfnvmtcdlon1000
248  character*200 sadrfnvstcdlon1000
249  character*200 sadrfnvmtcdeht1000
250 
251 c character*200 bfnvmtcdlat
252 c character*200 bfnvmtcdlon
253 c character*200 bfnvmtcdeht
254 c character*200 bfnvstcdlat
255 c character*200 bfnvstcdlon
256 c - Dummy
257  character*200 fused
258 c - Output GMT-batch file:
259  character*200 gmtfile
260 c - Grid spacing, converted to characters for file names
261 c - Presumes:
262 c - 1) igridsec is an integer
263 c - 2) 1 <= igridsec <= 99999
264  integer*4 igridsec
265  character*5 agridsec
266 
267  character*6 pid
268  character*80 card
269  character*80 cardcvlat,cardcvlon,cardcveht
270  character*80 cardvmlat,cardvmlon,cardvmeht,cardvmhor
271  character*80 cardvslat,cardvslon, cardvshor
272 
273 
274 c --------------------------------------
275 c --------------------------------------
276 c --------------------------------------
277 c -------BEGIN GUTS OF PROGRAM----------
278 c --------------------------------------
279 c --------------------------------------
280 c --------------------------------------
281  write(6,1001)
282  1001 format('BEGIN program mymedian5.f')
283 
284 c ------------------------------------------------------------------
285 c - Some required constants.
286 c ------------------------------------------------------------------
287  pi = 2.d0*dasin(1.d0)
288  d2r = pi/180.d0
289  rng2std = 1.d0/1.6929d0
290 
291 c ------------------------------------------------------------------
292 c - Read in arguments from batch file
293 c ------------------------------------------------------------------
294  read(5,'(a)')olddtm
295  read(5,'(a)')newdtm
296  read(5,'(a)')region
297  read(5,'(a)')agridsec
298 
299 c ------------------------------------------------------------------
300 c - Generate the suffixes used in all our files
301 c ------------------------------------------------------------------
302  read(agridsec,*)igridsec
303  suffix1=trim(olddtm)//'.'//trim(newdtm)//'.'//trim(region)
304  suffix2=trim(suffix1)//'.'//trim(agridsec)
305 
306  suffix2t00=trim(suffix2)//'.00'
307  suffix2t04=trim(suffix2)//'.04'
308  suffix2t10=trim(suffix2)//'.10'
309 
310  suffix2d1=trim(suffix2)//'.d1'
311  suffix2d2=trim(suffix2)//'.d2'
312  suffix2d3=trim(suffix2)//'.d3'
313 
314 
315 c ------------------------------------------------------------------
316 c - Open the GMT batch file for gridding the thinned data
317 c ------------------------------------------------------------------
318  gmtfile = 'gmtbat02.'//trim(suffix2)
319  open(99,file=gmtfile,status='new',form='formatted')
320  write(6,1011)trim(gmtfile)
321  1011 format(6x,'mymedian5.f: Creating GMT batch file ',a)
322  write(99,1030)trim(gmtfile)
323  1030 format('echo BEGIN batch file ',a)
324 
325 
326 c ------------------------------------------------------------------
327 c - Open the input, pre-thinned, GMT-ready files
328 c ------------------------------------------------------------------
329 
330 c - Coverage (11-13):
331  gfncvacdlat = 'cvacdlat.'//trim(suffix1)
332  open(11,file=gfncvacdlat,status='old',form='formatted')
333  write(6,1010)trim(gfncvacdlat)
334 
335  gfncvacdlon = 'cvacdlon.'//trim(suffix1)
336  open(12,file=gfncvacdlon,status='old',form='formatted')
337  write(6,1010)trim(gfncvacdlon)
338 
339  gfncvacdeht = 'cvacdeht.'//trim(suffix1)
340  open(13,file=gfncvacdeht,status='old',form='formatted')
341  write(6,1010)trim(gfncvacdeht)
342 
343 
344 c - Vectors, Meters (21-24):
345  gfnvmacdlat = 'vmacdlat.'//trim(suffix1)
346  open(21,file=gfnvmacdlat,status='old',form='formatted')
347  write(6,1010)trim(gfnvmacdlat)
348 
349  gfnvmacdlon = 'vmacdlon.'//trim(suffix1)
350  open(22,file=gfnvmacdlon,status='old',form='formatted')
351  write(6,1010)trim(gfnvmacdlon)
352 
353  gfnvmacdeht = 'vmacdeht.'//trim(suffix1)
354  open(23,file=gfnvmacdeht,status='old',form='formatted')
355  write(6,1010)trim(gfnvmacdeht)
356 
357  gfnvmacdhor = 'vmacdhor.'//trim(suffix1)
358  open(24,file=gfnvmacdhor,status='old',form='formatted')
359  write(6,1010)trim(gfnvmacdhor)
360 
361 c - Vectors, Seconds (26-29, skipping 28):
362  gfnvsacdlat = 'vsacdlat.'//trim(suffix1)
363  open(26,file=gfnvsacdlat,status='old',form='formatted')
364  write(6,1010)trim(gfnvsacdlat)
365 
366  gfnvsacdlon = 'vsacdlon.'//trim(suffix1)
367  open(27,file=gfnvsacdlon,status='old',form='formatted')
368  write(6,1010)trim(gfnvsacdlon)
369 
370  gfnvsacdhor = 'vsacdhor.'//trim(suffix1)
371  open(29,file=gfnvsacdhor,status='old',form='formatted')
372  write(6,1010)trim(gfnvsacdhor)
373 
374 
375  1010 format(6x,'mymedian5.f: ',
376  *'Opening existing unthinned file ',a)
377 
378 c ------------------------------------------------------------------
379 c - Open the output, thinned, GMT-ready files
380 c - "t" meaning "thinned"
381 c ------------------------------------------------------------------
382 
383 c - Coverage (31-33):
384  gfncvtcdlat = 'cvtcdlat.'//trim(suffix2)
385  open(31,file=gfncvtcdlat,status='new',form='formatted')
386  write(6,1022)trim(gfncvtcdlat)
387 
388  gfncvtcdlon = 'cvtcdlon.'//trim(suffix2)
389  open(32,file=gfncvtcdlon,status='new',form='formatted')
390  write(6,1022)trim(gfncvtcdlon)
391 
392  gfncvtcdeht = 'cvtcdeht.'//trim(suffix2)
393  open(33,file=gfncvtcdeht,status='new',form='formatted')
394  write(6,1022)trim(gfncvtcdeht)
395 
396 c - Vectors, Meters (41-44):
397  gfnvmtcdlat = 'vmtcdlat.'//trim(suffix2)
398  open(41,file=gfnvmtcdlat,status='new',form='formatted')
399  write(6,1012)trim(gfnvmtcdlat)
400 
401  gfnvmtcdlon = 'vmtcdlon.'//trim(suffix2)
402  open(42,file=gfnvmtcdlon,status='new',form='formatted')
403  write(6,1012)trim(gfnvmtcdlon)
404 
405  gfnvmtcdeht = 'vmtcdeht.'//trim(suffix2)
406  open(43,file=gfnvmtcdeht,status='new',form='formatted')
407  write(6,1012)trim(gfnvmtcdeht)
408 
409  gfnvmtcdhor = 'vmtcdhor.'//trim(suffix2)
410  open(44,file=gfnvmtcdhor,status='new',form='formatted')
411  write(6,1012)trim(gfnvmtcdhor)
412 
413 c - Vectors, Seconds (46-49, skipping 48):
414  gfnvstcdlat = 'vstcdlat.'//trim(suffix2)
415  open(46,file=gfnvstcdlat,status='new',form='formatted')
416  write(6,1012)trim(gfnvstcdlat)
417 
418  gfnvstcdlon = 'vstcdlon.'//trim(suffix2)
419  open(47,file=gfnvstcdlon,status='new',form='formatted')
420  write(6,1012)trim(gfnvstcdlon)
421 
422  gfnvstcdhor = 'vstcdhor.'//trim(suffix2)
423  open(49,file=gfnvstcdhor,status='new',form='formatted')
424  write(6,1012)trim(gfnvstcdhor)
425 
426 
427  1012 format(6x,'mymedian5.f: ',
428  *'Opening output thinned vector file ',a)
429  1022 format(6x,'mymedian5.f: ',
430  *'Opening output thinned coverage file ',a)
431 
432 c ------------------------------------------------------------------
433 c - Open the output, dropped, GMT-ready files
434 c - "d" meaning "dropped"
435 c ------------------------------------------------------------------
436 
437 c - Coverage (51-53):
438  gfncvdcdlat = 'cvdcdlat.'//trim(suffix2)
439  open(51,file=gfncvdcdlat,status='new',form='formatted')
440  write(6,1023)trim(gfncvdcdlat)
441 
442  gfncvdcdlon = 'cvdcdlon.'//trim(suffix2)
443  open(52,file=gfncvdcdlon,status='new',form='formatted')
444  write(6,1023)trim(gfncvdcdlon)
445 
446  gfncvdcdeht = 'cvdcdeht.'//trim(suffix2)
447  open(53,file=gfncvdcdeht,status='new',form='formatted')
448  write(6,1023)trim(gfncvdcdeht)
449 
450 c - Vectors, Meters (61-64):
451  gfnvmdcdlat = 'vmdcdlat.'//trim(suffix2)
452  open(61,file=gfnvmdcdlat,status='new',form='formatted')
453  write(6,1013)trim(gfnvmdcdlat)
454 
455  gfnvmdcdlon = 'vmdcdlon.'//trim(suffix2)
456  open(62,file=gfnvmdcdlon,status='new',form='formatted')
457  write(6,1013)trim(gfnvmdcdlon)
458 
459  gfnvmdcdeht = 'vmdcdeht.'//trim(suffix2)
460  open(63,file=gfnvmdcdeht,status='new',form='formatted')
461  write(6,1013)trim(gfnvmdcdeht)
462 
463  gfnvmdcdhor = 'vmdcdhor.'//trim(suffix2)
464  open(64,file=gfnvmdcdhor,status='new',form='formatted')
465  write(6,1013)trim(gfnvmdcdhor)
466 
467 c - Vectors, Seconds (66-69, skippin 68):
468  gfnvsdcdlat = 'vsdcdlat.'//trim(suffix2)
469  open(66,file=gfnvsdcdlat,status='new',form='formatted')
470  write(6,1013)trim(gfnvsdcdlat)
471 
472  gfnvsdcdlon = 'vsdcdlon.'//trim(suffix2)
473  open(67,file=gfnvsdcdlon,status='new',form='formatted')
474  write(6,1013)trim(gfnvsdcdlon)
475 
476  gfnvsdcdhor = 'vsdcdhor.'//trim(suffix2)
477  open(69,file=gfnvsdcdhor,status='new',form='formatted')
478  write(6,1013)trim(gfnvsdcdhor)
479 
480 
481  1013 format(6x,'mymedian5.f: ',
482  *'Opening output dropped vector file ',a)
483  1023 format(6x,'mymedian5.f: ',
484  *'Opening output dropped coverage file ',a)
485 
486 c ------------------------------------------------------------------
487 c - Open the output, thinned, GMT-ready files for pushing
488 c - through "surface" to get a grid.
489 c - "t" meaning "thinned"
490 c ------------------------------------------------------------------
491 
492 c - Surface ready vectors, meters (71-73):
493  gfnsmtcdlat = 'smtcdlat.'//trim(suffix2)
494  open(71,file=gfnsmtcdlat,status='new',form='formatted')
495  write(6,1014)trim(gfnsmtcdlat)
496 
497  gfnsmtcdlon = 'smtcdlon.'//trim(suffix2)
498  open(72,file=gfnsmtcdlon,status='new',form='formatted')
499  write(6,1014)trim(gfnsmtcdlon)
500 
501  gfnsmtcdeht = 'smtcdeht.'//trim(suffix2)
502  open(73,file=gfnsmtcdeht,status='new',form='formatted')
503  write(6,1014)trim(gfnsmtcdeht)
504 
505 c - Surface ready vectors, seconds (76-77):
506  gfnsstcdlat = 'sstcdlat.'//trim(suffix2)
507  open(76,file=gfnsstcdlat,status='new',form='formatted')
508  write(6,1014)trim(gfnsstcdlat)
509 
510  gfnsstcdlon = 'sstcdlon.'//trim(suffix2)
511  open(77,file=gfnsstcdlon,status='new',form='formatted')
512  write(6,1014)trim(gfnsstcdlon)
513 
514 
515  1014 format(6x,'mymedian5.f: ',
516  *'Opening output thinned vector file (for surface):',a)
517 
518 c ------------------------------------------------------------------
519 c - Each region has officially chosen boundaries (which may
520 c - or may not agree with the MAP boundaries). Get the
521 c - official grid boundaries here. See DRU-11, p. 126
522 c ------------------------------------------------------------------
523  call getgridbounds(region,glamx,glamn,glomn,glomx)
524 
525  write(6,1004)trim(region),glamn,glamx,glomn,glomx
526  1004 format(6x,'mymedian5.f: Region= ',a,/,
527  * 6x,'mymedian5.f: North = ',f12.6,/,
528  * 6x,'mymedian5.f: South = ',f12.6,/,
529  * 6x,'mymedian5.f: West = ',f12.6,/,
530  * 6x,'mymedian5.f: East = ',f12.6)
531 
532 c -------------------------------------------------------
533 c - Get the header information necessary for a ".b" file
534 c - 1) Convert "igridsec" to decimal degrees
535 c - 2) Count rows and columns
536 c -------------------------------------------------------
537  dgla = dble(igridsec)/3600.d0
538  dglo = dble(igridsec)/3600.d0
539 
540  nla=idnint((glamx-glamn)/dgla)+1
541  nlo=idnint((glomx-glomn)/dglo)+1
542 
543  write(6,3001)glamn,glamx,glomn,glomx,dgla,dglo,nla,nlo
544  3001 format(6x,'mymedian5.f : Cell Structure:',/,
545  *8x,'North = ',f16.10,/,
546  *8x,'South = ',f16.10,/,
547  *8x,'West = ',f16.10,/,
548  *8x,'East = ',f16.10,/,
549  *8x,'DLat = ',f16.10,/,
550  *8x,'DLon = ',f16.10,/,
551  *8x,'NLat = ',i16 ,/,
552  *8x,'NLon = ',i16 )
553 
554 c glamx=glamn+dgla*(nla-1) !*** register the far boundary
555 c glomx=glomn+dglo*(nlo-1)
556 
557 c -----------------------------------------------------------------------
558 c -----------------------------------------------------------------------
559 c -----------------------------------------------------------------------
560 c -----------------------------------------------------------------------
561 c - Median filter in a loop. First loop is to process on absolute horizontal
562 c - distance in METERS. Second loop on ellipsoid height, again on
563 c - absolute horizontal value in METERS. (All "arcsecond" stuff
564 c - is passively carried along to match the meter stuff. All the
565 c - WORK is done in meters for median thinning).
566 c -
567 c - In the first loop, when done processing on horizontal distance,
568 c - use the kept PIDs to generate the thinned LAT and thinned LON
569 c - files (in this way, the PID list for both LAT and LON files will
570 c - always be identical). Do NOT process medians on LAT and LON
571 c - separately!!!
572 c -----------------------------------------------------------------------
573 c -----------------------------------------------------------------------
574 c -----------------------------------------------------------------------
575 c -----------------------------------------------------------------------
576 
577  nthinhor = 0
578  ndrophor = 0
579  nthineht = 0
580  ndropeht = 0
581 
582  do 2001 iloop = 1,2
583  if (iloop.eq.1)then
584  lin = 24 ! vchor file
585 c fused = gfnvchor
586  fused = gfnvmacdhor
587  elseif(iloop.eq.2)then
588  lin = 23 ! vceht file
589 c fused = gfnvceht
590  fused = gfnvmacdeht
591  endif
592  write(6,2002)trim(fused)
593  2002 format(6x,'mymedian5.f: Median filtering file : ',a)
594  101 format(f16.10,1x,f15.10,1x,6x ,1x,12x ,1x,9x ,1x,f9.3,1x,a6)
595 
596 c - 2016 09 14:
597 c2003 format(6x,'mymedian5.f: Point outside boundaries: ',
598 c * f16.10,1x,f15.10,1x,f9.5,1x,a6)
599  2003 format(6x,'mymedian5.f: FATAL ERROR: ',
600  * 'Point outside boundaries: ',
601  * f16.10,1x,f15.10,1x,f9.5,1x,a6)
602 
603 c - Set the count of how many values are in each non-empty
604 c - grid cell to zero.
605  do 201 i=1,max
606  ppcell(i)=0
607  201 continue
608  ncells = 0
609 
610 c ------------------------------------------------------------
611 c - Top of READ loop
612 c - "ikt" is the count of all records in the vector file
613 c ------------------------------------------------------------
614  ikt = 0
615 c write(6,*) ' before read loop'
616  100 read(lin,'(a)',end=777)card
617 
618 c - Note that we will work in (aka "the z value") METERS on both horizontal
619 c - *AND* ellipsoid heights.
620  read(card,101)glo,gla,z,pid
621  ikt = ikt + 1
622 
623 c - 2016 09 14: Changed this code...if there is ANY point that comes
624 c - in from outside the grid, that will be a FATAL error
625 c - in this program.
626  if(gla.lt.glamn.or.gla.gt.glamx .or.
627  * glo.lt.glomn.or.glo.gt.glomx) then
628  write(6,2003)gla,glo,z,pid
629  stop
630  endif
631 
632 c - Store the read data
633  cards(ikt)=card
634 
635 c - Store the values too
636  zs(ikt) =z
637 
638 c - Set a default for ALL points that it will "not pass"
639 c - (e.g. "be dropped")
640  lpass(ikt)=.false.
641 
642 c - Determine which row and column our point falls in. Then
643 c - convert that combination of row/col into a single
644 c - value (encoded as ila*100000+ilo)
645  ila=idnint((gla-glamn)/dgla)+1
646  ilo=idnint((glo-glomn)/dglo)+1
647  ipos=ila*100000+ilo
648 
649 c - Determine the distance our point is from the cell's center.
650 c - This is used to break a tie when there are an even number
651 c - of points in a cell.
652  xla = glamn + (ila-1)*dgla
653  xlo = glomn + (ilo-1)*dglo
654  dist(ikt) = dsqrt((gla-xla)**2+(glo-xlo)**2)
655 
656 c - Is this point a new cell?
657  do 202 j=1,ncells
658 
659 c - OLD cell
660 c - Ratchet up the count of points in this cell
661 c - Store the unique input number as being in this cell
662 c - Jump out
663  if(ipos.eq.poscode(j))then
664  ppcell(j) = ppcell(j) + 1
665  whichcell(ikt) = j
666  goto 203
667  endif
668  202 continue
669 
670 c - NEW cell (jump over this code from previous loop if
671 c - an old cell were found)
672 
673  ncells = ncells + 1
674  poscode(ncells) = ipos
675  ilapcell(ncells) = ila
676  ilopcell(ncells) = ilo
677  ppcell(ncells) = 1
678 c 2016 09 14:
679  whichcell(ikt) = ncells
680 
681 c write(6,*) ' new cell : ',ncells,ipos
682 
683  203 continue
684 
685  goto 100
686 
687  777 continue
688 
689 c ------------------------------------------------------------
690 c - Bottom of READ loop
691 c ------------------------------------------------------------
692 
693  nkt = ikt
694 
695  write(6,2004) trim(fused),nkt,igridsec,ncells
696  2004 format(
697  *6x,'mymedian5.f: Done Reading from : ',a,/,
698  *6x,'mymedian5.f: Points in File : ',i10,/,
699  *6x,'mymedian5.f: Cell Size (Arcseconds) : ',i10,/,
700  *6x,'mymedian5.f: Number of Cells with Data : ',i10)
701  write(6,2005)
702  2005 format(6x,'mymedian5.f: Begin median filtering')
703 
704 
705 c ------------------------------------------------------------
706 c - TOP of SORTING section
707 c ------------------------------------------------------------
708 
709 c- Sort all of our data (nkt values) by their poscode
710  write(6,2020)
711  2020 format(6x,'mymedian5.f: Sorting data...')
712 c - 2016 09 14
713  call indexxi(nkt,max,whichcell,indx)
714 
715 c - 2016 09 14: Fixed, but kept commented out
716 c - Spit out our data in WHICHCELL order
717 c goto 400
718 c do 400 i=1,nkt
719 c j = indx(i)
720 c icell = whichcell(j)
721 c if(mod(i,1000).eq.0)then
722 c write(6,2010)cards(j),icell,ilapcell(icell),
723 c * ilopcell(icell),poscode(icell)
724 c endif
725 c 400 continue
726  2010 format(i8,1x,a80,1x,4(i10))
727 
728 c - Now go through each cell and sort it
729  inumlo = 0
730  inumhi = 0
731  ikt = 0
732  do 401 icell=1,ncells
733  inumlo = inumhi + 1
734  inumhi = inumlo + ppcell(icell) - 1
735 c write(6,*) ' inumlo/inumhi = ',inumlo,inumhi
736  do 402 jpt=1,ppcell(icell)
737  ikt = ikt + 1
738  j = indx(ikt)
739 c write(6,2010)ikt,cards(j),icell,ilapcell(icell),
740 c * ilopcell(icell),poscode(icell)
741  402 continue
742  call getmedian(zs,max,inumlo,inumhi,indx,
743  * maxppcell,dist,iiival)
744 
745 c - Set the median for this cell to "pass" (e.g. to be
746 c - put in the "thinned" data file.
747  if(iiival.lt.0)stop 10001
748  if(iiival.gt.nkt)stop 10002
749  lpass(iiival) = .true.
750  401 continue
751  write(6,408)
752  408 format(6x,'mymedian5.f: Done finding medians',/,
753  * 6x,'mymedian5.f: Populating thinned and dropped files')
754 
755  rewind(lin)
756 
757  ithin = 0
758  idrop = 0
759 
760 c - 2016 09 14
761  do 451 i=1,nkt
762 
763  if(iloop.eq.1)then
764  read(11,'(a)')cardcvlat
765  read(12,'(a)')cardcvlon
766 
767  read(21,'(a)')cardvmlat
768  read(22,'(a)')cardvmlon
769  read(24,'(a)')cardvmhor
770 
771  read(26,'(a)')cardvslat
772  read(27,'(a)')cardvslon
773  read(29,'(a)')cardvshor
774 
775  if(lpass(i))then
776  ithin = ithin + 1
777  write(31,'(a)')cardcvlat
778  write(32,'(a)')cardcvlon
779 
780  write(41,'(a)')cardvmlat
781  write(42,'(a)')cardvmlon
782  write(44,'(a)')cardvmhor
783 
784  write(46,'(a)')cardvslat
785  write(47,'(a)')cardvslon
786  write(49,'(a)')cardvshor
787 
788 c - write the thinned vectors to the surface files
789 c - NOTE: We need to extract LON/LAT/Value_in_correct_units
790 c - from the overall "card", and the correct value
791 c - depends on whether we're pulling out arcseconds or
792 c - meters.
793 
794  write(71,'(a,a,a)')cardvmlat(1:33),cardvmlat(64:72),
795  * cardvmlat(73:79)
796  write(72,'(a,a,a)')cardvmlon(1:33),cardvmlon(64:72),
797  * cardvmlat(73:79)
798 
799  write(76,'(a,a,a)')cardvslat(1:33),cardvslat(54:62),
800  * cardvmlat(73:79)
801  write(77,'(a,a,a)')cardvslon(1:33),cardvslon(54:62),
802  * cardvmlat(73:79)
803 
804  else
805  idrop = idrop + 1
806  write(51,'(a)')cardcvlat
807  write(52,'(a)')cardcvlon
808 
809  write(61,'(a)')cardvmlat
810  write(62,'(a)')cardvmlon
811  write(64,'(a)')cardvmhor
812 
813  write(66,'(a)')cardvslat
814  write(67,'(a)')cardvslon
815  write(69,'(a)')cardvshor
816  endif
817 
818  else
819  read(13,'(a)')cardcveht
820 
821  read(23,'(a)')cardvmeht
822 
823  if(lpass(i))then
824  ithin = ithin + 1
825  write(33,'(a)')cardcveht
826  write(43,'(a)')cardvmeht
827 c - write the thinned vectors to the surface file
828  write(73,'(a,a,a)')cardvmeht(1:33),cardvmeht(64:72),
829  * cardvmeht(73:79)
830  else
831  idrop = idrop + 1
832  write(53,'(a)')cardcveht
833  write(63,'(a)')cardvmeht
834  endif
835  endif
836  451 continue
837 
838  write(6,*) ' Number kept : ',ithin
839  write(6,*) ' Number dropped : ',idrop
840 
841  if(iloop.eq.1)then
842  nthinhor = ithin
843  ndrophor = idrop
844  else
845  nthineht = ithin
846  ndropeht = idrop
847  endif
848 
849 c do 300 i=1,ncells
850 c write(6,2006)i,poscode(i)
851 c do 301 j=1,ppcell(i)
852 c write(6,2007)j
853 c 301 continue
854 c 300 continue
855 
856 
857 c ------------------------------------------------------------
858 c - BOTTOM of SORTING section
859 c ------------------------------------------------------------
860 
861 
862  2001 continue
863 
864 c -------------------------------------------------------
865 c - Based on everything we have so far, put a bunch
866 c - of "surface" calls into the GMT batch file.
867 c - These will be to turn the median-thinned data
868 c - into grids. DO NOT GENERATE A CALL TO SURFACE
869 c - IF THE NUMBER OF THINNED DATA IS ZERO.
870 
871 c - Because "surface", as part of GMT, returns its
872 c - own binary grid format (called ".grd" herein), it must be
873 c - converted to ".b" format. The easiest way to
874 c - do so is to use the GMT built-in routine "grd2xyz"
875 c - with the "-bos" extension to create a binary XYZ
876 c - list file. Then run Dennis's homemade "xyz2b.for"
877 c - routine to finally arrive at the ".b" binary grid format.
878 c -------------------------------------------------------
879  cmidlat=dcos(((glamn+glamx)/2.d0)*d2r)
880  rfnvmtcdlat00 = 'vmtcdlat.'//trim(suffix2t00)//'.grd'
881  rfnvmtcdlon00 = 'vmtcdlon.'//trim(suffix2t00)//'.grd'
882  rfnvmtcdeht00 = 'vmtcdeht.'//trim(suffix2t00)//'.grd'
883  rfnvstcdlat00 = 'vstcdlat.'//trim(suffix2t00)//'.grd'
884  rfnvstcdlon00 = 'vstcdlon.'//trim(suffix2t00)//'.grd'
885  rfnvmtcdlat04 = 'vmtcdlat.'//trim(suffix2t04)//'.grd'
886  rfnvmtcdlon04 = 'vmtcdlon.'//trim(suffix2t04)//'.grd'
887  rfnvmtcdeht04 = 'vmtcdeht.'//trim(suffix2t04)//'.grd'
888  rfnvstcdlat04 = 'vstcdlat.'//trim(suffix2t04)//'.grd'
889  rfnvstcdlon04 = 'vstcdlon.'//trim(suffix2t04)//'.grd'
890  rfnvmtcdlat10 = 'vmtcdlat.'//trim(suffix2t10)//'.grd'
891  rfnvmtcdlon10 = 'vmtcdlon.'//trim(suffix2t10)//'.grd'
892  rfnvmtcdeht10 = 'vmtcdeht.'//trim(suffix2t10)//'.grd'
893  rfnvstcdlat10 = 'vstcdlat.'//trim(suffix2t10)//'.grd'
894  rfnvstcdlon10 = 'vstcdlon.'//trim(suffix2t10)//'.grd'
895 
896  sadrfnvmtcdlat1000 = 'vmtcdlat.'//trim(suffix2d3)//'.grd'
897  sadrfnvstcdlat1000 = 'vstcdlat.'//trim(suffix2d3)//'.grd'
898  sadrfnvmtcdlon1000 = 'vmtcdlon.'//trim(suffix2d3)//'.grd'
899  sadrfnvstcdlon1000 = 'vstcdlon.'//trim(suffix2d3)//'.grd'
900  sadrfnvmtcdeht1000 = 'vmtcdeht.'//trim(suffix2d3)//'.grd'
901 
902  zfnvmtcdlat00 = 'vmtcdlat.'//trim(suffix2t00)//'.xyz'
903  zfnvmtcdlon00 = 'vmtcdlon.'//trim(suffix2t00)//'.xyz'
904  zfnvmtcdeht00 = 'vmtcdeht.'//trim(suffix2t00)//'.xyz'
905  zfnvstcdlat00 = 'vstcdlat.'//trim(suffix2t00)//'.xyz'
906  zfnvstcdlon00 = 'vstcdlon.'//trim(suffix2t00)//'.xyz'
907  zfnvmtcdlat04 = 'vmtcdlat.'//trim(suffix2t04)//'.xyz'
908  zfnvmtcdlon04 = 'vmtcdlon.'//trim(suffix2t04)//'.xyz'
909  zfnvmtcdeht04 = 'vmtcdeht.'//trim(suffix2t04)//'.xyz'
910  zfnvstcdlat04 = 'vstcdlat.'//trim(suffix2t04)//'.xyz'
911  zfnvstcdlon04 = 'vstcdlon.'//trim(suffix2t04)//'.xyz'
912  zfnvmtcdlat10 = 'vmtcdlat.'//trim(suffix2t10)//'.xyz'
913  zfnvmtcdlon10 = 'vmtcdlon.'//trim(suffix2t10)//'.xyz'
914  zfnvmtcdeht10 = 'vmtcdeht.'//trim(suffix2t10)//'.xyz'
915  zfnvstcdlat10 = 'vstcdlat.'//trim(suffix2t10)//'.xyz'
916  zfnvstcdlon10 = 'vstcdlon.'//trim(suffix2t10)//'.xyz'
917 
918  bfnvmtcdlat00 = 'vmtcdlat.'//trim(suffix2t00)//'.b'
919  bfnvmtcdlon00 = 'vmtcdlon.'//trim(suffix2t00)//'.b'
920  bfnvmtcdeht00 = 'vmtcdeht.'//trim(suffix2t00)//'.b'
921  bfnvstcdlat00 = 'vstcdlat.'//trim(suffix2t00)//'.b'
922  bfnvstcdlon00 = 'vstcdlon.'//trim(suffix2t00)//'.b'
923  bfnvmtcdlat04 = 'vmtcdlat.'//trim(suffix2t04)//'.b'
924  bfnvmtcdlon04 = 'vmtcdlon.'//trim(suffix2t04)//'.b'
925  bfnvmtcdeht04 = 'vmtcdeht.'//trim(suffix2t04)//'.b'
926  bfnvstcdlat04 = 'vstcdlat.'//trim(suffix2t04)//'.b'
927  bfnvstcdlon04 = 'vstcdlon.'//trim(suffix2t04)//'.b'
928  bfnvmtcdlat10 = 'vmtcdlat.'//trim(suffix2t10)//'.b'
929  bfnvmtcdlon10 = 'vmtcdlon.'//trim(suffix2t10)//'.b'
930  bfnvmtcdeht10 = 'vmtcdeht.'//trim(suffix2t10)//'.b'
931  bfnvstcdlat10 = 'vstcdlat.'//trim(suffix2t10)//'.b'
932  bfnvstcdlon10 = 'vstcdlon.'//trim(suffix2t10)//'.b'
933 
934 c - Difference grid names
935  dbfnvmtcdlat1000 = 'vmtcdlat.'//trim(suffix2d1)//'.b'
936  dbfnvstcdlat1000 = 'vstcdlat.'//trim(suffix2d1)//'.b'
937  dbfnvmtcdlon1000 = 'vmtcdlon.'//trim(suffix2d1)//'.b'
938  dbfnvstcdlon1000 = 'vstcdlon.'//trim(suffix2d1)//'.b'
939  dbfnvmtcdeht1000 = 'vmtcdeht.'//trim(suffix2d1)//'.b'
940 
941  adbfnvmtcdlat1000 = 'vmtcdlat.'//trim(suffix2d2)//'.b'
942  adbfnvstcdlat1000 = 'vstcdlat.'//trim(suffix2d2)//'.b'
943  adbfnvmtcdlon1000 = 'vmtcdlon.'//trim(suffix2d2)//'.b'
944  adbfnvstcdlon1000 = 'vstcdlon.'//trim(suffix2d2)//'.b'
945  adbfnvmtcdeht1000 = 'vmtcdeht.'//trim(suffix2d2)//'.b'
946 
947  sadbfnvmtcdlat1000 = 'vmtcdlat.'//trim(suffix2d3)//'.b'
948  sadbfnvstcdlat1000 = 'vstcdlat.'//trim(suffix2d3)//'.b'
949  sadbfnvmtcdlon1000 = 'vmtcdlon.'//trim(suffix2d3)//'.b'
950  sadbfnvstcdlon1000 = 'vstcdlon.'//trim(suffix2d3)//'.b'
951  sadbfnvmtcdeht1000 = 'vmtcdeht.'//trim(suffix2d3)//'.b'
952 
953 c - Due to a bug in GMT, the call will use "-I*m" rather
954 c - than "-I*s". As such, convert igridsec to xgridmin.
955  xgridmin = dble(igridsec)/60.d0
956 
957 c - Do NOT generate a call to surface if there is no data
958 c - to grid.
959 
960 c - Call to grid the thinned latitudes
961  if(nthinhor.ne.0)then
962  write(99,501)trim(gfnsmtcdlat),glomn,glomx,glamn,glamx,
963  * xgridmin,trim(rfnvmtcdlat00),0.0,cmidlat
964  write(99,501)trim(gfnsstcdlat),glomn,glomx,glamn,glamx,
965  * xgridmin,trim(rfnvstcdlat00),0.0,cmidlat
966 
967  write(99,501)trim(gfnsmtcdlat),glomn,glomx,glamn,glamx,
968  * xgridmin,trim(rfnvmtcdlat04),0.4,cmidlat
969  write(99,501)trim(gfnsstcdlat),glomn,glomx,glamn,glamx,
970  * xgridmin,trim(rfnvstcdlat04),0.4,cmidlat
971 
972  write(99,501)trim(gfnsmtcdlat),glomn,glomx,glamn,glamx,
973  * xgridmin,trim(rfnvmtcdlat10),1.0,cmidlat
974  write(99,501)trim(gfnsstcdlat),glomn,glomx,glamn,glamx,
975  * xgridmin,trim(rfnvstcdlat10),1.0,cmidlat
976  endif
977 
978 c - Call to grid the thinned longitudes
979  if(nthinhor.ne.0)then
980  write(99,501)trim(gfnsmtcdlon),glomn,glomx,glamn,glamx,
981  * xgridmin,trim(rfnvmtcdlon00),0.0,cmidlat
982  write(99,501)trim(gfnsstcdlon),glomn,glomx,glamn,glamx,
983  * xgridmin,trim(rfnvstcdlon00),0.0,cmidlat
984 
985  write(99,501)trim(gfnsmtcdlon),glomn,glomx,glamn,glamx,
986  * xgridmin,trim(rfnvmtcdlon04),0.4,cmidlat
987  write(99,501)trim(gfnsstcdlon),glomn,glomx,glamn,glamx,
988  * xgridmin,trim(rfnvstcdlon04),0.4,cmidlat
989 
990  write(99,501)trim(gfnsmtcdlon),glomn,glomx,glamn,glamx,
991  * xgridmin,trim(rfnvmtcdlon10),1.0,cmidlat
992  write(99,501)trim(gfnsstcdlon),glomn,glomx,glamn,glamx,
993  * xgridmin,trim(rfnvstcdlon10),1.0,cmidlat
994  endif
995 
996 c - Call to grid the thinned ellipsoid heights
997  if(nthineht.ne.0)then
998  write(99,501)trim(gfnsmtcdeht),glomn,glomx,glamn,glamx,
999  * xgridmin,trim(rfnvmtcdeht00),0.0,cmidlat
1000 
1001  write(99,501)trim(gfnsmtcdeht),glomn,glomx,glamn,glamx,
1002  * xgridmin,trim(rfnvmtcdeht04),0.4,cmidlat
1003 
1004  write(99,501)trim(gfnsmtcdeht),glomn,glomx,glamn,glamx,
1005  * xgridmin,trim(rfnvmtcdeht10),1.0,cmidlat
1006  endif
1007 
1008 c - Calls to convert ".grd" to ".xyz"
1009  if(nthinhor.ne.0)then
1010  write(99,502)trim(rfnvmtcdlat00),trim(zfnvmtcdlat00)
1011  write(99,502)trim(rfnvstcdlat00),trim(zfnvstcdlat00)
1012 
1013  write(99,502)trim(rfnvmtcdlat04),trim(zfnvmtcdlat04)
1014  write(99,502)trim(rfnvstcdlat04),trim(zfnvstcdlat04)
1015 
1016  write(99,502)trim(rfnvmtcdlat10),trim(zfnvmtcdlat10)
1017  write(99,502)trim(rfnvstcdlat10),trim(zfnvstcdlat10)
1018  endif
1019 
1020  if(nthinhor.ne.0)then
1021  write(99,502)trim(rfnvmtcdlon00),trim(zfnvmtcdlon00)
1022  write(99,502)trim(rfnvstcdlon00),trim(zfnvstcdlon00)
1023 
1024  write(99,502)trim(rfnvmtcdlon04),trim(zfnvmtcdlon04)
1025  write(99,502)trim(rfnvstcdlon04),trim(zfnvstcdlon04)
1026 
1027  write(99,502)trim(rfnvmtcdlon10),trim(zfnvmtcdlon10)
1028  write(99,502)trim(rfnvstcdlon10),trim(zfnvstcdlon10)
1029  endif
1030 
1031  if(nthineht.ne.0)then
1032  write(99,502)trim(rfnvmtcdeht00),trim(zfnvmtcdeht00)
1033 
1034  write(99,502)trim(rfnvmtcdeht04),trim(zfnvmtcdeht04)
1035 
1036  write(99,502)trim(rfnvmtcdeht10),trim(zfnvmtcdeht10)
1037  endif
1038 
1039 c - Calls to convert ".xyz" to ".b"
1040  if(nthinhor.ne.0)then
1041  write(99,503)trim(zfnvmtcdlat00),trim(bfnvmtcdlat00)
1042  write(99,503)trim(zfnvstcdlat00),trim(bfnvstcdlat00)
1043 
1044  write(99,503)trim(zfnvmtcdlat04),trim(bfnvmtcdlat04)
1045  write(99,503)trim(zfnvstcdlat04),trim(bfnvstcdlat04)
1046 
1047  write(99,503)trim(zfnvmtcdlat10),trim(bfnvmtcdlat10)
1048  write(99,503)trim(zfnvstcdlat10),trim(bfnvstcdlat10)
1049  endif
1050 
1051  if(nthinhor.ne.0)then
1052  write(99,503)trim(zfnvmtcdlon00),trim(bfnvmtcdlon00)
1053  write(99,503)trim(zfnvstcdlon00),trim(bfnvstcdlon00)
1054 
1055  write(99,503)trim(zfnvmtcdlon04),trim(bfnvmtcdlon04)
1056  write(99,503)trim(zfnvstcdlon04),trim(bfnvstcdlon04)
1057 
1058  write(99,503)trim(zfnvmtcdlon10),trim(bfnvmtcdlon10)
1059  write(99,503)trim(zfnvstcdlon10),trim(bfnvstcdlon10)
1060  endif
1061 
1062  if(nthineht.ne.0)then
1063  write(99,503)trim(zfnvmtcdeht00),trim(bfnvmtcdeht00)
1064 
1065  write(99,503)trim(zfnvmtcdeht04),trim(bfnvmtcdeht04)
1066 
1067  write(99,503)trim(zfnvmtcdeht10),trim(bfnvmtcdeht10)
1068  endif
1069 
1070 
1071  501 format(
1072  *'surface ',a,' -R',f9.5,'/',f9.5,'/',sp,f9.5,'/',f9.5,s,
1073  *' -I',f0.2,'m -G',a,' -T',f3.1,' -A',s,f6.4,' -C0.01 -V')
1074 
1075 c 501 format(
1076 c *'surface ',a,' -R',f9.5,'/',f9.5,'/',sp,f9.5,'/',f9.5,s,
1077 c *' -I',f0.2,'m -G',a,' -T0.4 -A',s,f6.4,' -C0.01 -V')
1078 
1079  502 format(
1080  *'grd2xyz ',a,' -bo3f > ',a)
1081  503 format(
1082  *'xyz2b << !',/,
1083  *a,/,a,/,'!')
1084  504 format(
1085  *'subtrc << !',/,
1086  *a,/,a,/,a,/,'!')
1087  505 format(
1088  *'gabs << !',/,
1089  *a,/,a,/,'!')
1090  506 format(
1091  *'gscale << !',/,
1092  *a,/,f10.5,/,a,/,'!')
1093  507 format(
1094  *'b2xyz << !',/,a,/,'!',/,
1095  *'xyz2grd temp.xyz -R',f9.5,'/',f9.5,'/',sp,f9.5,'/',f9.5,s,
1096  *' -I',f0.2,'m -bi3f -G',a,/,
1097  *'rm -f temp.xyz')
1098 
1099 c - New, as of 10/27/2015:
1100 c - Subtract the T=0.0 transformation grid
1101 c - from the T=1.0 transformation grid.
1102 c - Then take the absolute value of that
1103 c - difference, and finally scale that
1104 c - grid by 1/1.6929. Do this for
1105 c - all 5 possible transformation grids
1106 c - All of these calls should be in the
1107 c - "gmtbat02" file.
1108 
1109 
1110 c - Differences first:
1111  if(nthinhor.ne.0)then
1112  write(99,504)trim(bfnvmtcdlat10),trim(bfnvmtcdlat00),
1113  * trim(dbfnvmtcdlat1000)
1114  write(99,504)trim(bfnvstcdlat10),trim(bfnvstcdlat00),
1115  * trim(dbfnvstcdlat1000)
1116 
1117  write(99,504)trim(bfnvmtcdlon10),trim(bfnvmtcdlon00),
1118  * trim(dbfnvmtcdlon1000)
1119  write(99,504)trim(bfnvstcdlon10),trim(bfnvstcdlon00),
1120  * trim(dbfnvstcdlon1000)
1121  endif
1122 
1123  if(nthineht.ne.0)then
1124  write(99,504)trim(bfnvmtcdeht10),trim(bfnvmtcdeht00),
1125  * trim(dbfnvmtcdeht1000)
1126  endif
1127 
1128 c - Make them absolute values:
1129  if(nthinhor.ne.0)then
1130  write(99,505)trim(dbfnvmtcdlat1000),trim(adbfnvmtcdlat1000)
1131  write(99,505)trim(dbfnvstcdlat1000),trim(adbfnvstcdlat1000)
1132  write(99,505)trim(dbfnvmtcdlon1000),trim(adbfnvmtcdlon1000)
1133  write(99,505)trim(dbfnvstcdlon1000),trim(adbfnvstcdlon1000)
1134  endif
1135 
1136  if(nthineht.ne.0)then
1137  write(99,505)trim(dbfnvmtcdeht1000),trim(adbfnvmtcdeht1000)
1138  endif
1139 
1140 c - Finally, scale by 1/1.6929
1141  if(nthinhor.ne.0)then
1142  write(99,506)trim(adbfnvmtcdlat1000),rng2std,
1143  * trim(sadbfnvmtcdlat1000)
1144  write(99,506)trim(adbfnvstcdlat1000),rng2std,
1145  * trim(sadbfnvstcdlat1000)
1146  write(99,506)trim(adbfnvmtcdlon1000),rng2std,
1147  * trim(sadbfnvmtcdlon1000)
1148  write(99,506)trim(adbfnvstcdlon1000),rng2std,
1149  * trim(sadbfnvstcdlon1000)
1150  endif
1151 
1152  if(nthineht.ne.0)then
1153  write(99,506)trim(adbfnvmtcdeht1000),rng2std,
1154  * trim(sadbfnvmtcdeht1000)
1155  endif
1156 
1157 c - After all this, I now have the "d3" files as ".b" grids,
1158 c - but in order to plot them, I'll have to convert them
1159 c - to ".grd" files. So here we go...
1160  if(nthinhor.ne.0)then
1161  write(99,507)trim(sadbfnvmtcdlat1000)
1162  * ,glomn,glomx,glamn,glamx,xgridmin,trim(sadrfnvmtcdlat1000)
1163  write(99,507)trim(sadbfnvstcdlat1000)
1164  * ,glomn,glomx,glamn,glamx,xgridmin,trim(sadrfnvstcdlat1000)
1165  write(99,507)trim(sadbfnvmtcdlon1000)
1166  * ,glomn,glomx,glamn,glamx,xgridmin,trim(sadrfnvmtcdlon1000)
1167  write(99,507)trim(sadbfnvstcdlon1000)
1168  * ,glomn,glomx,glamn,glamx,xgridmin,trim(sadrfnvstcdlon1000)
1169  endif
1170 
1171  if(nthineht.ne.0)then
1172  write(99,507)trim(sadbfnvmtcdeht1000)
1173  * ,glomn,glomx,glamn,glamx,xgridmin,trim(sadrfnvmtcdeht1000)
1174  endif
1175 
1176 ccccccccccccccccccccccccccccccccccccc
1177 c - 2016 06 29
1178 c - MASK, Mask, mask code for HARN/FBN/CONUS combination
1179  if(trim(olddtm).eq.'nad83_harn' .and.
1180  * trim(newdtm).eq.'nad83_fbn' .and.
1181  * trim(region).eq.'conus' )then
1182 
1183  write(99,601)
1184 
1185 
1186 c - See DRU-12, p. 39
1187 c - First, create the masked ".b" files...
1188 
1189  if(nthinhor.ne.0)then
1190  write(99,602)trim(bfnvmtcdlat04),
1191  * trim(bfnvmtcdlat04)//'.premask',
1192  * trim(bfnvmtcdlat04)//'.premask',
1193  * trim(bfnvmtcdlat04),
1194  * igridsec/30,igridsec/30
1195 
1196  write(99,602)trim(bfnvstcdlat04),
1197  * trim(bfnvstcdlat04)//'.premask',
1198  * trim(bfnvstcdlat04)//'.premask',
1199  * trim(bfnvstcdlat04),
1200  * igridsec/30,igridsec/30
1201 
1202  write(99,602)trim(bfnvmtcdlon04),
1203  * trim(bfnvmtcdlon04)//'.premask',
1204  * trim(bfnvmtcdlon04)//'.premask',
1205  * trim(bfnvmtcdlon04),
1206  * igridsec/30,igridsec/30
1207 
1208  write(99,602)trim(bfnvstcdlon04),
1209  * trim(bfnvstcdlon04)//'.premask',
1210  * trim(bfnvstcdlon04)//'.premask',
1211  * trim(bfnvstcdlon04),
1212  * igridsec/30,igridsec/30
1213  endif
1214 
1215  if(nthineht.ne.0)then
1216  write(99,602)trim(bfnvmtcdeht04),
1217  * trim(bfnvmtcdeht04)//'.premask',
1218  * trim(bfnvmtcdeht04)//'.premask',
1219  * trim(bfnvmtcdeht04),
1220  * igridsec/30,igridsec/30
1221  endif
1222 
1223 
1224 c - Second, create the masked ".xyz" and masked ".grd" files
1225  if(nthinhor.ne.0)then
1226  write(99,603)
1227  * trim(zfnvmtcdlat04),trim(zfnvmtcdlat04)//'.premask',
1228  * trim(bfnvmtcdlat04),
1229  * trim(zfnvmtcdlat04),
1230  * trim(rfnvmtcdlat04),
1231  * trim(rfnvmtcdlat04)//'.premask',
1232  * trim(zfnvmtcdlat04),
1233  * glomn,glomx,glamn,glamx,xgridmin,trim(rfnvmtcdlat04)
1234 
1235  write(99,603)
1236  * trim(zfnvstcdlat04),trim(zfnvstcdlat04)//'.premask',
1237  * trim(bfnvstcdlat04),
1238  * trim(zfnvstcdlat04),
1239  * trim(rfnvstcdlat04),
1240  * trim(rfnvstcdlat04)//'.premask',
1241  * trim(zfnvstcdlat04),
1242  * glomn,glomx,glamn,glamx,xgridmin,trim(rfnvstcdlat04)
1243 
1244  write(99,603)
1245  * trim(zfnvmtcdlon04),trim(zfnvmtcdlon04)//'.premask',
1246  * trim(bfnvmtcdlon04),
1247  * trim(zfnvmtcdlon04),
1248  * trim(rfnvmtcdlon04),
1249  * trim(rfnvmtcdlon04)//'.premask',
1250  * trim(zfnvmtcdlon04),
1251  * glomn,glomx,glamn,glamx,xgridmin,trim(rfnvmtcdlon04)
1252 
1253  write(99,603)
1254  * trim(zfnvstcdlon04),trim(zfnvstcdlon04)//'.premask',
1255  * trim(bfnvstcdlon04),
1256  * trim(zfnvstcdlon04),
1257  * trim(rfnvstcdlon04),
1258  * trim(rfnvstcdlon04)//'.premask',
1259  * trim(zfnvstcdlon04),
1260  * glomn,glomx,glamn,glamx,xgridmin,trim(rfnvstcdlon04)
1261  endif
1262 
1263  if(nthineht.ne.0)then
1264  write(99,603)
1265  * trim(zfnvmtcdeht04),trim(zfnvmtcdeht04)//'.premask',
1266  * trim(bfnvmtcdeht04),
1267  * trim(zfnvmtcdeht04),
1268  * trim(rfnvmtcdeht04),
1269  * trim(rfnvmtcdeht04)//'.premask',
1270  * trim(zfnvmtcdeht04),
1271  * glomn,glomx,glamn,glamx,xgridmin,trim(rfnvmtcdeht04)
1272  endif
1273 
1274  603 format(
1275  *'mv ',a,' ',a,/,
1276  *'b2xyz << !',/,a,/,'!',/,
1277  *'mv temp.xyz ',a,/,
1278  *'mv ',a,' ',a,/,
1279  *'xyz2grd ',a,' -R',f9.5,'/',f9.5,'/',sp,f9.5,'/',f9.5,s,
1280  *' -I',f0.2,'m -bis -G',a)
1281 
1282  endif
1283  601 format('echo Applying MASK for HARN FBN CONUS 04 grids')
1284 
1285  602 format(
1286  *'mv ',a,' ',a,/,
1287  *'regrd2 << !',/,
1288  *a,/,
1289  *'dummy1.b',/,
1290  *'3121',/,
1291  *'7081',/,
1292  *'!',/,
1293  *'convlv << !',/
1294  *'dummy1.b',/,
1295  *'Masks/mask.harnfbn.30.b',/,
1296  *'dummy2.b',/,
1297  *'!',/,
1298  *'decimate << !',/
1299  *'dummy2.b',/,
1300  *a,/,
1301  *i3,/,
1302  *i3,/,
1303  *'!',/,
1304  *'rm -f dummy1.b',/,
1305  *'rm -f dummy2.b')
1306 
1307 
1308 
1309 
1310 
1311 
1312 
1313 
1314 ccccccccccccccccccccccccccccccccccccc
1315 
1316 
1317 
1318  write(99,1031)trim(gmtfile)
1319  1031 format('echo END batch file ',a)
1320  close(99)
1321 
1322  write(6,9999)
1323  9999 format('END mymedian5.f')
1324 
1325 
1326  2006 format('Cell ',i8,' Poscode: ',i12)
1327  2007 format(6x,'Pt : ',i5)
1328  end
1329 c
1330 c -----------------------------------------------
1331 c
1332  subroutine getmedian(zs,max,inumlo,inumhi,indx,
1333  *maxppcell,dist,iiival)
1334  real*8 zs(max),dist(max)
1335  integer*4 indx(max),indx2(maxppcell)
1336  integer*4 inumlo,inumhi
1337 
1338  real*8 zz(maxppcell)
1339 
1340 
1341 c write(6,1)inumlo,inumhi
1342 c 1 format('Inside getmedian',/,
1343 c *'inumlo,inumhi = ',i8,1x,i8)
1344 
1345 c - When the whole set of "zs" data is sorted by
1346 c - "whichcell", then the "indx" values are our
1347 c - key to that sort. The "zs" data remains
1348 c - in "read order" (1-nkt), but the indx values
1349 c - (also in "read order" point to the sorted
1350 c - order.
1351 
1352 c write(6,*) ' UNSORTED data: '
1353 
1354  nval = inumhi-inumlo+1
1355  iq = 0
1356  do 2 i=inumlo,inumhi
1357  iq = iq + 1
1358  zz(iq) = zs(indx(i))
1359 c write(6,3)i,iq,zz(iq)
1360  2 continue
1361  3 format(i10,1x,i10,1x,f20.10)
1362 
1363 c - Sort this mini vector
1364 c call hpsort(nval,maxppcell,zz)
1365 
1366  call indexxd(nval,maxppcell,zz,indx2)
1367 
1368 c write(6,*) ' SORTED data: '
1369 
1370 c do 4 i=1,nval
1371 c write(6,3)i,indx2(i),zz(indx2(i))
1372 c 4 continue
1373  5 format(i10,1x,f20.10)
1374 
1375 c - True median comes from an odd number of values. If
1376 c - it is even, take the one which sits closest to the
1377 c - center of the cell.
1378  if(mod(nval,2).eq.1)then
1379  ival = (nval+1)/2
1380  else
1381  ival1 = nval/2
1382  ival2 = ival1 + 1
1383 c write(6,*) ' MedianLo = ',zz(indx2(ival1))
1384 c write(6,*) ' MedianHi = ',zz(indx2(ival2))
1385 c write(6,*) ' Dist Lo = ',dist(indx2(ival1))
1386 c write(6,*) ' Dist Hi = ',dist(indx2(ival2))
1387  if(dist(indx2(ival1)).lt.dist(indx2(ival2)))then
1388 c write(6,*) ' Median Set to LO = ',zz(indx2(ival1))
1389  ival = ival1
1390  else
1391 c write(6,*) ' Median Set to HI = ',zz(indx2(ival2))
1392  ival = ival2
1393  endif
1394  endif
1395 
1396 c - Note that "iiival" is the read-order number of the
1397 c - point chosen as median for the cell we're analyzing
1398 c - in this subroutine.
1399 c write(6,*) ' Median = ',zz(indx2(ival))
1400  igood1 = indx2(ival)
1401 c write(6,*) ' ival = ',ival
1402 c write(6,*) ' indx2(ival) = ',indx2(ival)
1403  iival = inumlo + indx2(ival) - 1
1404 c write(6,*) ' iival = ',iival
1405  iiival = indx(iival)
1406 c write(6,*) ' indx(iival) = ',indx(iival)
1407 
1408 
1409  return
1410  end
1411 
1412  include 'Subs/getgridbounds.f'
1413  include 'Subs/indexxi.for'
1414  include 'Subs/indexxd.for'
subroutine indexxd(n, nd, arr, indx)
Subroutine to perform ?? indexing on floating point data (double precision)
Definition: indexxd.for:24
subroutine indexxi(n, nd, arr, indx)
Subroutine to perform ?? indexing on integer data.
Definition: indexxi.for:24
subroutine getgridbounds(region, xn, xs, xw, xe)
Subroutine to collect up the GRID boundaries for use in creating NADCON 5.
Definition: getgridbounds.f:30
program mymedian5
Program to filter Map Data for GMT Plotting.
Definition: mymedian5.f:71
subroutine getmedian(zs, max, inumlo, inumhi, indx, maxppcell, dist, iiival)
Definition: mymedian5.f:1334