83 integer isec,hrs_obs_cutoff,min_obs_cutoff
84 integer sec_intvl,stat_miss_val,time_inc_betwn_succ_fld
85 integer perturb_num,num_ens_fcst
86 character*80 type_of_time_inc,stat_unit_time_key_succ
87 logical*1,
allocatable :: bmap(:)
89 integer,
allocatable :: mg(:)
91 integer,
parameter :: max_bytes=1000*1300000
92 integer,
parameter :: max_numbit=16
93 integer,
parameter :: lugi=650
94 character*255 fl_nametbl,fl_gdss3
95 logical :: first_grbtbl
97 public num_pset,pset,nrecout,gribit2,grib_info_init,first_grbtbl,grib_info_finalize,read_grib2_head,read_grib2_sngle
98 real(8),
EXTERNAL :: timef
104 subroutine grib_info_init()
114 character(len=80) outfile
115 character(len=10) envvar
122 if(pset%grid_num==0) &
124 if(trim(pset%sub_center)==
'') &
125 pset%sub_center=
"ncep_emc"
126 if(trim(pset%version_no)==
'') &
127 pset%version_no=
"v2003"
128 if(trim(pset%local_table_vers_no)==
'') &
129 pset%local_table_vers_no=
"local_table_no"
130 if(trim(pset%sigreftime)==
'') &
131 pset%sigreftime=
"fcst"
132 if(trim(pset%prod_status)==
'') &
133 pset%prod_status=
"oper_test"
134 if(trim(pset%data_type)==
'') &
135 pset%data_type=
"fcst"
136 if(trim(pset%orig_center)==
'') &
137 pset%orig_center=
"nws_ncep"
138 if(trim(pset%time_range_unit)==
'') &
139 pset%time_range_unit=
"hour"
140 if(trim(pset%gen_proc_type)==
'') &
141 pset%gen_proc_type=
"fcst"
142 if(trim(pset%gen_proc)==
'') &
143 pset%gen_proc=
"gfs_avn"
144 if(trim(pset%packing_method)==
'') &
145 pset%packing_method=
"jpeg"
146 if(trim(pset%field_datatype)==
'') &
147 pset%field_datatype=
"flting_pnt"
148 if(trim(pset%comprs_type)==
'') &
149 pset%comprs_type=
"lossless"
150 if(trim(pset%type_ens_fcst)==
'') &
151 pset%type_ens_fcst=
"pos_pert_fcst"
152 if(trim(pset%type_derived_fcst)==
'') &
153 pset%type_derived_fcst=
"unweighted_mean_all_mem"
162 type_of_time_inc=
'same_start_time_fcst_fcst_time_inc'
163 stat_unit_time_key_succ=
'missing'
164 time_inc_betwn_succ_fld=0
168 if(first_grbtbl)
then
169 fl_nametbl=
'params_grib2_tbl_new'
170 call open_and_read_4dot2( fl_nametbl, ierr )
171 if ( ierr /= 0 )
then
172 print*,
'Couldnt open table file - return code was ',ierr
180 end subroutine grib_info_init
184 subroutine grib_info_finalize
192 call close_4dot2(ierr)
194 end subroutine grib_info_finalize
197 subroutine gribit2(post_fname)
200 use ctlblk_mod
, only : im,jm,im_jm,num_procs,me,jsta,jend,ifhr,sdat,ihrst,imin, &
201 mpi_comm_comp,ntlfld,fld_info,datapd,icnt,idsp
207 character(255),
intent(in) :: post_fname
210 integer i,j,k,n,nm,nprm,nlvl,fldlvl1,fldlvl2,cstart,cgrblen,ierr
212 integer fh, clength,lunout
213 integer idisc,icatg,iparm,itblinfo,ntrange,leng_time_range_stat
214 integer,
allocatable :: nfld_pe(:),snfld_pe(:),enfld_pe(:)
215 integer(4),
allocatable :: isdsp(:),iscnt(:),ircnt(:),irdsp(:)
216 integer status(mpi_status_size)
217 integer(kind=MPI_OFFSET_KIND) idisp
218 integer,
allocatable :: jsta_pe(:),jend_pe(:)
219 integer,
allocatable :: grbmsglen(:)
220 real,
allocatable :: datafld(:,:)
221 real,
allocatable :: datafldtmp(:)
222 logical,
parameter :: debugprint = .false.
224 character(1),
dimension(:),
allocatable :: cgrib
229 allocate(cgrib(max_bytes))
237 nmod=mod(ntlfld,num_procs)
239 allocate(snfld_pe(num_procs),enfld_pe(num_procs),nfld_pe(num_procs))
242 snfld_pe(n)=nfpe*(n-1)+1
243 enfld_pe(n)=snfld_pe(n)+nfpe-1
246 snfld_pe(n)=nfpe*nmod+nf*(n-1-nmod)+1
247 enfld_pe(n)=snfld_pe(n)+nf-1
256 allocate(jsta_pe(num_procs),jend_pe(num_procs))
257 call mpi_allgather(jsta,1,mpi_integer,jsta_pe,1, &
258 mpi_integer,mpi_comm_comp,ierr)
259 call mpi_allgather(jend,1,mpi_integer,jend_pe,1, &
260 mpi_integer,mpi_comm_comp,ierr)
267 allocate(bmap(im_jm))
272 if(minval(nfld_pe)<1.or.num_procs==1)
then
275 allocate(datafld(im_jm,ntlfld) )
276 if(num_procs==1)
then
277 datafld=reshape(datapd,(/im_jm,ntlfld/))
280 call mpi_gatherv(datapd(:,:,i),icnt(me),mpi_real, &
281 datafld(:,i),icnt,idsp,mpi_real,0,mpi_comm_comp,ierr)
289 call baopenw(lunout,trim(post_fname),ierr)
290 print*,
'write_grib2: opened ',lunout, &
291 'for grib2 data ',trim(post_fname), &
292 'return code is ',ierr
295 nprm=fld_info(i)%ifld
297 fldlvl1=fld_info(i+snfld_pe(me+1)-1)%lvl1
298 fldlvl2=fld_info(i+snfld_pe(me+1)-1)%lvl2
299 if(trim(pset%param(nprm)%table_info)==
'NCEP')
then
308 call search_for_4dot2_entry( &
309 pset%param(nprm)%pname, &
311 idisc, icatg, iparm, ierr)
313 write(6,
'(3(A,I4),A,A)')
' discipline ',idisc, &
314 ' category ',icatg, &
315 ' parameter ',iparm, &
316 ' for var ',trim(pset%param(nprm)%pname)
318 call gengrb2msg(idisc,icatg, iparm,nprm,nlvl,fldlvl1,fldlvl2, &
319 fld_info(i)%ntrange,fld_info(i)%tinvstat,datafld(:,i), &
322 call wryte(lunout, clength, cgrib)
324 print *,
'WRONG, could not find ',trim(pset%param(nprm)%pname), &
325 " in WMO and NCEP table!, ierr=", ierr
330 call baclose(lunout,ierr)
331 print *,
'finish one grib file'
338 allocate(iscnt(num_procs),isdsp(num_procs))
339 allocate(ircnt(num_procs),irdsp(num_procs))
342 iscnt(n)=(jend_pe(me+1)-jsta_pe(me+1)+1)*im*nfld_pe(n)
343 if(n<num_procs)isdsp(n+1)=isdsp(n)+iscnt(n)
348 ircnt(n)=(jend_pe(n)-jsta_pe(n)+1)*im*nfld_pe(me+1)
349 if(n<num_procs)irdsp(n+1)=irdsp(n)+ircnt(n)
353 allocate(datafldtmp(im_jm*nfld_pe(me+1)) )
354 allocate(datafld(im_jm,nfld_pe(me+1)) )
356 call mpi_alltoallv(datapd,iscnt,isdsp,mpi_real, &
357 datafldtmp,ircnt,irdsp,mpi_real,mpi_comm_comp,ierr)
364 do j=jsta_pe(n),jend_pe(n)
367 datafld((j-1)*im+i,k)=datafldtmp(nm)
372 deallocate(datafldtmp)
382 nprm=fld_info(i+snfld_pe(me+1)-1)%ifld
383 nlvl=fld_info(i+snfld_pe(me+1)-1)%lvl
384 fldlvl1=fld_info(i+snfld_pe(me+1)-1)%lvl1
385 fldlvl2=fld_info(i+snfld_pe(me+1)-1)%lvl2
386 ntrange=fld_info(i+snfld_pe(me+1)-1)%ntrange
387 leng_time_range_stat=fld_info(i+snfld_pe(me+1)-1)%tinvstat
388 if(trim(pset%param(nprm)%table_info)==
'NCEP')
then
397 call search_for_4dot2_entry( &
398 pset%param(nprm)%pname, &
400 idisc, icatg, iparm, ierr)
403 write(6,
'(3(A,I4),A,A)')
' discipline ',idisc, &
404 ' category ',icatg, &
405 ' parameter ',iparm, &
406 ' for var ',trim(pset%param(nprm)%pname)
411 call gengrb2msg(idisc,icatg, iparm,nprm,nlvl,fldlvl1,fldlvl2,ntrange, &
412 leng_time_range_stat,datafld(:,i),cgrib(cstart),clength)
413 cstart=cstart+clength
417 print *,
'WRONG, could not find ',trim(pset%param(nprm)%pname), &
418 " in WMO and NCEP table!"
431 call mpi_barrier(mpi_comm_comp,ierr)
434 call mpi_file_open(mpi_comm_comp,trim(post_fname), &
435 mpi_mode_create+mpi_mode_wronly,mpi_info_null,fh,ierr)
439 allocate(grbmsglen(num_procs))
440 call mpi_allgather(cgrblen,1,mpi_integer,grbmsglen,1,mpi_integer, &
447 idisp=idisp+grbmsglen(n)
450 call mpi_file_write_at(fh,idisp,cgrib,cgrblen,mpi_character,status,ierr)
452 call mpi_file_close(fh,ierr)
456 deallocate(grbmsglen)
457 deallocate(iscnt,isdsp,ircnt,irdsp)
461 deallocate(datafld,bmap,mg)
462 deallocate(nfld_pe,snfld_pe,enfld_pe,jsta_pe,jend_pe)
465 end subroutine gribit2
470 subroutine gengrb2msg(idisc,icatg, iparm,nprm,nlvl,fldlvl1,fldlvl2,ntrange,tinvstat, &
471 datafld1,cgrib,lengrib)
475 use ctlblk_mod
, only : im,jm,im_jm,ifhr,idat,sdat,ihrst,ifmin,imin,fld_info,spval, &
477 use gridspec_mod
, only: maptype
478 use grib2_all_tables_module
, only: g2sec0,g2sec1, &
479 g2sec4_temp0,g2sec4_temp8,g2sec4_temp44,g2sec4_temp48, &
480 g2sec5_temp0,g2sec5_temp2,g2sec5_temp3,g2sec5_temp40, &
481 get_g2_sec5packingmethod
485 integer,
intent(in) :: idisc,icatg, iparm,nprm,fldlvl1,fldlvl2,ntrange,tinvstat
486 integer,
intent(inout) :: nlvl
487 real,
dimension(:),
intent(in) :: datafld1
488 character(1),
intent(inout) :: cgrib(max_bytes)
489 integer,
intent(inout) :: lengrib
491 integer,
parameter :: igdsmaxlen=200
493 integer,
parameter :: ipdstmplenmax=100
494 integer,
parameter :: ipdstmp4_0len=15
495 integer,
parameter :: ipdstmp4_1len=18
496 integer,
parameter :: ipdstmp4_8len=29
497 integer,
parameter :: ipdstmp4_11len=32
498 integer,
parameter :: ipdstmp4_12len=31
499 integer,
parameter :: ipdstmp4_44len=21
500 integer,
parameter :: ipdstmp4_48len=26
502 integer,
parameter :: idrstmplenmax=50
503 integer,
parameter :: idrstmp5_0len=5
504 integer,
parameter :: idrstmp5_2len=16
505 integer,
parameter :: idrstmp5_3len=18
506 integer,
parameter :: idrstmp5_40len=7
512 integer ipdstmpl(ipdstmplenmax)
514 integer idrstmpl(idrstmplenmax)
517 integer igdtlen,igdtn
519 integer ideflist(100)
520 integer idrsnum,numcoord,ipdsnum
521 integer scaled_val_fixed_sfc2,scale_fct_fixed_sfc1
522 integer scaled_val_fixed_sfc1,scale_fct_fixed_sfc2
523 character(80) fixed_sfc2_type
524 integer idec_scl,ibin_scl,ibmap,inumbits
526 integer igdstmpl(igdsmaxlen)
527 integer lat1,lon1,lat2,lon2,lad,ds1
531 integer ierr,ifhrorig,ihr_start
532 integer gefs1,gefs2,gefs3,gefs_status
533 character(len=4) cdum
534 integer perturb_num,num_ens_fcst,e1_type
540 if(trim(pset%gen_proc)==
'gefs')
then
541 call getenv(
'e1',cdum)
542 read(cdum,
'(I4)',iostat=gefs_status)gefs1
545 if(gefs_status /= 0) print *, &
546 "GEFS Run: Could not read e1 envir. var, User needs to set in script"
548 call getenv(
'e2',cdum)
549 read(cdum,
'(I4)',iostat=gefs_status)gefs2
552 if(gefs_status /= 0) print *, &
553 "GEFS Run: Could not read e2 envir. var, User needs to set in script"
557 call getenv(
'e3',cdum)
558 read(cdum,
'(I4)',iostat=gefs_status)gefs3
561 if(gefs_status /= 0) print *, &
562 "GEFS Run: Could not read e3 envir. var, User needs to set in script"
564 print*,
'GEFS env var ',e1_type,perturb_num,num_ens_fcst
567 print *,
"Processing for GEFS and default setting is tmpl4_1 and tmpl4_11"
568 if (trim(pset%param(nprm)%pdstmpl)==
'tmpl4_0')
then
569 pset%param(nprm)%pdstmpl=
'tmpl4_1'
570 elseif (trim(pset%param(nprm)%pdstmpl)==
'tmpl4_8')
then
571 pset%param(nprm)%pdstmpl=
'tmpl4_11'
578 call g2sec0(idisc,listsec0)
601 call g2sec1(pset%orig_center,pset%sub_center, &
602 pset%version_no,pset%local_table_vers_no,&
603 pset%sigreftime,nint(sdat(3)),nint(sdat(1)),nint(sdat(2)),ihrst,imin, &
604 isec,pset%prod_status,pset%data_type,listsec1)
607 if(trim(pset%gen_proc)==
'gefs')
then
610 if(e1_type==1.or.e1_type==2)
then
612 elseif(e1_type==3.or.e1_type==4)
then
615 print *,
"After g2sec1 call we need to set listsec1(2) = ",listsec1(2)
616 print *,
"After g2sec1 call we need to set listsec1(13) = ",listsec1(13)
621 call gribcreate(cgrib,max_bytes,listsec0,listsec1,ierr)
630 ldfgrd=(maptype==203.and.(trim(pset%param(nprm)%pname)==
'ugrd'.or. &
631 trim(pset%param(nprm)%pname)==
'vgrd'))
632 call getgds(ldfgrd,igdsmaxlen,igdtlen,igds,igdstmpl)
636 call addgrid(cgrib,max_bytes,igds,igdstmpl,igdtlen,ideflist,idefnum,ierr)
674 if(fldlvl1==0.and.fldlvl2==0)
then
676 if(
size(pset%param(nprm)%level)>1.and.
size(pset%param(nprm)%level)>=nlvl)
then
677 scaled_val_fixed_sfc1=nint(pset%param(nprm)%level(nlvl))
678 else if(
size(pset%param(nprm)%level)==1)
then
679 scaled_val_fixed_sfc1=nint(pset%param(nprm)%level(1))
681 scaled_val_fixed_sfc1=0
683 if(
size(pset%param(nprm)%scale_fact_fixed_sfc1)>1.and. &
684 size(pset%param(nprm)%scale_fact_fixed_sfc1)>=nlvl)
then
685 scale_fct_fixed_sfc1=pset%param(nprm)%scale_fact_fixed_sfc1(nlvl)
686 else if(
size(pset%param(nprm)%scale_fact_fixed_sfc1)==1)
then
687 scale_fct_fixed_sfc1=pset%param(nprm)%scale_fact_fixed_sfc1(1)
689 scale_fct_fixed_sfc1=0
694 scaled_val_fixed_sfc1=fldlvl1
695 scale_fct_fixed_sfc1=0
696 scaled_val_fixed_sfc2=fldlvl2
697 scale_fct_fixed_sfc2=0
700 fixed_sfc2_type=pset%param(nprm)%fixed_sfc2_type
701 if(
size(pset%param(nprm)%level2)>1.and.
size(pset%param(nprm)%level2)<nlvl)
then
705 if (
associated(pset%param(nprm)%level2))
then
706 if(
size(pset%param(nprm)%level2)>1.and.
size(pset%param(nprm)%level2)>=nlvl)
then
707 scaled_val_fixed_sfc2=nint(pset%param(nprm)%level2(nlvl))
708 else if(
size(pset%param(nprm)%level2)==1)
then
709 scaled_val_fixed_sfc2=nint(pset%param(nprm)%level2(1))
711 scaled_val_fixed_sfc2=0
714 scaled_val_fixed_sfc2=0
717 if(
size(pset%param(nprm)%scale_fact_fixed_sfc2)>1 .and. &
718 size(pset%param(nprm)%scale_fact_fixed_sfc2)>=nlvl)
then
719 scale_fct_fixed_sfc2=pset%param(nprm)%scale_fact_fixed_sfc2(nlvl)
720 else if(
size(pset%param(nprm)%scale_fact_fixed_sfc2)==1)
then
721 scale_fct_fixed_sfc2=pset%param(nprm)%scale_fact_fixed_sfc2(1)
723 scale_fct_fixed_sfc2=0
726 ihr_start = ifhr-tinvstat
727 if(modelname==
'RAPR'.and.vtimeunits==
'FMIN')
then
729 ifhr = ifhr*60 + ifmin
730 ihr_start = max(0,ifhr-tinvstat)
733 pset%time_range_unit=
"minute"
735 ifhr = ifhr*60 + ifmin
736 ihr_start = max(0,ifhr-tinvstat*60)
745 if(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_0')
then
747 ipdstmpllen=ipdstmp4_0len
748 call g2sec4_temp0(icatg,iparm,pset%gen_proc_type, &
749 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
750 pset%time_range_unit,ifhr, &
751 pset%param(nprm)%fixed_sfc1_type, &
752 scale_fct_fixed_sfc1, &
753 scaled_val_fixed_sfc1, &
755 scale_fct_fixed_sfc2, &
756 scaled_val_fixed_sfc2, &
757 ipdstmpl(1:ipdstmpllen))
759 elseif(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_1')
then
761 ipdstmpllen=ipdstmp4_1len
762 call g2sec4_temp1(icatg,iparm,pset%gen_proc_type, &
763 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
764 pset%time_range_unit,ifhr, &
765 pset%param(nprm)%fixed_sfc1_type, &
766 scale_fct_fixed_sfc1, &
767 scaled_val_fixed_sfc1, &
769 scale_fct_fixed_sfc2, &
770 scaled_val_fixed_sfc2, &
771 pset%type_ens_fcst,perturb_num,num_ens_fcst, &
772 ipdstmpl(1:ipdstmpllen))
775 elseif(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_8')
then
778 ipdstmpllen=ipdstmp4_8len
779 call g2sec4_temp8(icatg,iparm,pset%gen_proc_type, &
780 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
781 pset%time_range_unit,ihr_start, &
782 pset%param(nprm)%fixed_sfc1_type, &
783 scale_fct_fixed_sfc1, &
784 scaled_val_fixed_sfc1, &
785 pset%param(nprm)%fixed_sfc2_type, &
786 scale_fct_fixed_sfc2, &
787 scaled_val_fixed_sfc2, &
788 idat(3),idat(1),idat(2),idat(4),idat(5), &
789 sec_intvl,ntrange,stat_miss_val, &
790 pset%param(nprm)%stats_proc,type_of_time_inc, &
791 pset%time_range_unit, tinvstat, &
792 stat_unit_time_key_succ,time_inc_betwn_succ_fld, &
793 ipdstmpl(1:ipdstmpllen))
796 elseif(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_11')
then
798 ipdstmpllen=ipdstmp4_11len
799 call g2sec4_temp11(icatg,iparm,pset%gen_proc_type, &
800 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
801 pset%time_range_unit,ifhr-tinvstat, &
802 pset%param(nprm)%fixed_sfc1_type, &
803 scale_fct_fixed_sfc1, &
804 scaled_val_fixed_sfc1, &
805 pset%param(nprm)%fixed_sfc2_type, &
806 scale_fct_fixed_sfc2, &
807 scaled_val_fixed_sfc2, &
808 pset%type_ens_fcst,perturb_num,num_ens_fcst, &
809 idat(3),idat(1),idat(2),idat(4),idat(5), &
810 sec_intvl,ntrange,stat_miss_val, &
811 pset%param(nprm)%stats_proc,type_of_time_inc, &
812 pset%time_range_unit, tinvstat, &
813 stat_unit_time_key_succ,time_inc_betwn_succ_fld, &
814 ipdstmpl(1:ipdstmpllen))
817 elseif(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_12')
then
819 ipdstmpllen=ipdstmp4_12len
820 call g2sec4_temp12(icatg,iparm,pset%gen_proc_type, &
821 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
822 pset%time_range_unit,ifhr-tinvstat, &
823 pset%param(nprm)%fixed_sfc1_type, &
824 scale_fct_fixed_sfc1, &
825 scaled_val_fixed_sfc1, &
827 scale_fct_fixed_sfc2, &
828 scaled_val_fixed_sfc2, &
829 pset%type_derived_fcst,num_ens_fcst, &
830 idat(3),idat(1),idat(2),idat(4),idat(5), &
831 sec_intvl,ntrange,stat_miss_val, &
832 pset%param(nprm)%stats_proc,type_of_time_inc, &
833 pset%time_range_unit, tinvstat, &
834 stat_unit_time_key_succ,time_inc_betwn_succ_fld, &
835 ipdstmpl(1:ipdstmpllen))
838 elseif(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_44')
then
841 ipdstmpllen=ipdstmp4_44len
842 call g2sec4_temp44(icatg,iparm,pset%param(nprm)%aerosol_type, &
843 pset%param(nprm)%typ_intvl_size, &
844 pset%param(nprm)%scale_fact_1st_size, &
845 pset%param(nprm)%scale_val_1st_size, &
846 pset%param(nprm)%scale_fact_2nd_size, &
847 pset%param(nprm)%scale_val_2nd_size, &
848 pset%gen_proc_type, &
849 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
850 pset%time_range_unit,ifhr, &
851 pset%param(nprm)%fixed_sfc1_type, &
852 scale_fct_fixed_sfc1, &
853 scaled_val_fixed_sfc1, &
854 pset%param(nprm)%fixed_sfc2_type, &
855 scale_fct_fixed_sfc2, &
856 scaled_val_fixed_sfc2, &
857 ipdstmpl(1:ipdstmpllen))
860 elseif(trim(pset%param(nprm)%pdstmpl)==
'tmpl4_48')
then
863 ipdstmpllen=ipdstmp4_48len
864 call g2sec4_temp48(icatg,iparm,pset%param(nprm)%aerosol_type, &
865 pset%param(nprm)%typ_intvl_size, &
866 pset%param(nprm)%scale_fact_1st_size, &
867 pset%param(nprm)%scale_val_1st_size, &
868 pset%param(nprm)%scale_fact_2nd_size, &
869 pset%param(nprm)%scale_val_2nd_size, &
870 pset%param(nprm)%typ_intvl_wvlen, &
871 pset%param(nprm)%scale_fact_1st_wvlen, &
872 pset%param(nprm)%scale_val_1st_wvlen, &
873 pset%param(nprm)%scale_fact_2nd_wvlen, &
874 pset%param(nprm)%scale_val_2nd_wvlen, &
875 pset%gen_proc_type, &
876 pset%gen_proc,hrs_obs_cutoff,min_obs_cutoff, &
877 pset%time_range_unit,ifhr, &
878 pset%param(nprm)%fixed_sfc1_type, &
879 scale_fct_fixed_sfc1, &
880 scaled_val_fixed_sfc1, &
881 pset%param(nprm)%fixed_sfc2_type, &
882 scale_fct_fixed_sfc2, &
883 scaled_val_fixed_sfc2, &
884 ipdstmpl(1:ipdstmpllen))
890 if(modelname==
'RAPR'.and.vtimeunits==
'FMIN')
then
902 call get_g2_sec5packingmethod(pset%packing_method,idrsnum,ierr)
903 if(maxval(datafld1)==minval(datafld1))
then
907 if(modelname==
'RAPR')
then
908 if((abs(maxval(datafld1)-minval(datafld1)) < 1.1) .and. (datafld1(1) > 500.0))
then
913 if(trim(pset%param(nprm)%shortname)==
'UGRD_ON_SPEC_HGT_LVL_ABOVE_GRND_10m'.or.&
914 trim(pset%param(nprm)%shortname)==
'VGRD_ON_SPEC_HGT_LVL_ABOVE_GRND_10m'.or.&
915 trim(pset%param(nprm)%shortname)==
'VWSH_ON_SPEC_HGT_LVL_ABOVE_GRND'.or.&
916 trim(pset%param(nprm)%shortname)==
'VUCSH_ON_SPEC_HGT_LVL_ABOVE_GRND_0-1km'.or.&
917 trim(pset%param(nprm)%shortname)==
'VVCSH_ON_SPEC_HGT_LVL_ABOVE_GRND_0-1km'.or.&
918 trim(pset%param(nprm)%shortname)==
'VUCSH_ON_SPEC_HGT_LVL_ABOVE_GRND_0-6km'.or.&
919 trim(pset%param(nprm)%shortname)==
'VVCSH_ON_SPEC_HGT_LVL_ABOVE_GRND_0-6km')
then
932 if(any(datafld1>=spval))
then
934 where(abs(datafld1)>=spval)bmap=.false.
937 if(
size(pset%param(nprm)%level)==
size(pset%param(nprm)%scale))
then
938 if(
size(pset%param(nprm)%level)==1) nlvl=1
940 fldscl=nint(pset%param(nprm)%scale(nlvl))
944 else if (
size(pset%param(nprm)%scale)==1)
then
945 fldscl=nint(pset%param(nprm)%scale(1))
949 if(trim(pset%param(nprm)%pname)==
'APCP' .or. &
950 trim(pset%param(nprm)%pname)==
'ACPCP' .or. &
951 trim(pset%param(nprm)%pname)==
'NCPCP')mxbit=22
952 if(mxbit>16)print*,
'increased MXBIT for ', pset%param(nprm)%pname,mxbit
953 call g2getbits(mxbit,ibmap,fldscl,
size(datafld1),bmap,datafld1,ibin_scl,idec_scl,inumbits)
955 if( idrsnum==40 )
then
956 idrstmplen=idrstmp5_40len
957 call g2sec5_temp40(idec_scl,ibin_scl,inumbits,pset%comprs_type,idrstmpl(1:idrstmplen))
958 elseif( idrsnum==2 )
then
959 idrstmplen=idrstmp5_2len
960 call g2sec5_temp2(idec_scl,ibin_scl,idrstmpl(1:idrstmplen))
961 elseif( idrsnum==3 )
then
962 idrstmplen=idrstmp5_3len
963 call g2sec5_temp3(idec_scl,ibin_scl,pset%order_of_sptdiff,idrstmpl(1:idrstmplen))
964 elseif( idrsnum==0 )
then
965 idrstmplen=idrstmp5_0len
966 call g2sec5_temp0(idec_scl,ibin_scl,inumbits,idrstmpl(1:idrstmplen))
978 call addfield(cgrib,max_bytes,ipdsnum,ipdstmpl(1:ipdstmpllen), &
979 ipdstmpllen,coordlist,numcoord,idrsnum,idrstmpl, &
980 idrstmplen ,datafld1,im_jm,ibmap,bmap,ierr)
987 call gribend(cgrib,max_bytes,lengrib,ierr)
990 end subroutine gengrb2msg
997 subroutine read_grib2_head(filenameG2,nx,ny,nz,rlonmin,rlatmax,rdx,rdy)
1003 character*256,
intent(in) :: filenameg2
1004 integer,
intent(out) :: nx,ny,nz
1005 real,
intent(out) :: rlonmin,rlatmax
1006 real*8,
intent(out) :: rdx,rdy
1009 type(gribfield
) :: gfld
1010 logical :: expand=.true.
1012 character(len=1),
allocatable,
dimension(:) :: cgrib
1013 integer,
parameter :: msk1=32000
1014 integer :: lskip, lgrib,iseek
1016 integer :: icount , lengrib
1017 integer :: listsec0(3)
1018 integer :: listsec1(13)
1019 integer year, month, day, hour, minute, second, fcst
1020 integer :: numfields,numlocal,maxlocal,ierr
1021 integer :: grib_edition
1024 real :: scale_factor,scale_factor2
1027 integer :: nn,n,j,iret
1028 real :: fldmax,fldmin,sum
1043 call baopenr(ifile,trim(filenameg2),iret)
1047 call skgb(ifile,iseek,msk1,lskip,lgrib)
1049 if (lgrib.eq.0)
then
1053 if (lgrib.gt.currlen)
then
1054 if (
allocated(cgrib))
deallocate(cgrib)
1055 allocate(cgrib(lgrib))
1059 call baread(ifile,lskip,lgrib,lengrib,cgrib)
1060 if(lgrib.ne.lengrib)
then
1061 write(*,*)
'ERROR, read_grib2 lgrib ne lengrib', &
1068 call gb_info(cgrib,lengrib,listsec0,listsec1, &
1069 numfields,numlocal,maxlocal,ierr)
1071 write(6,*)
'Error querying GRIB2 message',ierr
1075 grib_edition=listsec0(2)
1076 if (grib_edition.ne.2)
then
1084 call gf_getfld(cgrib,lengrib,n,.false.,expand,gfld,ierr)
1085 year =gfld%idsect(6)
1086 month =gfld%idsect(7)
1088 hour =gfld%idsect(9)
1089 minute=gfld%idsect(10)
1090 second=gfld%idsect(11)
1091 write(*,*)
'year,month,day,hour,minute,second='
1092 write(*,*) year,month,day,hour,minute,second
1093 write(*,*)
'source center =',gfld%idsect(1)
1094 write(*,*)
'Indicator of model =',gfld%ipdtmpl(5)
1095 write(*,*)
'observation level (m)=',gfld%ipdtmpl(12)
1096 write(*,*)
'map projection=',gfld%igdtnum
1097 if (gfld%igdtnum.eq.0)
then
1099 nx = gfld%igdtmpl(8)
1100 ny = gfld%igdtmpl(9)
1102 rdx = gfld%igdtmpl(17)/scale_factor
1103 rdy = gfld%igdtmpl(18)/scale_factor
1104 rlatmax = gfld%igdtmpl(12)/scale_factor
1105 rlonmin = gfld%igdtmpl(13)/scale_factor
1109 else if (gfld%igdtnum.eq.1)
then
1110 nx = gfld%igdtmpl(8)
1111 ny = gfld%igdtmpl(9)
1113 rdx = gfld%igdtmpl(17)/scale_factor
1114 rdy = gfld%igdtmpl(18)/scale_factor
1115 rlatmax = gfld%igdtmpl(12)/scale_factor
1116 rlonmin = gfld%igdtmpl(13)/scale_factor
1120 else if (gfld%igdtnum.eq.30)
then
1121 nx = gfld%igdtmpl(8)
1122 ny = gfld%igdtmpl(9)
1124 rdx = gfld%igdtmpl(15)/scale_factor2
1125 rdy = gfld%igdtmpl(16)/scale_factor2
1126 rlatmax = gfld%igdtmpl(10)/scale_factor
1127 rlonmin = gfld%igdtmpl(11)/scale_factor
1132 write(*,*)
'unknown projection'
1138 CALL baclose(ifile,ierr)
1140 if (
allocated(cgrib))
deallocate(cgrib)
1143 end subroutine read_grib2_head
1147 subroutine read_grib2_sngle(filenameG2,ntot,height,var)
1153 character*256,
intent(in) :: filenameg2
1154 integer,
intent(in) :: ntot
1155 real,
intent(out) :: var(ntot)
1156 integer,
intent(out) :: height
1159 type(gribfield
) :: gfld
1160 logical :: expand=.true.
1162 character(len=1),
allocatable,
dimension(:) :: cgrib
1163 integer,
parameter :: msk1=32000
1164 integer :: lskip, lgrib,iseek
1166 integer :: icount , lengrib
1167 integer :: listsec0(3)
1168 integer :: listsec1(13)
1169 integer year, month, day, hour, minute, second, fcst
1170 integer :: numfields,numlocal,maxlocal,ierr
1171 integer :: grib_edition
1174 real :: dx,dy,lat1,lon1,rtnum
1175 real :: ref_value,bin_scale_fac,dec_scale_fac,bit_number,field_type
1177 real :: scale_factor,scale_factor2
1180 integer :: nn,n,j,iret
1181 real :: fldmax,fldmin,sum
1196 call baopenr(ifile,trim(filenameg2),iret)
1200 call skgb(ifile,iseek,msk1,lskip,lgrib)
1202 if (lgrib.eq.0)
then
1206 if (lgrib.gt.currlen)
then
1207 if (
allocated(cgrib))
deallocate(cgrib)
1208 allocate(cgrib(lgrib))
1212 call baread(ifile,lskip,lgrib,lengrib,cgrib)
1213 if(lgrib.ne.lengrib)
then
1214 write(*,*)
'ERROR, read_grib2 lgrib ne lengrib', &
1222 call gb_info(cgrib,lengrib,listsec0,listsec1, &
1223 numfields,numlocal,maxlocal,ierr)
1225 write(6,*)
'Error querying GRIB2 message',ierr
1229 grib_edition=listsec0(2)
1230 if (grib_edition.ne.2)
then
1238 call gf_getfld(cgrib,lengrib,n,.false.,expand,gfld,ierr)
1239 year =gfld%idsect(6)
1240 month =gfld%idsect(7)
1242 hour =gfld%idsect(9)
1243 minute=gfld%idsect(10)
1244 second=gfld%idsect(11)
1251 height=gfld%ipdtmpl(12)
1252 if (gfld%igdtnum.eq.0)
then
1254 nx = gfld%igdtmpl(8)
1255 ny = gfld%igdtmpl(9)
1256 dx = gfld%igdtmpl(17)/scale_factor
1257 dy = gfld%igdtmpl(18)/scale_factor
1258 lat1 = gfld%igdtmpl(12)/scale_factor
1259 lon1 = gfld%igdtmpl(13)/scale_factor
1263 else if (gfld%igdtnum.eq.1)
then
1264 nx = gfld%igdtmpl(8)
1265 ny = gfld%igdtmpl(9)
1266 dx = gfld%igdtmpl(17)/scale_factor
1267 dy = gfld%igdtmpl(18)/scale_factor
1268 lat1 = gfld%igdtmpl(12)/scale_factor
1269 lon1 = gfld%igdtmpl(13)/scale_factor
1273 else if (gfld%igdtnum.eq.30)
then
1274 nx = gfld%igdtmpl(8)
1275 ny = gfld%igdtmpl(9)
1276 dx = gfld%igdtmpl(15)/scale_factor2
1277 dy = gfld%igdtmpl(16)/scale_factor2
1278 lat1 = gfld%igdtmpl(10)/scale_factor
1279 lon1 = gfld%igdtmpl(11)/scale_factor
1284 rtnum = gfld%idrtnum
1286 ref_value = gfld%idrtmpl(1)
1287 bin_scale_fac = gfld%idrtmpl(2)
1288 dec_scale_fac = gfld%idrtmpl(3)
1289 bit_number = gfld%idrtmpl(4)
1290 field_type = gfld%idrtmpl(5)
1291 bit_map = gfld%ibmap
1299 write(*,*)
'unknown projection'
1304 num_fields:
do n = 1, numfields
1306 call gf_getfld(cgrib,lengrib,n,.true.,expand,gfld,ierr)
1308 write(*,*)
' ERROR extracting field gf_getfld = ',ierr
1317 if(ntot .ne. gfld%ngrdpts)
then
1318 write(*,*)
'Error, wrong dimension ',ntot, gfld%ngrdpts
1330 CALL baclose(ifile,ierr)
1331 if (
allocated(cgrib))
deallocate(cgrib)
1335 end subroutine read_grib2_sngle
1339 subroutine g2sec3tmpl40(nx,nY,lat1,lon1,lat2,lon2,lad,ds1,len3,igds,ifield3)
1342 integer(4),
intent(inout) :: nx,ny,lat1,lon1,lat2,lon2,lad,ds1,len3
1343 integer(4),
intent(inout) :: ifield3(len3),igds(5)
1379 end subroutine g2sec3tmpl40
1383 subroutine g2getbits(MXBIT,ibm,scl,len,bmap,g,ibs,ids,nbits)
1404 INTEGER,
INTENT(IN) :: mxbit,ibm,len
1405 LOGICAL*1,
INTENT(IN) :: bmap(len)
1406 REAL,
INTENT(IN) :: scl,g(len)
1407 INTEGER,
INTENT(OUT) :: ibs,ids,nbits
1412 real,
PARAMETER :: alog2=0.69314718056,hpeps=0.500001
1415 INTEGER :: i,i1,icnt,ipo,le,irange
1416 REAL :: ground,gmin,gmax,s,rmin,rmax,range,rr,rng2,po,rln2
1418 DATA rln2/0.69314718/
1427 gmax = max(gmax,g(i))
1428 gmin = min(gmin,g(i))
1443 gmax = max(gmax,g(i))
1444 gmin = min(gmin,g(i))
1461 IF ( range <= 1.e-30 )
THEN
1466 IF ( scl == 0.0 )
THEN
1469 ELSE IF ( scl > 0.0 )
THEN
1470 ipo = int(alog10( range ))
1472 if(ipo<0.and.ipo+scl<-20)
then
1473 print *,
'for small range,ipo=',ipo,
'ipo+scl=',ipo+scl,
'scl=',scl
1478 IF ( range < 1.00 ) ipo = ipo - 1
1479 po = float(ipo) - scl + 1.
1481 rr = range * 10. ** ( -po )
1482 nbits = int( alog( rr ) / rln2 ) + 1
1485 rng2 = range * 2. ** (-ibs)
1486 nbits = int( alog( rng2 ) / rln2 ) + 1
1492 IF(abs(gmin) >= 1.)
THEN
1493 ids = -int(alog10(abs(gmin)))
1494 ELSE IF (abs(gmin) < 1.0.AND.abs(gmin) > 0.0)
THEN
1495 ids = -int(alog10(abs(gmin)))+1
1500 nbits = min(nbits,mxbit)
1503 IF ( scl > 0.0 )
THEN
1510 gmax = max(gmax,g(i)*s)
1511 gmin = min(gmin,g(i)*s)
1527 gmax = max(gmax,g(i)*s)
1528 gmin = min(gmin,g(i)*s)
1538 if(gmax == gmin)
then
1541 ibs = nint(alog(range/(2.**nbits-0.5))/alog2+hpeps)
1549 END subroutine g2getbits
1553 subroutine getgds(ldfgrd,len3,ifield3len,igds,ifield3)
1557 use ctlblk_mod
, only : im,jm,gdsdegr,modelname
1558 use gridspec_mod
, only: dxval,dyval,cenlat,cenlon,latstart,lonstart,latlast, &
1559 & lonlast,maptype,standlon,latstartv,cenlatv,lonstartv, &
1560 cenlonv,truelat1,truelat2,latstart_r,lonstart_r, &
1565 logical,
intent(in) :: ldfgrd
1566 integer(4),
intent(in) :: len3
1567 integer(4),
intent(out) :: ifield3len
1568 integer(4),
intent(inout) :: ifield3(len3),igds(5)
1579 IF(maptype == 1)
THEN
1587 ifield3(10) = latstart
1588 ifield3(11) = lonstart
1591 if (modelname ==
'FV3R')
then
1595 ifield3(13) = truelat1
1596 ifield3(14) = standlon
1605 ifield3(19) = truelat1
1606 ifield3(20) = truelat2
1609 ELSE IF(maptype == 2)
THEN
1617 ifield3(10) = latstart
1618 ifield3(11) = lonstart
1620 ifield3(13) = truelat1
1621 ifield3(14) = standlon
1628 ELSE IF(maptype==3)
THEN
1636 ifield3(10) = latstart
1637 ifield3(11) = lonstart
1639 ifield3(13) = truelat1
1640 ifield3(14) = latlast
1641 ifield3(15) = lonlast
1648 ELSE IF(maptype == 203)
THEN
1657 if(.not.ldfgrd)
then
1660 ifield3(11) = 45000000
1662 ifield3(12) = latstart
1663 ifield3(13) = lonstart
1665 ifield3(15) = cenlat
1666 ifield3(16) = cenlon
1672 ELSE IF(maptype == 205 .OR. maptype == 6)
THEN
1681 if(.not.ldfgrd)
then
1684 ifield3(11) = 45000000
1686 ifield3(12) = latstart
1687 ifield3(13) = lonstart
1689 ifield3(15) = cenlat
1690 ifield3(16) = cenlon
1694 ifield3(20) = latlast
1695 ifield3(21) = lonlast
1698 ELSE IF(maptype == 207)
THEN
1707 if(.not.ldfgrd)
then
1710 ifield3(11) = 45000000
1712 ifield3(12) = latstart_r
1713 ifield3(13) = lonstart_r
1716 if(modelname==
'FV3R')
then
1720 ifield3(15) = latlast_r
1721 ifield3(16) = lonlast_r
1725 ifield3(20) = cenlat-90.*gdsdegr
1726 ifield3(21) = cenlon
1732 ELSE IF(maptype == 4 )
THEN
1742 ifield3(12) = latstart
1743 ifield3(13) = lonstart
1745 ifield3(15) = latlast
1746 ifield3(16) = lonlast
1747 ifield3(17) = nint(360./(im)*1000000.)
1748 ifield3(18) = nint(jm/2.0)
1749 if( latstart < latlast )
then
1756 ELSE IF(maptype == 0 )
THEN
1766 ifield3(12) = latstart
1767 ifield3(13) = lonstart
1769 ifield3(15) = latlast
1770 ifield3(16) = lonlast
1772 ifield3(17) = abs(lonlast-lonstart)/(im-1)
1777 ifield3(18) = abs(latlast-latstart)/(jm-1)
1779 if( latstart < latlast )
then
1788 end subroutine getgds