Skip to content
Snippets Groups Projects
Commit de0bf8e5 authored by Bavo Langerock's avatar Bavo Langerock
Browse files

added support for changing DATA_LOCATION through the filename, added support...

added support for changing DATA_LOCATION through the filename, added support for setting an attribute to an empty string, improved default value for DATA_MODIFICATIONS
parent 4e228c52
No related branches found
No related tags found
No related merge requests found
......@@ -29,11 +29,13 @@ Example to add a DOI to an existing GEOMS file and increment its DATA_FILE_VERSI
nkeys=args.attributes[0::2];nvalues=args.attributes[1::2]
assert len(nkeys)==len(nvalues),f'not all keys have a value'
newsource='_'.join(os.path.basename(outfile).split('_')[1:-4])
newlocation=os.path.basename(outfile).split('_')[-4]
newversion=os.path.basename(outfile).split('_')[-1].rsplit('.',1)[0]
shutil.copyfile(infile,outfile)
if h5py.is_hdf5(infile):
with h5py.File(outfile,'a') as fid:
fid.attrs['DATA_SOURCE']=np.array(newsource.upper(),dtype='S')
fid.attrs['DATA_LOCATION']=np.array(newlocation.upper(),dtype='S')
fid.attrs['FILE_NAME']=np.array(os.path.basename(outfile),dtype='S')
fid.attrs['DATA_FILE_VERSION']=np.array(newversion,dtype='S')
if 'DATA_MODIFICATIONS' in fid.attrs: X=np.string_(fid.attrs['DATA_MODIFICATIONS']) if isinstance(fid.attrs['DATA_MODIFICATIONS'],str) else fid.attrs['DATA_MODIFICATIONS']
......@@ -44,13 +46,15 @@ Example to add a DOI to an existing GEOMS file and increment its DATA_FILE_VERSI
else:
hdf4id=SD.SD(outfile,SD.SDC.WRITE)
att4=hdf4id.attr('DATA_SOURCE');att4.set(SD.SDC.CHAR8,newsource.upper())
att4=hdf4id.attr('DATA_LOCATION');att4.set(SD.SDC.CHAR8,newlocation.upper())
att4=hdf4id.attr('FILE_NAME');att4.set(SD.SDC.CHAR8,os.path.basename(outfile))
att4=hdf4id.attr('DATA_FILE_VERSION');att4.set(SD.SDC.CHAR8,newversion)
try: X=getattr(hdf4id,'DATA_MODIFICATIONS');
except: X=''
finally: att4=hdf4id.attr('DATA_MODIFICATIONS')
if X: X=';'.join([x for x in (X,'changed DATA_SOURCE by %s on %s, origin is %s'%(user,dt.datetime.now().strftime('%Y%m%dT%H%M%Sz'),infile)) if x.lower() not in ('','none')]);att4.set(SD.SDC.CHAR8,X)
if X: X=';'.join([x for x in (X,'changed %s by %s on %s, origin is %s'%(','.join(nkeys),user,dt.datetime.now().strftime('%Y%m%dT%H%M%Sz'),infile)) if x.lower() not in ('','none')]);att4.set(SD.SDC.CHAR8,X)
for nk,nv in zip(nkeys,nvalues):
if nv=='': nv='\x00'
att4=hdf4id.attr(nk);att4.set(SD.SDC.CHAR8,nv)
if __name__ == '__main__': main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment