forked from NCAR/UFFDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g2ctl.pl
executable file
·1742 lines (1518 loc) · 45.4 KB
/
g2ctl.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 grib2 files
#
# requires wgrib2 and Perl5
#
# usage: g2ctl [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)
#
# $ g2ctl -0 example.grib >example.ctl
# $ gribmap -0 -i example.ctl
#
# Forecasts: (using verifiation time)
#
# $ g2ctl example.grib >example.ctl
# $ gribmap -i example.ctl
#
# bugs:
# many
#
# requires new wgrib2
#
# documentation: http://www.cpc.ncep.noaa.gov/products/wesley/g2ctl.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
#
# requires grads 2.0a3+ - thinned grid update
#
$version="0.1.2";
use POSIX;
use Math::Trig qw(deg2rad rad2deg);
# ***** if wgrib2 is not on path, add it here
$wgrib2='./wgrib2';
# $wgrib2='/export/cpc-lw-webisuzak/wd51we/grib2/wgrib2/wgrib2';
$wflag="";
$tflag="-VT";
$file="";
$index="";
$pdef="";
$prs="prs";
$calendar="";
$lc="";
$timestep="";
# $nearest_neighbor="";
$kludge="";
$no235z="";
$template="";
$pdef_nearest=1;
$raw="no";
$thinned_grid_msg=0;
$time_info=0;
$old_ens_name=0;
# set pdef_offset = 0 for old code, 1 for new code
$pdef_offset=0;
$use_minutes=1;
# vertical levels allowd, index = grib level number
$allow_profile[20] = '-tmp';
$profile_units[20] = 'temperature';
$allow_profile[100] = '-prs';
$profile_units[100] = 'hPa';
$allow_profile[102] = '+m';
$profile_units[102] = 'm above MSL';
$allow_profile[103] = '+m';
$profile_units[103] = 'm above ground';
$allow_profile[104] = '-';
$profile_units[104] = 'sigma';
$allow_profile[105] = '+hlev';
$profile_units[105] = 'hybrid';
$allow_profile[107] = '+iso';
$profile_units[107] = 'potential temperature';
$allow_profile[160] = '-dsl';
$profile_units[160] = 'm below sea level';
for ($i = 0; $i <= $#ARGV; $i++) {
$_ = $ARGV[$i];
SWITCH: {
/^-verf/ && do { $tflag="-VT" ; last SWITCH; };
/^-0/ && do { $tflag="-T" ; last SWITCH; };
/^-b/ && do { $tflag ="-start_FT" ; last SWITCH };
/^-no_prs/ && do { $prs="" ; last SWITCH; };
/^-365/ && do { $calendar="365"; last SWITCH; };
/^-no_min/ && do { $use_minutes = 0; last SWITCH; };
/^-ts(\d+\w+)/ && do { $timestep=$1; last SWITCH; };
/^-lc$/ && do { $lc="on"; last SWITCH; };
/^-kludge/ && do { $kludge="on"; last SWITCH; };
/^-no_235z/ && do { $no235z="on"; last SWITCH; };
/^-iso_profile/ && do { undef @allow_profile; $allow_profile[235]='yes'; last SWITCH };
/^-prs_profile/ && do { undef @allow_profile; $allow_profile[100]='yes'; last SWITCH };
/^-m_profile/ && do { undef @allow_profile; $allow_profile[103]='yes';
$allow_profile[105]='yes' ; last SWITCH };
/^-no_profile/ && do { undef @allow_profile; last SWITCH };
/^-time_info/ && do { $time_info=1 ; last SWITCH };
/^-raw/ && do { $raw="yes" ; last SWITCH };
/^-ens/ && do { @ens_names = split(',', $ARGV[++$i]);
$ens_match = $ARGV[$i];
$ens_match =~ s/,/|/g;
last SWITCH };
/^-old_ens_name/ && do { $old_ens_name=1; last SWITCH };
/^-pdef_linear/ && do { $pdef_nearest=0 ; 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 grib2 files\n";
print STDERR " usage: $0 [options] [grib_file] [optional index file] [optional pdef file] >[ctl file]\n";
print STDERR " -verf .. use end of ave/acc period or fcst time (default), run gribmap\n";
print STDERR " -0 .. use analysis or fcst_hour=0, run gribmap -0\n";
print STDERR " -b .. use use start of ave/acc period or fcst time, run gribmap -b\n";
print STDERR " -365 .. 365 day calendar\n";
print STDERR " -no_mins .. ignore minutes\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 " -time_info .. timing info in comment section\n";
print STDERR " -pdef_linear .. linear interpolation for thinned grids\n";
print STDERR " -raw .. raw grid\n";
print STDERR " -iso_profile .. make profile using subsurface isoterms\n";
print STDERR " -ens \"e1,..,en\" .. list of ensemble names for use with %e template ALPHA\n";
print STDERR " -old_ens_name .. use old ensemble names (versions before 8/2010)\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 grids, default: grib_file.pdef\n";
print STDERR "Note 3: template options supported: %y4 %y2 %m2 %m1 %d2 %d1 %h2 %h1 %n2 %f2 %f3 %e %j3\n";
print STDERR " %iy4 %iy2 %im2 %im1 %id2 %id1 %ih2 %ih1 %in2 %fn2 %fhn %fdhn\n";
exit 8;
}
$_ = $file;
if (/%y4/ || /%y2/ || /%m2/ || /%m1/ || /%d2/ || /%d1/ || /%h2/ || /%h1/ || /%n2/ || /%h1/
|| /%f2/ || /%j3/ || /%f3/ || /%e/ || /%iy2/ || /%iy4/ || /%im1/ || /%im2/ ||
/%id1/ || /%id2/ || /%ih1/ || /%ih2/ || /%in2/ || /%fn2/ || /%fhn/ || /%fdhn/ ) { $template='on'; }
# if (-d "c:\\") {
# if (-d "c:\\") {
if ($^O eq 'MSWin32') {
$ListA="$ENV{TEMP}\\h$$.tmp";
$TmpFile="NUL";
unlink $ListA;
$sys="win";
}
else {
$TmpFile="/dev/null";
$j=0;
while (-f "/tmp/g.$$.$j.tmp") {
$j++;
}
$ListA="/tmp/g.$$.$j.tmp";
$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/%iy4/\\d{4}/g;
$gfile =~ s/%y2/\\d{2}/g;
$gfile =~ s/%iy2/\\d{2}/g;
$gfile =~ s/%m2/\\d{2}/g;
$gfile =~ s/%im2/\\d{2}/g;
$gfile =~ s/%m1/\\d{1,2}/g;
$gfile =~ s/%im1/\\d{1,2}/g;
$gfile =~ s/%d2/\\d{2}/g;
$gfile =~ s/%id2/\\d{2}/g;
$gfile =~ s/%d1/\\d{1,2}/g;
$gfile =~ s/%id1/\\d{1,2}/g;
$gfile =~ s/%h2/\\d{2}/g;
$gfile =~ s/%ih2/\\d{2}/g;
$gfile =~ s/%h1/\\d{1,2}/g;
$gfile =~ s/%ih1/\\d{1,2}/g;
$gfile =~ s/%n2/\\d{1,2}/g;
$gfile =~ s/%in2/\\d{1,2}/g;
$gfile =~ s/%h3/\\d{3}/g;
$gfile =~ s/%f2/\\d{2,8}/g;
$gfile =~ s/%f3/\\d{3,8}/g;
$gfile =~ s/%j3/\\d{1,2,3}/g;
$gfile =~ s/%fn2/\\d{2}/g;
$gfile =~ s/%fhn/\\d{4}/g;
$gfile =~ s/%fdhn/\\d{6}/g;
$gfile_e = $gfile;
$dir_e = $gfile;
$gfile =~ s/%e/$ens_names[0]/;
$dir=$gfile;
$dir =~ s=(/*)[^/]*$=$1=;
$dir_e =~ s=(/*)[^/]*$=$1=;
$gfile =~ s/^$dir//;
$gfile_e =~ s/^$dir_e//;
$head=$gfile;
$head =~ s=\\d\{.*==;
$tail=$gfile;
$tail =~ s=.*\\d\{.*\}==;
if ($tail eq $head) { $tail=''; }
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 "$wgrib2 $wflag $tflag -lev -lev0 -ctl_ens -ftime -ctl_inv \"$dir$head$min_tval$tail\" >$ListA";
if ($#allfiles >= 1) {
system "$wgrib2 $wflag $tflag -lev -lev0 -ctl_ens -ftime -ctl_inv \"$dir$head$min_t2val$tail\" >>$ListA";
}
if ($#allfiles >= 2) {
system "$wgrib2 $wflag $tflag -lev -lev0 -ctl_ens -ftime -ctl_inv \"$dir$head$max_tval$tail\" >>$ListA";
}
}
else {
system "$wgrib2 $wflag $tflag -lev -lev0 -ctl_ens -ftime -ctl_inv \"$file\" >$ListA";
}
if ( ! -s $ListA ) {
print STDERR "Big problem:\n";
print STDERR " either $file is missing or not a grib file\n";
print STDERR " or wgrib2 is not on your path or wgrib2 is too old\n";
print STDERR " or can not write to $ListA (full disk or permissions).\n";
unlink $ListA;
exit 8;
}
# make table of dates and variables
scan_ListA();
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);
$minute = substr($time,10,2);
if ($use_minutes == 0) { $minute = 0; }
if ($mo < 0 || $mo > 12) {
print "illegal date code $time\n";
exit 8;
}
$month=substr("janfebmaraprmayjunjulaugsepoctnovdec",$mo*3-3,3);
if ($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);
$minute1 = substr($sdates[1],10,2);
if ($use_minutes == 0) { $minute1 = 0; }
$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);
$minute_last = substr($sdates[$#sdates],10,2);
if ($use_minutes == 0) { $minute_last = 0; }
}
# ---------------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 g2ctl v$version\n";
print "* command line options: @ARGV\n";
# ------------------- grid -----------------------
$griddef = `$wgrib2 $wflag \"$file\" -one_line -d 1 -nxny -grid -vector_dir`;
$griddef =~ / stagger *(\d*) /;
$stagger=$1 or $stagger=0;
if ($stagger ne 0) {
print STDERR "Staggered grids are not supported (stagger=$stagger)\n";
exit 8;
}
$_ = $griddef;
$_ =~ s/^[^(]*//;
$_ =~ s/:.*//;
/\((\S*) x -*(\d*)\)/;
$nx=$1;
$ny=$2;
$_=$griddef;
$t = substr($griddef,0,240);
print "* griddef=$t\n";
print "dtype grib2\n";
if ($template eq "on") { print "options template\n"; }
if ($raw eq 'yes') {
print "xdef $nx linear 0 0.1\n";
print "ydef $ny linear 0 0.1\n";
}
elsif (/: Gaussian grid:/) {
/ lon (\S*) to (\S*)/;
$lon0=$1;
$lon1=$2;
if ($lon1 <= $lon0) { $lon1 += 360.0; }
$dlon = ($lon1 - $lon0) / ($nx - 1);
/ lat (\S*) to (\S*)/;
if ($1 < $2) {
$lat0=$1;
$lat1=$2;
}
else {
$lat0=$2;
$lat1=$1;
}
/number of latitudes between pole-equator=(\S*)/;
$nGauLats=2*$1;
print "* ny=$ny nlat=$nGauLats lat0=$lat0 lat1=$lat1\n";
print "xdef $nx linear $lon0 $dlon\n";
print "ydef $ny levels\n";
&print_range_gau_lats;
}
elsif (/ thinned global Gaussian grid:/) {
/ input (\S*)/;
$scan = $1;
if ($scan ne 'WE:NS' && $scan ne 'WE:SN') { print "\n* **** unsupported scan mode $scan\n"; }
/#grid points by latitude: (.*)/;
$list = $1;
/#points=(\d*)/;
$npnts=$1;
$i = 1; $nx = 0;
foreach $t (split(' ',$list)) {
$t =~ s/:.*//;
$nlon[$i++] = $t;
if ($nx < $t) { $nx = $t; }
}
$dx = 360.0 / $nx;
$t = 0;
printf "xdef $nx linear 0 $dx\n";
print "ydef $ny levels\n";
&print_gau_lats;
# now to create the pdef file
open (PDEF, ">$pdef");
binmode PDEF;
$regional_thinned_grid=0;
if ($pdef_nearest == 0) {
pdef_linear();
} else {
pdef_near();
}
$thinned_grid_msg=1;
close(PDEF);
}
elsif (/ thinned (global|regional) lat-lon grid:/) {
/ lat *(\S*) to (\S*) by (\S*) /;
$lat0 = $1;
$lat1 = $2;
$dy = $3;
/ lon (\S*) to (\S*) with variable spacing/;
$lon0=$1;
$lon1=$2;
/\(-1 x (\d*)\)/;
$ny = $1;
/ #points=(\d*) /;
$npnts = $1;
/ input (\S*)/;
$scan = $1;
if ($scan ne 'WE:NS' && $scan ne 'WE:SN') { print "\n* **** unsupported scan mode $scan\n"; }
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/^.*points by latitude: //;
$_ =~ s/:.*//;
$list=$_;
$i = 1;
foreach $t (split(' ',$list)) {
$nlon[$i++] = $t;
}
if ($lon1 <= $lon0) { $lon1 += 360.0; }
#
# check if global
#
$nx = $nlon[$ny];
$dx = ($lon1 - $lon0) / ($nx - 1);
$dx=$dx*$nx;
$regional_thinned_grid = 1;
if (abs($dx-360.0) < 0.01) {$regional_thinned_grid=0; }
#
# figure out dx and nx for the projection grid
#
$nx=$nlon[1];
if ($nx < $nlon[$ny]) { $nx = $nlon[$ny]; }
$dx = ($lon1 - $lon0) / ($nx - 1);
print "xdef $nx linear $lon0 $dx\n";
open (PDEF, ">$pdef");
binmode PDEF;
$regional_thinned_grid=1;
if ($pdef_nearest == 0) {
pdef_linear();
} else {
pdef_near();
}
close(PDEF);
$thinned_grid_msg=1;
}
elsif (/: lat-lon grid:/) {
/ lat (\S*) to (\S*) by (\S*)/;
$lat0=$1;
$lat1=$2;
$dlat=$3;
/ lon (\S*) to (\S*) by (\S*)/;
$lon0=$1;
# $lon1=$2;
$dlon=$3;
if ($lat0 > $lat1) {
print "ydef $ny linear $lat1 ", abs($dlat), "\n"
}
else {
print "ydef $ny linear $lat0 ", abs($dlat), "\n"
}
if ($lon0 + ($nx-1) * $dlon > 360.0) { $lon0 -= 360.0; }
print "xdef $nx linear $lon0 $dlon\n";
}
elsif (/ Mercator grid: /) {
# beta: mercator
# scan modes .. assumes west to east
/lat *(\S*) to (\S*) /;
$lat1 = $1;
$lat2 = $2;
/lon (\S*) to (\S*) /;
$lon1 = $1;
$lon2 = $2;
/grid: \((\d*) x (\d*)\) /;
$nx = $1;
$ny = $2;
$dlon = $lon2 - $lon1;
if ($dlon <= 0) {
$dlon = $dlon + 360;
}
$dlon = $dlon / ($nx - 1);
if ($lon1 + ($nx-1) * $dlon > 360.0) { $lon1 -= 360.0 }
print "xdef $nx linear $lon1 $dlon\n";
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"; }
}
elsif (/ Lambert Conformal: /) {
/ Lat1 (\S*) Lon1 (\S*) LoV (\S*) LatD \S* Latin1 (\S*) Latin2 (\S*) /;
$lat1 = $1;
$lon1 = $2;
$lov = $3;
$latin1 = $4;
$latin2 = $5;
/Pole \((\S*) x (\S*)\) Dx (\S*) m Dy (\S*) m /;
$nx = $1;
$ny = $2;
$dx = $3;
$dy = $4;
$t='lcc';
if (/:winds\(grid\)/) { $t='lccr' ; }
if ($dx == 0 || $dy == 0) {
print STDERR "Problem: DX or DY is missing! Cannot make a control file\n";
exit;
}
&domain;
# make sure than lon1 is within 180 degrees of lov
# lov, lon1 should be between 0 and 360
if ($lon1 < $lov - 180.0) { $lon1 += 360.0; }
if ($lon1 > $lov + 180.0) { $lon1 -= 360.0; }
# make sure that lov is within 180 degrees of west
# lov is between 0 and 360 and west is between -180 and 180
if ($lov < ($west+$east)/2 - 180.0) { $lov += 360.0 ; $lon1 += 360.0; };
if ($lov > ($west+$east)/2 + 180.0) { $lov -= 360.0 ; $lon1 -= 360.0; };
print "pdef $nx $ny $t $lat1 $lon1 1 1 $latin1 $latin2 $lov $dx $dy\n";
# if ($lon1 - $west >= 360.0) {
# $lon1 -= 360.0;
# $lov -= 360.0;
# }
# if ($lov < $lon1 - 180.0) { $lov += 360.0; }
# if ($lov > $lon1 + 180.0) { $lov -= 360.0; }
# print "pdef $nx $ny $t $lat1 $lon1 1 1 $latin1 $latin2 $lov $dx $dy\n";
$dx = $dx / (110000.0 * cos($lat1*3.141592654/180.0));
$dy = $dy / 110000.0;
$tmp = int(($east - $west) / $dx + 1);
if (($west == -180) && ($east == 180)) {
$tmp -= 1;
}
print "xdef $tmp linear $west $dx\n";
$tmp = int(($north - $south) / $dy + 1);
print "ydef $tmp linear $south $dy\n";
}
elsif (/ polar stereographic grid: /) {
/ (\S*) pole lat1 (\S*) lon1 (\S*) latD (\S*) lonV (\S*) dx (\S*) m dy (\S*) m/;
$pole = $1;
$lat1 = $2;
$lon1 = $3;
$orient = $5;
$dx = $6;
$dy = $7;
/ \((\S*) x (\S*)\) input (\S*) /;
$nx=$1;
$ny=$2;
$scan=64;
# $scan=-1;
# if ($3 eq 'WE:NS') { $scan = 0; }
# if ($3 eq 'WE:SN') { $scan = 64; }
# if ($scan == -1) { printf STDERR "HELP polar stereographic code needs to be extended!!\n"; }
# probably only works for scan=64
$dpr=3.14159265358979/180.0;
$rearth=6.3712e6;
$h=1;
$proj="nps";
if ($pole eq "South") {
$h=-1;
$orient -= 180.0;
$proj="sps";
}
$hi=1;
$hj=-1;
if (($scan/128 % 2) == 1) {
$hi=-1;
}
if (($scan/64 % 2) == 1) {
$hj=1;
}
if ($dx == 0 || $dy == 0) {
print STDERR "Problem: DX or DY is missing! Cannot make a control file\n";
exit;
}
$dxs=$dx*$hi;
$dys=$dy*$hj;
$de=(1+sin(60*$dpr))*$rearth;
$dr=$de*cos($lat1*$dpr)/(1+$h*sin($lat1*$dpr));
$xp=1-$h*sin(($lon1-$orient)*$dpr)*$dr/$dxs;
$yp=1+cos(($lon1-$orient)*$dpr)*$dr/$dys;
$dx=$h*$dx/1000;
printf "pdef $nx $ny $proj $xp $yp $orient $dx\n";
&domain;
if ( 1 <= $xp && $xp <= $nx && 1 <= $yp && $yp <= $ny) {
# pole is in the grid!
$dx = 1000.0 * abs($dx);
$nx = int(2 * $rearth * 3.14 / $dx + 1);
$dx = 360 /($nx-1);
print "xdef $nx linear -180 $dx\n";
if ($proj eq "nps") { $north=90; }
else { $south = -90; }
$dy = abs($dy);
$ny = int($rearth * 3.14 * ($north-$south)/180.0 / $dy + 1);
$dy = ($north-$south)/($ny-1);
$tmp = int(($north - $south) / $dy + 1);
print "ydef $tmp linear $south $dy\n";
}
else {
$dx = 1000.0 * abs($dx);
$nx = int(2 * $rearth * 3.14 * ($east-$west)/360.0 / $dx + 1);
$dx = ($east-$west) /($nx-1);
# make dx a faction of 360 so grads can id as a global field
$nx = int(360.0 / $dx) + 1.0;
$dx = 360.0 / $nx;
$nx = int( ($east-$west)/$dx ) + 1;
$dy = abs($dy);
$ny = int($rearth * 3.14 * ($north-$south)/180.0 / $dy + 1);
$dy = ($north-$south)/($ny-1);
$tmp = int(($east - $west) / $dx + 1);
if (($west == -180) && ($east == 180)) {
$tmp -= 1;
}
print "xdef $tmp linear $west $dx\n";
$tmp = int(($north - $south) / $dy + 1);
print "ydef $tmp linear $south $dy\n";
}
}
elsif ( / thinned gaussian:/) {
/ 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);
$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; }
$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);
$thinned_grid_msg=1;
}
# rotated LatLon grid
elsif (/ rotated lat-lon grid/) {
/ lat (\S*) to (\S*) by (\S*)/;
$lat0 = $1;
$lat1 = $2;
$dlat = $3;
/ lon (\S*) to (\S*) by (\S*)/;
$lon0 = $1;
$lon1 = $2;
$dlon = $3;
if ($lon0 > 180.0) { $lon0 -= 360.0; }
/rotated lat-lon grid:\((\S*) x (\S*)\)/;
$nx = $1;
$ny = $2;
/ south pole lat=(\S*) lon=(\S*) angle of rot=(\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";
if ($dlon < $dlat) { $dlat = $dlon ;}
$dlon = $dlat;
# $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\(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);
if ($lat0 > $lat1) {
($swlat, $swlon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat1,$lon0);
($nwlat, $nwlon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat0,$lon0);
($nelat, $nelon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat0,$lon1);
($selat, $selon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat1,$lon1);
$northlat=($nwlat>$nelat)?$nwlat:$nelat;
$southlat=($swlat<$selat)?$swlat:$selat;
$goodny=($northlat-$southlat)/$dlat;
$nlat=$northlat-fmod($northlat,abs($dlat));
print "options yrev\n";
print "ydef $goodny linear $nlat ", abs($dlat), "\n"
}
else {
($swlat, $swlon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat0,$lon0);
($nwlat, $nwlon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat1,$lon0);
($nelat, $nelon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat1,$lon1);
($selat, $selon) = rr2ll($lat_sp,$lon_sp,$rot_angle,$lat0,$lon1);
$northlat=($nwlat>$nelat)?$nwlat:$nelat;
$southlat=($swlat<$selat)?$swlat:$selat;
$goodny=floor(($northlat-$southlat)/$dlat)+1;
$slat=$southlat-fmod($southlat,abs($dlat));
print "ydef $goodny linear $slat ", abs($dlat), "\n"
}
$eastlon=($nelon>$selon)?$nelon:$selon;
$westlon=($swlon<$nwlon)?$swlon:$nwlon;
$goodnx=floor(($eastlon-$westlon)/$dlon)+1;
$wlon=$westlon-fmod($westlon,abs($dlon));
print "xdef $goodnx linear $wlon $dlon\n";
}
elsif (/:grid_template=204:/) {
print "pdef 1200 1684 bilin sequential binary-big pdef_ncep_12.atl\n";
print "xdef 1300 linear -105.04 0.1325\n";
print "ydef 1500 linear -25.2 0.0678\n";
}
else {
print STDERR "*** script needs to be modified ***\n";
print STDERR "*** unknown grid ***\n";
exit 8;
}
# make the tdef statement
&tdef;
# ------------------var-------------------------------------;
# Find the vertical profile
$nlevelmax=0;
$levelsmax=0;
$profile_kpds6=-1;
foreach $fname (sort keys(%flevels)) {
($name, $kpds6) = split(/:/, $fname);
if (defined($allow_profile[$kpds6])) {
$_=$flevels{$fname};
$nlev = (tr/ / /) - 1;
if ($nlev > $nlevelmax) {
$nlevelmax = $nlev;
$levelsmax = $flevels{$fname};
$profile_kpds6 = $kpds6;
}
}
}
if ($nlevelmax <= 1) {
print "zdef 1 linear 1 1\n";
}
else {
print "* PROFILE $profile_units[$profile_kpds6]\n";
print "zdef $nlevelmax levels ";
$tail=substr($allow_profile[$profile_kpds6],1);
($_ = $levelsmax) =~ s/.//;
chop($_);
if (substr($allow_profile[$profile_kpds6],0,1) eq '+') {
$levelsmax = join ' ', sort {$a <=> $b} split(/ /,$_);
}
else {
$levelsmax = join ' ', sort {$b <=> $a} split(/ /,$_);
}
print "$levelsmax\n";
if ($profile_kpds6 == 100) { print "options pascals\n"; }
}
if ($nlevelmax == 1) { $profile_kpds6 = -1; }
$nvar = 0;
#
foreach $fname (sort keys(%fcomments)) {
($name, $kpds6) = split(/:/, $fname);
($kpds5, $comment) = split(/:/, $fcomments{$fname});
# fix names to be grads compatible
# eliminate dashes, underscores, blanks and put no in front of leading digits
$_ = $name;
$_ =~ tr/_\- //d;
if ($lc) { $_ =~ s/(.*)/\L$1/gi; }
if ( /^\d/ ) { $_ = "no$_"; }
$name = $_;