123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641 |
- module file_hdf_base
- use GO, only : gol, goPr, goErr
- implicit none
-
- ! --- in/out --------------------------
-
- private
- public :: wpi
-
- public :: wp_int8, wp_int16, wp_int32, wp_int64
- public :: wp_float32, wp_float64
- public :: MAX_DATA_RANK
- public :: SD_UNLIMITED
-
- public :: THdfFile
- public :: TSds
- public :: TSdsDim
- public :: Init, Done
-
- public :: Defined
- public :: Select
- public :: GetInfo, CheckInfo
- public :: Compress
- public :: SetName
- public :: FindAttribute
- public :: GetAttributeInfo, CheckAttributeInfo
- public :: FindDataSet
-
-
- ! --- const ----------------------------
-
- character(len=*), parameter :: mname = 'file_hdf_base'
-
- ! ** hdf constants
-
- include "hdf.f90"
-
- ! ** working precision of hdf library
-
- integer, parameter :: wpi = 4
-
- ! ** working precisions of data
-
- integer, parameter :: wp_int8 = 1
- integer, parameter :: wp_int16 = 2
- integer, parameter :: wp_int32 = 4
- integer, parameter :: wp_int64 = 8
-
- integer, parameter :: wp_float32 = 4
- integer, parameter :: wp_float64 = 8
-
- ! ** maximum array ranks
-
- integer, parameter :: MAX_DATA_RANK = 32
- ! --- types ---------------------------
-
- ! ~~ scientific data set:
-
- type TSds
- ! internal id:
- integer(wpi) :: id
- ! hdf file name:
- character(len=256) :: hdfname
- ! name:
- character(len=64) :: name
- ! data specification:
- integer :: dfnt
- character(len=3) :: typ
- integer :: knd
- integer :: rnk
- integer :: shp(7)
- end type TSds
-
- ! ~~ dimension
-
- type TSdsDim
- ! internal id:
- integer(wpi) :: id
- end type TSdsDim
-
- ! ~~ hdf file
-
- type THdfFile
- ! internal id:
- integer(wpi) :: id
- ! file name
- character(len=256) :: fname
- end type THdfFile
-
- ! --- interfaces ------------------------
-
- interface Init
- module procedure sds_Init
- module procedure sds_Init_select
- module procedure sds_Init_create
- module procedure dim_Init
- module procedure hdf_Init
- end interface
-
- interface Done
- module procedure sds_Done
- module procedure dim_Done
- module procedure hdf_Done
- end interface
-
- interface Defined
- module procedure sds_Defined
- end interface
- interface Select
- module procedure sds_Select_index
- module procedure dim_Select
- end interface
- interface GetInfo
- module procedure sds_GetInfo
- module procedure hdf_GetInfo
- end interface
-
- interface CheckInfo
- module procedure sds_CheckInfo
- end interface
-
- interface Compress
- module procedure sds_Compress
- end interface
-
- interface SetName
- module procedure dim_SetName
- end interface
-
- interface FindAttribute
- module procedure obj_FindAttribute
- module procedure sds_FindAttribute
- module procedure hdf_FindAttribute
- end interface
- interface GetAttributeInfo
- module procedure obj_GetAttributeInfo
- module procedure sds_GetAttributeInfo
- module procedure hdf_GetAttributeInfo
- end interface
- interface CheckAttributeInfo
- module procedure obj_CheckAttributeInfo
- module procedure sds_CheckAttributeInfo
- module procedure hdf_CheckAttributeInfo
- end interface
- interface FindDataSet
- module procedure hdf_FindDataSet
- end interface
- contains
- ! ############################################################
- ! ###
- ! ### tools
- ! ###
- ! ############################################################
-
-
- !
- ! compare character strings case independent
- !
-
- logical function leq( s1, s2 )
-
- ! --- in/out ------------------------
-
- character(len=*), intent(in) :: s1, s2
-
- ! --- local -------------------------
-
- character(len=2) :: cc
- integer :: k
-
- ! --- begin -------------------------
-
- if ( len_trim(s1) /= len_trim(s2) ) then
- leq = .false.
- return
- end if
-
- do k = 1, len_trim(s1)
-
- select case ( s1(k:k) )
- case ( 'A', 'a' ); cc = 'Aa'
- case ( 'B', 'b' ); cc = 'Bb'
- case ( 'C', 'c' ); cc = 'Cc'
- case ( 'D', 'd' ); cc = 'Dd'
- case ( 'E', 'e' ); cc = 'Ee'
- case ( 'F', 'f' ); cc = 'Ff'
- case ( 'G', 'g' ); cc = 'Gg'
- case ( 'H', 'h' ); cc = 'Hh'
- case ( 'I', 'i' ); cc = 'Ii'
- case ( 'J', 'j' ); cc = 'Jj'
- case ( 'K', 'k' ); cc = 'Kk'
- case ( 'L', 'l' ); cc = 'Ll'
- case ( 'M', 'm' ); cc = 'Mm'
- case ( 'N', 'n' ); cc = 'Nn'
- case ( 'O', 'o' ); cc = 'Oo'
- case ( 'P', 'p' ); cc = 'Pp'
- case ( 'Q', 'q' ); cc = 'Qq'
- case ( 'R', 'r' ); cc = 'Rr'
- case ( 'S', 's' ); cc = 'Ss'
- case ( 'T', 't' ); cc = 'Tt'
- case ( 'U', 'u' ); cc = 'Uu'
- case ( 'V', 'v' ); cc = 'Vv'
- case ( 'W', 'w' ); cc = 'Ww'
- case ( 'X', 'x' ); cc = 'Xx'
- case ( 'Y', 'y' ); cc = 'Yy'
- case ( 'Z', 'z' ); cc = 'Zz'
- case default; cc = '**'
- end select
-
- if ( cc == '**' ) then
- if ( s2(k:k) /= s1(k:k) ) then
- leq = .false.
- return
- end if
- else
- if ( s2(k:k) /= cc(1:1) .and. s2(k:k) /= cc(2:2) ) then
- leq = .false.
- return
- end if
- end if
-
- end do
-
- leq = .true.
-
- end function leq
-
-
- ! ############################################################
- ! ###
- ! ### objects
- ! ###
- ! ############################################################
-
-
- subroutine obj_FindAttribute( obj_id, name, attr_index, status )
-
- ! --- in/out -------------------------
-
- integer(wpi), intent(in) :: obj_id
- character(len=*), intent(in) :: name
- integer, intent(out) :: attr_index
- integer, intent(inout) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/obj_FindAttribute'
-
- ! --- local -------------------------------
-
- integer :: istat
- logical :: verbose
-
- ! --- external ----------------------------
- integer(wpi), external :: sfFAttr
- ! --- begin -------------------------------
-
- ! write error messages ?
- verbose = status == 0
-
- ! extract id of attribute:
- attr_index = sfFAttr( obj_id, name )
- if ( attr_index == FAIL ) then
- if ( verbose ) then
- write (gol,'("finding attribute `",a,"`")') trim(name); call goErr
- write (gol,'("in ",a)') rname; call goErr
- end if
- status=-1; return
- end if
-
- ! ok
- status = 0
- end subroutine obj_FindAttribute
- ! ***
- !
- ! argument attr_index : 0,..,n-1
- !
- subroutine obj_GetAttributeInfo( obj_id, attr_index, status, &
- name, data_type, data_type_descr, n_values )
-
- ! --- in/out -------------------------
-
- integer(wpi), intent(in) :: obj_id
- integer, intent(in) :: attr_index
- integer, intent(out) :: status
- character(len=*), intent(out), optional :: name
- integer, intent(out), optional :: data_type
- character(len=1), intent(out), optional :: data_type_descr
- integer, intent(out), optional :: n_values
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/obj_GetAttributeInfo'
- ! --- local -------------------------------
-
- character(len=64) :: attr_name
- integer :: attr_data_type
- integer :: attr_n_values
-
- ! --- external ----------------------------
- integer(wpi), external :: sfGAInfo
- ! --- begin -------------------------------
-
- ! extract info:
- status = sfGAInfo( obj_id, attr_index, attr_name, attr_data_type, attr_n_values )
- if ( status /= SUCCEED ) then
- write (gol,'("getting attribute info")') ; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! return values:
- if ( present(name) ) name = attr_name
-
- if ( present(data_type) ) data_type = attr_data_type
- if ( present(data_type_descr) ) then
- select case ( attr_data_type )
- case ( DFNT_INT8, DFNT_INT16, DFNT_INT32, DFNT_INT64 )
- data_type_descr = 'i'
- case ( DFNT_FLOAT32, DFNT_FLOAT64 )
- data_type_descr = 'r'
- case ( DFNT_CHAR )
- data_type_descr = 's'
- case default
- write (gol,'("do not know the data type description")'); call goErr
- write (gol,'(" attribute name : ",a)') trim(attr_name); call goErr
- write (gol,'(" attribute data type : ",i6)') attr_data_type; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end select
- end if
- if ( present(n_values) ) n_values = attr_n_values
-
- ! ok
- status = 0
- end subroutine obj_GetAttributeInfo
- ! ***
-
- !
- ! argument attr_index : 0,..,n-1
- !
- subroutine obj_CheckAttributeInfo( obj_id, attr_index, status, &
- name, data_type, n_values )
-
- ! --- in/out -------------------------
-
- integer(wpi), intent(in) :: obj_id
- integer, intent(in) :: attr_index
- integer, intent(inout) :: status
- character(len=*), intent(in), optional :: name
- integer, intent(in), optional :: data_type
- integer, intent(in), optional :: n_values
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/obj_CheckAttributeInfo'
-
- ! --- local -------------------------------
-
- logical :: verbose
- character(len=64) :: attr_name
- integer :: attr_data_type
- integer :: attr_n_values
-
- ! --- begin -------------------------------
-
- ! write error messages ?
- verbose = status == 0
-
- ! check name
- if ( present(name) ) then
- call GetAttributeInfo( obj_id, attr_index, status, name=attr_name )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( .not. leq(attr_name,name) ) then
- if ( verbose ) then
- write (gol,'("found different attribute name :")'); call goErr
- write (gol,'(" requested : ",a)') trim(name); call goErr
- write (gol,'(" found : ",a)') trim(attr_name); call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! check data type
- if ( present(data_type) ) then
- call GetAttributeInfo( obj_id, attr_index, status, &
- data_type=attr_data_type, name=attr_name )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( attr_data_type /= data_type ) then
- if ( verbose ) then
- write (gol,'("found different data type :")'); call goErr
- write (gol,'(" requested : ",i6)') data_type; call goErr
- write (gol,'(" found : ",i6)') attr_data_type; call goErr
- write (gol,'(" attribute :")') trim(attr_name); call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! check number of values:
- if ( present(n_values) ) then
- call GetAttributeInfo( obj_id, attr_index, status, &
- n_values=attr_n_values, name=attr_name )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( attr_n_values /= n_values ) then
- if ( verbose ) then
- write (gol,'("found different number of values :")'); call goErr
- write (gol,'(" requested : ")') n_values; call goErr
- write (gol,'(" found : ")') attr_n_values; call goErr
- write (gol,'(" attribute : ")') trim(attr_name); call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! ok
- status = 0
- end subroutine obj_CheckAttributeInfo
- ! ############################################################
- ! ###
- ! ### scientific data sets
- ! ###
- ! ############################################################
-
- ! ================================================================
- ! init, done
- ! ================================================================
-
- subroutine sds_Init( sds, status )
-
- ! --- in/out -----------------------------
-
- type(Tsds), intent(out) :: sds
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_Init'
-
- ! --- begin ------------------------------
-
- ! dummy ...
- sds%hdfname = 'unknown-hdf-file'
- sds%typ = 'xxx'
- ! no id yet
- sds%id = -1
-
- ! ok
- status = 0
-
- end subroutine sds_Init
-
- ! ***
-
-
- subroutine sds_Done( sds, status )
-
- ! --- in/out -----------------------------
-
- type(Tsds), intent(inout) :: sds
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_Done'
-
- ! --- external ----------------------------
- integer(wpi), external :: sfEndAcc
- ! --- begin ------------------------------
-
- if ( sds%id /= -1 ) then
- status = sfEndAcc( sds%id )
- if ( status == FAIL ) then
- write (gol,'("ending scientific data set ",i6)') sds%id; call goErr
- write (gol,'(" hdf file name : ",a)') trim(sds%hdfname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
- end if
-
- ! ok
- status = 0
-
- end subroutine sds_Done
-
-
- ! ***
-
-
- logical function sds_Defined( sds )
-
- ! --- in/out ------------------------------
-
- type(TSds), intent(in) :: sds
-
- ! --- begin ------------------------------
-
- sds_Defined = sds%id /= -1
-
- end function sds_Defined
-
- ! ================================================================
- ! === select sds
- ! ================================================================
-
- subroutine sds_Select_index( sds, hdf, ind, status )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(out) :: sds
- type(THdfFile), intent(in) :: hdf
- integer, intent(in) :: ind
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_Select_index'
-
- ! --- external ------------------------
-
- integer(wpi), external :: sfSelect
-
- ! --- begin ---------------------------
-
- sds%id = sfSelect( hdf%id, ind ) ! <-- 0,..,n-1
- if ( sds%id == FAIL ) then
- write (gol,'("unable to locate data set with index ",i6)') ind; call goErr
- write (gol,'(" hdf file name : ",a)') trim(hdf%fname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! ok
- status = 0
-
- end subroutine sds_Select_index
-
-
- ! ***
-
-
- subroutine sds_Init_select( sds, hdf, name, status )
-
- ! --- in/out -------------------------
-
- type(Tsds), intent(out) :: sds
- type(THdfFile), intent(inout) :: hdf
- character(len=*), intent(in) :: name
- integer, intent(inout) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_Init_select'
-
- ! --- local -------------------------------
-
- integer :: sds_index
- ! --- external ------------------------
-
- integer(wpi), external :: sfN2Index
-
- ! --- begin -------------------------------
- ! default init
- call Init( sds, status )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
-
- ! fill hdf and sds names:
- sds%hdfname = hdf%fname
- sds%name = name
-
- ! search for the record
- sds_index = sfN2Index( hdf%id, name )
- if ( sds_index == FAIL ) then
- write (gol,'("converting sds name to index :")'); call goErr
- write (gol,'(" sds name : ",a)') trim(sds%name); call goErr
- write (gol,'(" hdf name : ",a)') trim(sds%hdfname); call goErr
- write (gol,'("in ",a)') rname; call goErr
- status=1; return
- end if
-
- ! select sds id:
- call Select( sds, hdf, sds_index, status )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
-
- ! ok
- status = 0
-
- end subroutine sds_Init_select
- ! =============================================================
- ! === sds info
- ! =============================================================
-
- subroutine sds_GetInfo( sds, status, &
- name, data_rank, data_dims, data_type, num_attrs )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(in) :: sds
- integer, intent(out) :: status
- character(len=*), intent(out), optional :: name
- integer, intent(out), optional :: data_rank
- integer, intent(out), optional :: data_type
- integer, intent(out), optional :: data_dims(:)
- integer, intent(out), optional :: num_attrs
-
- ! --- local -------------------------------
-
- integer :: istat
- character(len=64) :: sds_name
- integer :: sds_data_rank, sds_data_type
- integer :: sds_data_dims(MAX_DATA_RANK)
- integer :: sds_num_attrs
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_GetInfo'
- ! --- external ----------------------------
- integer(wpi), external :: sfGInfo
- ! --- begin -------------------------------
-
- ! extract info about record:
- istat = sfGInfo( sds%id, sds_name, sds_data_rank, sds_data_dims, sds_data_type, sds_num_attrs )
- if ( istat /= SUCCEED ) then
- write (gol,'("error getting info")'); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! return values:
- if ( present(name) ) name = sds_name
- if ( present(data_rank) ) data_rank = sds_data_rank
- if ( present(data_type) ) data_type = sds_data_type
- if ( present(data_dims) ) data_dims = sds_data_dims(1:size(data_dims))
- if ( present(num_attrs) ) num_attrs = sds_num_attrs
-
- ! ok
- status = 0
-
- end subroutine sds_GetInfo
- ! ***
-
- subroutine sds_CheckInfo( sds, status, &
- name, data_rank, data_dims, data_type, num_attrs )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(in) :: sds
- integer, intent(inout) :: status
-
- character(len=*), intent(in), optional :: name
- integer, intent(in), optional :: data_rank
- integer, intent(in), optional :: data_type
- integer, intent(in), optional :: data_dims(:)
- integer, intent(in), optional :: num_attrs
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_CheckInfo'
- ! --- local -------------------------------
-
- logical :: verbose
- character(len=64) :: sds_name
- integer :: sds_data_rank, sds_data_type
- integer, allocatable :: sds_data_dims(:)
- integer :: sds_num_attrs
-
- ! --- begin -------------------------------
-
- ! write error messages ?
- verbose = status == 0
-
- ! check name
- if ( present(name) ) then
- call GetInfo( sds, status, name=sds_name )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( .not. leq(sds_name,name) ) then
- if ( verbose ) then
- write (gol,'("found different name :")'); call goErr
- write (gol,'(" requested : ",a)') trim(name); call goErr
- write (gol,'(" found : ",a)') trim(sds_name); call goErr
- write (gol,'("in ",a)') rname; call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! check data rank
- if ( present(data_rank) ) then
- call GetInfo( sds, status, data_rank=sds_data_rank )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( sds_data_rank /= data_rank ) then
- if ( verbose ) then
- write (gol,'("found different data rank :")'); call goErr
- write (gol,'(" requested : ",i6)') data_rank; call goErr
- write (gol,'(" found : ",i6)') sds_data_rank; call goErr
- write (gol,'("in ",a)') rname; call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! check data type
- if ( present(data_type) ) then
- call GetInfo( sds, status, data_type=sds_data_type )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( sds_data_type /= data_type ) then
- if ( verbose ) then
- write (gol,'("found different data type :")'); call goErr
- write (gol,'(" requested : ",i6)') data_type; call goErr
- write (gol,'(" found : ",i6)') sds_data_type; call goErr
- write (gol,'("in ",a)') rname; call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! check data dimensions
- if ( present(data_dims) ) then
- allocate( sds_data_dims(size(data_dims)) )
- call GetInfo( sds, status, data_dims=sds_data_dims )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( any( sds_data_dims /= data_dims ) ) then
- if ( verbose ) then
- write (gol,'("different data dims :")'); call goErr
- write (gol,'(" requested : ",7i4)') data_dims; call goErr
- write (gol,'(" found : ",7i4)') sds_data_dims; call goErr
- write (gol,'("in ",a)') rname; call goErr
- end if
- status=-1; return
- end if
- deallocate( sds_data_dims )
- end if
-
- ! check number of attributes:
- if ( present(num_attrs) ) then
- call GetInfo( sds, status, num_attrs=sds_num_attrs )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
- if ( sds_num_attrs /= num_attrs ) then
- if ( verbose ) then
- write (gol,'("different data num_attrs :")'); call goErr
- write (gol,'(" requested : ")') num_attrs; call goErr
- write (gol,'(" found : ")') sds_num_attrs; call goErr
- write (gol,'("in ",a)') rname; call goErr
- end if
- status=-1; return
- end if
- end if
-
- ! ok
- status = 0
- end subroutine sds_CheckInfo
- ! =============================================================
- ! === create sds data
- ! =============================================================
- !
- ! 'int8' 'integer(1)' 'int'|'integer', bits=8 |knd=1
- ! 'int16' 'integer(2)' 'int'|'integer', bits=16|knd=2
- ! 'int32' 'integer(4)' 'int'|'integer', bits=32|knd=4
- ! 'int64' 'integer(8)' 'int'|'integer', bits=64|knd=8
- !
- ! 'float32' 'real(4)' 'float'|'real', bits=32|knd=4
- ! 'float64' 'real(8)' 'float'|'real', bits=64|knd=8
- !
- ! 'char'
- !
- subroutine sds_Init_create( sds, hdf, name, shp, typekey, status, &
- knd, bits )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(out) :: sds
- type(THdfFile), intent(inout) :: hdf
- character(len=*), intent(in) :: name
- integer, intent(in) :: shp(:)
- character(len=*), intent(in) :: typekey
- integer, intent(out) :: status
- integer, intent(in), optional :: knd
- integer, intent(in), optional :: bits
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_Init_create'
-
- ! --- local -------------------------------
-
- integer :: dfnt
- character(len=3) :: dtyp
- integer :: dbits, dknd
-
- ! --- external ----------------------------
-
- integer(wpi), external :: sfCreate
-
- ! --- begin -------------------------------
-
- ! default initialisation:
- call Init( sds, status )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
-
- ! fill hdf file name
- sds%hdfname = hdf%fname
- ! fill hdf variable name
- sds%name = name
- ! determine data type:
- select case ( typekey )
- case ( 'int8', 'integer(1)' )
- dfnt = DFNT_INT8
- dtyp = 'int'
- dknd = 1
- case ( 'int16', 'integer(2)' )
- dfnt = DFNT_INT16
- dtyp = 'int'
- dknd = 2
- case ( 'int32', 'integer(4)' )
- dfnt = DFNT_INT32
- dtyp = 'int'
- dknd = 4
- case ( 'int64', 'integer(8)' )
- dfnt = DFNT_INT64
- dtyp = 'int'
- dknd = 8
- case ( 'int', 'integer' )
- if ( present(bits) ) then
- dbits = bits
- else if ( present(knd) ) then
- dbits = knd * 8
- else
- dbits = kind(1) * 8
- end if
- select case ( dbits )
- case ( 8 )
- dfnt = DFNT_INT8
- dknd = 1
- case ( 16 )
- dfnt = DFNT_INT16
- dknd = 2
- case ( 32 )
- dfnt = DFNT_INT32
- dknd = 4
- case ( 64 )
- dfnt = DFNT_INT64
- dknd = 8
- case default
- write (gol,'("integer data not implemented for dbits=",i6)') dbits; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end select
- dtyp = 'int'
- case ( 'float32', 'real(4)' )
- dfnt = DFNT_FLOAT32
- dtyp = 'flt'
- dknd = 4
- case ( 'float64', 'real(8)' )
- dfnt = DFNT_FLOAT64
- dtyp = 'flt'
- dknd = 8
- case ( 'float', 'real' )
- if ( present(bits) ) then
- dbits = bits
- else if ( present(knd) ) then
- dbits = knd * 8
- else
- dbits = kind(1) * 8
- end if
- select case ( dbits )
- case ( 32 )
- dfnt = DFNT_FLOAT32
- dknd = 4
- case ( 64 )
- dfnt = DFNT_FLOAT64
- dknd = 8
- case default
- write (gol,'("real data not implemented for dbits=",i6)') dbits; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end select
- dtyp = 'flt'
- case ( 'char' )
- dfnt = DFNT_CHAR
- dtyp = 'chr'
- dknd = 1
- case default
- write (gol,'("typekey not implemented: ",a)') trim(typekey); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end select
-
- ! store type and kind
- sds%dfnt = dfnt
- sds%typ = dtyp
- sds%knd = dknd
-
- ! store rank
- sds%rnk = size(shp)
- if ( sds%rnk < 1 .or. sds%rnk > 7 ) then
- write (gol,'("invalid rank : ",i4)') sds%rnk; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! store shape
- sds%shp(1:sds%rnk) = shp
- ! start new record:
- sds%id = sfCreate( hdf%id, name, sds%dfnt, sds%rnk, sds%shp(1:sds%rnk) )
- if ( sds%id == FAIL ) then
- write (gol,'("from sfCreate :")'); call goErr
- write (gol,'(" name : ",a)') trim(name); call goErr
- write (gol,'(" hdf file : ",a)') trim(sds%hdfname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! ok
- status = 0
-
- end subroutine sds_Init_create
-
-
- ! ================================================================
- ! compression
- ! ================================================================
-
-
- subroutine sds_Compress( sds, compression, status, &
- skip_size, deflate_level )
-
- ! --- in/out -------------------------
-
- type(Tsds), intent(inout) :: sds
- character(len=*), intent(in) :: compression
- integer, intent(out) :: status
- integer, intent(in), optional :: skip_size
- integer, intent(in), optional :: deflate_level
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_Compress'
-
- ! --- local -------------------------------
-
- integer :: comp_type
- integer :: comp_prm(1)
- ! --- external ---------------------------
-
- integer(wpi), external :: sfsCompress
-
- ! --- begin -------------------------------
-
- ! default compression parameters:
- comp_prm = (/ 0 /)
- ! set compression type and parameters given key:
- select case ( compression )
- case ( 'none' )
- comp_type = COMP_CODE_NONE
- case ( 'rle' ) ! run-length encoding
- comp_type = COMP_CODE_RLE
- case ( 'skphuff' ) ! skipping Huffman
- comp_type = COMP_CODE_SKPHUFF
- comp_prm = (/ 1 /)
- if ( present(skip_size) ) comp_prm(1) = skip_size
- case ( 'deflate' ) ! gzip
- comp_type = COMP_CODE_DEFLATE
- comp_prm = (/ 6 /)
- if ( present(deflate_level) ) comp_prm(1) = deflate_level
- case default
- write (gol,'("unknown compression type : ",a)') trim(compression); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end select
- ! call HDF routine:
- status = sfsCompress( sds%id, comp_type, comp_prm )
- if ( status == FAIL ) then
- write (gol,'("from sfsCompress : ")'); call goErr
- write (gol,'(" compression : ",a )') trim(compression); call goErr
- write (gol,'(" compress type : ",i6)') comp_type; call goErr
- write (gol,'(" compress param : ",i6)') comp_prm; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! ok
- status = 0
- end subroutine sds_Compress
-
- ! =============================================================
- ! === sds attributes
- ! =============================================================
-
- subroutine sds_FindAttribute( sds, name, attr_index, status )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(in) :: sds
- character(len=*), intent(in) :: name
- integer, intent(out) :: attr_index
- integer, intent(inout) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_FindAttribute'
- ! --- local -------------------------------
-
- logical :: verbose
- ! --- begin -------------------------------
-
- ! write error messages ?
- verbose = status == 0
-
- call FindAttribute( sds%id, name, attr_index, status )
- if (status<0) then
- ! not found ..
- if (verbose) then; write (gol,'("in ",a)') rname; call goErr; end if
- status=-1; return
- else if ( status == 0 ) then
- ! ok
- status=0; return
- else
- ! error
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
- end subroutine sds_FindAttribute
- ! ***
-
- subroutine sds_GetAttributeInfo( sds, attr_index, status, &
- name, data_type, data_type_descr, &
- n_values )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(in) :: sds
- integer, intent(in) :: attr_index
- integer, intent(out) :: status
- character(len=*), intent(out), optional :: name
- integer, intent(out), optional :: data_type
- character(len=1), intent(out), optional :: data_type_descr
- integer, intent(out), optional :: n_values
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_GetAttributeInfo'
-
- ! --- begin -------------------------------
-
- call GetAttributeInfo( sds%id, attr_index, status, &
- name=name, &
- data_type=data_type, data_type_descr=data_type_descr, &
- n_values=n_values )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
-
- ! ok
- status = 0
- end subroutine sds_GetAttributeInfo
- ! ***
-
- subroutine sds_CheckAttributeInfo( sds, attr_index, status, &
- name, data_type, n_values )
-
- ! --- in/out -------------------------
-
- type(TSds), intent(in) :: sds
- integer, intent(in) :: attr_index
- integer, intent(inout) :: status
-
- character(len=*), intent(in), optional :: name
- integer, intent(in), optional :: data_type
- integer, intent(in), optional :: n_values
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/sds_CheckAttributeInfo'
-
- ! --- local -------------------------------
-
- logical :: verbose
- ! --- begin -------------------------------
- ! write error messages ?
- verbose = status == 0
-
- call CheckAttributeInfo( sds%id, attr_index, &
- name=name, data_type=data_type, n_values=n_values, &
- status=status )
- if ( status < 0 ) then
- ! error
- if (verbose) then; write (gol,'("in ",a)') rname; call goErr; end if
- status=-1; return
- else if ( status == 0 ) then
- ! ok
- status=0; return
- else
- ! error
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
- end subroutine sds_CheckAttributeInfo
- ! ############################################################
- ! ###
- ! ### dimensions
- ! ###
- ! ############################################################
-
- ! ================================================================
- ! init, done
- ! ================================================================
- subroutine dim_Init( sdim, status )
-
- ! --- in/out ------------------------------
-
- type(TSdsDim), intent(out) :: sdim
- integer, intent(out) :: status
-
- ! --- begin -------------------------------
-
- sdim%id = FAIL
-
- ! ok
- status = 0
-
- end subroutine dim_Init
-
-
- ! *
-
-
- subroutine dim_Done( sdim, status )
-
- ! --- in/out ------------------------------
-
- type(TSdsDim), intent(inout) :: sdim
- integer, intent(out) :: status
-
- ! --- begin -------------------------------
-
- ! nothing to be done
-
- ! ok
- status = 0
-
- end subroutine dim_Done
-
- ! ================================================================
- ! select
- ! ================================================================
- !
- ! argument ind : 0,..,n-1
- !
- subroutine dim_Select( sdim, sds, ind, status )
-
- ! --- in/out -------------------------
-
- type(TSdsDim), intent(out) :: sdim
- type(TSds), intent(in) :: sds
- integer, intent(in) :: ind
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/dim_Select'
-
- ! --- external ------------------------
-
- integer(wpi), external :: sfDimID
-
- ! --- begin ---------------------------
- sdim%id = sfDimID( sds%id, ind ) ! <-- 0,..,n-1
- if ( sdim%id == FAIL ) then
- write (gol,'("error selecting dimension :")'); call goErr
- write (gol,'(" index : ",i6)') ind; call goErr
- write (gol,'(" sds name : ",a)') trim(sds%name); call goErr
- write (gol,'(" hdf name : ",a)') trim(sds%hdfname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! ok
- status = 0
-
- end subroutine dim_Select
-
-
- ! ================================================================
- ! set dimension name
- ! ================================================================
-
-
- subroutine dim_SetName( sdim, name, status )
-
- ! --- in/out -------------------------
-
- type(TSdsDim), intent(inout) :: sdim
- character(len=*), intent(in) :: name
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/dim_SetName'
-
- ! --- external ---------------------------
-
- integer(wpi), external :: sfSDmName
-
- ! --- begin ---------------------------
-
- ! set dimension name
- status = sfSDmName( sdim%id, name )
- if ( status == FAIL ) then
- write (gol,'("setting dimension name :")'); call goErr
- write (gol,'(" dim name : ",a)') name; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! ok
- status = 0
- end subroutine dim_SetName
-
-
- ! ############################################################
- ! ###
- ! ### hdf files
- ! ###
- ! ############################################################
-
- ! ================================================================
- ! init, done
- ! ================================================================
-
- subroutine hdf_Init( hdf, fname, key, status )
-
- ! --- in/out ------------------------------
-
- ! !ARGUMENTS:
- type(THdfFile), intent(out) :: hdf
- character(len=*), intent(in) :: fname
- character(len=*), intent(in) :: key
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_Init'
-
- ! --- local -------------------------------
-
- integer :: dfacc
-
- ! --- external ----------------------------
-
- integer(wpi), external :: sfStart
-
- ! --- begin -------------------------------
-
- ! code to open file:
- select case ( key )
- case ( 'read' )
- dfacc = DFACC_READ
- case ( 'write' )
- dfacc = DFACC_WRITE
- case ( 'create' )
- dfacc = DFACC_CREATE
- case default
- write (gol,'("do not know what how to access hdf for `",a,"`:")') key; call goErr
- write (gol,'(" file name : ",a)') trim(fname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end select
-
- ! open file:
- hdf%id = sfStart( fname, dfacc )
- if ( hdf%id == FAIL ) then
- write (gol,'("from starting access to hdf file:")'); call goErr
- write (gol,'(" file name : ",a)') trim(fname); call goErr
- write (gol,'(" access key : ",a)') trim(key); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! save file name:
- hdf%fname = fname
-
- ! ok
- status = 0
- end subroutine hdf_Init
- ! ***
-
-
- subroutine hdf_Done( hdf, status )
-
- ! --- in/out ------------------------------
-
- ! !ARGUMENTS:
- type(THdfFile), intent(out) :: hdf
- integer, intent(out) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_Done'
-
- ! --- external ----------------------------
-
- integer(wpi), external :: sfEnd
-
- ! --- begin -------------------------------
-
- ! close file:
- status = sfEnd( hdf%id )
- if ( status == FAIL ) then
- write (gol,'("while closing HDF file:")'); call goErr
- write (gol,'(" file name : ",a)') trim(hdf%fname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- ! ok
- status = 0
-
- end subroutine hdf_Done
-
-
- ! ================================================================
- ! info
- ! ================================================================
-
-
- subroutine hdf_GetInfo( hdf, status, num_datasets, num_global_attrs )
-
- ! --- in/out -------------------------
-
- type(THdfFile), intent(inout) :: hdf
- integer, intent(out) :: status
-
- integer, intent(out), optional :: num_datasets
- integer, intent(out), optional :: num_global_attrs
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_GetInfo'
-
- ! --- local -------------------------------
-
- integer :: istat
- integer :: f_num_datasets, f_num_global_attrs
-
- ! --- external ----------------------------
-
- integer(wpi), external :: sfFInfo
-
- ! --- begin -------------------------------
-
- ! extract info
- istat = sfFInfo( hdf%id, f_num_datasets, f_num_global_attrs )
- if ( istat == FAIL ) then
- write (gol,'("from sfFInfo :")'); call goErr
- write (gol,'(" hdf file : ",a)') hdf%fname; call goErr
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
- ! return result
- if ( present(num_datasets) ) num_datasets = f_num_datasets
- if ( present(num_global_attrs) ) num_global_attrs = f_num_global_attrs
- ! ok
- status = 0
- end subroutine hdf_GetInfo
- ! =============================================================
- ! === hdf data sets
- ! =============================================================
-
- subroutine hdf_FindDataSet( hdf, name, sds_index, status )
-
- ! --- in/out -------------------------
-
- type(THdfFile), intent(in) :: hdf
- character(len=*), intent(in) :: name
- integer, intent(out) :: sds_index
- integer, intent(inout) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_FindDataSet'
-
- ! --- external -------------------------------
- integer(wpi), external :: sfN2Index
-
- ! --- local -------------------------------
-
- logical :: verbose
- ! --- begin -------------------------------
-
- ! write error messages ?
- verbose = status == 0
-
- ! find index from name:
- sds_index = sfN2Index( hdf%id, name )
- if ( status < 0 ) then
- ! not found ...
- if (verbose) then
- write (gol,'("data set not found ")'); call goErr
- write (gol,'(" name : ",a)') trim(name)
- write (gol,'(" file name : ",a)') trim(hdf%fname); call goErr
- write (gol,'("in ",a)') rname; call goErr; status=-1; return
- end if
- else if ( status == 0 ) then
- ! ok
- status=0; return
- else
- ! error ...
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
-
- end subroutine hdf_FindDataSet
- ! =============================================================
- ! === hdf global attributes
- ! =============================================================
-
- subroutine hdf_FindAttribute( hdf, name, attr_index, status )
-
- ! --- in/out -------------------------
-
- type(THdfFile), intent(in) :: hdf
- character(len=*), intent(in) :: name
- integer, intent(out) :: attr_index
- integer, intent(inout) :: status
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_FindAttribute'
-
- ! --- local -------------------------------
-
- logical :: verbose
- ! --- begin -------------------------------
-
- ! write error messages ?
- verbose = status == 0
-
- ! find attribute index from name:
- call FindAttribute( hdf%id, name, attr_index, status )
- if ( status < 0 ) then
- ! not found ...
- if (verbose) then; write (gol,'("in ",a)') rname; call goErr; end if
- status=-1; return
- else if ( status == 0 ) then
- ! ok
- status=0; return
- else
- ! error ...
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
- end subroutine hdf_FindAttribute
- ! ***
-
- subroutine hdf_GetAttributeInfo( hdf, attr_index, status, &
- name, &
- data_type, data_type_descr, &
- n_values )
-
- ! --- in/out ----------------------------
-
- type(THdfFile), intent(in) :: hdf
- integer, intent(in) :: attr_index
- integer, intent(inout) :: status
- character(len=*), intent(out), optional :: name
- integer, intent(out), optional :: data_type
- character(len=1), intent(out), optional :: data_type_descr
- integer, intent(out), optional :: n_values
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_GetAttributeInfo'
-
- ! --- begin -------------------------------
-
- call GetAttributeInfo( hdf%id, attr_index, status, &
- name=name, &
- data_type=data_type, data_type_descr=data_type_descr, &
- n_values=n_values )
- if (status/=0) then; write (gol,'("in ",a)') rname; call goErr; status=1; return; end if
-
- ! ok
- status = 0
- end subroutine hdf_GetAttributeInfo
- ! ***
-
- subroutine hdf_CheckAttributeInfo( hdf, attr_index, status, &
- name, data_type, n_values )
-
- ! --- in/out -------------------------
-
- type(THdfFile), intent(in) :: hdf
- integer, intent(in) :: attr_index
- integer, intent(inout) :: status
-
- character(len=*), intent(in), optional :: name
- integer, intent(in), optional :: data_type
- integer, intent(in), optional :: n_values
-
- ! --- const -------------------------------
-
- character(len=*), parameter :: rname = mname//'/hdf_CheckAttributeInfo'
-
- ! --- local -------------------------------
-
- logical :: verbose
- ! --- begin -------------------------------
- ! write error messages ?
- verbose = status == 0
-
- call CheckAttributeInfo( hdf%id, attr_index, status, &
- name=name, data_type=data_type, n_values=n_values )
- if ( status < 0 ) then
- ! check failed ...
- if (verbose) then; write (gol,'("in ",a)') rname; call goErr; end if
- status=-1; return
- else if ( status == 0 ) then
- ! ok
- status = 0; return
- else
- ! error ...
- write (gol,'("in ",a)') rname; call goErr; status=1; return
- end if
- ! ok
- status = 0
- end subroutine hdf_CheckAttributeInfo
- end module file_hdf_base
|