-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrib2ctl.pl
executable file
·1766 lines (1589 loc) · 49 KB
/
grib2ctl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# makes a GrADS control file for grib files
#
# requires wgrib and Perl5
#
# usage: grib2ctl [options] [grib file] [optional index file] >[control file]
#
# note: this script does not make the index file .. you have to run gribmap
#
# Analyses: (using initial time)
#
# $ grib2ctl.pl example.grib >example.ctl
# $ gribmap -i example.ctl -0
#
# Forecasts: (using verifiation time)
#
# $ grib2ctl.pl -verf example.grib >example.ctl
# $ gribmap -i example.ctl
#
# bugs:
# many
# will fail under number of situations
# finite number of NCEP grids are supported
#
# requires wgrib v1.8.0.12 or higher
#
# documentation: http://www.cpc.ncep.noaa.gov/products/wesley/grib2ctl.html
#
#
# added output for rotated LatLon grids
# Helmut P. Frank, [email protected]
# Fri Sep 14 13:54:00 GMT 2001
# -ts, -lc options: Ag Stephens, BADC 3/2003
#
#
# added -analysis option
# December 2007, Davide Sacchetti davide.sacchetti.AT.arpal.org
#
# rotated lat-lon grid: Graziano Giuliani
#
$version="2.0.2";
use POSIX;
use Math::Trig qw(deg2rad rad2deg);
# ***** if wgrib is not on path, add it here
# $wgrib='/u/wx51we/home/bin/wgrib';
$wgrib='./wgrib';
# **** directory of interpolation files
$pdef_dir='/glade/apps/opt/grads/2.0.2';
#$pdef_dir='/u/wx51we/home/grads';
$wflag="";
$file="";
$index="";
$pdef="";
$prs="prs";
$suffix="";
$model="WRF";
$calendar="";
$lc="";
$global_map="";
$timestep="";
$rr="";
$nearest_neighbor="";
$kludge="";
$no235z="";
$template="";
$ec_gbl="";
$pdef_offset = 0;
$raw="no";
$pdef_nearest=1;
$analysis="";
# vertical levels allowed, index = grib level number
# 100 = mb, 103=m above msl 105-m above ground 107=sigma 109=hybrid level 111=cm down 113=K
# 115=mb above ground 125=cm above ground 160=m below sea level
$allow_profile[20] = '+';
$allow_profile[100] = '-';
$allow_profile[103] = '+';
$allow_profile[105] = '+';
$allow_profile[107] = '-';
$allow_profile[109] = '+';
$allow_profile[111] = '-';
$allow_profile[113] = '+';
$allow_profile[115] = '+';
$allow_profile[125] = '-';
$allow_profile[160] = '-';
$allow_profile[235] = '-';
for ($i = 0; $i <= $#ARGV; $i++) {
$_ = $ARGV[$i];
SWITCH: {
/^-verf/ && do { $wflag="$wflag -verf" ; last SWITCH; };
/^-ncep_opn/ && do { $wflag="$wflag -ncep_opn" ; last SWITCH; };
/^-ncep_rean/ && do { $wflag="$wflag -ncep_rean" ; last SWITCH; };
/^-no_prs/ && do { $prs="" ; last SWITCH; };
/^-no_suffix/ && do { $suffix="no" ; last SWITCH; };
/^-rev_z/ && do { last SWITCH; };
/^-365/ && do { $calendar="365"; last SWITCH; };
/^-ts(\d+\w+)/ && do { $timestep=$1; last SWITCH; };
/^-lc$/ && do { $lc="on"; last SWITCH; };
/^-rr_nn$/ && do { $rr="on"; $nearest_neighbor="on"; $model="ETA"; last SWITCH; };
/^-rr$/ && do { $rr="on"; $model="ETA"; last SWITCH; };
/^-eta$/ && do { $model="ETA"; last SWITCH; };
/^-mrf$/ && do { $model="GFS"; last SWITCH; };
/^-gfs$/ && do { $model="GFS"; last SWITCH; };
/^-ruc$/ && do { $model="RUC"; last SWITCH; };
/^-global$/ && do { $global_map = "on"; last SWITCH; };
/^-noah$/ && do { $wflag="$wflag -ncep_opn"; last SWITCH; };
/^-osu/ && do { last SWITCH; };
/^-ec_gbl/ && do { $ec_gbl = 1; last SWITCH; };
/^-kludge/ && do { $kludge="on"; last SWITCH; };
/^-no_235z/ && do { $no235z="on"; last SWITCH; };
/^-iso_profile/ && do { undef @allow_profile; $allow_profile[235]='-'; last SWITCH };
/^-prs_profile/ && do { undef @allow_profile; $allow_profile[100]='-'; last SWITCH };
/^-m_profile/ && do { undef @allow_profile; $allow_profile[103]='+';
$allow_profile[105]='+' ; last SWITCH };
/^-no_profile/ && do { undef @allow_profile; last SWITCH };
/^-raw/ && do { $raw="yes" ; last SWITCH };
/^-pdef_linear/ && do { $pdef_nearest=0 ; last SWITCH };
/^-analysis/ && do { $analysis=$ARGV[++$i]; last SWITCH; };
/^-/ && do { print STDERR "unknown option: $_\n"; exit 8; };
if ($file eq "") {
$file = $_;
}
elsif ($index eq "") {
$index = $_;
}
else {
$pdef = $_;
}
}
}
if ($file eq "") {
if ($#ARGV >= 0) {
print STDERR "*** missing grib file ***\n\n\n";
}
print STDERR "$0 $version wesley ebisuzaki\n";
print STDERR " makes a Grads control file for grib files\n";
print STDERR " usage: $0 [options] [grib_file] [optional index file] [optional pdef file] >[ctl file]\n";
print STDERR " -ncep_opn .. use NCEP opn grib table for T62 NCEP fields\n";
print STDERR " -ncep_rean .. use NCEP reanalysis grib table for T62 NCEP fields\n";
print STDERR " -verf .. use forecast verification times\n";
print STDERR " -no_prs .. no prs suffix on variable name\n";
print STDERR " -no_suffix .. no suffix on variable name\n";
print STDERR " -365 .. 365 day calendar\n";
print STDERR " -ts[timestep] .. set timestep for individual time files (e.g. -ts6hr)\n";
print STDERR " -lc .. set lowercase option for parameter names\n";
print STDERR " -iso_profile .. set z coordinate to ocean isotherms\n";
print STDERR " -prs_profile .. set z coordinate to pressure (mb)\n";
print STDERR " -m_profile .. set z coordinate to meters above ground/msl\n";
print STDERR " -no_profile .. no z coordinates\n";
print STDERR " -raw .. raw grid\n";
print STDERR " -pdef_linear .. use linear interpolation for thinned Gaussian grids\n";
print STDERR " -iso_profile .. make profile using subsurface isoterms\n";
print STDERR " -analysis YYYYMMDDHH .. set initial time (e.g. -analysis 1999123100)\n";
print STDERR " initial time must have correct time spacing\n";
print STDERR "\n";
print STDERR "Note 1: the index file will be generated by the gribmap program, default: grib_file.idx\n";
print STDERR "Note 2: the pdef file is only generated for thinned lat-lon/Gaussian grids, default: [grib_file].pdef\n";
exit 8;
}
$_ = $file;
if (/%y4/ || /%y2/ || /%m2/ || /%m1/ || /%d2/ || /%d1/ || /%h2/ ||
/%h1/ || /%f2/ || /%f3/) { $template='on'; }
if (-d "c:\\") {
$ListA="c:\\g$$.tmp";
$TmpFile="c:\\h$$.tmp";
unlink ($ListA, $TmpFile);
$sys="win";
}
else {
$ListA="/tmp/g$$.tmp";
$TmpFile="/dev/null";
unlink $ListA;
$sys="unix";
}
# ctlfilename = name used by control file (different for template option(
# file = file name (of first matching file)
$ctlfilename=$file;
# inventory of All records
if ($template eq "on") {
$gfile=$file;
if ($sys eq 'win') {
$gfile =~ s=\\=/=g;
}
$gfile =~ s/%y4/\\d{4}/g;
$gfile =~ s/%y2/\\d{2}/g;
$gfile =~ s/%m2/\\d{2}/g;
$gfile =~ s/%m1/\\d{1,2}/g;
$gfile =~ s/%d2/\\d{2}/g;
$gfile =~ s/%d1/\\d{1,2}/g;
$gfile =~ s/%h2/\\d{2}/g;
$gfile =~ s/%h1/\\d{1,2}/g;
$gfile =~ s/%h3/\\d{3}/g;
$gfile =~ s/%f2/\\d{2,3}/g;
$gfile =~ s/%f3/\\d{3}/g;
$dir=$gfile;
$dir =~ s=(/*)[^/]*$=$1=;
$gfile =~ s/^$dir//;
$head=$gfile;
$head =~ s=\\d\{.*==;
$tail=$gfile;
$tail =~ s=.*\\d\{.*\}==;
if ($dir eq "") {
opendir(DIR,'.');
}
else {
opendir(DIR,$dir);
}
@allfiles = grep /^$gfile$/, readdir DIR;
closedir DIR;
if ($#allfiles <= -1 ) {
print STDERR "\nError: could not find any files in directory: $dir\n";
exit 8;
}
# allfiles has the name of all the files
# need to find times: t0, t1, .. t-las
for ($i = 0; $i <= $#allfiles; $i++) {
$_ = $allfiles[$i];
$_ =~ s/$head//;
$_ =~ s/$tail//;
# now $_ has the date code / forecast hour
if ($i == 0) {
$min_tval = $_;
$min_t2val = $_;
$max_tval = $_;
}
elsif ($min_tval eq $min_t2val && $min_tval eq $max_tval) {
if ($_ > $min_tval) {
$min_t2val = $_;
$max_tval = $_;
}
else {
$min_tval = $_;
}
}
else {
if ($_ > $max_tval) { $max_tval = $_; }
elsif ($_ < $min_tval) {
$min_t2val = $min_tval;
$min_tval = $_;
}
elsif ($_ < $min_t2val && $_ > $min_tval) {
$min_t2val = $_;
}
}
}
$file="$dir$head$min_tval$tail";
if ($sys eq 'win') {
$file =~ s=/=\\=g;
}
# make inventory of first two files and last file
# need to get dt and last date
system "$wgrib $wflag -v $dir$head$min_tval$tail >$ListA";
if ($#allfiles >= 1) {
system "$wgrib $wflag -v $dir$head$min_t2val$tail >>$ListA";
}
if ($#allfiles >= 2) {
system "$wgrib $wflag -v $dir$head$max_tval$tail >>$ListA";
}
}
else {
system "$wgrib $wflag -v $file >$ListA";
}
if ( ! -s $ListA ) {
print STDERR "Big problem:\n";
print STDERR " either $file is missing or not a grib file\n";
print STDERR " wgrib is not on your path or can not write to $ListA (full disk or permissions)\n";
exit 8;
}
# make table of dates and variables
open (FileDate, "<$ListA");
while (defined($_ = <FileDate>)) {
# date table
$_ =~ s/^.*D=//;
$d=substr($_, 0, 10);
$dates{$d}="";
# variable/level list
@Fld = split(':', $_, 99);
$kpds=substr($Fld[3],5);
($kpds5,$kpds6,$kpds7) = split(/,/,$kpds);
$varname = "$Fld[1]:$kpds6";
if (defined $flevels{$varname}) {
if (!($flevels{$varname} =~ / $kpds7 /)) {
$flevels{$varname} .= "$kpds7 ";
}
}
else {
$flevels{$varname} = " $kpds7 ";
$fcomments{$varname} = "$kpds5:$Fld[$#Fld]";
}
}
close (FileDate);
if ($sys eq "win") {
unlink $TmpFile;
}
unlink $ListA;
@sdates=sort keys(%dates);
# number of time 1 or greater
$ntime=$#sdates + 1;
$time=$sdates[0];
$year = substr($time,0,4);
$mo = substr($time,4,2);
$day = substr($time,6,2);
$hour = substr($time,8,2);
if ("$analysis" ne "") {
$yeara = substr($analysis,0,4);
$moa = substr($analysis,4,2);
$daya = substr($analysis,6,2);
$houra = substr($analysis,8,2);
}
else {
$yeara = $year;
$moa = $mo;
$daya = $day;
$houra = $hour;
}
if ($mo < 0 || $mo > 12) {
print "illegal date code $time\n";
exit 8;
}
if ($ntime == 1 && "$analysis" ne "") {
if ($analysis <= $time) {
$year1 = $year;
$mo1 = $mo;
$day1 = $day;
$hour1 = $hour;
$year_last = $year;
$mo_last = $mo;
$day_last = $day;
$hour_last = $hour;
$year = $yeara;
$mo = $moa;
$day = $daya;
$hour = $houra;
} else {
$year1 = $yeara;
$mo1 = $moa;
$day1 = $daya;
$hour1 = $houra;
$year_last = $yeara;
$mo_last = $moa;
$day_last = $daya;
$hour_last = $houra;
$yeara = $year;
$moa = $mo;
$daya = $day;
$houra = $hour;
}
$ntime = 2;
}
elsif ($ntime > 1) {
$year1 = substr($sdates[1],0,4);
$mo1 = substr($sdates[1],4,2);
$day1 = substr($sdates[1],6,2);
$hour1 = substr($sdates[1],8,2);
$year_last = substr($sdates[$#sdates],0,4);
$mo_last = substr($sdates[$#sdates],4,2);
$day_last = substr($sdates[$#sdates],6,2);
$hour_last = substr($sdates[$#sdates],8,2);
}
# ---------------intro------------------------------------
if ("$index" eq "") {$index="$file.idx";}
if ("$pdef" eq "") { $pdef = "$file.pdef";}
if ($sys eq "unix") {
$caret1 = (substr($file,0,1) eq "/") ? "" : '^';
$caret2 = (substr($index,0,1) eq "/") ? "" : '^';
$caret3 = (substr($pdef,0,1) eq "/") ? "" : '^';
}
else {
$caret1 = (substr($file,1,1) eq ":") ? "" : '^';
$caret2 = (substr($index,1,1) eq ":") ? "" : '^';
$caret3 = (substr($pdef,1,1) eq ":") ? "" : '^';
}
print "dset $caret1$ctlfilename\nindex $caret2$index\n";
print "undef 9.999E+20\ntitle $file\n* produced by grib2ctl v$version\n";
# ------------------- grid -----------------------
$griddef = `$wgrib $wflag -V $file -d 1 -o $TmpFile`;
$_=$griddef;
/ winds\((\S*)\)/;
$winds=$1;
/ center (\S*) /;
$center=$1;
/ subcenter (\S*) /;
$subcenter=$1;
/ grid=(\S*) /;
$grid=$1;
$LCC='lcc';
if ( $winds eq 'grid') { $LCC='lccr'; }
if ($center == 7 && $subcenter == 15) { $LCC='lcc'; }
# ------------ test for bad ecmwf reduced gaussian grids ------------
if ($center == 98) {
if (/ thinned gaussian:/) {
/ long (\S*) to (\S*), (\S*) grid pts \(-1 x (\S*)\) /;
$lon0=$1;
$lon1=$2;
$nxny = $3;
$ny = $4;
if ($lon0 != $lon1) {
s/\n//g;
/ bdsgrid \S* (.*) min/;
$list=$1;
($maxlons) = split(' ' , $list);
$minlons = $maxlons;
foreach $t (split(' ',$list)) {
if ($t > $maxlons) { $maxlons = $t; }
if ($t < $minlons) { $minlons = $t; }
}
if ($lon1 < $lon0) { $lon1 += 360.0; }
if (abs(360 - 360/$maxlons - $lon1) < 0.001) {
print "* -ec_gbl set\n";
$ec_gbl='1';
}
if (abs(360 - 360/$minlons - $lon1) < 0.001) {
print "* -ec_gbl set.\n";
$ec_gbl='1';
}
}
}
}
print "dtype grib $grid\n";
if ($template eq "on") {
print "options template\n";
}
if ($raw eq 'yes') {
/ nx (\S*) ny (\S*) /;
$nx=$1;
$ny=$2;
print "xdef $nx linear 0 0.1\n";
print "ydef $ny linear 0 0.1\n";
}
elsif (/ thinned latlon: /) {
/ lat *(\S*) to (\S*) by (\S*) /;
$lat0 = $1;
$lat1 = $2;
$dy = $3;
/ long (\S*) to (\S*), (\S*) grid pts \(-1 x (\S*)\) /;
$lon0=$1;
$lon1=$2;
$nxny = $3;
$ny = $4;
if ($lat0 > $lat1) {
$yrev = 1;
print "ydef $ny linear $lat1 ", abs($dy), "\n"
}
else {
$yrev = 0;
print "ydef $ny linear $lat0 ", abs($dy), "\n"
}
$t=$_;
s/\n//g;
/ bdsgrid \S* (.*) min/;
$list=$1;
$i = 1;
foreach $t (split(' ',$list)) {
$nlon[$i++] = $t;
}
$nx=$nlon[1];
if ($nx < $nlon[$ny]) { $nx = $nlon[$ny]; }
if ($lon1 <= $lon0) { $lon1 += 360.0; }
$dx = ($lon1 - $lon0) / ($nx - 1);
print "xdef $nx linear $lon0 $dx\n";
# now to create the pdef file
open (PDEF, ">$pdef");
binmode PDEF;
print "pdef $nxny 1 file 1 stream binary $caret3$pdef\n";
if ($yrev == 0) {
$offset = 0;
for ($j = 1; $j <= $ny; $j++) {
for ($i = 0; $i < $nx; $i++) {
$x = $i / ($nx - 1.0) * ($nlon[$j] - 1);
$x = floor($x + 0.5) + $offset + $pdef_offset;
print PDEF pack("L", $x);
}
$offset += $nlon[$j];
}
}
else {
$offset = $nxny;
for ($j = $ny; $j >= 1; $j--) {
$offset -= $nlon[$j];
for ($i = 0; $i < $nx; $i++) {
$x = $i / ($nx - 1.0) * ($nlon[$j] - 1);
$x = floor($x + 0.5) + $offset + $pdef_offset;
print PDEF pack("L", $x);
}
}
}
# print weights
$x = pack("f", 1.0);
for ($i = 0; $i < $nx*$ny; $i++) {
print PDEF $x;
}
# print wind rotation
$x = pack("L", -999);
for ($i = 0; $i < $nx*$ny; $i++) {
print PDEF $x;
}
close(PDEF);
}
elsif (/ latlon: /) {
/ lat (\S*) to (\S*) by (\S*) /;
$lat0=$1;
$lat1=$2;
$dlat=$3;
/ long (\S*) to (\S*) by (\S*), \((\S*) x (\S*)\)/;
$lon0=$1;
# $lon1=$2;
$dlon=$3;
$nx =$4;
$ny =$5;
if ($lat0 > $lat1) {
print "options yrev\n";
print "ydef $ny linear $lat1 ", abs($dlat), "\n"
}
else {
print "ydef $ny linear $lat0 ", abs($dlat), "\n"
}
print "xdef $nx linear $lon0 $dlon\n";
}
elsif ($grid == 5 && $center == 7) {
print "pdef 53 57 nps 27 49 -105 190.5\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 6 && $center == 7) {
print "pdef 53 45 nps 27 49 -105 190.5\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 87 && $center == 7) {
print "pdef 81 62 nps 31.9 112.53 -105 68.513\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 96 && $kludge eq "on" && $center == 7) {
# quick and dirty pdef for eta 12 km
# use -kludge option
print "pdef 606 1067 eta.u -111 50 0.17520661 0.075046904\n";
print "xdef 1440 linear -200 0.125\n";
print "ydef 721 linear 0 0.125\n";
}
elsif ($grid == 101 && $center == 7) {
print "pdef 113 91 nps 58.5 92.5 -105 91.452\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 104 && $center == 7) {
print "pdef 147 110 nps 75.5 109.5 -105 90.75464\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 105 && $center == 7) {
print "pdef 83 83 nps 40.5 88.5 -105 90.75464\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 106 && $center == 7) {
print "pdef 165 117 nps 80 176 -105 45.37732\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 107 && $center == 7) {
print "pdef 120 92 nps 46 167 -105 45.37732\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 192 && $rr ne "" && $center == 7) {
# quick and dirty pdef for RR egrid
# need -rr option to use
print "pdef 237 387 eta.u -111 50 0.4491525 0.2072539\n";
print "xdef 360 linear -180 0.5\n";
print "ydef 181 linear 0 0.5\n";
}
elsif ($grid == 201 && $center == 7) {
print "pdef 65 65 nps 33 33 -105 381\n";
print "xdef 180 linear -180 2\n";
print "ydef 51 linear -10 2\n";
}
elsif ($grid == 202 && $center == 7) {
print "pdef 65 43 nps 33 45 -105 190.5\n";
print "xdef 91 linear -200 2\n";
print "ydef 41 linear 10 2\n";
}
elsif ($grid == 203 && $center == 7) {
print "pdef 45 39 nps 27 37 -150 190.5\n";
print "xdef 103 linear -250 2\n";
print "ydef 33 linear 26 2\n";
}
elsif ($grid == 205 && $center == 7) {
print "pdef 45 39 nps 27 57 -60 190.5\n";
print "xdef 50 linear -120 2\n";
print "ydef 46 linear 0 2\n";
}
elsif ($grid == 207 && $center == 7) {
print "pdef 49 35 nps 25 51 -150 95.25\n";
print "xdef 51 linear -200 2\n";
print "ydef 30 linear 45 1\n";
}
elsif ($grid == 211 && $center == 7) {
# awips lambert conformal
print "pdef 93 65 $LCC 12.19 -133.459 1 1 25 25 -95 81270.5 81270.5\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
elsif ($grid == 212 && $center == 7) {
# awips lambert conformal
print "pdef 185 129 $LCC 35.0 -95.0 105 49 25 25 -95 40635 40635\n";
print "xdef 181 linear -140 0.5\n";
print "ydef 91 linear 15 0.5\n";
}
elsif ($grid == 213 && $center == 7) {
print "pdef 129 85 nps 65 89 -105 95.25\n";
print "xdef 170 linear -190 1\n";
print "ydef 81 linear 10 1\n";
}
elsif ($grid == 215 && $center == 7) {
# lambert conformal
print "pdef 369 257 $LCC 12.19 -133.46 1 1 25 25 -95 20318 20318\n";
print "xdef 289 linear -136 0.25\n";
print "ydef 157 linear 18 0.25\n";
}
elsif ($grid == 216 && $center == 7) {
print "pdef 147 110 nps 75.5 109.5 -105 91.452\n";
print "xdef 181 linear -180 1\n";
print "ydef 91 linear 0 1\n";
}
elsif ($grid == 221 && $nearest_neighbor eq "on" && $center == 7) {
# awips lambert conformal nearest neighbor
print "pdef 96673 1 file 1 sequential binary-little $pdef_dir/grib221nn.pdef\n";
print "xdef 1111 linear -250 0.333333\n";
print "ydef 247 linear 8 0.333333\n";
}
elsif ($grid == 221 && $center == 7) {
# awips lambert conformal
print "pdef 349 277 $LCC 1 -145.5 1 1 50 50 -107 32463 32463\n";
print "xdef 1111 linear -250 0.333333\n";
print "ydef 247 linear 8 0.333333\n";
}
# old grid 240
#elsif ($grid == 240) {
# # nps usa
# print "pdef 1160 880 nps 441 1601 255 4.763\n";
# print "xdef 801 linear -130 0.1\n";
# print "ydef 401 linear 20 0.1\n";
#}
# new grid 240
elsif ($grid == 240 && $center == 7) {
# nps usa
print "pdef 1121 881 nps 401 1601 255 4.7625\n";
print "xdef 1601 linear -130 0.05\n";
print "ydef 801 linear 20 0.05\n";
}
elsif ($grid == 241 && $center == 7) {
print "pdef 386 293 nps 147.315 534.0 -105 14.2875\n";
print "xdef 161 linear -140 0.5\n";
print "ydef 81 linear 20 0.5\n";
}
else {
# unknown grid
$_ = $griddef;
GRD: {
/ polar stereo: Lat1 16.125000 Long1 234.983000 Orient -100.0/ && do {
print "options yrev\n";
print "pdef 129 86 nps 64 136 -100 60\n";
print "xdef 720 linear 0 0.5\n";
print "ydef 148 linear 16 0.5\n";
last GRD; };
/ polar stereo: Lat1 -4.860000 Long1 -122.614000 Orient -80.000000/ && do {
print "pdef 49 51 nps 24 26 -80 381\n";
print "xdef 144 linear 0 2.5\n";
print "ydef 45 linear -20 2.5\n";
last GRD; };
/ Lambert Conf:.* Lov 265.*\(151 x 113\)/s && do {
print "options yrev\n";
print "pdef 151 113 $LCC 16.281 233.8622 1 1 25 25 265 40635 40635\n";
print "xdef 141 linear -130 0.5\n";
print "ydef 71 linear 20 0.5\n";
last GRD; };
# beta: mercator
# scan modes .. assumes west to east
/ Mercator: / && do {
/lat *(\S*) to (\S*) /;
$lat1 = $1;
$lat2 = $2;
/long (\S*) to (\S*) /;
$lon1 = $1;
$lon2 = $2;
/ nx (\S*) ny (\S*) /;
$nx = $1;
$ny = $2;
if ($lat1 > $lat2) {
print "options yrev\n";
$t = $lat2;
$lat2 = $lat1;
$lat1 = $t;
}
print "ydef $ny levels\n";
$i = 0;
$n1 = log(tan((45+$lat1/2)*3.1415927/180));
$n2 = log(tan((45+$lat2/2)*3.1415927/180));
$dy = ($n2 - $n1) / ($ny - 1);
while ($i < $ny) {
$nn = $n1 + $dy * $i;
$lat = (atan(exp($nn))*180/3.1415927-45)*2;
printf ("%9.4f ", $lat);
$i++;
if ($i % 7 == 0) { print "\n"; }
}
if ($i % 7 != 0) { print "\n"; }
$dlon = $lon2 - $lon1;
if ($dlon < 0) {
$dlon = $dlon + 360;
}
$dlon = $dlon / ($nx - 1);
print "xdef $nx linear $lon1 $dlon\n";
last GRD; };
# beta: generalized lambert conformal pdef/xdef/ydef
# not very good .. needs to calculate the all
# vertices for better xdef and ydef
# for improvements .. pull out code from lcgrib
/ Lambert Conf: / && do {
/ Lat1 (\S*) Lon1 (\S*) Lov (\S*)/;
$lat1 = $1;
$lon1 = $2;
$lov = $3;
/Latin1 (\S*) Latin2 (\S*) /;
$latin1 = $1;
$latin2 = $2;
/Pole \((\S*) x (\S*)\) Dx (\S*) Dy (\S*) /;
$nx = $1;
$ny = $2;
$dx = 1000*$3;
$dy = 1000*$4;
print "pdef $nx $ny $LCC $lat1 $lon1 1 1 $latin1 $latin2 $lov $dx $dy\n";
if ($global_map eq "") {
$dx = $dx / (110000.0 * cos($lat1*3.141592654/180.0));
$dy = $dy / 110000.0;
if ($lon1 > 180) {
$lon1 = $lon1 - 360.0;
}
print "xdef $nx linear $lon1 $dx\n";
print "ydef $ny linear $lat1 $dy\n";
}
else {
print "xdef 360 linear 0 1\n";
print "ydef 181 linear -90 1\n";
}
last GRD; };
/ thinned gaussian:/ && do {
/ lat *(\S*) to (\S*)/;
$lat0 = $1;
$lat1 = $2;
/ long (\S*) to (\S*), (\S*) grid pts \(-1 x (\S*)\) /;
$lon0=$1;
$lon1=$2;
$nxny = $3;
$ny = $4;
print "ydef $ny levels\n";
$yrev = 0;
if ($lat0 > $lat1) { $yrev = 1; }
$eps = 3e-14;
$m=int(($ny+1)/2);
# get gaussian latitudes
$i=1;
while ($i <= $m) {
$z=cos(3.141592654*($i-0.25)/($ny+0.5));
do {
$p1 = 1;
$p2 = 0;
$j = 1;
while ($j <= $ny) {
$p3 = $p2;
$p2 = $p1;
$p1=((2*$j-1)*$z*$p2-($j-1)*$p3)/$j;
$j++;
}
$pp = $ny*($z*$p1-$p2)/($z*$z-1);
$z1 = $z;
$z = $z1 - $p1/$pp;
} until abs($z-$z1) < $eps;
$x[$i] = -atan2($z,sqrt(1-$z*$z))*180/3.141592654;
$x[$ny+1-$i] = -$x[$i];
$i++;
}
$i = 1;
while ($i < $ny) {
printf " %7.3f", $x[$i];
if (($i % 10) == 0) { print "\n"; }
$i++;
}
printf " %7.3f\n", $x[$ny];
$t=$_;
s/\n//g;
/ bdsgrid \S* (.*) min/;
$list=$1;
$i = 1;
$nx = 0;
foreach $t (split(' ',$list)) {
$nlon[$i++] = $t;
if ($nx < $t) { $nx = $t; }
}
if ($lon1 <= $lon0) { $lon1 += 360.0; }
if ($ec_gbl eq "") {
$dx = ($lon1 - $lon0) / ($nx - 1);
$t = 1;
}
else {
$dx = 360.0 / $nx;
$t = 0;
}
print "xdef $nx linear $lon0 $dx\n";
# now to create the pdef file
open (PDEF, ">$pdef");
binmode PDEF;
if ($pdef_nearest == 0) {
gau_pdf_linear ();
}
else {
gau_pdf_near ();
}
close(PDEF);
last GRD; };
/ gaussian:/ && do {
/ lat (\S*) to (\S*)/;
$lat0=$1;
$lat1=$2;
/ long (\S*) to (\S*) by (\S*), \((\S*) x (\S*)\)/;
$lon0=$1;
# $lon1=$2;
$dlon=$3;
$nx =$4;
$ny =$5;
$dlon = 360 / $nx;
if ($lat0 > $lat1) {
print "options yrev\n";
}
print "xdef $nx linear $lon0 $dlon\n";
print "ydef $ny levels\n";
$eps = 3e-14;
$m=int(($ny+1)/2);
$i=1;
while ($i <= $m) {
$z=cos(3.141592654*($i-0.25)/($ny+0.5));
do {
$p1 = 1;
$p2 = 0;
$j = 1;
while ($j <= $ny) {
$p3 = $p2;
$p2 = $p1;
$p1=((2*$j-1)*$z*$p2-($j-1)*$p3)/$j;
$j++;
}
$pp = $ny*($z*$p1-$p2)/($z*$z-1);
$z1 = $z;
$z = $z1 - $p1/$pp;
} until abs($z-$z1) < $eps;
$x[$i] = -atan2($z,sqrt(1-$z*$z))*180/3.141592654;
$x[$ny+1-$i] = -$x[$i];
$i++;
}
$i = 1;
while ($i < $ny) {
printf " %7.3f", $x[$i];
if (($i % 10) == 0) { print "\n"; }
$i++;
}
printf " %7.3f\n", $x[$ny];
last GRD; };
# rotated LatLon grid
/ rotated LatLon grid/ && do {
/ LatLon grid lat (\S*) to (\S*) lon (\S*) to (\S*)/;
$lat0 = $1;
$lat1 = $2;
$lon0 = $3;
$lon1 = $4;
/nxny \S+ \((\S*) x (\S*)\)/;
$nx = $1;
$ny = $2;
/ south pole lat (\S*) lon (\S*) rot angle (\S*)/;
$lat_sp = $1;
$lon_sp = $2;
$rot_angle = $3;
print "* Rotated LatLon grid: South pole lat $lat_sp lon $lon_sp",
" rot angle $rot_angle\n";
$dlon = ( $lon1-$lon0)/($nx-1);
$dlat = ( $lat1-$lat0)/($ny-1);
$t0 = $lon_sp+180.0;
if ($t0 > 360.0) { $t0 -= 360.0; }
$t1 = -$lat_sp;
$pdef='rotll';
if ($winds eq 'grid') { $pdef='rotllr'; }
print "pdef $nx $ny $pdef $t0 $t1 $dlon $dlat $lon0 $lat0\n";
($swlat, $swlon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat1,$lon0);