Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Each flux structure contains a FLT_1D data array named 'data', a INT_1D array named ' ' validity_timed', a INT_0D scalar named 'validity', a FLT_1D data array named 'time'.

...

Let's add a 'magnetics' IDS to the pulse file previously created in section 1.1.1.

The first part of the code below is opening a data_entry then a magnetics IDS is created and written to the data_entry using the put() operation:

...

Code Block
import imas
import getpass
import numpy as np
from imas import imasdef

#creates the Data Entry object 'data_entry' associated  to the pulse file with shot=15000, run=1, belonging to database 'data_access_tutorial' of the current user, using the MDS+ backend
data_entry = imas.DBEntry(imasdef.MDSPLUS_BACKEND, 'data_access_tutorial, 15000, 1, user_name=getpass.getuser())

#opens the pulse file associated to the Data Entry object 'data_entry' previously created
data_entry.open() 

#creates the 'magnetics' IDS and initializes it
magnetics_ids = imas.magnetics() #creates a 'magnetics' IDS
magnetics_ids.ids_properties.homogeneous_time=1 #setting the homogeneous time (mandatory)
magnetics_ids.ids_properties.comment='IDS created for testing the IMAS Data Access layer' #setting the ids_properties.comment attribute
magnetics_ids.time=np.array([0]) #the time(vector) basis must be not empty if homogeneous_time==1 otherwise an error will occur at runtime

#adding the WEST data of the 10 first flux loops
nb_flux_loops = 10
west_data_entry = imas.DBEntry(imasdef.MDSPLUS_BACKEND, 'west', 54178, 0, 'g2lfleur')
west_magnetics_ids = west_data_entry.get('magnetics', 0) #reading occurrence 0
magnetics_ids.flux_loop.resize(nb_flux_loops)
for i in range(nb_flux_loops):
  magnetics_ids.flux_loop[i].flux.data = west_magnetics_ids.flux_loop[i].flux.data #copies data
  if west_data_entry.close() #closing the WEST pulse file

#writes the 'magnetics' IDS
data_entry.put(magnetics_ids, 0) #writes magnetics data to the data_entry associated to the pulse file. The second argument 0 is the so-called IDS occurrence.

#closes the pulse file associated to the 'data_entry' object
magnetics_ids.ids_properties.homogeneous_time==0:
    magnetics_ids.flux_loop[i].flux.time = west_magnetics_ids.flux_loop[i].flux.time #copies the time basis in case WEST IDS arrays don't accept a common time basis

if west_magnetics_ids.ids_properties.homogeneous_time==1:
   magnetics_ids.time = west_magnetics_ids.time #copies the 'root' time basis in case WEST IDS arrays accept a common time basis
   
west_data_entry.close() 	 

...

#closing the WEST pulse file

#writes the 'magnetics' IDS
data_entry.put(magnetics_ids, 0) #writes magnetics data to the data_entry associated to the pulse file. The second argument 0 is the so-called IDS occurrence.

#closes the pulse file associated to the 'data_entry' object
data_entry.close() 	 


put_slice

An IDS containg dynamic data structures can be built progressively using data time slices.

A dynamic data structure:

  • is either an array with type INT_nD or FLT_nD where n=1 to 6, which accepts a time coordinate
  • or a so-called dynamic array of structures AOS[i] where i runs along a time index

In the example below, we illustrate the use of put_slice on a 'camera_visible' IDS which contains the dynamic array of structures 'frame'.

Let's first have a look to the Data Dictionary of the 'camera_visible' IDS description:

Image Added


As indicated in the description, 'frame(itime)' is a dynamic data structure representing a set of frames.

Each element of 'frame' is a structure containing:

  • 'image_raw', a INT_2D array
  • 'radiance', a FLT_2D array
  • 'time', a time basis (used only in case the IDS has no common time basis)

Adding a time slice using put_slice() on the 'camera_visible' will have for effect to add a new element to the dynamic array 'frame', that is a new frame is appended to the array of structures 'frame'.   

The example below shows how to add time slices to the 'camera_visible' IDS, for channel 0 and detector 0:

  • The number of time slices to be appended is given by the variable 'nb_frames'.
  • Each time slice is represented by the data structure: camera_visible_ids.channel[0].detector[0].frame[0]
  • Only the data of the INT_2D 'image_raw" and the data of global time basis 'time' (since the 'camera_visible' has homogeneous_time = 1) are populated in the time slice to be appended


Warning
The first time slice needs to be appended using a call to put(), not put_slice(). Only subsequent time slices are appended using put_slice().



Warning

Time slices should be always appended using increasing time value.


Code Block
import imas
import getpass
import numpy as np
from imas import imasdef

#creates the Data Entry object 'data_entry' associated  to the pulse file with shot=15000, run=1, belonging to database 'data_access_tutorial' of the current user, using the MDS+ backend
data_entry = imas.DBEntry(imasdef.MDSPLUS_BACKEND, 'data_access_tutorial', 15000, 1, user_name=getpass.getuser())

#opens the pulse file associated to the data_entry object
data_entry.open()

#creates the 'magnetics' IDS and initializes it
camera_visible_ids = imas.camera_visible()
camera_visible_ids.ids_properties.homogeneous_time = 1
camera_visible_ids.channel.resize(1) #using only 1 channel (channel 0) for this example
camera_visible_ids.channel[0].detector.resize(1) #using only 1 detector for channel 0
camera_visible_ids.channel[0].detector[0].frame.resize(1) #the array of structure 'frame' contains only 1 element, it is the frame to be appended to the IDS 

X = 3 #number of horizontal pixels in the frame
Y = 5 #number of vertical pixels in the frame

camera_visible_ids.channel[0].detector[0].frame[0].image_raw.resize(X,Y) #setting the size of the image of the frame
camera_visible_ids.time.resize(1) #the time vector contains only 1 element, it is the time of the slice

nb_frames=10 #number of frames to be added

for i in range(nb_frames):
  camera_visible_ids.time[0] = float(i) #time of the slice
  for j in range(X):
    for k in range(Y):
       camera_visible_ids.channel[0].detector[0].frame[0].image_raw[j,k] = float(j + k +  i) #image_raw is a 2D array containing the data (pixels) of the frame
  if i==0:
    data_entry.put(camera_visible_ids) #the first frame has to be added using put() in order to store static data as well
  else:
    data_entry.put_slice(camera_visible_ids)  #appending the slice (frame) to the IDS

...