Path: blob/devel/elmerice/IceSheet/Tools/ConservativeInterpolation/READ_CELL_NETCDF.F90
3206 views
!/*****************************************************************************/1! *2! * Elmer/Ice, a glaciological add-on to Elmer3! * http://elmerice.elmerfem.org4! *5! *6! * This program is free software; you can redistribute it and/or7! * modify it under the terms of the GNU General Public License8! * as published by the Free Software Foundation; either version 29! * of the License, or (at your option) any later version.10! *11! * This program is distributed in the hope that it will be useful,12! * but WITHOUT ANY WARRANTY; without even the implied warranty of13! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14! * GNU General Public License for more details.15! *16! * You should have received a copy of the GNU General Public License17! * along with this program (in file fem/GPL-2); if not, write to the18! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,19! * Boston, MA 02110-1301, USA.20! *21! *****************************************************************************/22! ******************************************************************************23! *24! * Authors: F. Gillet-Chaulet (IGE-France)25! * Web: http://elmerice.elmerfem.org26! * Original Date: 04/201927! *28! *****************************************************************************29!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!30! Read a cell variable stored in a netcdf file31! The netcdf should contains all the cell of the serial mesh32! IF used in parallel, the parallel mesh should have the same global33! cell ordering as the serial mesh34!35! IF netcdf contains a time dimension, the current simulation time is36! used as time index : if t = ]0._dp,1._dp] => index=1 etc...37!38! Required input parameters:39! File Name = File <netcdf file>40! VarName = File <name of the netcdf variable>41! Target Variable Name = String OPTIONAL <name of the Elmer variable>42!43!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!44SUBROUTINE READ_CELL_NETCDF( Model,Solver,dt,TransientSimulation )45!------------------------------------------------------------------------------46USE DefUtils47USE NETCDF48IMPLICIT NONE49!------------------------------------------------------------------------------50TYPE(Solver_t), TARGET :: Solver51TYPE(Model_t) :: Model52REAL(KIND=dp) :: dt53LOGICAL :: TransientSimulation54!------------------------------------------------------------------------------55! Local variables56TYPE(ValueList_t), POINTER :: SolverParams57TYPE(Variable_t),POINTER :: Var58TYPE(Element_t), POINTER :: Element59INTEGER :: i60INTEGER :: NElements61CHARACTER (len=100) :: FName62CHARACTER (len=100) :: VarName,TVarName63INTEGER :: varid,ncells,ncid,ndims,ntime,TVarID64INTEGER :: NetCDFstatus65REAL(KIND=dp), ALLOCATABLE :: Values(:)66REAL(KIND=dp) :: time67INTEGER :: TimeIndex68INTEGER,SAVE :: SavedTime=-169INTEGER :: EIndex70CHARACTER(LEN=MAX_NAME_LEN) :: SolverName="READ_NETCDF_CELL"71LOGICAL :: Parallel,Found7273! get parameters74SolverParams => GetSolverParams()75FName = ListGetString(SolverParams,'File Name',UnFoundFatal=.TRUE.)7677write(Message,'(a,a)') 'File name: ',Trim(FName)78CALL INFO(SolverName,Message,Level=4)7980VarName = ListGetString(SolverParams,'Variable Name',UnFoundFatal=.TRUE.)81TVarName = ListGetString(SolverParams,'Target Variable Name',Found)82IF (.NOT.Found) TVarName=VarName8384! check if this is a parallel run85Parallel=(ParEnv % PEs > 1)8687! get variable88Var => VariableGet( Model % Mesh % Variables,TRIM(TVarName),UnFoundFatal=.TRUE.)89IF(Var % TYPE /= Variable_on_elements) &90CALL FATAL(SolverName,'Wrong variable type; use -elem ')9192NetCDFstatus = NF90_OPEN(trim(FName),NF90_NOWRITE,ncid)93IF ( NetCDFstatus /= NF90_NOERR ) &94CALL FATAL(SolverName,"file open failed")9596NetCDFstatus = nf90_inq_dimid(ncid, 'ncells' , varid)97IF ( NetCDFstatus /= NF90_NOERR ) &98CALL FATAL(SolverName,"unable to get ncells dim")99100NetCDFstatus = nf90_inquire_dimension(ncid,varid,len=ncells)101IF ( NetCDFstatus /= NF90_NOERR ) &102CALL FATAL(SolverName,"unable to get ncells")103104allocate(Values(ncells))105106! get variable ID107NetCDFstatus = nf90_inq_varid(ncid,trim(VarName),TVarId)108IF ( NetCDFstatus /= NF90_NOERR ) &109CALL FATAL(SolverName,"unable to get varid")110111! variable dimensions112NetCDFstatus = nf90_inquire_variable(ncid, TVarId, ndims=ndims)113IF ( NetCDFstatus /= NF90_NOERR ) &114CALL FATAL(SolverName,"unable to get variable dimensions")115116! if ndim > 1 we should have a time dimension117IF (ndims.GT.1) THEN118NetCDFstatus = nf90_inq_dimid(ncid, 'time' , varid)119IF ( NetCDFstatus /= NF90_NOERR ) &120CALL FATAL(SolverName,"unable to get time dimension")121122NetCDFstatus = nf90_inquire_dimension(ncid,varid,len=ntime)123IF ( NetCDFstatus /= NF90_NOERR ) &124CALL FATAL(SolverName,"unable to get ntime")125126! get time index127time = GetTime()128dt = GetTimeStepSize()129TimeIndex = floor(time-dt/2) + 1130TimeIndex = max(1,min(TimeIndex,ntime))131132! we have already done this; return133IF (SavedTime.EQ.TimeIndex) THEN134NetCDFstatus=nf90_close(ncid)135CALL INFO(SolverName,"Nothing to do; return",level=4)136RETURN137ENDIF138SavedTime=TimeIndex139140WRITE(Message,'(a,i0)') 'reading time step: ',TimeIndex141CALL INFO(SolverName,Message,level=4)142143NetCDFstatus=nf90_get_var(ncid,TVarId,Values,start=(/1,TimeIndex/),count=(/ncells,1/))144IF ( NetCDFstatus /= NF90_NOERR ) &145CALL FATAL(SolverName,"unable to get variable")146147ELSE148! we have already done this; return149IF (SavedTime.GT.0) THEN150NetCDFstatus=nf90_close(ncid)151CALL INFO(SolverName,"Nothing to do; return",level=4)152RETURN153ENDIF154SavedTime=+1155156NetCDFstatus=nf90_get_var(ncid,TVarId,Values,start=(/1/),count=(/ncells/))157IF ( NetCDFstatus /= NF90_NOERR ) &158CALL FATAL(SolverName,"unable to get variable")159160END IF161162! close file163NetCDFstatus=nf90_close(ncid)164165NElements = GetNOFActive()166DO i=1,NElements167Element => GetActiveElement(i)168IF (Parallel) THEN169EIndex=Element % GElementIndex170ELSE171EIndex=Element % ElementIndex172ENDIF173Var % Values(Var % Perm(Element % ElementIndex))=Values(EIndex)174END DO175176DEALLOCATE(Values)177178179END SUBROUTINE READ_CELL_NETCDF180181182