Changeset 282


Ignore:
Timestamp:
Feb 19, 2011 11:54:34 AM (14 years ago)
Author:
saunders
Message:

Got read_dataset and write_dataset working for primary and secondary. Need to work on
dim_datasets next.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified hdf5_exchange/idl/trunk/nxexchange.pro

    r281 r282  
    3030;       NXEXCHANGE::CLOSE          closes HDF5 file
    3131;       NXEXCHANGE::WRITE_DATASET  writes out one dataset
     32;       NXEXCHANGE::READ_DATASET   reads one dataset
    3233;
    3334;
    3435; EXAMPLE:
    35 ;       ;; Create nxexchange object for writing data.h5 file
    36 ;       nxe = obj_new('nxexchange', version='0.3b')
     36;       ;; Create nxexchange object for reading/writing data files
     37;       nxe = obj_new('nxexchange')
    3738;
    3839;       ;; open an HDF5 file for writing
    39 ;       success = nxe->open('data.h5', 'w')
     40;       nxe->open, 'data.h5', 'w'
    4041;
    4142;       ;; make a 3x3 array of ints
     
    4344;
    4445;       ;; write it out as primary dataset
    45 ;       success = nxe->write_dataset('image',['x','y'],'flourescence', idataset)
     46;       nxe->write_dataset, 'image',['x','y'],'flourescence', idataset
    4647;
    4748;       ;; write out a secondary dataset associated with above image
    4849;       tdataset = fltarr(5)
    49 ;       success = nxe->write_dataset('temps',['C'],'', tdataset, parent='image')
     50;       nxe->write_dataset, 'temps',['C'],'', tdataset, parent='image'
    5051;       
    5152;       ;; close the file
     53;       nxe->close
     54;
     55;       ;; re-open the file for reading
     56;       nxe->open, 'data.h5','r'
     57;
     58;       ;; read in a primary dataset
     59;       idataset = nxe->read_dataset('image')
     60;
     61;       ;; read in a secondary dataset
     62;       tdataset = nxe->read_dataset('temps', parent='image')
     63;
     64;       ;; close the file again
    5265;       nxe->close
    5366;
     
    104117;; A simple struct to hold a primary dataset index along with
    105118;;   associated HDF5 group ids
    106 pro datasetentry__define
    107   COMPILE_OPT strictarr
    108   struct = {datasetentry, $
     119pro nxexchangedatasetentry__define
     120  COMPILE_OPT strictarr
     121  struct = {nxexchangedatasetentry, $
    109122            index: 0L, $
    110123            entry_exchange_gid: 0L, $
     
    114127
    115128; =============================================================
    116 ;
     129; INTRINSIC
    117130; METHODNAME:
    118131;       NXEXCHANGE::INIT
     
    129142;
    130143; RETURNS:
    131 ;       A new nxexchange object.
     144;       1 - success in initializing, 0 - failure
    132145;
    133146; EXAMPLE:
    134147;       nxe = obj_new('nxexchange', VERSION='0.3b')
     148;       ;; the above indirectly invokes this init function during
     149;       ;;   object construction
    135150;       
    136151;
     
    138153
    139154  COMPILE_OPT strictarr
    140   CATCH, error
    141   if error ne 0 then begin
    142       print, 'NXEXCHANGE::init error: ',error,' ',!ERROR_STATE.MSG
    143       CATCH, /CANCEL
    144       return, 0
    145   endif
    146155 
    147156  if keyword_set(vers) then begin
     
    181190;       none
    182191;
    183 ; RETURNS:
    184 ;       1 - success, 0 - failure
    185 ;
    186192; EXAMPLE:
    187 ;       nxe->open('data.nxs', 'w')
     193;       nxe->open, 'data.h5', 'w'
    188194;       
    189195;
    190 function nxexchange::open, filename, mode
    191 
    192   COMPILE_OPT strictarr
    193   CATCH, error
    194   if error ne 0 then begin
    195       print, 'NXEXCHANGE::open error: ',error,' ',!ERROR_STATE.MSG
    196       CATCH, /CANCEL
    197       return, 0
    198   endif
     196pro nxexchange::open, filename, mode
     197
     198  COMPILE_OPT strictarr
    199199
    200200  if (self.fid ne 0) then begin
    201       message, 'a file is already open named: ' + self.filename
     201      message, 'a file is already open named: ' + self.filename, /informational
     202      return
    202203  endif
    203204
     
    211212    self.fid = H5F_CREATE(self.filename)
    212213    ;; build out some initial structure
    213     success = self->build_default_structure()
    214     if (~success) then begin
    215         message, 'unable to build default structure'
    216     endif
     214    self->build_default_structure
    217215
    218216  endif else if (self.mode eq 'R') then begin
     
    223221    if (~success) then begin
    224222        self.fid = 0
    225         message, 'this file does not appear to be valid'
     223        message, 'this file does not appear to be valid', /informational
     224        return
    226225    endif
    227226
    228227  endif else if (self.mode eq 'A') then begin
    229228    ;; self.fid = H5F_OPEN(self.filename, /WRITE)
    230     message, 'the append (A) mode is not supported yet'
     229    message, 'the append (A) mode is not supported yet', /informational
     230    return
    231231
    232232  endif else begin
    233     message, 'the mode argument must be w or r or a'
     233    message, 'the mode argument must be w or r or a', /informational
     234    return
    234235  endelse
    235236
    236   return, 1
     237  return
    237238end
    238239
     
    255256;                 belongs to given parent dataset
    256257;
    257 ; RETURNS:
    258 ;       1 - success, 0 - failure
    259258;
    260259; EXAMPLE:
    261 ;       nxe->write_dataset('data',['ttheta','energy'],'transmission',data)
    262 ;
    263 ;
    264 function nxexchange::write_dataset, name, axes, data_type, dataset, parent=parent
    265   COMPILE_OPT strictarr
    266   CATCH, error
    267   if error ne 0 then begin
    268       print, 'NXEXCHANGE::write_dataset error: ',error,' ',!ERROR_STATE.MSG
    269       CATCH, /CANCEL
    270       return, 0
     260;       nxe->write_dataset, 'data',['ttheta','energy'],'transmission', data
     261;
     262;
     263pro nxexchange::write_dataset, name, axes, data_type, dataset, parent=parent
     264  COMPILE_OPT strictarr
     265
     266  if (self.fid eq 0) then begin
     267      message, 'you cannot write a dataset without opening file first', /informational
     268      return
    271269  endif
    272 
    273   if (self.fid eq 0) then return, 0
    274270
    275271  ;; if parent dataset name given, then we are now entering a
     
    279275      parent_exists = self.primary_dataset_set->iscontained(parent)
    280276      if (~parent_exists) then begin
    281           message, 'parent dataset '+parent+' does not exist'
     277          message, 'parent dataset '+parent+' does not exist', /informational
     278          return
    282279      endif else begin
    283280          primary_dataset = self.primary_dataset_set->get(parent)
     
    295292  axes_n_elems = n_elements(axes)
    296293  if ((n_dims gt 0) and (n_dims ne axes_n_elems)) then begin
    297       message, 'axes must have same number of elements as dimensions of dataset'
     294      message, 'axes must have same number of elements as dimensions of dataset', /informational
     295      return
    298296  endif
    299297
     
    308306  ;; write primary dataset
    309307  if (~secondary) then begin
    310       dataset_entry = {datasetentry}
     308      dataset_entry = {nxexchangedatasetentry}
    311309      dataset_entry.index = self.primary_dataset_index + 1;
    312310
     
    338336          H5A_CLOSE, signal_aid
    339337      endif
    340       existing_dataset_path = '/entry/' + name + '/data'
     338      exchange_groupname = self->exchange_group_name(self.primary_dataset_index)
     339      target_path = '/entry/'+exchange_groupname+'/'+name
    341340      target_aid = H5A_CREATE(dsid, 'target', self.string_dtid, self.simple_dspid)
    342       H5A_WRITE, target_aid, existing_dataset_path
     341      H5A_WRITE, target_aid, target_path
    343342      H5A_CLOSE, target_aid   
    344343
     
    385384  endif
    386385
    387   return, 1
     386  return
    388387end
    389388
     
    404403;
    405404; RETURNS:
    406 ;       dataset - success, null_ptr - failure
     405;       dataset
    407406;
    408407; EXAMPLE:
    409 ;       nxe->read_dataset('data')
     408;       my_primary_dataset = nxe->read_dataset('image')
     409;       my_secondary_dataset = nxe->read_dataset('temps', parent='image')
    410410;
    411411;
    412412function nxexchange::read_dataset, name, parent=parent
    413413  COMPILE_OPT strictarr
    414   CATCH, error
    415   if error ne 0 then begin
    416       print, 'NXEXCHANGE::read_dataset error: ',error,' ',!ERROR_STATE.MSG
    417       CATCH, /CANCEL
    418       return, ptr_new()
     414
     415  if (self.fid eq 0) then begin
     416      message, 'you cannot read a dataset without opening file first', /informational
     417      return, 0
    419418  endif
    420 
    421   if (self.fid eq 0) then return, ptr_new()
    422419
    423420  ;; if parent dataset name given, then we are reading a
    424421  ;;   secondary dataset
    425422  if keyword_set(parent) then begin 
    426       ;; have to iterate exchange groups to find secondary dataset
    427 ;;      n_entry_groups = H5G_GET_NMEMBERS(self.fid, 'entry')
    428 
    429 ;;      for i=0, n_entry_groups-1 do begin
    430 ;;          obj_name = H5G_GET_MEMBER_NAME(self.fid, 'entry', i)
    431 ;;          s = strpos(obj_name, 'exchange')
    432 ;;          if (s eq 0) then begin
    433 ;;              gid = H5G_OPEN(self.entry_gid, obj_name)
    434 ;;              dsid = H5D_OPEN(gid, parent)
    435 ;;              CATCH, error
    436 ;;              if (error ne 0)
    437              
    438 
    439 ;;          endif
    440 ;;          oid = H5G_OPEN(self.entry_gid, obj_name)
    441 ;;          type_string = H5I_GET_TYPE(oid)
    442 ;;          if (type_string eq 'GROUP') then begin
    443               ;; see if group has @NX_class=NXsubentry
    444 ;;              H5A_OPEN_NAME(oid
    445              
    446 ;;          endif
    447 ;;      endfor
    448 
    449 ;; try this a different way...
    450       ;; validate that parent group exists
    451 ;;      parent_gid = H5G_OPEN(self.entry_gid,parent)
    452 ;;      link_dest_path = H5G_GET_LINKVAL(parent_gid,'data')
    453       ;; slice off end to get group path
    454 ;;      end_pos = strpos(link_dest_path, parent, /REVERSE_SEARCH)
    455 ;;      exchange_path = strpos(link_dest_path, 0, end_pos)
    456 
    457 ;;      print, '**** exchange_path: ' + exchange_path
     423      ;; install temporary exception handler (yuck)
     424      CATCH, error
     425      if (error ne 0) then begin
     426          message, 'given parent dataset ' + parent + ' does not exist in file', /informational
     427          return, 0
     428      endif
     429      ;; attempt to find /entry/'parent' NXdata group
     430      parent_gid = H5G_OPEN(self.entry_gid,parent)
     431      CATCH, /CANCEL   ;; cancel temporary exception handler
    458432     
    459       ;; open dataset
    460       dsid = H5D_OPEN(parent_gid, name)
     433      ;; open NXdata dataset
     434      dsid = H5D_OPEN(parent_gid, 'data')
     435      ;; get @target attribute
     436      target_aid = H5A_OPEN_NAME(dsid,'target')
     437      target_path = H5A_READ(target_aid)
     438      H5A_CLOSE, target_aid
     439      ;; close out the linked dataset
     440      H5D_CLOSE, dsid
     441      ;; close out NXdata group
     442      H5G_CLOSE, parent_gid
     443
     444      ;; slice off end of target path to get group path
     445      end_pos = strpos(target_path, parent, /REVERSE_SEARCH)
     446      exchange_path = strmid(target_path, 0, end_pos)
     447     
     448      ;; open exchange group
     449      parent_gid = H5G_OPEN(self.fid, exchange_path)
     450      ;; finally! - read the dataset we are looking for
     451      dsid = H5D_OPEN(parent_gid, name);
    461452      dataset = H5D_READ(dsid)
    462453
     
    470461  H5D_CLOSE, dsid
    471462
    472   ;; clean up gids
     463  ;; clean up gid
    473464  H5G_CLOSE, parent_gid
    474465
     
    479470; Internal METHOD - builds initial /entry
    480471;
    481 function nxexchange::build_default_structure
    482   COMPILE_OPT strictarr
    483   CATCH, error
    484   if error ne 0 then begin
    485       print, 'NXEXCHANGE::build_default_structure error: ',error,' ',!ERROR_STATE.MSG
    486       CATCH, /CANCEL
    487       return, 0
    488   endif
    489 
    490   if (self.fid eq 0) then begin
    491       return, 0
    492   endif
     472pro nxexchange::build_default_structure
     473  COMPILE_OPT strictarr
    493474
    494475  ;; create /entry group with @NX_class=NXentry
     
    498479  H5A_CLOSE, nxclass_aid
    499480
    500   return, 1
     481  return
    501482end
    502483
    503484; =============================================================
    504485; Internal METHOD - reads in initial file structure and validates
     486;    returns 1 - valid, 0 - invalid
    505487;
    506488function nxexchange::read_default_structure
    507489  COMPILE_OPT strictarr
    508   CATCH, error
    509   if error ne 0 then begin
    510       print, 'NXEXCHANGE::read_default_structure error: ',error,' ',!ERROR_STATE.MSG
    511       CATCH, /CANCEL
    512       return, 0
    513   endif
    514490
    515491  ;; read /entry group and verify it has @NX_class=NXentry
     
    529505;         index=2 produces /entry/exchange2
    530506;   
    531 ;     returns open gid for new group, or 0 if error
     507;     returns open gid for new group
    532508;
    533509function nxexchange::build_exchange_group, index
    534510  COMPILE_OPT strictarr
    535   CATCH, error
    536   if error ne 0 then begin
    537       print, 'NXEXCHANGE::build_exchange_group error: ',error,' ',!ERROR_STATE.MSG
    538       CATCH, /CANCEL
     511
     512  if (index lt 1) then begin
     513      message, 'index must be 1 or greater', /informational
    539514      return, 0
    540515  endif
    541516
    542   if (index lt 1) then begin
    543       print, 'NXEXCHANGE::build_exchange_group error: index must be 1 or greater'
    544       return, 0
    545   endif
    546 
    547   if (index eq 1) then begin
    548       groupname = 'exchange'
    549   endif else begin
    550       groupname = 'exchange'+STRTRIM(STRING(index),1)
    551   endelse
    552 
    553     ;; create /entry/exchange(n) group with @NX_class=NXsubentry
     517  groupname = self->exchange_group_name(index)
     518
     519  ;; create /entry/exchange(n) group with @NX_class=NXsubentry
    554520  entry_exchange_gid = H5G_CREATE(self.entry_gid, groupname)
    555521  nxclass_aid = H5A_CREATE(entry_exchange_gid, 'NX_class', self.string_dtid, self.simple_dspid)
     
    568534end
    569535
     536function nxexchange::exchange_group_name, index
     537  COMPILE_OPT strictarr
     538  if (index eq 1) then begin
     539      groupname = 'exchange'
     540  endif else begin
     541      groupname = 'exchange'+STRTRIM(STRING(index),1)
     542  endelse
     543  return, groupname
     544end
     545
    570546; =============================================================
    571547; Internal METHOD - builds a single nxdata group with given name
    572 ;     returns open gid for new group, or 0 if error
     548;     returns open gid for new group
    573549;
    574550function nxexchange::build_nxdata_group, name
    575551  COMPILE_OPT strictarr
    576   CATCH, error
    577   if error ne 0 then begin
    578       print, 'NXEXCHANGE::build_nxdata_group error: ',error,' ',!ERROR_STATE.MSG
    579       CATCH, /CANCEL
    580       return, 0
    581   endif
    582552
    583553  ;; create /entry/'name' with @NX_class=NXdata
     
    611581pro nxexchange::close
    612582  COMPILE_OPT strictarr
    613   CATCH, error
    614   if error ne 0 then begin
    615       print, 'NXEXCHANGE::close error: ',error,' ',!ERROR_STATE.MSG
    616       CATCH, /CANCEL
    617   endif
    618583
    619584  if (self.fid ne 0) then begin
    620585      ;; close out various subgroup gids we have been holding onto
    621586      keys = self.primary_dataset_set->keys()
    622       for idx = 0L, N_ELEMENTS(keys)-1 do begin
     587      if (keys eq '') then begin
     588          numkeys = 0
     589      endif else begin
     590          numkeys = n_elements(keys)
     591      endelse
     592      for idx = 0, numkeys-1 do begin
    623593          key = keys[idx]
    624594          dataset_entry = self.primary_dataset_set->get(key)
     
    644614
    645615; =============================================================
    646 ;
     616; INTRINSIC
    647617; METHODNAME:
    648618;       NXEXCHANGE::CLEANUP
     
    660630; EXAMPLE:
    661631;       OBJ_DESTROY, nxe
     632;       ;; the above winds up calling this cleanup procedure
    662633;
    663634;
    664635pro nxexchange::cleanup
    665636  COMPILE_OPT strictarr
    666   CATCH, error
    667   if error ne 0 then begin
    668       print, 'NXEXCHANGE::cleanup error: ',error,' ',!ERROR_STATE.MSG
    669       CATCH, /CANCEL
    670   endif
    671637
    672638  ;; close out useful types and space definitions
     
    680646; =============================================================
    681647; METHODNAME: NXEXCHANGE__DEFINE
    682 internal method: defines nxexchange internal data structures
     648 defines nxexchange class data structure
    683649;
    684650pro nxexchange__define
Note: See TracChangeset for help on using the changeset viewer.