X
4866

EOS Swath (HDF4) example program

 Topic

This article provides an example program called "dj_write_hdf4_example"  that shows how IDL can be used to generate an EOS (HDF4) file with 4 datasets. The code in this article also show how you can read the file and plot the data contained within it onto a map.

Discussion

The code below shows how IDL can be used to generate an EOS file. The procedure used to generate the file is called "dj_write_hdf4_example". The following process was used to generate the file:

-Open the file and create the swath
-define the dimensions for the datasets
-Inside a FOR loop: define all of the fields
-Detach from the swath
-In a FOR loop: Reattach to the swath, write the data, detach from the swath
-Close file

The EOS routines that are used in the example code are shown below:

- EOS_SW_OPEN
- EOS_SW_CREATE
- EOS_SW_DEFDIM
- EOS_SW_DEFDATAFIELD
- EOS_SW_DEFGEOFIELD
- EOS_SW_DETACH
- EOS_SW_WRITEFIELD
- EOS_SW_CLOSE
- EOS_SW_INQDATAFIELDS
- EOS_SW_INQGEOFIELDS
- EOS_SW_READFIELD

The code includes three routines. First is "dj_generate_data" which is a function that creates a rectangle of data with latitude and longitude values. The second routine is "dj_read_hdf4_example" which reads the file generated with the "dj_write_hdf4_example". The final (and primary) routine is call "dj_write_hdf4_example" which writes and hdf4 file. The code is shown below:

function dj_generate_data, latmin, lonmin
   compile_opt idl2

  ;box a data that can be used to generate
  ;a really simple contour on a map

  ;create some data
  a = fltarr(200,200)
  a[0:50,0:50]=200
  a[10:19,10:19]=100


  ;create lat grid
  lats_1d = (findgen(200)/200)*20 + latmin
  lats = congrid(transpose(lats_1d),200,200)

  ;create lon grid
  lons_1d = (findgen(200)/200)*20 + lonmin
  lons = congrid(transpose(lons_1d),200,200)
  lons = transpose(lons)

  ll = list(a,lats,lons)

  return, ll

end



pro dj_read_hdf4_example, filepath
  compile_opt idl2


  ;this procedure is used to read the newly generated file
  ;and then generates a contour of it. i used this to test
  ;whether or not it worked

   ;open the new file and attach to the swath
   fid = eos_sw_open(filepath,/read)  
   sfid = eos_sw_attach(fid, "test_swath")
   
   ;quiery for the datafield and goefield information
   nfields = eos_sw_inqdatafields(sfid, datafieldlist, rank, numbertype)
   ngeofields = eos_sw_inqgeofields(sfid, geofieldlist, georank, geonumbertype)
   
   ;make sure correct number of fields are in the file
   if 2*nfields ne ngeofields then message, "data and geo fields do not match"
   
   ;split the string into separte element in an array
   datafieldlist = strsplit(datafieldlist,',',/extract)
   geofieldlist = strsplit(geofieldlist,',',/extract)   
   
   ;generate a map and plot the continents
   m = map('geographic', limit=[0,90,60,135])
   mc = mapcontinents()
   
   ;read each field and plot the data onto the map
   for ind = 0l, nfields-1 do begin
       
       geo_ind = ind*2
       
       status = eos_sw_readfield(sfid, datafieldlist[ind], data)
       status = eos_sw_readfield(sfid, geofieldlist[geo_ind], lons)
       status = eos_sw_readfield(sfid, geofieldlist[geo_ind+1], lats)
       
       c = contour(data,reform(lons[*,0]),reform(lats[0,*])$
                   ,/over,/fill,grid_units='degrees')

   endfor

  status = eos_sw_detach(sfid)
  status = eos_sw_close(fid)
       

end


pro dj_write_hdf4_example
  compile_opt idl2

   hdf4_filename = "hdf_eos_test_output.hdf"

  ;create new hdf4 file
  fid = eos_sw_open(hdf4_filename, /create)
 
  ;create new swath file
  sfid = eos_sw_create(fid, "test_swath")
 
 
  ;create the dimensions
  status = eos_sw_defdim(sfid, "geotrack", 200)
  status = eos_sw_defdim(sfid, "geoxtrack", 200)
  status = eos_sw_defdim(sfid, "datatrack", 200)
  status = eos_sw_defdim(sfid, "dataxtrack", 200)
 
 
 
  for ind = 1l, 4 do begin
    
     ;write different field names for each set of
     ;data
     data_field_name = "data_field_" + strtrim(ind,2)
     lon_field_name = "longitude_" + strtrim(ind,2)
     lat_field_name = "latitude_" + strtrim(ind,2)
     
     ;use defdatafield to create the data field. then
     ;use defgeofield to  write the geographical data
     ;fields (latitude and longitude).   
     status = eos_sw_defdatafield(sfid, data_field_name, $
       "datatrack, dataxtrack", 5)
     status = eos_sw_defgeofield(sfid, lon_field_name, $
       "geotrack, geoxtrack", 5)
     status = eos_sw_defgeofield(sfid, lat_field_name, $
       "geotrack, geoxtrack", 5)
    
  endfor

  ;detach from the newly defined swath
  status = eos_sw_detach(sfid)
 
 
  for ind = 1l, 4 do begin
    
    ;reattach to swath
    sfid = eos_sw_attach(fid, "test_swath")
    
    ;generate a dataset
    ll = dj_generate_data(10*ind*0.75,100)
    
    ;parse the returned list
    a = ll[0] ;data
    lats = ll[1] ;latitude
    lons = ll[2] ;longitude
    
    ;recreate the field names
    data_field_name = "data_field_" + strtrim(ind,2)
    lon_field_name = "longitude_" + strtrim(ind,2)
    lat_field_name = "latitude_" + strtrim(ind,2)

    ;write data into each field in the data set
    status = eos_sw_writefield(sfid, data_field_name, a)
    status = eos_sw_writefield(sfid, lon_field_name, lons)
    status = eos_sw_writefield(sfid, lat_field_name, lats)
    status = eos_sw_detach(sfid)
  endfor

  ;close the field
  status = eos_sw_close(fid)
 
  dj_read_hdf4_example, "hdf_eos_test_output.hdf"

end


Written by DS. Reviewed by JU (05/19/2014).