SWIG

From Wicare

Jump to: navigation, search

He following file is has to be adapted manually to machine variables (hdfi.i) :

   typedef char                int8;
   typedef unsigned char       uint8;
   typedef short int           int16;
   typedef unsigned short int  uint16;
   typedef long int            int32;
   typedef unsigned long int   uint32;
   typedef float               float32;
   typedef double              float64;

The first step is to create a netcdffiledata.i file :

   %module netcdffiledata 
   %{
   #include "netcdffiledata.h"
   %}
   
   
   %include "hdfi.i"
   
   %include "std_vector.i"
   %include "std_string.i"
   %include "carrays.i"
   
   %template(vectorl) std::vector<long >;
   
   %array_class(int8, int8Array);
   %array_class(uint8, uint8Array);
   %array_class(int16, int16Array);
   %array_class(uint16, uint16Array);
   %array_class(int32, int32Array);
   %array_class(uint32, uint32Array);
   %array_class(float32, float32Array);
   %array_class(float64, float64Array);
   
   class NETCDFFileData {
   public:
       NETCDFFileData(const std::string &filename, const std::string &mode);    
   
       ~NETCDFFileData();
   
       void* read_data(void* data,const char* sds_name,long* start= NULL,long* stride= NULL,long *edges=NULL,long rank=-1);
   
       std::vector<long > get_dataset_dimension(const std::string &sds_name);
   
       bool dataset_exists(const char *sds_name);
   
       void get_dataset_fill_value(const std::string &sds_name, void* fill_value);
   
       int get_n_dataset();
   
       std::string get_dataset_name(int i);
   
       int get_dataset_data_type(std::string sds_name);
   
       std::string get_values_attr_dataset(std::string sds_name,std::string attr_name);
   
       bool has_attr_dataset(std::string sds_name,std::string attr_name);
   
       void close_data_file(){SDend(ncfile);ncfile=-1;};
   
       void open_data_file();
   };
   

then is is necessary to run SWIG:

   swig -c++ -python  netcdffiledata.i

the result must be compiled like this:

   g++ -shared  /home/manley/filedata/src/netcdffiledata.cpp netcdffiledata_wrap.cxx -I/usr/local/include/python2.5
   -I/home/manley/include -I/usr/lib/hdf/include -L/home/manley/lib -L/usr/lib/hdf/lib/ -L/usr/lib
   -L/DATA/LIENS/exploit/bin/python2.5 -lfiledata -lhdfcpp -ltools -lmfhdf -ldf
   /DATA/LIENS/exploit/bin/python2.5/lib/python2.5/config/libpython2.5.a /usr/lib/libc.a
   /usr/lib/gcc/i386-redhat-linux/3.4.6/libstdc++.a  -ljpeg -lz -lm -o _netcdffiledata.so

to use the library, you need to import the library, and call the constructor after a netcdffiledata namespace:

   import netcdffiledata
   import numpy
   
   def main():
       file = netcdffiledata.NETCDFFileData("/home/manley/output.nc","r")
       i = file.get_n_dataset()
       for j in range(0,i):
           name =  file.get_dataset_name(j)     
   
       vect = file.get_dataset_dimension("longitude") 
       print type(vect)
       i = vect[0]      
       
       data = netcdffiledata.float32Array(i)
       file.read_data(data, "longitude")
       for t in range(0,i):
           print data[t]
   
   if __name__ == '__main__':
       main()
Personal tools
<
October 2017
>
MTWTFSS
1
2345678
9101112131415
16171819202122
23242526272829
3031