module ghcnm_read ! Global Historical Climatology Network - Monthly (GHCNM) ! Last Updated: 04/05/2012, Version 1.0 ! Minimum Requirements: Fortran 95 compiler that implements ! Technical Report ISO/IEC 15581 TR:1998(E) ! (Note: most Fortran 95 compilers implement this tech report) !******************************************************************************* ! Module Declarations integer, private, parameter :: END_OF_FILE=-1 ! Note: compiler dependent value integer, private, parameter :: MEMORY_ERROR=0 ! Note: compiler dependent value integer, public, parameter :: GHCNM_NUMBER_OF_ELEMENTS=3 integer, public, parameter :: GHCNM_TMAX=1 integer, public, parameter :: GHCNM_TAVG=2 integer, public, parameter :: GHCNM_TMIN=3 real, public, parameter :: GHCNM_MISSING=-999.9 type ghcnm_structure integer :: number_of_stations integer, dimension(1:GHCNM_NUMBER_OF_ELEMENTS) :: first_year integer, dimension(1:GHCNM_NUMBER_OF_ELEMENTS) :: last_year real, allocatable, dimension(:) :: longitude real, allocatable, dimension(:) :: latitude real, allocatable, dimension(:) :: elevation real, allocatable, dimension(:,:,:,:) :: data character(len=01), allocatable, dimension(:,:,:,:) :: data_measurement character(len=01), allocatable, dimension(:,:,:,:) :: data_quality character(len=01), allocatable, dimension(:,:,:,:) :: data_source character(len=11), allocatable, dimension(:) :: station_id character(len=30), allocatable, dimension(:) :: station_name end type ghcnm_structure contains subroutine get_ghcnm(metadatafile,datafile,ghcnm,begin_year,end_year) !*************************************** ! subroutine get_ghcnm Declarations character(len=*), intent(in) :: metadatafile character(len=*), intent(in) :: datafile type(ghcnm_structure), intent(out) :: ghcnm integer, intent(in), optional :: begin_year integer, intent(in), optional :: end_year integer :: begin integer :: counter integer :: element integer :: end integer :: month integer :: year integer :: station integer :: station_number integer :: status integer, dimension(1:12) :: value character(len=01), dimension(1:12) :: flag1 character(len=01), dimension(1:12) :: flag2 character(len=01), dimension(1:12) :: flag3 character(len=08) :: date real :: elevation real :: latitude real :: longitude character(len=04) :: char_element character(len=11) :: station_id character(len=11) :: previous_station_id character(len=30) :: station_name logical :: file_found logical :: station_not_found !*************************************** ! subroutine get_ghcnm Metadata file processing inquire(file=metadatafile,exist=file_found) if (.not.(file_found)) stop "ERROR: ghcnm_read module, metadatafile does not exist" ghcnm%number_of_stations=0 open(unit=22,file=metadatafile,status='old') do read(22,*,IOSTAT=status) if (status==END_OF_FILE) exit ghcnm%number_of_stations=ghcnm%number_of_stations+1 enddo close(22) allocate(ghcnm%longitude(1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%latitude(1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%elevation(1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%station_id(1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%station_name(1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" counter=0 open(unit=22,file=metadatafile,status='old') do read(22,'(a11,f9.4,1x,f9.4,f7.1,1x,a30)',IOSTAT=status) station_id,latitude,longitude,elevation,station_name if (status==END_OF_FILE) exit counter=counter+1 ghcnm%station_id(counter)=station_id ghcnm%longitude(counter)=longitude ghcnm%latitude(counter)=latitude ghcnm%elevation(counter)=elevation ghcnm%station_name(counter)=station_name enddo close(22) !*************************************** ! subroutine get_ghcnm Data file processing if (present(begin_year).and.present(end_year)) then if (begin_year > end_year) stop "ERROR: ghcnm module, begin_year > end_year in subroutine get_ghcnm" begin=begin_year end=end_year else begin=1690 call date_and_time(DATE=date) read(date(1:4),'(i4)') end endif allocate(ghcnm%data(1:12,begin:end,1:GHCNM_NUMBER_OF_ELEMENTS,1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%data_measurement(1:12,begin:end,1:GHCNM_NUMBER_OF_ELEMENTS,1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%data_quality(1:12,begin:end,1:GHCNM_NUMBER_OF_ELEMENTS,1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" allocate(ghcnm%data_source(1:12,begin:end,1:GHCNM_NUMBER_OF_ELEMENTS,1:ghcnm%number_of_stations),STAT=status) if (status > MEMORY_ERROR) stop "ERROR: ghcnm_read module, memory error" ghcnm%data(:,:,:,:)=GHCNM_MISSING ghcnm%data_measurement(:,:,:,:)=" " ghcnm%data_quality(:,:,:,:)=" " ghcnm%data_source(:,:,:,:)=" " inquire(file=datafile,exist=file_found) if (.not.(file_found)) stop "ERROR: ghcnm_read module, datafile does not exist" previous_station_id="" open(unit=22,file=datafile,status='old') do read(22,22,IOSTAT=status) station_id,year,char_element,(value(month),flag1(month),flag2(month),flag3(month),month=1,12) if (status==END_OF_FILE) exit if (year < begin .or. year > end ) cycle if (station_id/=previous_station_id) then station_not_found=.true. do station=1,ghcnm%number_of_stations if (station_id==ghcnm%station_id(station)) then station_number=station previous_station_id=station_id station_not_found=.false. exit endif enddo if (station_not_found) cycle endif if (char_element=="TMAX") element=GHCNM_TMAX if (char_element=="TAVG") element=GHCNM_TAVG if (char_element=="TMIN") element=GHCNM_TMIN do month=1,12 if (value(month)/=-9999) ghcnm%data(month,year,element,station_number)=value(month) ghcnm%data_measurement(month,year,element,station_number)=flag1(month) ghcnm%data_quality(month,year,element,station_number)=flag2(month) ghcnm%data_source(month,year,element,station_number)=flag3(month) enddo enddo close(22) 22 format(a11,i4,a4,12(i5,3a1)) end subroutine get_ghcnm end module ghcnm_read