43 caller_linenum, point_count, x, y) bind(c,name='pj_init_and_transform')
45 character(c_char) :: from_projection(*)
46 character(c_char) :: to_projection(*)
47 character(c_char) :: caller_name(*)
48 integer(c_int),
value :: caller_linenum
49 integer(c_long),
value :: point_count
50 real(c_double) :: x(*)
51 real(c_double) :: y(*)
58 integer (c_int) :: inx
59 integer (c_int) :: iny
60 integer (c_int) :: inumgridcells
61 integer (c_int) :: idatatype
62 character (len=:),
allocatable :: sproj4_string
63 character (len=:),
allocatable :: sfilename
64 real (c_double) :: rgridcellsize
65 integer (c_int) :: ilengthunits= -99999
66 real (c_double) :: rx0, rx1
67 real (c_double) :: ry0, ry1
69 integer (c_int),
dimension(:,:),
allocatable :: idata
70 real (c_float),
dimension(:,:),
allocatable :: rdata
71 real (c_float),
dimension(:,:),
allocatable :: fdata
72 real (c_double),
dimension(:,:),
allocatable :: dpdata
73 real (c_double),
dimension(:,:),
allocatable :: rx
74 real (c_double),
dimension(:,:),
allocatable :: ry
75 integer (c_int),
dimension(:,:),
allocatable :: imask
85 real (c_double) :: rxll, ryll
86 real (c_double) :: rxul, ryul
87 real (c_double) :: rxlr, rylr
88 real (c_double) :: rxur, ryur
89 character (len=256) :: sproj4_string
94 character(len=40) :: cerrormessagetext
95 integer :: ierrornumber
144 error_message_t(
"geocentric transformation missing z or ellps ", -45), &
147 error_message_t(
"point not within available datum shift grids ", -48), &
170 integer (c_int),
parameter,
private ::
column = 1
171 integer (c_int),
parameter,
private ::
row = 2
183 character (len=*),
intent(in) :: sdirname
187 call logs%write(
"ASCII grids will be written to subdirectory " &
189 ilinesbefore=1, lecho=
true )
213 integer (c_int),
intent(in) :: inx, iny
214 real (c_double),
intent(in) :: rx0, ry0
215 real (c_double),
intent(in) :: rx1, ry1
216 integer (c_int),
intent(in) :: idatatype
220 integer (c_int) :: istat
222 allocate ( pgrd, stat=istat )
223 call assert ( istat == 0, &
224 "Could not allocate pointer to T_GRID object", __file__,__line__ )
226 if (inx <= 0 .or. iny <= 0)
then
227 call logs%write(
"Illegal grid dimensions: ")
235 "INTERNAL PROGRAMMING ERROR? - Illegal grid dimensions specified", __file__,__line__)
238 select case (idatatype)
241 allocate ( pgrd%iData( inx, iny ), stat=istat )
242 call assert (istat == 0, &
243 "Could not allocate integer data", &
248 allocate ( pgrd%rData( inx, iny ), stat=istat )
249 call assert (istat == 0, &
250 "Could not allocate real data", &
255 allocate ( pgrd%dpData( inx, iny ), stat=istat )
256 call assert (istat == 0, &
257 "Could not allocate double-precision data", &
259 pgrd%dpData = 0.0_c_double
262 call assert (
false,
'Internal error -- illegal grid data type' )
265 pgrd%iDataType = idatatype
272 pgrd%rGridCellSize = (pgrd%rX1 - pgrd%rX0) / real(pgrd%iNX, c_float)
273 pgrd%iNumGridCells = inx * iny
275 allocate(pgrd%iMask(inx, iny))
284 integer (c_int),
intent(in) :: inx, iny
285 real (c_double),
intent(in) :: rx0, ry0
286 real (c_double),
intent(in) :: rgridcellsize
287 integer (c_int),
intent(in) :: idatatype
292 integer (c_int) :: istat
294 allocate ( pgrd, stat=istat )
295 call assert ( istat == 0, &
296 "Could not allocate pointer to T_GRID object", __file__,__line__ )
297 call assert ( inx > 0 .and. iny > 0, &
298 "Illegal grid dimensions specified: NX="//
ascharacter(inx) &
301 select case (idatatype)
304 allocate ( pgrd%iData( inx, iny ), stat=istat )
305 call assert (istat == 0, &
306 "Could not allocate integer data", &
308 pgrd%iData = pgrd%iNoDataValue
311 allocate ( pgrd%rData( inx, iny ), stat=istat )
312 call assert (istat == 0, &
313 "Could not allocate real data", &
315 pgrd%rData = pgrd%rNoDataValue
318 allocate ( pgrd%dpData( inx, iny ), stat=istat )
319 call assert (istat == 0, &
320 "Could not allocate double-precision data", &
322 pgrd%dpData = pgrd%dpNoDataValue
325 call assert (
false,
'Internal error -- illegal grid data type' )
328 pgrd%iDataType = idatatype
332 pgrd%rX1 = rx0 + real(inx, c_double) * rgridcellsize
334 pgrd%rY1 = ry0 + real(iny, c_double) * rgridcellsize
335 pgrd%rGridCellSize = rgridcellsize
336 pgrd%iNumGridCells = inx * iny
338 allocate(pgrd%iMask(inx, iny))
370 integer (c_int) :: istat
372 if(
associated(pgrd) )
then
375 deallocate ( pgrd%iData, stat=istat )
376 call assert ( istat == 0,
"Failed to deallocate integer grid" )
378 deallocate ( pgrd%rData, stat=istat )
379 call assert ( istat == 0,
"Failed to deallocate real grid" )
381 deallocate ( pgrd%dpData, stat=istat )
382 call assert ( istat == 0,
"Failed to deallocate double-precision grid" )
395 call assert (
false,
"Internal error -- unknown grid type", &
399 if(
allocated(pgrd%rX) )
then
400 deallocate( pgrd%rX, stat=istat)
401 call assert ( istat == 0,
"Failed to deallocate X-coordinate data structure associated with grid", &
405 if(
allocated(pgrd%rY) )
then
406 deallocate( pgrd%rY, stat=istat)
407 call assert ( istat == 0,
"Failed to deallocate Y-coordinate data structure associated with grid", &
446function grid_read ( sFilename, sFileType, iDataType )
result ( pGrd )
449 character (len=*),
intent(in) :: sfilename
450 character (len=*),
intent(in) :: sfiletype
451 integer (c_int),
intent(in) :: idatatype
455 if ( trim(sfiletype) ==
"ARC_GRID" )
then
457 else if ( trim(sfiletype) ==
"SURFER" )
then
460 call assert(
false,
"Illegal grid file type requested" )
463 pgrd%sFilename = trim(sfilename)
472 character (len=*),
intent(in) :: sfilename
473 character (len=*),
intent(in) :: sfiletype
476 if ( trim(sfiletype) ==
"ARC_GRID" )
then
478 else if ( trim(sfiletype) ==
"SURFER" )
then
481 call assert(
false,
"Illegal grid file type requested" )
484 pgrd%sFilename = trim(sfilename)
516 character (len=*),
intent(in) :: sfilename
517 integer (c_int),
intent(in) :: idatatype
521 character (len=256) :: sinputrecord
522 character (len=256) :: sdirective
523 character (len=256) :: sargument
524 character (len=256) :: snodatavalue
525 character (len=8192) :: sbuf
526 integer (c_int) :: istat
527 integer (c_int) :: inx,iny
528 integer (c_int) :: ihdrrecs
529 integer (c_int) :: icol,irow,k
530 real (c_double) :: rx0,rx1
531 real (c_double) :: ry0,ry1
532 real (c_double) :: rcellsize
533 integer (c_int) :: icount,icumlcount
534 logical (c_bool) :: lxllcenter, lyllcenter
535 logical (c_bool) :: lfileexists
536 logical (c_bool) :: lisopen
539 inquire(file=trim(sfilename), exist=lfileexists)
540 call assert( lfileexists,
"The Arc ASCII grid file "//
dquote(sfilename)// &
541 " could not be found.",__file__,__line__)
543 inquire(unit=
lu_grid, opened=lisopen )
544 if (lisopen )
close( unit=
lu_grid )
546 open ( newunit=
lu_grid, iostat=istat, file=trim(sfilename) )
547 call assert( istat == 0, &
548 "Could not open input file " // trim(sfilename) )
556 read ( unit=
lu_grid, fmt=
"(a256)", iostat=istat ) sinputrecord
557 call assert ( istat == 0, &
558 "Could not read input record - file:"//trim(sfilename) )
559 call chomp ( sinputrecord, sdirective )
561 call chomp ( sinputrecord, sargument )
562 if ( sdirective ==
"NCOLS" )
then
563 read ( unit=sargument, fmt=*, iostat=istat ) inx
564 call assert ( istat == 0,
"Could not read NCOLS" )
565 ihdrrecs = ihdrrecs + 1
566 else if ( sdirective ==
"NROWS" )
then
567 read ( unit=sargument, fmt=*, iostat=istat ) iny
568 call assert ( istat == 0,
"Could not read NROWS" )
569 ihdrrecs = ihdrrecs + 1
570 else if ( sdirective ==
"XLLCENTER" )
then
571 read ( unit=sargument, fmt=*, iostat=istat ) rx0
572 call assert ( istat == 0,
"Could not read XLLCENTER" )
574 ihdrrecs = ihdrrecs + 1
575 else if ( sdirective ==
"YLLCENTER" )
then
576 read ( unit=sargument, fmt=*, iostat=istat ) ry0
577 call assert ( istat == 0,
"Could not read YLLCENTER" )
579 ihdrrecs = ihdrrecs + 1
580 else if ( sdirective ==
"XLLCORNER" )
then
581 read ( unit=sargument, fmt=*, iostat=istat ) rx0
582 call assert ( istat == 0,
"Could not read XLLCORNER" )
583 ihdrrecs = ihdrrecs + 1
584 else if ( sdirective ==
"YLLCORNER" )
then
585 read ( unit=sargument, fmt=*, iostat=istat ) ry0
586 call assert ( istat == 0,
"Could not read YLLCORNER" )
587 ihdrrecs = ihdrrecs + 1
588 else if ( sdirective ==
"CELLSIZE" )
then
589 read ( unit=sargument, fmt=*, iostat=istat ) rcellsize
590 call assert ( istat == 0,
"Could not read CELLSIZE" )
591 ihdrrecs = ihdrrecs + 1
592 else if ( sdirective ==
"NODATA_VALUE" )
then
593 snodatavalue = trim(sargument)
594 ihdrrecs = ihdrrecs + 1
597 if ( lxllcenter ) rx0 = rx0 - 0.5_c_float*rcellsize
598 if ( lyllcenter ) ry0 = ry0 - 0.5_c_float*rcellsize
599 rx1 = rx0 + real(inx, c_double) * rcellsize
600 ry1 = ry0 + real(iny, c_double) * rcellsize
602 pgrd =>
grid_create( inx, iny, rx0, ry0, rx1, ry1, idatatype )
604 pgrd%rGridCellSize = rcellsize
606 rewind( unit=
lu_grid, iostat=istat )
607 call assert ( istat == 0,
"Failed to rewind grid file" )
609 read ( unit=
lu_grid, fmt=
"(a256)", iostat=istat ) sinputrecord
610 call assert ( istat == 0, &
611 "Could not read input record - file: "//trim(sfilename))
614 select case ( idatatype )
619 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%iData(:,irow)
620 call assert ( istat == 0, &
621 "Failed to read integer grid data - file: " &
622 //trim(sfilename)//
" row num: "//trim(
ascharacter(irow)), &
625 if(len_trim(snodatavalue) > 0)
then
626 read(unit=snodatavalue, fmt=*, iostat=istat) pgrd%iNoDataValue
627 call assert ( istat == 0, &
628 "Failed to read NODATA value in grid data - file: " &
629 //
dquote(sfilename), __file__,__line__ )
635 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%rData(:,irow)
636 call assert ( istat == 0, &
637 "Failed to read real grid data - file: " &
638 //trim(sfilename)//
" row num: "//trim(
ascharacter(irow)), &
641 if(len_trim(snodatavalue) > 0)
then
642 read(unit=snodatavalue, fmt=*, iostat=istat) pgrd%rNoDataValue
643 call assert ( istat == 0, &
644 "Failed to read NODATA value in grid data - file: " &
645 //trim(sfilename), __file__,__line__ )
651 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%dpData(:,irow)
652 call assert ( istat == 0, &
653 "Failed to read double-precision grid data - file: " &
654 //trim(sfilename)//
" row num: "//trim(
ascharacter(irow)), &
657 if(len_trim(snodatavalue) > 0)
then
658 read(unit=snodatavalue, fmt=*, iostat=istat) pgrd%dpNoDataValue
659 call assert ( istat == 0, &
660 "Failed to read NODATA value in grid data - file: " &
661 //trim(sfilename), __file__,__line__ )
667 "Internal error -- illegal ARC GRID data type", &
677 open ( unit=
lu_grid, iostat=istat )
678 call assert ( istat == 0,
"Failed to close grid file" )
687 character (len=*),
intent(in) :: sFileName
688 type (GENERAL_GRID_T),
pointer :: pGrd
691 character (len=256) :: sInputRecord
692 character (len=256) :: sDirective
693 character (len=256) :: sArgument
694 character (len=256) :: sNoDataValue
695 character (len=8192) :: sBuf
696 integer (c_int) :: iStat
697 integer (c_int) :: iNX,iNY
698 integer (c_int) :: iHdrRecs
699 integer (c_int) :: iCol,iRow,k
700 real (c_double) :: rX0,rX1
701 real (c_double) :: rY0,rY1
702 real (c_double) :: rCellSize
703 integer (c_int) :: iCount,iCumlCount
704 logical (c_bool) :: lXLLCenter, lYLLCenter
705 logical (c_bool) :: lFileExists
706 logical (c_bool) :: lIsOpen
710 inquire(file=trim(sfilename), exist=lfileexists, opened=lisopen )
711 call assert( lfileexists,
"The Arc ASCII grid file "//
dquote(sfilename)// &
712 " could not be found.",__file__,__line__)
714 inquire(unit=
lu_grid, opened=lisopen )
715 if (lisopen )
close( unit=
lu_grid )
717 open ( newunit=
lu_grid, iostat=istat, file=trim(sfilename) )
718 call assert( istat == 0, &
719 "Could not open input file " // trim(sfilename) )
727 read ( unit=
lu_grid, fmt=
"(a256)", iostat=istat ) sinputrecord
728 call assert ( istat == 0, &
729 "Could not read input record - file:"//trim(sfilename) )
730 call chomp ( sinputrecord, sdirective )
732 call chomp ( sinputrecord, sargument )
733 if ( sdirective ==
"NCOLS" )
then
734 read ( unit=sargument, fmt=*, iostat=istat ) inx
735 call assert ( istat == 0,
"Could not read NCOLS" )
736 ihdrrecs = ihdrrecs + 1
737 else if ( sdirective ==
"NROWS" )
then
738 read ( unit=sargument, fmt=*, iostat=istat ) iny
739 call assert ( istat == 0,
"Could not read NROWS" )
740 ihdrrecs = ihdrrecs + 1
741 else if ( sdirective ==
"XLLCENTER" )
then
742 read ( unit=sargument, fmt=*, iostat=istat ) rx0
743 call assert ( istat == 0,
"Could not read XLLCENTER" )
745 ihdrrecs = ihdrrecs + 1
746 else if ( sdirective ==
"YLLCENTER" )
then
747 read ( unit=sargument, fmt=*, iostat=istat ) ry0
748 call assert ( istat == 0,
"Could not read YLLCENTER" )
750 ihdrrecs = ihdrrecs + 1
751 else if ( sdirective ==
"XLLCORNER" )
then
752 read ( unit=sargument, fmt=*, iostat=istat ) rx0
753 call assert ( istat == 0,
"Could not read XLLCORNER" )
754 ihdrrecs = ihdrrecs + 1
755 else if ( sdirective ==
"YLLCORNER" )
then
756 read ( unit=sargument, fmt=*, iostat=istat ) ry0
757 call assert ( istat == 0,
"Could not read YLLCORNER" )
758 ihdrrecs = ihdrrecs + 1
759 else if ( sdirective ==
"CELLSIZE" )
then
760 read ( unit=sargument, fmt=*, iostat=istat ) rcellsize
761 call assert ( istat == 0,
"Could not read CELLSIZE" )
762 ihdrrecs = ihdrrecs + 1
763 else if ( sdirective ==
"NODATA_VALUE" )
then
764 snodatavalue = trim(sargument)
765 ihdrrecs = ihdrrecs + 1
770 rewind( unit=
lu_grid, iostat=istat )
771 call assert ( istat == 0,
"Failed to rewind grid file" )
773 read ( unit=
lu_grid, fmt=
"(a256)", iostat=istat ) sinputrecord
774 call assert ( istat == 0, &
775 "Could not read input record - file: "//trim(sfilename))
778 select case ( pgrd%iDataType )
782 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%iData(:,irow)
783 call assert ( istat == 0, &
784 "Failed to read integer grid data - file: " &
785 //trim(sfilename)//
"; row num: "//trim(
ascharacter(irow)) &
786 //
"; line num: "//trim(
ascharacter(irow + ihdrrecs)) &
790 if(len_trim(snodatavalue) > 0)
then
791 read(unit=snodatavalue, fmt=*, iostat=istat) pgrd%iNoDataValue
792 call assert ( istat == 0, &
793 "Failed to read NODATA value in grid data - file: " &
794 //trim(sfilename), __file__,__line__ )
800 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%rData(:,irow)
802 call assert ( istat == 0, &
803 "Failed to read real grid data - file: " &
804 //trim(sfilename)//
"; row num: "//trim(
ascharacter(irow)) &
805 //
"; line num: "//trim(
ascharacter(irow + ihdrrecs)) &
809 if(len_trim(snodatavalue) > 0)
then
810 read(unit=snodatavalue, fmt=*, iostat=istat) pgrd%rNoDataValue
811 call assert ( istat == 0, &
812 "Failed to read NODATA value in grid data - file: " &
813 //trim(sfilename), __file__,__line__ )
818 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%dpData(:,irow)
819 call assert ( istat == 0, &
820 "Failed to read double-precision grid data - file: " &
821 //trim(sfilename)//
"; row num: "//trim(
ascharacter(irow)) &
822 //
"; line num: "//trim(
ascharacter(irow + ihdrrecs)) &
826 if(len_trim(snodatavalue) > 0)
then
827 read(unit=snodatavalue, fmt=*, iostat=istat) pgrd%dpNoDataValue
828 call assert ( istat == 0, &
829 "Failed to read NODATA value in grid data - file: " &
830 //trim(sfilename), __file__,__line__ )
835 "Internal error -- illegal ARC GRID data type", &
844 open ( unit=
lu_grid, iostat=istat )
845 call assert ( istat == 0,
"Failed to close grid file" )
877 character (len=*),
intent(in) :: sfilename
878 integer (c_int),
intent(in) :: idatatype
882 character (len=4) :: ssentinel
883 integer (c_int) :: istat
884 integer (c_int) :: inx,iny
885 integer (c_int) :: icol,irow
886 real (c_double) :: rx0,rx1
887 real (c_double) :: ry0,ry1
888 real (c_float) :: rz0,rz1
889 logical (c_bool) :: lfileexists
890 logical (c_bool) :: lisopen
892 inquire(file=trim(sfilename), exist=lfileexists)
893 call assert( lfileexists,
"The Surfer ASCII grid file "//
dquote(sfilename)// &
894 " could not be found.",__file__,__line__)
896 inquire(unit=
lu_grid, opened=lisopen )
897 if (lisopen )
close( unit=
lu_grid )
899 open ( newunit=
lu_grid, iostat=istat, file=trim(sfilename) )
900 call assert( istat == 0, &
901 "Could not open input file " // trim(sfilename) )
903 read ( unit=
lu_grid, fmt=*, iostat=istat ) ssentinel
904 call assert ( istat == 0, &
905 "Could not read first record of SURFER grid" )
906 call assert (
LOGICAL(trim(sSentinel) == "DSAA",c_bool), &
907 "This is not a SURFER grid" )
908 read ( unit=
lu_grid, fmt=*, iostat=istat ) inx, iny
909 call assert ( istat == 0, &
910 "Error reading SURFER grid dimensions" )
911 read ( unit=
lu_grid, fmt=*, iostat=istat ) rx0, rx1
912 call assert ( istat == 0, &
913 "Error reading SURFER X limits" )
914 read ( unit=
lu_grid, fmt=*, iostat=istat ) ry0, ry1
915 call assert ( istat == 0, &
916 "Error reading SURFER y limits" )
917 read ( unit=
lu_grid, fmt=*, iostat=istat ) rz0, rz1
918 call assert ( istat == 0, &
919 "Error reading SURFER Z limits" )
921 pgrd =>
grid_create( inx, iny, rx0, ry0, rx1, ry1, idatatype )
922 select case ( idatatype )
925 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%iData(:,irow)
926 call assert ( istat == 0,
"Failed to read integer grid data" )
930 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%rData(:,irow)
931 call assert ( istat == 0,
"Failed to read real grid data" )
935 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%dpData(:,irow)
936 call assert ( istat == 0,
"Failed to read double-precision grid data" )
939 call assert (
false,
"Internal error -- illegal SURFER grid data type" )
942 call assert(inx>0,
"Must have a non-zero number of columns surfer grid file...")
943 call assert(iny>0,
"Must have a non-zero number of rows in a surfer grid file...")
945 pgrd%rGridCellSize = (rx1-rx0)/inx
949 open ( unit=
lu_grid, iostat=istat )
950 call assert ( istat == 0,
"Failed to close grid file" )
959 character (len=*),
intent(in) :: sFileName
960 type (GENERAL_GRID_T),
pointer :: pGrd
963 character (len=4) :: sSentinel
964 integer (c_int) :: iStat
965 integer (c_int) :: iNX,iNY
966 integer (c_int) :: iCol,iRow
967 real (c_double) :: rX0,rX1
968 real (c_double) :: rY0,rY1
969 real (c_float) :: rZ0,rZ1
970 logical (c_bool) :: lFileExists
971 logical (c_bool) :: lIsOpen
973 inquire(file=trim(sfilename), exist=lfileexists)
974 call assert( lfileexists,
"The Surfer ASCII grid file "//
dquote(sfilename)// &
975 " could not be found.",__file__,__line__)
977 inquire(unit=
lu_grid, opened=lisopen )
978 if (lisopen )
close( unit=
lu_grid )
980 open ( newunit=
lu_grid, iostat=istat, file=trim(sfilename) )
981 call assert( istat == 0, &
982 "Could not open input file " // trim(sfilename) )
984 read ( unit=
lu_grid, fmt=*, iostat=istat ) ssentinel
985 call assert ( istat == 0, &
986 "Could not read first record of SURFER grid" )
987 call assert (
LOGICAL(trim(sSentinel) == "DSAA",c_bool), &
988 "This is not a SURFER grid" )
989 read ( unit=
lu_grid, fmt=*, iostat=istat ) inx, iny
990 call assert ( istat == 0, &
991 "Error reading SURFER grid dimensions" )
992 read ( unit=
lu_grid, fmt=*, iostat=istat ) rx0, rx1
993 call assert ( istat == 0, &
994 "Error reading SURFER X limits" )
995 read ( unit=
lu_grid, fmt=*, iostat=istat ) ry0, ry1
996 call assert ( istat == 0, &
997 "Error reading SURFER y limits" )
998 read ( unit=
lu_grid, fmt=*, iostat=istat ) rz0, rz1
999 call assert ( istat == 0, &
1000 "Error reading SURFER Z limits" )
1002 select case ( pgrd%iDataType )
1005 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%iData(:,irow)
1006 call assert ( istat == 0,
"Failed to read integer grid data" )
1010 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%rData(:,irow)
1011 call assert ( istat == 0,
"Failed to read real grid data" )
1015 read ( unit=
lu_grid, fmt=*, iostat=istat ) pgrd%dpData(:,irow)
1016 call assert ( istat == 0,
"Failed to read double-precision grid data" )
1019 call assert (
false,
"Internal error -- illegal SURFER grid data type" )
1022 call assert(inx > 0,
"Must have a non-zero number of grid cells in a surfer grid file...")
1023 call assert(iny > 0,
"Must have a non-zero number of grid cells in a surfer grid file...")
1027 open ( unit=
lu_grid, iostat=istat )
1028 call assert ( istat == 0,
"Failed to close grid file" )
1037 character (len=*),
intent(in) :: sfilename
1039 integer (c_int) :: ioutputformat
1058 character (len=*),
intent(in) :: sfilename
1063 integer (c_int) :: icol,irow
1064 integer (c_int) :: inumcols, inumrows
1065 integer (c_int) :: istat
1066 character(len=256) :: sbuf
1069 inumcols =
size(pgrd%iData,1)
1070 inumrows =
size(pgrd%iData,2)
1072 inumcols =
size(pgrd%rData,1)
1073 inumrows =
size(pgrd%rData,2)
1075 inumcols =
size(pgrd%dpData,1)
1076 inumrows =
size(pgrd%dpData,2)
1078 call assert(
false,
"Internal programming error - Unsupported grid type", &
1083 write(sbuf,fmt=
"(a,a,a)")
'(',trim(
ascharacter(inumcols)),
'(a,1x))'
1086 call assert( istat==0,
"Could not open output file "//
dquote(sfilename), &
1089 write ( unit=
lu_temp, fmt=
"('NCOLS ',i10)", iostat=istat ) inumcols
1090 call assert( istat==0,
"Error writing grid file header", __file__, __line__)
1092 write ( unit=
lu_temp, fmt=
"('NROWS ',i10)", iostat=istat ) inumrows
1093 call assert( istat==0,
"Error writing grid file header", __file__, __line__)
1095 write ( unit=
lu_temp, fmt=
"('XLLCORNER ',f14.3)", iostat=istat ) pgrd%rX0
1096 call assert( istat==0,
"Error writing X limits", __file__, __line__)
1098 write ( unit=
lu_temp, fmt=
"('YLLCORNER ',f14.3)", iostat=istat ) pgrd%rY0
1099 call assert( istat==0,
"Error writing Y limits", __file__, __line__)
1101 write ( unit=
lu_temp, fmt=
"('CELLSIZE ',f14.3)", iostat=istat ) pgrd%rGridCellSize
1102 call assert( istat==0,
"Error writing cell size", __file__, __line__)
1106 write ( unit=
lu_temp, fmt=
"('NODATA_VALUE ',i14)", iostat=istat ) pgrd%iNoDataValue
1107 call assert( istat==0,
"Error writing NODATA value", __file__, __line__)
1109 write( unit=
lu_temp, fmt=trim(sbuf), iostat=istat ) &
1110 (trim(
ascharacter(pgrd%iData(icol,irow) ) ),icol=1,inumcols)
1111 call assert( istat==0,
"Error writing Arc ASCII INTEGER grid data", &
1117 write ( unit=
lu_temp, fmt=
"('NODATA_VALUE ',g0.4)", iostat=istat ) pgrd%rNoDataValue
1118 call assert( istat==0,
"Error writing NODATA value", __file__, __line__)
1120 write( unit=
lu_temp, fmt=trim(sbuf), iostat=istat ) &
1121 (trim(
ascharacter( pgrd%rData(icol,irow), fmt_string=
"g0.5" )),icol=1,inumcols)
1122 call assert( istat==0,
"Error writing Arc ASCII REAL grid data", &
1128 write ( unit=
lu_temp, fmt=
"('NODATA_VALUE ',g0.4)", iostat=istat ) pgrd%dpNoDataValue
1129 call assert( istat==0,
"Error writing NODATA value", __file__, __line__)
1131 write( unit=
lu_temp, fmt=trim(sbuf), iostat=istat ) &
1132 (trim(
ascharacter( pgrd%dpData(icol,irow), fmt_string=
"g0.5" )),icol=1,inumcols)
1133 call assert( istat==0,
"Error writing Arc ASCII REAL grid data", &
1148 character (len=*),
intent(in) :: sfilename
1152 integer (c_int) :: icol,irow
1153 integer (c_int) :: inumcols, inumrows
1154 integer (c_int) :: istat
1155 character(len=256) :: sbuf
1156 real (c_float) :: fhalfcell
1159 inumcols =
size(pgrd%iData,1)
1160 inumrows =
size(pgrd%iData,2)
1162 inumcols =
size(pgrd%rData,1)
1163 inumrows =
size(pgrd%rData,2)
1165 inumcols =
size(pgrd%dpData,1)
1166 inumrows =
size(pgrd%dpData,2)
1168 call assert(
false,
"Internal programming error - Unsupported grid type", &
1172 fhalfcell = pgrd%rGridCellSize * 0.5_c_float
1175 write(sbuf,fmt=
"(a,a,a)")
'(',trim(
ascharacter(inumcols)),
'(a,1x))'
1178 call assert( istat==0,
"Could not open output file "//
dquote(sfilename), &
1181 write ( unit=
lu_temp, fmt=
"('DSAA')", iostat=istat )
1182 call assert( istat==0,
"Error writing SURFER header", &
1185 write ( unit=
lu_temp, fmt=
"(2i8)", iostat=istat ) inumcols, inumrows
1186 call assert( istat==0,
"Error writing SURFER dimensions", &
1189 write ( unit=
lu_temp, fmt=
"(2f14.3)", iostat=istat ) &
1190 pgrd%rX0 + fhalfcell , pgrd%rX1 - fhalfcell
1191 call assert( istat==0,
"Error writing SURFER X limits", &
1194 write ( unit=
lu_temp, fmt=
"(2f14.3)", iostat=istat ) &
1195 pgrd%rY0 + fhalfcell, pgrd%rY1 - fhalfcell
1196 call assert( istat==0,
"Error writing SURFER Y limits", &
1201 write ( unit=
lu_temp, fmt=
"(2i14)", iostat=istat ) minval(pgrd%iData),maxval(pgrd%iData)
1202 call assert( istat==0,
"Error writing SURFER Z limits", &
1206 do irow=inumrows,1,-1
1207 write( unit=
lu_temp, fmt=trim(sbuf), iostat=istat ) &
1208 (trim(
ascharacter(pgrd%iData(icol,irow) ) ),icol=1,inumcols)
1209 call assert( istat==0,
"Error writing SURFER grid data" , &
1215 write ( unit=
lu_temp, fmt=
"(2f14.3)", iostat=istat ) minval(pgrd%rData),maxval(pgrd%rData)
1216 call assert( istat==0,
"Error writing SURFER Z limits", &
1219 do irow=inumrows,1,-1
1220 write( unit=
lu_temp, fmt=trim(sbuf), iostat=istat ) &
1221 (trim(
ascharacter(pgrd%rData(icol,irow) ) ),icol=1,inumcols)
1222 call assert( istat==0,
"Error writing SURFER grid data" , &
1228 write ( unit=
lu_temp, fmt=
"(2f14.3)", iostat=istat ) minval(pgrd%dpData),maxval(pgrd%dpData)
1229 call assert( istat==0,
"Error writing SURFER Z limits", &
1232 do irow=inumrows,1,-1
1233 write( unit=
lu_temp, fmt=trim(sbuf), iostat=istat ) &
1234 (trim(
ascharacter(pgrd%dpData(icol,irow) ) ),icol=1,inumcols)
1235 call assert( istat==0,
"Error writing SURFER grid data" , &
1272 real (c_float),
intent(in),
optional :: rtolerance
1274 logical (c_bool) :: lconform
1276 real (c_float) :: rtol
1277 real (c_float),
parameter :: rdefault_tolerance = 0.5_c_float
1279 if (
present ( rtolerance ) )
then
1280 rtol = rtolerance * ( pgrd1%rX1 - pgrd1%rX0 )
1282 rtol = rdefault_tolerance * ( pgrd1%rX1 - pgrd1%rX0 )
1287 if ( pgrd1%iNX /= pgrd2%iNX )
then
1289 call logs%write(
"Unequal number of columns between grids", iloglevel=
log_all)
1292 if ( pgrd1%iNY /= pgrd2%iNY )
then
1294 call logs%write(
"Unequal number of rows between grids", iloglevel=
log_all)
1297 if( abs( pgrd1%rX0 - pgrd2%rX0 ) > rtol )
then
1298 call logs%write(
"Lower left-hand side X coordinates do not match:", iloglevel=
log_all)
1304 if( abs( pgrd1%rY0 - pgrd2%rY0 ) > rtol )
then
1305 call logs%write(
"Lower left-hand side Y coordinates do not match:", iloglevel=
log_all)
1311 if( abs( pgrd1%rX1 - pgrd2%rX1 ) > rtol )
then
1312 call logs%write(
"Upper right-hand side X coordinates do not match:", iloglevel=
log_all)
1318 if( abs( pgrd1%rY1 - pgrd2%rY1 ) > rtol )
then
1319 call logs%write(
"Upper right-hand side Y coordinates do not match:", iloglevel=
log_all)
1336 real (c_float),
intent(in),
optional :: rtolerance
1338 logical (c_bool) :: lcompletelycover
1340 real (c_float) :: rtol
1341 real (c_float) :: rdefault_tolerance
1343 rdefault_tolerance = -pbasegrd%rGridCellSize / 100.
1346 if (
present ( rtolerance ) )
then
1349 rtol = rdefault_tolerance
1352 if ( (pbasegrd%rX0 - pothergrd%rX0 >= rtol) .and. &
1353 (pbasegrd%rY0 - pothergrd%rY0 >= rtol) .and. &
1354 (pothergrd%rX1 - pbasegrd%rX1 >= rtol) .and. &
1355 (pothergrd%rY1 - pbasegrd%rY1 >= rtol) )
then
1356 lcompletelycover =
true
1358 lcompletelycover =
false
1360 call logs%write(
"Extents of the data grid file "//
dquote(pothergrd%sFilename) &
1361 //
" do not cover the base grid extents.")
1363 call logs%write(
"BASE GRID EXTENTS DATA GRID EXTENTS DIFFERENCE", &
1364 itab=18, ilinesbefore=1)
1367 //
" "//trim(
ascharacter(pbasegrd%rX0 - pothergrd%rX0)) )
1370 //
" "//trim(
ascharacter(pbasegrd%rY0 - pothergrd%rY0)) )
1371 call logs%write(
"X (upper-right): "//trim(
ascharacter(pbasegrd%rX1)) &
1373 //
" "//trim(
ascharacter(pothergrd%rX1 - pbasegrd%rX1)) )
1374 call logs%write(
"Y (upper-right): "//trim(
ascharacter(pbasegrd%rY1)) &
1376 //
" "//trim(
ascharacter(pothergrd%rY1 - pbasegrd%rY1)), ilinesafter=1 )
1429 type ( GENERAL_GRID_T ),
pointer :: pGrd
1430 real (c_float),
intent(in) :: rXval
1431 integer (c_int),
intent(out) :: iBefore,iAfter
1432 real (c_float),
intent(out) :: rFrac
1434 real (c_float) :: rColPosition
1436 rcolposition = (pgrd%iNX - 1) * (rxval - pgrd%rX0) / (pgrd%rX1 - pgrd%rX0)
1438 ibefore = floor(rcolposition) + 1
1439 iafter = ibefore + 1
1440 if ( ibefore > pgrd%iNX .or. ibefore < 1)
then
1443 else if ( iafter > pgrd%iNX )
then
1446 rfrac = mod(rcolposition, 1.0_c_float )
1497 type ( GENERAL_GRID_T ),
pointer :: pGrd
1498 real (c_float),
intent(in) :: rYval
1499 integer (c_int),
intent(out) :: iBefore,iAfter
1500 real (c_float),
intent(out) :: rFrac
1502 real (c_float) :: rColPosition
1504 rcolposition = (pgrd%iNY - 1) * (pgrd%rY1 - ryval) / (pgrd%rY1 - pgrd%rY0)
1506 ibefore = floor(rcolposition) + 1
1507 iafter = ibefore + 1
1508 if ( ibefore > pgrd%iNY )
then
1511 else if ( ibefore < 1)
then
1514 else if ( iafter > pgrd%iNY )
then
1517 rfrac = mod(rcolposition, 1.0_c_float )
1542 character (len=*) :: sfromproj4, stoproj4
1543 character (kind=c_char, len=len_trim(sFromPROJ4)) :: csfromproj4
1544 character (kind=c_char, len=len_trim(sToPROJ4)) :: cstoproj4
1545 real (c_double),
optional :: rx(:)
1546 real (c_double),
optional :: ry(:)
1549 integer (c_int) :: iretval
1550 integer (c_int) :: i
1551 logical (c_bool),
dimension(pGrd%iNY, pGrd%iNX) :: lmask
1553 if (
present(rx) .and.
present(ry))
then
1565 csfromproj4 = trim(sfromproj4)
1566 cstoproj4 = trim(stoproj4)
1571 if ( ( csfromproj4 .containssimilar.
"latlon" ) &
1572 .or. ( csfromproj4 .containssimilar.
"latlong" ) &
1573 .or. ( csfromproj4 .containssimilar.
"lonlat" ) &
1574 .or. ( csfromproj4 .containssimilar.
"longlat" ) )
then
1582 cstoproj4//c_null_char, &
1583 __file__//c_null_char, &
1585 int(pgrd%iNumGridCells, c_long), pgrd%rX, pgrd%rY)
1592 if( index(string=cstoproj4, substring=
"latlon") > 0 &
1593 .or. index(string=cstoproj4, substring=
"lonlat") > 0 )
then
1601 pgrd%rGridCellSize = ( maxval(pgrd%rX) - minval(pgrd%rX) ) &
1602 / real(pgrd%iNX - 1, c_double)
1604 pgrd%rX0 = minval(pgrd%rX) - pgrd%rGridCellSize / 2_c_float
1605 pgrd%rX1 = maxval(pgrd%rX) + pgrd%rGridCellSize / 2_c_float
1606 pgrd%rY0 = minval(pgrd%rY) - pgrd%rGridCellSize / 2_c_float
1607 pgrd%rY1 = maxval(pgrd%rY) + pgrd%rGridCellSize / 2_c_float
1610 pgrd%sPROJ4_string = trim(stoproj4)
1622 type ( GENERAL_GRID_T ),
pointer :: pGrd
1623 character (len=*),
intent(in) :: sFilename
1626 integer (c_int) :: iRunningSum
1627 integer (c_int) :: iIndex
1628 integer (c_int) :: iCount
1629 integer (c_int) :: iRecord
1630 character (len=10) :: sBuf0
1631 character (len=14) :: sBuf1
1632 character (len=10) :: sBuf2
1633 character (len=40) :: sBuf3
1678 integer (c_int) :: iretval
1679 character (len=*) :: sfromproj4
1680 character (len=*) :: stoproj4
1683 integer (c_int) :: i
1684 logical (c_bool) :: lfound
1685 character (len=256) :: serrormessage
1689 if (iretval /= 0)
then
1691 write(serrormessage,fmt=
"(a,a,a,a,a)") &
1692 "There was an error transforming a grid from this projection:~", &
1693 dquote(sfromproj4),
" to:~", &
1698 serrormessage = serrormessage &
1699 //
"~ PROJ4 Error number "//
ascharacter(iretval)//
" reported.~" &
1700 //
" ==> error description: "//trim(
terrormessage(i)%cErrorMessageText)
1706 call assert(
false, trim(serrormessage), __file__, __line__)
1744 real (c_float),
intent(in) :: rxval,ryval
1746 real (c_float) :: rvalue
1748 integer (c_int) :: ib,jb,ia,ja
1749 real (c_float) :: ylocal,u,v
1753 call assert (ib>0 .and. ia>0 .and. ib <= ubound(pgrd%rData,1) &
1754 .and. ia <= ubound(pgrd%rData,1), &
1755 "Internal programming error: illegal bounds caught~requested column value " &
1757 //
" out of range", __file__, __line__)
1761 if ( ryval < pgrd%rY0 )
then
1763 else if ( ryval > pgrd%rY1 )
then
1775 call assert (jb>0 .and. ja>0 .and. jb <= ubound(pgrd%rData,2) &
1776 .and. ja <= ubound(pgrd%rData,2), &
1777 "Internal programming error: illegal bounds caught~requested row value " &
1779 //
" out of range", __file__, __line__)
1781 rvalue = ( 1.0_c_float -u) * ( 1.0_c_float -v) * pgrd%rData(ib,jb) + &
1782 u * ( 1.0_c_float -v) * pgrd%rData(ib,ja) + &
1783 ( 1.0_c_float -u) * v * pgrd%rData(ia,jb) + &
1784 u * v * pgrd%rData(ia,ja)
1836 real (c_float),
intent(in) :: rxval,rzval,rnodata
1838 real (c_float) :: rvalue
1840 integer (c_int) :: ib,ia,istat,irow
1841 real (c_float) :: u,v,frac,rprev
1842 real (c_float),
dimension(:),
allocatable :: rcol
1843 character (len=128) :: buf
1846 allocate ( rcol(pgrd%iNY), stat=istat )
1847 call assert(
LOGICAL(istat==0,c_bool), &
1848 "Couldn't allocate temporary array" )
1856 call assert (ib>0 .and. ia>0 .and. ib <= ubound(pgrd%rData,1) &
1857 .and. ia <= ubound(pgrd%rData,1), &
1858 "Internal programming error: requested X value " &
1860 //
" out of range", __file__, __line__)
1862 call assert(ubound(rcol,1) == ubound(pgrd%rData,2), &
1863 "Internal programming error: upper bound of rCol /= upper " &
1864 //
"bound of first array element of pGrd%rData~" &
1865 //
"ubound(rCol)="//trim(
ascharacter(ubound(rcol,1))) &
1866 //
"~ubound(pGrd%rData)="//trim(
ascharacter(ubound(pgrd%rData,1))), &
1872 rcol = u * pgrd%rData(ia,:) + ( 1.0_c_float -u)*pgrd%rData(ib,:)
1876 if ( pgrd%rData(ia, irow) == rnodata .and. pgrd%rData(ib, irow) == rnodata )
then
1877 rcol(irow) = rnodata
1878 else if ( pgrd%rData(ib,irow) == rnodata .and. u>0.9_c_float )
then
1879 rcol(irow) = pgrd%rData(ia,irow)
1880 else if ( pgrd%rData(ia,irow) == rnodata .and. u<0.1_c_float )
then
1881 rcol(irow) = pgrd%rData(ib,irow)
1882 else if ( pgrd%rData(ia,irow) == rnodata .or. pgrd%rData(ib,irow) == rnodata )
then
1883 rcol(irow) = rnodata
1892 if ( rcol(irow) == rnodata )
then
1893 if ( rprev == rnodata )
then
1898 v = real(irow-1) / real(pgrd%iNY-1)
1899 rvalue = ( 1.0_c_float -v)*pgrd%rY0 + v*pgrd%rY1
1903 if ( rprev == rnodata )
then
1906 if ( sign( 1.0_c_float ,rcol(irow)-rzval) /= sign( 1.0_c_float ,rprev-rzval) )
then
1908 frac = (rzval-rcol(irow-1)) / (rcol(irow)-rcol(irow-1))
1910 v = ( real(irow-2)+frac ) / real(pgrd%iNY-1)
1911 rvalue = v*pgrd%rY0 + ( 1.0_c_float -v)*pgrd%rY1
1913 else if ( rzval < rprev .and. rcol(irow) > rprev )
then
1915 v = real(irow-2) / real(pgrd%iNY-1)
1916 rvalue = v*pgrd%rY0 + ( 1.0_c_float -v)*pgrd%rY1
1918 else if ( rzval > rprev .and. rcol(irow) < rprev )
then
1919 v = real(irow-2) / real(pgrd%iNY-1)
1920 rvalue = v*pgrd%rY0 + ( 1.0_c_float -v)*pgrd%rY1
1922 else if ( irow == pgrd%iNY )
then
1924 rvalue = v*pgrd%rY0 + ( 1.0_c_float -v)*pgrd%rY1
1963 real (c_float),
intent(in) :: rxval,ryval
1964 real (c_float) :: rvalue
1965 integer (c_int) :: icol,irow
1967 irow = int( pgrd%iNY * (pgrd%rY0 - ryval) / (pgrd%rY0 - pgrd%rY1) ) + 1
1968 if ( irow > pgrd%iNY ) irow = pgrd%iNY
1969 icol = int( pgrd%iNX * (rxval - pgrd%rX0) / (pgrd%rX1 - pgrd%rX0) ) + 1
1970 if ( icol > pgrd%iNX ) icol = pgrd%iNX
1971 rvalue = pgrd%rData(icol,irow)
1981 real (c_double) :: rx
1982 real (c_double) :: ry
1983 logical (c_bool) :: coordinates_fall_inside_grid
1985 coordinates_fall_inside_grid = ( ( rx >= pgrd%rX0 ) .and. ( rx <= pgrd%rX1 ) &
1986 .and. ( ry >= pgrd%rY0 ) .and. ( ry <= pgrd%rY1 ) )
1995 integer (c_int) :: irow
1996 integer (c_int) :: icol
1997 logical (c_bool) :: row_col_falls_inside_grid
1999 row_col_falls_inside_grid = ( ( irow >= 1 ) .and. ( irow <= pgrd%iNY ) &
2000 .and. ( icol >= 1 ) .and. ( icol <= pgrd%iNX ) )
2009 real (c_double) :: rx
2010 integer (c_int) :: icolumnnumber
2014 icolumnnumber = nint(real(pgrd%iNX, c_double) &
2015 * ( rx - pgrd%rX0 ) / (pgrd%rX1 - pgrd%rX0) + 0.5_c_double, c_int)
2042 real (c_double) :: ry
2043 integer (c_int) :: irownumber
2045 irownumber = pgrd%iNY - nint(real(pgrd%iNY, c_double) &
2046 * ( ry - pgrd%rY0 ) / (pgrd%rY1 - pgrd%rY0) - 0.5_c_double, c_int)
2063 real (c_double) :: rx, ry
2064 integer (c_int),
dimension(2) :: icolrow
2067 real (c_float) :: rdist, rmindistance, rdist2
2069 integer (c_int),
save :: ilastcolnum, ilastrownum
2070 integer (c_int) :: istartingcolnum, istartingrownum
2071 integer (c_int) :: irowboundlower, irowboundupper
2072 integer (c_int) :: icolboundlower, icolboundupper
2073 integer (c_int) :: icol, irow
2074 integer (c_int) :: icandidaterow, icandidatecol
2075 integer (c_int) :: istat
2076 logical (c_bool) :: lchanged
2086 if (istartingcolnum > 0 .and. istartingcolnum <= pgrd%iNX &
2087 .and. istartingrownum > 0 .and. istartingrownum <= pgrd%iNY)
then
2088 rdist = hypot(pgrd%rX(istartingcolnum,istartingrownum) - rx, &
2089 pgrd%rY(istartingcolnum,istartingrownum) - ry)
2093 if (ilastcolnum > 0 .and. ilastcolnum <= pgrd%iNX &
2094 .and. ilastrownum > 0 .and. ilastrownum <= pgrd%iNY)
then
2095 rdist2 = hypot(pgrd%rX(ilastcolnum,ilastrownum) - rx, &
2096 pgrd%rY(ilastcolnum,ilastrownum) - ry)
2101 if (rdist > rdist2)
then
2102 icandidaterow = ilastrownum
2103 icandidatecol = ilastcolnum
2105 icandidaterow = istartingrownum
2106 icandidatecol = istartingcolnum
2119 irowboundlower =
clip(
value=icandidaterow - 1, minval=1, maxval=pgrd%iNY)
2120 irowboundupper =
clip(
value=icandidaterow + 1, minval=1, maxval=pgrd%iNY)
2123 icolboundlower =
clip(
value=icandidatecol - 1, minval=1, maxval=pgrd%iNX)
2124 icolboundupper =
clip(
value=icandidatecol + 1, minval=1, maxval=pgrd%iNX)
2128 do irow=irowboundlower,irowboundupper
2129 do icol=icolboundlower,icolboundupper
2131 rdist = hypot(pgrd%rX(icol,irow) - rx, pgrd%rY(icol,irow) - ry)
2133 if (rdist < rmindistance )
then
2135 rmindistance = rdist
2136 icandidatecol = icol
2137 icandidaterow = irow
2147 if (.not. lchanged )
exit
2151 ilastcolnum = icandidatecol
2152 ilastrownum = icandidaterow
2154 icolrow(
column) = icandidatecol
2155 icolrow(
row) = icandidaterow
2159 icolrow(
column) = istartingcolnum
2160 icolrow(
row) = istartingrownum
2168 integer (c_int),
intent(in),
optional :: ivalue
2169 real (c_float),
intent(in),
optional :: fvalue
2171 if (
present( ivalue ) ) pgrd%iNoDataValue = ivalue
2172 if (
present( fvalue ) ) pgrd%rNoDataValue = fvalue
2179 real (c_double) :: rx
2180 integer (c_int) :: icolumnnumber
2182 rx = pgrd%rX0 + pgrd%rGridCellSize * (real(icolumnnumber, c_double) - 0.5_c_double )
2191 real (c_double) :: ry
2192 integer (c_int) :: irownumber
2195 - pgrd%rGridCellSize * (real(irownumber, c_double) - 0.5_c_double )
2205 real (c_double),
optional :: rx(:)
2206 real (c_double),
optional :: ry(:)
2209 integer (c_int) :: icol,irow
2210 integer (c_int) :: istat
2211 integer (c_int) :: nx, ny
2213 if ( .not.
allocated(pgrd%rX) )
then
2215 ALLOCATE (pgrd%rX(pgrd%iNX, pgrd%iNY), stat=istat)
2216 call assert( istat == 0, &
2217 "Could not allocate memory for x-coordinates within grid data structure", &
2221 if ( .not.
allocated(pgrd%rY) )
then
2222 ALLOCATE (pgrd%rY(pgrd%iNX, pgrd%iNY), stat=istat)
2223 call assert( istat == 0, &
2224 "Could not allocate memory for y-coordinates within grid data structure", &
2228 if (
present(rx) .and.
present(ry))
then
2233 call assert( nx == pgrd%iNX, &
2234 "Internal programming error: number of 'X' values does not match number of x values in grid", &
2237 call assert( ny == pgrd%iNY, &
2238 "Internal programming error: number of 'X' values does not match number of x values in grid", &
2243 pgrd%rX(icol, irow) = rx(icol)
2244 pgrd%rY(icol, irow) = ry(irow)
2274 if (
associated( pgrd) )
then
2276 call logs%write(
"---------------------------------------------------")
2277 call logs%write(
"GRID DETAILS:")
2278 call logs%write(
"---------------------------------------------------")
2279 call logs%write(
"file: "//
dquote(pgrd%sFilename) )
2282 call logs%write(
"cellsize: "//trim(
ascharacter(pgrd%rGridCellSize) ) )
2288 call logs%write(
"PROJ4 string: "//
dquote(pgrd%sPROJ4_string) )
2289 call logs%write(
"---------------------------------------------------")
2293 call logs%write(
"---------------------------------------------------")
2294 call logs%write(
" ** grid not allocated ** ")
2295 call logs%write(
"---------------------------------------------------")
2307 integer (c_int),
intent(in) :: icol
2308 integer (c_int),
intent(in) :: irow
2309 integer (c_int) :: ivalue
2312 integer (c_int),
dimension(2) :: icolrow
2313 integer (c_int) :: isrccol, isrcrow
2314 integer (c_int) :: ispread
2322 rx=real(pgrdto%rX(icol, irow), c_double), &
2323 ry=real(pgrdto%rY(icol, irow), c_double))
2325 if ( icolrow(
column) < 1 .or. icolrow(
column) > pgrdfrom%iNX ) &
2329 if ( icolrow(
row) < 1 .or. icolrow(
row) > pgrdfrom%iNY ) &
2335 ispread = max(1, nint(pgrdto%rGridCellSize / pgrdfrom%rGridCellSize / 2.))
2338 itargetcol=icolrow(
column), &
2339 itargetrow=icolrow(
row), &
2340 inodatavalue=pgrdfrom%iNoDataValue, &
2353 logical (c_bool),
intent(in) :: lusemajorityfilter
2356 integer (c_int) :: icol, irow
2357 integer (c_int),
dimension(2) :: icolrow
2358 integer (c_int) :: isrccol, isrcrow
2359 integer (c_int) :: ispread
2360 real (c_float) :: fgridcellratio
2367 fgridcellratio = pgrdto%rGridCellSize / pgrdfrom%rGridCellSize
2370 if ( lusemajorityfilter )
then
2372 call logs%write(
"** Majority filter in use for data from grid file "//
dquote(pgrdfrom%sFilename), lecho=
true )
2374 ispread = max( 1, nint( fgridcellratio / 2.0_c_float ) )
2376 do irow=1,pgrdto%iNY
2377 do icol=1,pgrdto%iNX
2380 rx=real(pgrdto%rX(icol, irow), c_double), &
2381 ry=real(pgrdto%rY(icol, irow), c_double) )
2387 call assert(icolrow(
row) > 0 .and. icolrow(
row) <= pgrdfrom%iNY, &
2388 "Illegal row number supplied: "//trim(
ascharacter(icolrow(
row))), &
2392 itargetcol=icolrow(
column), &
2393 itargetrow=icolrow(
row), &
2394 inodatavalue=pgrdfrom%iNoDataValue, &
2402 do irow=1,pgrdto%iNY
2403 do icol=1,pgrdto%iNX
2406 rx=real(pgrdto%rX(icol, irow), c_double), &
2407 ry=real(pgrdto%rY(icol, irow), c_double))
2413 call assert(icolrow(
row) > 0 .and. icolrow(
row) <= pgrdfrom%iNY, &
2414 "Illegal row number supplied: "//trim(
ascharacter(icolrow(
row))), &
2417 pgrdto%iData(icol,irow) = pgrdfrom%iData( icolrow(
column), icolrow(
row) )
2435 integer (c_int),
dimension(2) :: icolrow
2436 integer (c_int) :: icol, irow
2437 integer (c_int) :: isrccol, isrcrow
2438 real (c_float),
dimension(3,3) :: rkernel
2451 do irow=1,pgrdto%iNY
2452 do icol=1,pgrdto%iNX
2455 rx=real(pgrdto%rX(icol, irow), c_double), &
2456 ry=real(pgrdto%rY(icol, irow), c_double))
2458 if ( icolrow(
column) < 1 .or. icolrow(
column) > pgrdfrom%iNX) cycle
2459 if ( icolrow(
row) < 1 .or. icolrow(
row) > pgrdfrom%iNY) cycle
2461 pgrdto%rData(icol,irow) = pgrdfrom%rData( icolrow(
column), icolrow(
row) )
2485 iTargetRow, iNoDataValue, iSpread)
result(iRetVal)
2488 integer (c_int) :: itargetcol
2489 integer (c_int) :: itargetrow
2490 integer (c_int) :: inodatavalue
2491 integer (c_int) :: iretval
2492 integer (c_int) :: ispread
2496 integer (c_int),
dimension(625) :: ivalue
2497 integer (c_int),
dimension(625) :: icount
2498 logical (c_bool) :: lmatch
2499 integer (c_int) :: irow
2500 integer (c_int) :: icol
2501 integer (c_int) :: i, isize, ilast, iindex, icellnum
2509 ispread = min(ispread, 12)
2511 do irow=max(1,itargetrow - ispread), min(pgrdfrom%iNY, itargetrow + ispread)
2512 do icol=max(1,itargetcol - ispread), min(pgrdfrom%iNX, itargetcol + ispread)
2514 icellnum = icellnum + 1
2519 if (ivalue(i) == pgrdfrom%iData(icol, irow) )
then
2520 icount(i) = icount(i) + 1
2528 if (.not. lmatch)
then
2530 ivalue(ilast) = pgrdfrom%iData(icol, irow)
2531 icount(ilast) = icount(ilast) + 1
2538 iindex = maxloc(icount, dim=1)
2539 iretval = ivalue(iindex)
2544 iTargetRow, rKernel, rNoDataValue)
result(rRetVal)
2547 integer (c_int) :: inx
2548 integer (c_int) :: iny
2549 integer (c_int) :: itargetcol
2550 integer (c_int) :: itargetrow
2551 real (c_float),
dimension(:,:) :: rkernel
2552 real (c_float),
optional :: rnodatavalue
2553 real (c_float) :: rretval
2556 integer (c_int) :: irowmin, irowmax
2557 integer (c_int) :: icolmin, icolmax
2558 integer (c_int) :: ikernelsize
2559 integer (c_int) :: iincvalue
2560 integer (c_int) :: icol, irow, irownum, icolnum
2561 real (c_float) :: rkernelsum
2566 ikernelsize =
size(rkernel, dim=1)
2567 iincvalue = (ikernelsize - 1) / 2
2569 inx = ubound(pgrdfrom%rData,1)
2570 iny = ubound(pgrdfrom%rData,2)
2572 irowmin = max(1,itargetrow - iincvalue)
2573 irowmax = min(iny, itargetrow + iincvalue)
2575 icolmin = max(1,itargetcol - iincvalue)
2576 icolmax = min(iny, itargetcol + iincvalue)
2578 if( (irowmax - irowmin + 1) /= ikernelsize &
2579 .or. (icolmax - icolmin + 1) /= ikernelsize )
then
2585 rretval = pgrdfrom%rData(itargetcol, itargetrow)
2589 do icol=0,ikernelsize-1
2590 do irow=0,ikernelsize-1
2592 irownum = irow + irowmin
2593 icolnum = icol + icolmin
2595 if (irownum > iny .or. icolnum > inx)
then
2599 rretval = rretval + pgrdfrom%rData(icolnum,irownum) * rkernel(icol+1, irow+1)
2600 rkernelsum = rkernelsum + rkernel(icol+1, irow+1)
2605 if (rkernelsum > 0.) rretval = rretval / rkernelsum
This module contains physical constants and convenience functions aimed at performing unit conversion...
logical(c_bool), parameter, public true
real(c_float), parameter, public rzero
integer(c_int), parameter datatype_real
logical(c_bool), parameter, public false
real(c_double), parameter, public degrees_to_radians
real(c_float), parameter, public rbigval
integer(c_int), parameter datatype_int
real(c_double), parameter, public radians_to_degrees
integer(c_int), parameter datatype_double
subroutine, public die(smessage, smodule, iline, shints, scalledby, icalledbyline)
Provides support for input and output of gridded ASCII data, as well as for creation and destruction ...
type(general_grid_t) function, pointer, public grid_createcomplete(inx, iny, rx0, ry0, rx1, ry1, idatatype)
Creates a grid of a specified type.
integer(c_int) function, public grid_getgridcolnum(pgrd, rx)
logical(c_bool) function, public grid_coordinatesfallinsidegrid(pgrd, rx, ry)
type(error_message_t), dimension(49) terrormessage
subroutine, public grid_populatexy(pgrd, rx, ry)
real(c_float), parameter, public grid_nodata_real
real(c_float) function grid_searchcolumn(pgrd, rxval, rzval, rnodata)
character(len=256) output_grid_directory_name
logical(c_bool) function, public grid_completelycover(pbasegrd, pothergrd, rtolerance)
integer(c_int), parameter eight_cells
integer(c_int), parameter, public grid_active_cell
type(general_grid_t) function, pointer, public grid_read(sfilename, sfiletype, idatatype)
real(c_float) function grid_lookupreal(pgrd, rxval, ryval)
integer(c_int), parameter four_cells
real(c_float) function grid_convolve_sgl(pgrdfrom, itargetcol, itargetrow, rkernel, rnodatavalue)
subroutine, public grid_writegrid(sfilename, pgrd, ioutputformat)
real(c_float), parameter nc_fill_float
subroutine, public grid_dumpgridextent(pgrd)
integer(c_int) function, public grid_getgridrownum(pgrd, ry)
type(general_grid_t) function, pointer grid_readarcgrid_fn(sfilename, idatatype)
type(general_grid_t) function, pointer, public grid_createsimple(inx, iny, rx0, ry0, rgridcellsize, idatatype)
integer(c_int), parameter, public grid_datatype_int
subroutine, public grid_destroy(pgrd)
subroutine, public grid_writearcgrid(sfilename, pgrd)
integer(c_int), parameter, private row
subroutine grid_lookuprow(pgrd, ryval, ibefore, iafter, rfrac)
real(c_double), parameter, public grid_nodata_double
subroutine, public grid_set_nodata_value(pgrd, ivalue, fvalue)
subroutine grid_lookupcolumn(pgrd, rxval, ibefore, iafter, rfrac)
integer(c_int) function grid_gridtopoint_int(pgrdfrom, pgrdto, icol, irow)
subroutine, public grid_gridtogrid_int(pgrdfrom, pgrdto, lusemajorityfilter)
integer(c_int) function, dimension(2) grid_getgridcolrownum(pgrd, rx, ry)
logical(c_bool) function, public grid_rowcolfallsinsidegrid(pgrd, irow, icol)
integer(c_int), parameter, public grid_nodata_int
integer(c_int), parameter, public grid_datatype_real
subroutine, public grid_readexisting(sfilename, sfiletype, pgrd)
integer(c_int), parameter, public output_arc
real(c_float) function, public grid_interpolate(pgrd, rxval, ryval)
integer(c_int), parameter, private column
integer(c_int), parameter, public output_surfer
subroutine grid_readsurfergrid_sub(sfilename, pgrd)
integer(c_int), parameter nc_fill_int
real(c_double), parameter nc_fill_double
subroutine, public grid_gridtogrid_sgl(pgrdfrom, pgrdto)
real(c_double) function, public grid_getgridy(pgrd, irownumber)
subroutine, public grid_writesurfergrid(sfilename, pgrd)
subroutine grid_checkintegergridvalues(pgrd, sfilename)
logical(c_bool) function, public grid_conform(pgrd1, pgrd2, rtolerance)
subroutine, public grid_checkforproj4error(iretval, sfromproj4, stoproj4)
subroutine, public grid_set_output_directory_name(sdirname)
subroutine, public grid_transform(pgrd, sfromproj4, stoproj4, rx, ry)
Call PROJ4 to transform coordinates.
real(c_double) function, public grid_getgridx(pgrd, icolumnnumber)
type(general_grid_t) function, pointer grid_readsurfergrid_fn(sfilename, idatatype)
integer(c_int), parameter, public grid_datatype_double
subroutine grid_readarcgrid_sub(sfilename, pgrd)
integer(c_int) function grid_majorityfilter_int(pgrdfrom, itargetcol, itargetrow, inodatavalue, ispread)
type(logfile_t), public logs