!> @virtual_measurement_mod.f90 !------------------------------------------------------------------------------! ! This file is part of the PALM model system. ! ! PALM is free software: you can redistribute it and/or modify it under the ! terms of the GNU General Public License as published by the Free Software ! Foundation, either version 3 of the License, or (at your option) any later ! version. ! ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ! A PARTICULAR PURPOSE. See the GNU General Public License for more details. ! ! You should have received a copy of the GNU General Public License along with ! PALM. If not, see . ! ! Copyright 2017 Leibniz Universitaet Hannover !------------------------------------------------------------------------------! ! ! Current revisions: ! ----------------- ! ! ! Former revisions: ! ----------------- ! $Id: virtual_measurement_mod.f90 3522 2018-11-13 12:14:36Z eckhard $ ! Sampling of variables ! ! 3494 2018-11-06 14:51:27Z suehring ! Bugfixing ! ! 3473 2018-10-30 20:50:15Z suehring ! Initial revision ! ! Authors: ! -------- ! @author Matthias Suehring ! ! Description: ! ------------ !> The module acts as an interface between 'real-world' observations and !> model simulations. Virtual measurements will be taken in the model at the !> coordinates representative for the 'real-world' measurement positions. !> More precisely, coordinates and measured quanties will be read from a !> NetCDF file which contains all required information. In the model, !> the same quantities (as long as all the required components are switched-on) !> will be sampled at the respective positions and output into an extra file, !> which allows for straight-forward comparison of model results with !> observations. !> !> @todo list_of_allowed variables needs careful checking !> @todo output (binary or NetCDF) needs to be implemented !> @todo clean-up anything from current test modus !> @todo Check if sign of surface fluxes for heat, radiation, etc., follows !> the (UC)2 standard !> @note Fluxes are not processed !------------------------------------------------------------------------------! MODULE virtual_measurement_mod USE arrays_3d, & ONLY: q, pt, u, v, w, zu, zw USE chem_modules, & ONLY: nspec USE chemistry_model_mod, & ONLY: chem_species USE control_parameters, & ONLY: air_chemistry, dz, humidity, neutral, message_string, & virtual_measurement USE cpulog, & ONLY: cpu_log, log_point USE grid_variables, & ONLY: dx, dy USE indices, & ONLY: nzb, nzt, nxl, nxr, nys, nyn, nx, ny USE kinds IMPLICIT NONE TYPE virt_mea CHARACTER(LEN=100) :: feature_type !< type of the measurement CHARACTER(LEN=100) :: site !< name of the measurement site CHARACTER(LEN=10), DIMENSION(:), ALLOCATABLE :: measured_vars_name !< name of the measured variables INTEGER(iwp) :: ns = 0 !< total number of observation points for a site on subdomain, i.e. sum of all trajectories INTEGER(iwp) :: ntraj !< number of trajectories of a measurement INTEGER(iwp) :: nvar !< number of measured variables INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: dim_t !< number observations individual for each trajectory or station that are no _FillValues INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: i !< grid index for measurement position in x-direction INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: j !< grid index for measurement position in y-direction INTEGER(iwp), DIMENSION(:), ALLOCATABLE :: k !< grid index for measurement position in k-direction LOGICAL :: trajectory = .FALSE. !< flag indicating that the observation is a mobile observation LOGICAL :: timseries = .FALSE. !< flag indicating that the observation is a stationary point measurement LOGICAL :: timseries_profile = .FALSE. !< flag indicating that the observation is a stationary profile measurement REAL(wp) :: fill_eutm !< fill value for UTM coordinates in case of missing values REAL(wp) :: fill_nutm !< fill value for UTM coordinates in case of missing values REAL(wp) :: fill_zag !< fill value for heigth coordinates in case of missing values REAL(wp) :: fillout = -999.9 !< fill value for output in case a observation is taken from inside a building REAL(wp) :: origin_x_obs !< origin of the observation in UTM coordiates in x-direction REAL(wp) :: origin_y_obs !< origin of the observation in UTM coordiates in y-direction REAL(wp), DIMENSION(:,:), ALLOCATABLE :: measured_vars !< measured variables END TYPE virt_mea CHARACTER(LEN=5) :: char_eutm = "E_UTM" !< dimension name for UTM coordinate easting CHARACTER(LEN=11) :: char_feature = "featureType" !< attribute name for feature type CHARACTER(LEN=10) :: char_fillvalue = "_FillValue" !< variable attribute name for _FillValue CHARACTER(LEN=18) :: char_mv = "measured_variables" !< variable name for the array with the measured variable names CHARACTER(LEN=5) :: char_nutm = "N_UTM" !< dimension name for UTM coordinate northing CHARACTER(LEN=18) :: char_numstations = "number_of_stations" !< attribute name for number of stations CHARACTER(LEN=8) :: char_origx = "origin_x" !< attribute name for station coordinate in x CHARACTER(LEN=8) :: char_origy = "origin_y" !< attribute name for station coordinate in y CHARACTER(LEN=4) :: char_site = "site" !< attribute name for site name CHARACTER(LEN=19) :: char_zag = "height_above_ground" !< attribute name for height above ground variable CHARACTER(LEN=10) :: type_ts = 'timeSeries' !< name of stationary point measurements CHARACTER(LEN=10) :: type_traj = 'trajectory' !< name of line measurements CHARACTER(LEN=17) :: type_tspr = 'timeSeriesProfile' !< name of stationary profile measurements ! !-- MS: List requires careful revision! CHARACTER(LEN=10), DIMENSION(1:47), PARAMETER :: list_allowed_variables = & !< variables that can be sampled in PALM (/ 'hfls ', & ! surface latent heat flux (W/m2) 'hfss ', & ! surface sensible heat flux (W/m2) 'hur ', & ! relative humidity (-) 'hus ', & ! specific humidity (g/kg) 'haa ', & ! absolute atmospheric humidity (kg/m3) 'mcpm1 ', & ! mass concentration of PM1 (kg/m3) 'mcpm2p5 ', & ! mass concentration of PM2.5 (kg/m3) 'mcpm10 ', & ! mass concentration of PM10 (kg/m3) 'mcpm10 ', & ! mass concentration of PM10 (kg/m3) 'mcco ', & ! mass concentration of CO (kg/m3) 'mcco2 ', & ! mass concentration of CO2 (kg/m3) 'mcbcda ', & ! mass concentration of black carbon paritcles (kg/m3) 'ncaa ', & ! number concentation of particles (1/m3) 'mfco ', & ! mole fraction of CO (mol/mol) 'mfco2 ', & ! mole fraction of CO2 (mol/mol) 'mfch4 ', & ! mole fraction of methane (mol/mol) 'mfnh3 ', & ! mole fraction of amonia (mol/mol) 'mfno ', & ! mole fraction of nitrogen monoxide (mol/mol) 'mfno2 ', & ! mole fraction of nitrogen dioxide (mol/mol) 'mfso2 ', & ! mole fraction of sulfur dioxide (mol/mol) 'mfh20 ', & ! mole fraction of water (mol/mol) 'plev ', & ! ? air pressure - hydrostaic + perturbation? 'rlds ', & ! surface downward longwave flux (W/m2) 'rlus ', & ! surface upward longwave flux (W/m2) 'rsds ', & ! surface downward shortwave flux (W/m2) 'rsus ', & ! surface upward shortwave flux (W/m2) 'ta ', & ! air temperature (degree C) 't_va ', & ! virtual accoustic temperature (K) 'theta ', & ! potential temperature (K) 'tro3 ', & ! mole fraction of ozone air (mol/mol) 'ts ', & ! scaling parameter of temperature (K) 'wspeed ', & ! ? wind speed - horizontal? 'wdir ', & ! wind direction 'us ', & ! friction velocity 'msoil ', & ! ? soil moisture - which depth? 'tsoil ', & ! ? soil temperature - which depth? 'u ', & ! u-component 'ua ', & ! eastward wind (is there any difference to u?) 'v ', & ! v-component 'va ', & ! northward wind (is there any difference to v?) 'w ', & ! w-component 'rld ', & ! downward longwave radiative flux (W/m2) 'rlu ', & ! upnward longwave radiative flux (W/m2) 'rsd ', & ! downward shortwave radiative flux (W/m2) 'rsu ', & ! upward shortwave radiative flux (W/m2) 'rsddif ', & ! downward shortwave diffuse radiative flux (W/m2) 'rnds ' & ! surface net downward radiative flux (W/m2) /) INTEGER(iwp) :: id_vm !< NetCDF file id for virtual measurements INTEGER(iwp) :: nvm = 0 !< number of virtual measurements INTEGER(iwp) :: observation_coverage_xy = 0 !< horizontal distance from the measurement point where observations should be taken in the surrounding INTEGER(iwp) :: observation_coverage_z = 0 !< vertical distance from the measurement point where observations should be taken in the surrounding LOGICAL :: use_virtual_measurement = .FALSE. !< Namelist parameter LOGICAL :: global_attribute = .TRUE. !< flag indicating a global attribute REAL(wp) :: vm_time_start = 0.0 !< time after virtual measurements should start TYPE( virt_mea ), DIMENSION(:), ALLOCATABLE :: vmea !< virtual measurement data structure INTERFACE vm_check_parameters MODULE PROCEDURE vm_check_parameters END INTERFACE vm_check_parameters INTERFACE vm_init MODULE PROCEDURE vm_init END INTERFACE vm_init INTERFACE vm_parin MODULE PROCEDURE vm_parin END INTERFACE vm_parin INTERFACE vm_sampling MODULE PROCEDURE vm_sampling END INTERFACE vm_sampling SAVE PRIVATE ! !-- Public interfaces PUBLIC vm_check_parameters, vm_init, vm_parin, vm_sampling ! !-- Public variables PUBLIC vmea, vm_time_start CONTAINS !------------------------------------------------------------------------------! ! Description: ! ------------ !> Check parameters for virtual measurement module !------------------------------------------------------------------------------! SUBROUTINE vm_check_parameters USE control_parameters, & ONLY: message_string, virtual_measurement USE netcdf_data_input_mod, & ONLY: input_pids_static IMPLICIT NONE ! !-- In case virtual measurements are taken, a static input file is required. !-- This is because UTM coordinates for the PALM domain origin are required !-- for correct mapping of the measurements. !-- ToDo: Revise this later and remove this requirement. IF ( virtual_measurement .AND. .NOT. input_pids_static ) THEN message_string = 'If virtual measurements are taken a static input ' // & 'file is mandatory.' CALL message( 'vm_check_parameters', 'PA0000', 1, 2, 0, 6, 0 ) ENDIF END SUBROUTINE vm_check_parameters !------------------------------------------------------------------------------! ! Description: ! ------------ !> Read namelist for the virtual measurement module !------------------------------------------------------------------------------! SUBROUTINE vm_parin CHARACTER (LEN=80) :: line !< dummy string that contains the current line of the parameter file NAMELIST /virtual_measurement_parameters/ use_virtual_measurement, & vm_time_start line = ' ' ! !-- Try to find stg package REWIND ( 11 ) line = ' ' DO WHILE ( INDEX( line, '&virtual_measurement_parameters' ) == 0 ) READ ( 11, '(A)', END=20 ) line ENDDO BACKSPACE ( 11 ) ! !-- Read namelist READ ( 11, virtual_measurement_parameters, ERR = 10, END = 20 ) ! !-- Set flag that indicates that the virtual measurement module is switched on IF ( use_virtual_measurement ) virtual_measurement = .TRUE. GOTO 20 10 BACKSPACE( 11 ) READ( 11 , '(A)') line CALL parin_fail_message( 'virtual_measurement_parameters', line ) 20 CONTINUE END SUBROUTINE vm_parin !------------------------------------------------------------------------------! ! Description: ! ------------ !> Initialize virtual measurements: read coordiante arrays and measured !> variables, set indicies indicating the measurement points, read further !> attributes, etc.. !------------------------------------------------------------------------------! SUBROUTINE vm_init USE arrays_3d, & ONLY: zu, zw USE grid_variables, & ONLY: ddx, ddy, dx, dy USE indices, & ONLY: nxl, nxr, nyn, nys USE netcdf_data_input_mod, & ONLY: init_model, input_file_vm, & netcdf_data_input_get_dimension_length, & netcdf_data_input_att, netcdf_data_input_var USE surface_mod, & ONLY: get_topography_top_index_ji IMPLICIT NONE CHARACTER(LEN=5) :: dum !< dummy string indicate station id CHARACTER(LEN=10), DIMENSION(50) :: measured_variables_file = '' !< array with all measured variables read from NetCDF CHARACTER(LEN=10), DIMENSION(50) :: measured_variables = '' !< dummy array with all measured variables that are allowed INTEGER(iwp) :: dim_eutm !< dimension size of UTM easting coordinate INTEGER(iwp) :: dim_nutm !< dimension size of UTM northing coordinate INTEGER(iwp) :: dim_ntime !< dimension size of time coordinate INTEGER(iwp) :: dim_zag !< dimension size of height coordinate INTEGER(iwp) :: i !< grid index of virtual observation point in x-direction INTEGER(iwp) :: ii !< running index over all coordinate points of a measurement INTEGER(iwp) :: is !< grid index of real observation point of the respective station in x-direction INTEGER(iwp) :: j !< grid index of observation point in x-direction INTEGER(iwp) :: js !< grid index of real observation point of the respective station in y-direction INTEGER(iwp) :: k !< grid index of observation point in x-direction INTEGER(iwp) :: kl !< lower vertical index of surrounding grid points of an observation coordinate INTEGER(iwp) :: ks !< grid index of real observation point of the respective station in z-direction INTEGER(iwp) :: ksurf !< topography top index INTEGER(iwp) :: ku !< upper vertical index of surrounding grid points of an observation coordinate INTEGER(iwp) :: l !< running index over all stations INTEGER(iwp) :: len_char !< character length of single measured variables without Null character INTEGER(iwp) :: ll !< running index over all measured variables in file INTEGER(iwp) :: lll !< running index over all allowed variables INTEGER(iwp) :: n !< running index over trajectory coordinates INTEGER(iwp) :: ns !< counter variable for number of observation points on subdomain INTEGER(iwp) :: t !< running index over number of trajectories INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE :: meas_flag !< mask array indicating measurement positions LOGICAL :: chem_include !< flag indicating that chemical species is considered in modelled mechanism LOGICAL :: on_pe !< flag indicating that the respective measurement coordinate is on subdomain REAL(wp) :: fill_eutm !< _FillValue for coordinate array E_UTM REAL(wp) :: fill_nutm !< _FillValue for coordinate array N_UTM REAL(wp) :: fill_zag !< _FillValue for height coordinate REAL(wp), DIMENSION(:,:), ALLOCATABLE :: e_utm !< easting UTM coordinate, temporary variable REAL(wp), DIMENSION(:,:), ALLOCATABLE :: n_utm !< northing UTM coordinate, temporary variable, REAL(wp), DIMENSION(:,:), ALLOCATABLE :: z_ag !< height coordinate relative to origin_z, temporary variable ! !-- Obtain number of virtual measurement stations CALL netcdf_data_input_att( nvm, char_numstations, id_vm, input_file_vm, & global_attribute, 'open', '' ) ! write(9,*) "num stationi", nvm ! flush(9) ! !-- ALLOCATE data structure ALLOCATE( vmea(1:nvm) ) ! !-- Allocate flag array ALLOCATE( meas_flag(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ) meas_flag = 0 ! !-- Read station coordinates and further attributes. !-- Note all coordinates are in UTM coordinates. DO l = 1, nvm ! !-- Determine suffix which contains the ID IF( l < 10 ) THEN WRITE( dum, '(I1)') l ELSEIF( l < 100 ) THEN WRITE( dum, '(I2)') l ELSEIF( l < 1000 ) THEN WRITE( dum, '(I3)') l ELSEIF( l < 10000 ) THEN WRITE( dum, '(I4)') l ELSEIF( l < 100000 ) THEN WRITE( dum, '(I5)') l ENDIF CALL netcdf_data_input_att( vmea(l)%origin_x_obs, char_origx & // TRIM( dum ), id_vm, '', global_attribute,& '', '' ) CALL netcdf_data_input_att( vmea(l)%origin_y_obs, char_origy & // TRIM( dum ), id_vm, '', global_attribute,& '', '' ) CALL netcdf_data_input_att( vmea(l)%site, char_site & // TRIM( dum ), id_vm, '', global_attribute,& '', '' ) CALL netcdf_data_input_att( vmea(l)%feature_type, char_feature & // TRIM( dum ), id_vm, '', global_attribute,& '', '' ) ! !--- Set logicals depending on the type of the measurement IF ( INDEX( vmea(l)%feature_type, type_tspr ) /= 0 ) THEN vmea(l)%timseries_profile = .TRUE. ELSEIF ( INDEX( vmea(l)%feature_type, type_ts ) /= 0 ) THEN vmea(l)%timseries = .TRUE. ELSEIF ( INDEX( vmea(l)%feature_type, type_traj ) /= 0 ) THEN vmea(l)%trajectory = .TRUE. ELSE ! !-- Give error message message_string = 'Attribue featureType = ' // & TRIM( vmea(l)%feature_type ) // & ' is not allowed.' CALL message( 'vm_init', 'PA0000', 1, 2, 0, 6, 0 ) ENDIF ! !-- Read string with all measured variables at this station measured_variables_file = '' CALL netcdf_data_input_var( measured_variables_file, & char_mv // TRIM( dum ), id_vm ) ! !-- Count the number of measured variables which match with the variables !-- which are allowed to be measured in PALM. Please note, for some !-- NetCDF interal reasons characters end with a NULL, i.e. also empty !-- characters contain a NULL. Therefore, check the strings for a Null to !-- get the correct character length in order to compare them with the list !-- of allowed variables. vmea(l)%nvar = 0 DO ll = 1, SIZE( measured_variables_file ) IF ( measured_variables_file(ll)(1:1) /= CHAR(0) .AND. & measured_variables_file(ll)(1:1) /= ' ') THEN ! !-- Obtain character length of the character len_char = 1 DO WHILE ( measured_variables_file(ll)(len_char:len_char) /= CHAR(0)& .AND. measured_variables_file(ll)(len_char:len_char) /= ' ' ) len_char = len_char + 1 ENDDO len_char = len_char - 1 ! !-- Now, compare the measured variable with the list of allowed !-- variables. DO lll= 1, SIZE( list_allowed_variables ) IF ( measured_variables_file(ll)(1:len_char) == & TRIM( list_allowed_variables(lll) ) ) THEN vmea(l)%nvar = vmea(l)%nvar + 1 measured_variables(vmea(l)%nvar) = & measured_variables_file(ll)(1:len_char) ENDIF ENDDO ENDIF ENDDO ! !-- Allocate array for the measured variables names for the station l. ALLOCATE( vmea(l)%measured_vars_name(1:vmea(l)%nvar) ) DO ll = 1, vmea(l)%nvar vmea(l)%measured_vars_name(ll) = TRIM( measured_variables(ll) ) ENDDO ! !-- In case of chemistry, check if species is considered in the modelled !-- chemistry mechanism. IF ( air_chemistry ) THEN DO ll = 1, vmea(l)%nvar chem_include = .FALSE. DO n = 1, nspec IF ( TRIM( vmea(l)%measured_vars_name(ll) ) == & TRIM( chem_species(n)%name ) ) chem_include = .TRUE. ENDDO IF ( .NOT. chem_include ) THEN message_string = TRIM( vmea(l)%measured_vars_name(ll) ) // & ' is not considered in the modelled ' // & 'chemistry mechanism' CALL message( 'vm_init', 'PA0000', 0, 0, 0, 6, 0 ) ENDIF ENDDO ENDIF ! !-- For the actual measurement ID read the UTM coordinates. Based on these, !-- define the index space on each subdomain where measurements should be !-- taken. Note, the entire coordinate arrays will not be stored on data !-- type as this would exceed memory requirements, particularly for !-- trajectory measurements. If no variable will be virtually measured, !-- skip the reading. IF ( vmea(l)%nvar > 0 ) THEN ! !-- For stationary measurements UTM coordinates are just one value and !-- its dimension is "station", while for mobile measurements UTM !-- coordinates are arrays. First, inquire dimension length for !-- UTM coordinates. IF ( vmea(l)%trajectory ) THEN ! !-- For non-stationary measurements read the number of trajectories CALL netcdf_data_input_get_dimension_length( id_vm, & vmea(l)%ntraj, & "traj" // & TRIM( dum ) ) CALL netcdf_data_input_get_dimension_length( id_vm, dim_ntime, & "ntime" // & TRIM( dum ) ) ! !-- For stationary measurements the dimension for UTM coordinates is 1 ELSE vmea(l)%ntraj = 1 dim_ntime = 1 ENDIF ! !- Allocate array which defines individual time frame for each !-- trajectory or station ALLOCATE( vmea(l)%dim_t(1:vmea(l)%ntraj) ) ! !-- Allocate temporary arrays for UTM and height coordinates. Note, !-- on file UTM coordinates might be 1D or 2D variables ALLOCATE( e_utm(1:vmea(l)%ntraj,1:dim_ntime) ) ALLOCATE( n_utm(1:vmea(l)%ntraj,1:dim_ntime) ) ALLOCATE( z_ag(1:vmea(l)%ntraj,1:dim_ntime) ) ! !-- Read _FillValue attributes CALL netcdf_data_input_att( fill_eutm, char_fillvalue, & id_vm, '', .NOT. global_attribute, '', & char_eutm // TRIM( dum ) ) CALL netcdf_data_input_att( fill_nutm, char_fillvalue, & id_vm, '', .NOT. global_attribute, '', & char_nutm // TRIM( dum ) ) CALL netcdf_data_input_att( fill_zag, char_fillvalue, & id_vm, '', .NOT. global_attribute, '', & char_zag // TRIM( dum ) ) ! !-- Read UTM and height coordinates coordinates for all trajectories and !-- times. IF ( vmea(l)%trajectory ) THEN CALL netcdf_data_input_var( e_utm, char_eutm // TRIM( dum ), id_vm, & 0, dim_ntime-1, 0, vmea(l)%ntraj-1 ) CALL netcdf_data_input_var( n_utm, char_nutm // TRIM( dum ), id_vm, & 0, dim_ntime-1, 0, vmea(l)%ntraj-1 ) CALL netcdf_data_input_var( z_ag, char_zag // TRIM( dum ), id_vm, & 0, dim_ntime-1, 0, vmea(l)%ntraj-1 ) ELSE CALL netcdf_data_input_var( e_utm(1,:), char_eutm // TRIM( dum ), id_vm ) CALL netcdf_data_input_var( n_utm(1,:), char_nutm // TRIM( dum ), id_vm ) CALL netcdf_data_input_var( z_ag(1,:), char_zag // TRIM( dum ), id_vm ) ENDIF ! !-- For testing: e_utm = e_utm - e_utm(1,1) n_utm = n_utm - n_utm(1,1) ! !-- First, compute relative x- and y-coordinates with respect to the !-- lower-left origin of the model domain, which is the difference !-- betwen UTM coordinates. ! e_utm(t,1:vmea(l)%dim_t(t)) = e_utm(t,1:vmea(l)%dim_t(t)) & ! - init_model%origin_x ! n_utm(t,1:vmea(l)%dim_t(t)) = n_utm(t,1:vmea(l)%dim_t(t)) & ! - init_model%origin_y ! !-- Based on UTM coordinates, check if the measurement station or parts !-- of the trajectory is on subdomain. This case, setup grid index space !-- sample these quantities. meas_flag = 0 DO t = 1, vmea(l)%ntraj ! !-- Determine the individual time coordinate length for each station and !-- trajectory. This is required as several stations and trajectories !-- are merged into one file but they do not have the same number of !-- points in time, hence, missing values may occur and cannot be !-- processed further. vmea(l)%dim_t(t) = 0 DO n = 1, dim_ntime IF ( e_utm(t,n) /= fill_eutm .AND. & n_utm(t,n) /= fill_nutm .AND. & z_ag(t,n) /= fill_zag ) vmea(l)%dim_t(t) = n ENDDO ! !-- Compute grid indices relative to origin and check if these are !-- on the subdomain. Note, virtual measurements will be taken also !-- at grid points surrounding the station, hence, check also for !-- these grid points. DO n = 1, vmea(l)%dim_t(t) is = INT( ( e_utm(t,n) + 0.5_wp * dx ) * ddx, KIND = iwp ) js = INT( ( n_utm(t,n) + 0.5_wp * dy ) * ddy, KIND = iwp ) ! !-- Is the observation point on subdomain? on_pe = ( is >= nxl .AND. is <= nxr .AND. & js >= nys .AND. js <= nyn ) ! !-- Check if observation coordinate is on subdomain IF ( on_pe ) THEN ! !-- Determine vertical index which correspond to the observation !-- height. ksurf = get_topography_top_index_ji( js, is, 's' ) ks = MINLOC( ABS( zu - zw(ksurf) - z_ag(t,n) ), DIM = 1 ) - 1 ! !-- Set mask array at the observation coordinates. Also, flag the !-- surrounding coordinate points, but first check whether the !-- surrounding coordinate points are on the subdomain. kl = MERGE( ks-1, ks, ks-1 >= nzb .AND. ks-1 > ksurf ) ku = MERGE( ks+1, ks, ks+1 <= nzt+1 ) meas_flag(kl:ku,js-1:js+1,is-1:is+1) = & IBSET( meas_flag(kl:ku,js-1:js+1,is-1:is+1), 0 ) ENDIF ENDDO ENDDO ! !-- Based on the flag array count the number of observation points. ns = 0 DO is = nxl-1, nxr+1 DO js = nys-1, nyn+1 DO ks = nzb, nzt+1 ns = ns + MERGE( 1, 0, BTEST( meas_flag(ks,js,is), 0 ) ) ENDDO ENDDO ENDDO ! !-- Store number of observation points on subdomain and allocate index !-- arrays. vmea(l)%ns = ns ns = 0 ALLOCATE( vmea(l)%i(1:vmea(l)%ns) ) ALLOCATE( vmea(l)%j(1:vmea(l)%ns) ) ALLOCATE( vmea(l)%k(1:vmea(l)%ns) ) ! !-- Based on the flag array store the grid indices which correspond to !-- the observation coordinates. DO is = nxl-1, nxr+1 DO js = nys-1, nyn+1 DO ks = nzb, nzt+1 IF ( BTEST( meas_flag(ks,js,is), 0 ) ) THEN ns = ns + 1 vmea(l)%i(ns) = is vmea(l)%j(ns) = js vmea(l)%k(ns) = ks ENDIF ENDDO ENDDO ENDDO ! write(9,*) l, "NS: ", ns ! IF ( ns > 0 ) THEN ! write(9,*) "i ", vmea(l)%i ! write(9,*) "j ", vmea(l)%j ! write(9,*) "k ", vmea(l)%k ! write(9,*) ! ENDIF ! !-- Allocate array to save the sampled values. !-- Todo: Is it better to allocate for all variables at a station !-- and store all the values before writing, or sample the variables !-- directly in the data output? ALLOCATE( vmea(l)%measured_vars(1:vmea(l)%nvar,1:vmea(l)%ns) ) ! !-- Initialize with _FillValue vmea(l)%measured_vars(1:vmea(l)%nvar,1:vmea(l)%ns) = vmea(l)%fillout ! !-- Deallocate temporary coordinate arrays IF ( ALLOCATED( e_utm ) ) DEALLOCATE( e_utm ) IF ( ALLOCATED( n_utm ) ) DEALLOCATE( n_utm ) IF ( ALLOCATED( z_ag ) ) DEALLOCATE( z_ag ) ENDIF ENDDO ! flush(9) ! !-- Close input file for virtual measurements. Therefore, just call !-- the read attribute routine with the "close" option. CALL netcdf_data_input_att( nvm, char_numstations, id_vm, '', & global_attribute, 'close', '' ) ! !-- Dellocate flag array DEALLOCATE( meas_flag ) END SUBROUTINE vm_init !------------------------------------------------------------------------------! ! Description: ! ------------ !> Sampling of the actual quantities along the observation coordinates !------------------------------------------------------------------------------! SUBROUTINE vm_sampling USE arrays_3d, & ONLY: exner, pt, q, u, v, w USE basic_constants_and_equations_mod, & ONLY: pi USE radiation_model_mod, & ONLY: radiation USE surface_mod, & ONLY: surf_def_h, surf_lsm_h, surf_usm_h IMPLICIT NONE CHARACTER(LEN=10) :: trimvar !< dummy for the measured variable name INTEGER(iwp) :: i !< grid index in x-direction INTEGER(iwp) :: j !< grid index in y-direction INTEGER(iwp) :: k !< grid index in z-direction INTEGER(iwp) :: l !< running index over the number of stations INTEGER(iwp) :: m !< running index over all virtual observation coordinates INTEGER(iwp) :: mm !< index of surface element which corresponds to the virtual observation coordinate INTEGER(iwp) :: n !< running index over all measured variables at a station INTEGER(iwp) :: nn !< running index over the number of chemcal species ! !-- Loop over all stations. For each possible variable loop over all !-- observation points DO l = 1, nvm ! !-- Loop over all measured variables at this station. Please note, !-- velocity components are interpolated onto scalar grid. DO n = 1, vmea(l)%nvar SELECT CASE ( TRIM( vmea(l)%measured_vars_name(n) ) ) CASE ( 'theta' ) IF ( .NOT. neutral ) THEN DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = pt(k,j,i) ENDDO ENDIF CASE ( 'ta', 't_va' ) IF ( .NOT. neutral ) THEN DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = pt(k,j,i) * exner( k ) ENDDO ENDIF CASE ( 'hus', 'haa' ) IF ( humidity ) THEN DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = q(k,j,i) ENDDO ENDIF CASE ( 'u', 'ua' ) DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = 0.5_wp * ( u(k,j,i) + u(k,j,i+1) ) ENDDO CASE ( 'v', 'va' ) DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = 0.5_wp * ( v(k,j,i) + v(k,j+1,i) ) ENDDO CASE ( 'w' ) DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = w(k,j,i) !0.5_wp * ( w(k,j,i) + w(k-1,j,i) ) ENDDO CASE ( 'wspeed' ) DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = SQRT( & ( 0.5_wp * ( u(k,j,i) + u(k,j,i+1) ) )**2 + & ( 0.5_wp * ( v(k,j,i) + v(k,j+1,i) ) )**2 & ) ENDDO CASE ( 'wdir' ) DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = ATAN2( & - 0.5_wp * ( u(k,j,i) + u(k,j,i+1) ), & - 0.5_wp * ( v(k,j,i) + v(k,j+1,i) ) & ) * 180.0_wp / pi ENDDO ! !-- MS: list of variables needs extension. CASE ( 'mcpm1', 'mcpm2p5', 'mcpm10', 'mcco', 'mcco2', 'mcbcda', & 'ncaa', 'mfco2', 'mfco', 'mfch4', 'mfnh3', 'mfno', & 'mfno2', 'mfso2', 'mfh20', 'tr03' ) IF ( air_chemistry ) THEN DO nn = 1, nspec IF ( TRIM( vmea(l)%measured_vars_name(m) ) == & TRIM( chem_species(nn)%name ) ) THEN DO m = 1, vmea(l)%ns k = vmea(l)%k(m) j = vmea(l)%j(m) i = vmea(l)%i(m) vmea(l)%measured_vars(n,m) = & chem_species(nn)%conc(k,j,i) ENDDO ENDIF ENDDO ENDIF CASE ( 'us' ) DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_def_h(0)%start_index(j,i), & surf_def_h(0)%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_def_h(0)%us(mm) ENDDO DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%us(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%us(mm) ENDDO ENDDO CASE ( 'ts' ) DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_def_h(0)%start_index(j,i), & surf_def_h(0)%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_def_h(0)%ts(mm) ENDDO DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%ts(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%ts(mm) ENDDO ENDDO CASE ( 'hfls' ) DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_def_h(0)%start_index(j,i), & surf_def_h(0)%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_def_h(0)%qsws(mm) ENDDO DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%qsws(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%qsws(mm) ENDDO ENDDO CASE ( 'hfss' ) DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_def_h(0)%start_index(j,i), & surf_def_h(0)%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_def_h(0)%shf(mm) ENDDO DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%shf(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%shf(mm) ENDDO ENDDO CASE ( 'rnds' ) IF ( radiation ) THEN DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%rad_net(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%rad_net(mm) ENDDO ENDDO ENDIF CASE ( 'rsus', 'rsu' ) IF ( radiation ) THEN DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%rad_sw_out(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%rad_sw_out(mm) ENDDO ENDDO ENDIF CASE ( 'rsds', 'rsd' ) IF ( radiation ) THEN DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%rad_sw_in(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%rad_sw_in(mm) ENDDO ENDDO ENDIF CASE ( 'rlus', 'rlu' ) IF ( radiation ) THEN DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%rad_lw_out(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%rad_lw_out(mm) ENDDO ENDDO ENDIF CASE ( 'rlds', 'rld' ) IF ( radiation ) THEN DO m = 1, vmea(l)%ns ! !-- Surface data is only available on inner subdomains, not !-- on ghost points. Hence, limit the indices. j = MERGE( vmea(l)%j(m), nys, vmea(l)%j(m) < nys ) j = MERGE( j , nyn, j > nyn ) i = MERGE( vmea(l)%i(m), nxl, vmea(l)%i(m) < nxl ) i = MERGE( i , nxr, i > nxr ) DO mm = surf_lsm_h%start_index(j,i), & surf_lsm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_lsm_h%rad_lw_in(mm) ENDDO DO mm = surf_usm_h%start_index(j,i), & surf_usm_h%end_index(j,i) vmea(l)%measured_vars(n,m) = surf_usm_h%rad_lw_in(mm) ENDDO ENDDO ENDIF ! !-- More will follow ... END SELECT ENDDO ENDDO END SUBROUTINE vm_sampling END MODULE virtual_measurement_mod