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