Class: NumRu::NetCDF

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/netcdf.rb,
lib/numru/netcdf/version.rb,
ext/numru/netcdfraw.c

Constant Summary collapse

Max_Try =
100
NCVERSION =
NetCDF.libvers
VERSION =
"0.7.2"
NC_NOWRITE =

Class Constants Definition

INT2FIX(NC_NOWRITE)
NC_WRITE =
INT2FIX(NC_WRITE)
NC_SHARE =
INT2FIX(NC_SHARE)
NC_CLOBBER =
INT2FIX(NC_CLOBBER)
NC_NOCLOBBER =
INT2FIX(NC_NOCLOBBER)
NC_64BIT_OFFSET =
INT2FIX(NC_64BIT_OFFSET)
NC_NETCDF4 =

NC_64BIT_OFFSET supports large files in the class data format

INT2FIX(NC_NETCDF4)
NC_CLASSIC_MODEL =
INT2FIX(NC_CLASSIC_MODEL)
NC_ENDIAN_NATIVE =

for use as ( NC_NETCDF4 | NC_CLASSIC_MODEL ) to ensure the classic

data model in NetCDF4 by disabling new features like groups
INT2FIX(NC_ENDIAN_NATIVE)
NC_ENDIAN_LITTLE =
INT2FIX(NC_ENDIAN_LITTLE)
NC_ENDIAN_BIG =
INT2FIX(NC_ENDIAN_BIG)
NC_FORMAT_CLASSIC =
INT2FIX(NC_FORMAT_CLASSIC)
NC_FORMAT_64BIT =
INT2FIX(NC_FORMAT_64BIT)
NC_FORMAT_NETCDF4 =
INT2FIX(NC_FORMAT_NETCDF4)
NC_FORMAT_NETCDF4_CLASSIC =
INT2FIX(NC_FORMAT_NETCDF4_CLASSIC)
SUPPORT_BIGMEM =
Qfalse
@@nc4 =
false
@@cr_format =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_tmpfile(path) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/numru/netcdf.rb', line 129

def clean_tmpfile(path)
	  proc {
	     print "removing ", path, "..." if $DEBUG
	     if File.exist?(path)
		File.unlink(path) 
	     end
	     print "done\n" if $DEBUG
	  }
end

.create(filename, noclobber = false, share = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/numru/netcdf.rb', line 106

def NetCDF.create(filename,noclobber=false,share=false)
  case(noclobber)
  when false
	noclobber=NC_CLOBBER
  when true
	noclobber=NC_NOCLOBBER
  else
	raise NetcdfError,"noclobber (2nd argument) must be true or false"
  end
  case(share)
  when false
	share=0
  when true
	share=NC_SHARE
  else
	raise NetcdfError,"share (3rd argument) must be true or false"
  end
  
  cmode=noclobber | share | @@cr_format
  nc_create(filename,cmode)
end

.create_tmp(tmpdir = ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', share = false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/numru/netcdf.rb', line 141

def NetCDF.create_tmp(tmpdir=ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'.', 
 share=false)
   basename = 'temp'
   if $SAFE > 0 and tmpdir.tainted?
	  tmpdir = '.'
   end

   n = 0
   while true
	 begin
tmpname = sprintf('%s/%s%d_%d.nc', tmpdir, basename, $$, n)
unless File.exist?(tmpname)
   netcdf = NetCDF.create(tmpname, true, share)
   ObjectSpace.define_finalizer(netcdf, 
	   NetCDF.clean_tmpfile(tmpname))
   break
end
	 rescue
raise NetcdfError, "cannot generate tempfile `%s'" % tmpname if n >= Max_Try
	 end
	 n += 1
   end
   netcdf
end

.creation_formatObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/numru/netcdf.rb', line 53

def NetCDF.creation_format
  raise("This method is available only for NetCDF >= 4") unless @@nc4
  case @@cr_format
  when 0
    "TRADITIONAL"
  when NC_64BIT_OFFSET
    "64BIT_OFFSET"
  when NC_NETCDF4
    "NETCDF4"
  when (NC_NETCDF4 | NC_CLASSIC_MODEL)
    "NETCDF4_CLASSIC"
  end
end

.creation_format=(cmode) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/numru/netcdf.rb', line 35

def NetCDF.creation_format=(cmode)
  raise("This method is available only for NetCDF >= 4") unless @@nc4
  case cmode
  when  0, nil, NC_CLASSIC_MODEL, /^CLASSIC$/i, NC_FORMAT_CLASSIC
        # classic netcdf ver 3 fmt
    @@cr_format = 0
  when NC_64BIT_OFFSET, /^64BIT_OFFSET$/i, NC_FORMAT_64BIT
    @@cr_format = NC_64BIT_OFFSET
  when NC_NETCDF4, /^NETCDF4$/i, NC_FORMAT_NETCDF4
    @@cr_format = NC_NETCDF4
  when ( NC_NETCDF4 | NC_CLASSIC_MODEL), /^NETCDF4_CLASSIC$/i, NC_FORMAT_NETCDF4_CLASSIC
    # NetCDF4 but disabling new data models
    @@cr_format = NC_NETCDF4 | NC_CLASSIC_MODEL
  else
    raise ArgumentError, "Unsupported creation mode: #{cmod.to_s}"
  end
end

.libversObject

The methods of the NetCDF class



563
564
565
566
567
568
569
# File 'ext/numru/netcdfraw.c', line 563

VALUE
NetCDF_inq_libvers(VALUE mod)
{
  VALUE str;
  str = rb_str_new2(nc_inq_libvers());
  return(str);
}

.nc4?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/numru/netcdf.rb', line 29

def NetCDF.nc4?
  @@nc4
end

.nc_create(filename, cmode) ⇒ Object

rb_define_singleton_method(cNetCDF,“new”,NetCDF_open,2);



1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'ext/numru/netcdfraw.c', line 1056

VALUE
NetCDF_create(VALUE mod,VALUE filename,VALUE cmode)
{
  int ncid;
  int status;
  char* c_filename;
  int c_cmode;
  struct Netcdf *ncfile;
  
  Check_Type(filename,T_STRING);
  SafeStringValue(filename);
  c_filename=RSTRING_PTR(filename);
  Check_Type(cmode,T_FIXNUM);
  c_cmode=NUM2INT(cmode);
  
  status = nc_create(c_filename,c_cmode,&ncid);
  if(status != NC_NOERR) NC_RAISE2(status, c_filename);

  ncfile = NetCDF_init(ncid,c_filename);
  return( Data_Wrap_Struct(cNetCDF,0,NetCDF_free,ncfile) );
}

.nc_open(filename, omode) ⇒ Object



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'ext/numru/netcdfraw.c', line 1032

VALUE
NetCDF_open(VALUE mod,VALUE filename,VALUE omode)     
{
  int status;
  int ncid;
  char* c_filename;
  int c_omode;
  struct Netcdf *ncfile;
  VALUE retval;

  Check_Type(filename,T_STRING);
  SafeStringValue(filename);
  c_filename=RSTRING_PTR(filename);
  Check_Type(omode,T_FIXNUM);
  c_omode=NUM2INT(omode);
  
  status = nc_open(c_filename,c_omode,&ncid);
  if(status !=NC_NOERR){NC_RAISE2(status,c_filename);}

  ncfile = NetCDF_init(ncid,c_filename);
  retval = Data_Wrap_Struct(cNetCDF,0,NetCDF_free,ncfile);
  return( retval );
}

.open(filename, mode = "r", share = false) ⇒ Object Also known as: new



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/numru/netcdf.rb', line 67

def NetCDF.open(filename,mode="r",share=false)
   call_create=false   # false-> nc_open; true->nc_create
   case(mode)
   when "r","rb"                          # read only
	  mode=NC_NOWRITE
   when "w","w+","wb","w+b"               # overwrite if exits
      call_create=true
	  mode=NC_CLOBBER
   when "a","a+","r+","ab","a+b","r+b"    # append if exits
	  if( File.exists?(filename) )
  mode=NC_WRITE
	  else
  call_create=true   #(nonexsitent --> create)
  mode=NC_CLOBBER
	  end
   else
	  raise NetcdfError, "Mode #{mode} is not supported"
   end
   case(share)
   when false
	  share=0
   when true
	  share=NC_SHARE
   else
	  raise NetcdfError, "We can't use the sharing mode you typed"
   end
   omode = mode | share
   if(!call_create)
	  nc_open(filename,omode)
   else
	  nc_create(filename,omode)
   end
end

Instance Method Details

#==(fileb) ⇒ Object

Redifinition of the “==” and “eql?” methods



1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
# File 'ext/numru/netcdfraw.c', line 1985

VALUE 
NetCDF_eql(VALUE filea,VALUE fileb)
{
  struct Netcdf *ncfilea;
  struct Netcdf *ncfileb;
  
  if( rb_obj_is_kind_of(fileb, cNetCDF) ){
      Data_Get_Struct(filea,struct Netcdf,ncfilea);
      Data_Get_Struct(fileb,struct Netcdf,ncfileb);
    
      if(ncfilea->ncid == ncfileb->ncid && 
	 strcmp(ncfilea->name,ncfileb->name)==0){
	  return Qtrue;
      } else {
	  return Qfalse;
      }
  } else {
      return Qfalse;
  }
}

#att(att_name) ⇒ Object



901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'ext/numru/netcdfraw.c', line 901

VALUE
NetCDF_att(VALUE file,VALUE att_name)
{
  int ncid;
  int status;
  int attnump;
  char *c_att_name;
  struct Netcdf *Netcdffile;
  struct NetCDFAtt *Netcdf_att;
  VALUE Attribute;

  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;
  Check_Type(att_name,T_STRING);
  c_att_name=RSTRING_PTR(att_name);
  

  status = nc_inq_attid(ncid,NC_GLOBAL,c_att_name,&attnump);
  if(status != NC_NOERR){
    if(status == NC_ENOTATT){
      return(Qnil);
    }
    else{
      NC_RAISE(status);
    }
  }
  
  Netcdf_att = NetCDF_att_init(ncid,NC_GLOBAL,c_att_name);
  
  Attribute = Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att);
   
  return Attribute;
}

#att_namesObject



262
263
264
265
266
267
268
269
270
# File 'lib/numru/netcdf.rb', line 262

def att_names
  num_att=natts()
  names=[]
  for attnum in 0..num_att-1
	obj_Att=id2att(attnum)    
	names=names+[obj_Att.name]
  end
  return names
end

#cloneObject



511
512
513
514
515
516
517
518
519
520
521
522
# File 'ext/numru/netcdfraw.c', line 511

VALUE
NetCDF_clone(VALUE file)
{
    VALUE clone;
    struct Netcdf *nc1, *nc2;

    Data_Get_Struct(file, struct Netcdf, nc1);
    nc2 = NetCDF_init(nc1->ncid, nc1->name);
    clone = Data_Wrap_Struct(cNetCDF, 0, NetCDF_free, nc2);
    CLONESETUP(clone, file);
    return clone;
}

#closeObject



571
572
573
# File 'ext/numru/netcdfraw.c', line 571

VALUE
NetCDF_close(file)
VALUE file;

#def_dim(dim_name, length) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'ext/numru/netcdfraw.c', line 594

VALUE
NetCDF_def_dim(VALUE file,VALUE dim_name,VALUE length)
{
  char* c_dim_name;
  size_t c_length;
  int ncid;
  int dimidp;
  int status;
  struct Netcdf *Netcdffile;
  struct NetCDFDim *Netcdf_dim;
  VALUE Dimension;
  
  rb_secure(3);
  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  
  Check_Type(dim_name,T_STRING);
  c_dim_name=RSTRING_PTR(dim_name);
  c_length=NUM2UINT(length);
  ncid=Netcdffile->ncid;

  status = nc_def_dim(ncid,c_dim_name,c_length,&dimidp);
  if(status !=NC_NOERR) NC_RAISE(status);

  Netcdf_dim = NetCDF_dim_init(ncid,dimidp);

  Dimension = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
  return Dimension;
}

#def_var(var_name, vartype, dimensions) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'ext/numru/netcdfraw.c', line 776

VALUE
NetCDF_def_var(VALUE file,VALUE var_name,VALUE vartype,VALUE dimensions)
{
  int ncid;
  char *c_var_name;
  static int xtype;
  long c_ndims;
  int varidp;
  int dimidp;
  int i=0;
  int status;
  char *c_dim_name;
  int c_dimids[NC_MAX_DIMS];
  struct Netcdf *Netcdffile;
  struct NetCDFVar *Netcdf_var;
  struct NetCDFDim *Netcdf_dim;
  VALUE Var;

  rb_secure(3);
  Check_Type(var_name,T_STRING);
  Check_Type(dimensions,T_ARRAY);

  c_var_name=RSTRING_PTR(var_name);
  c_ndims=RARRAY_LEN(dimensions);
  
  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;

  if (TYPE(vartype) == T_STRING){
      xtype = natype2nctype(RSTRING_PTR(vartype));     
  } else if (TYPE(vartype) == T_FIXNUM){
      xtype = natypecode2nctype(NUM2INT(vartype));     
  } else {
      rb_raise(rb_eNetcdfError,
	       "type specfication must be by a string or nil");
  }

  for(i=0;i<c_ndims;i++){
    switch(TYPE(RARRAY_PTR(dimensions)[c_ndims-1-i])){
    case T_STRING:
      Check_Type(RARRAY_PTR(dimensions)[c_ndims-1-i],T_STRING);
      c_dim_name=StringValueCStr(RARRAY_PTR(dimensions)[c_ndims-1-i]);
      status=nc_inq_dimid(ncid,c_dim_name,&dimidp);
      if(status != NC_NOERR) NC_RAISE(status);
      c_dimids[i]=dimidp;
      break;
    case T_DATA:
      Data_Get_Struct(RARRAY_PTR(dimensions)[c_ndims-1-i],struct NetCDFDim,Netcdf_dim);
      c_dimids[i]=Netcdf_dim->dimid;
      break;
    default:
      rb_raise(rb_eNetcdfError, "No such object of the netCDF dimension class.");
    }
  }
  
  status = nc_def_var(ncid,c_var_name,xtype,c_ndims,c_dimids,&varidp);
  if(status != NC_NOERR) NC_RAISE(status);
  
  Netcdf_var = NetCDF_var_init(ncid,varidp,file);
  
  Var=Data_Wrap_Struct(cNetCDFVar,nc_mark_obj,NetCDF_var_free,Netcdf_var);
  return Var;
}

#def_var_with_dim(name, vartype, shape_ul0, dimnames) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/numru/netcdf.rb', line 171

def def_var_with_dim(name, vartype, shape_ul0, dimnames)
   # Same as def_var but defines dimensions first if needed.
   # Use zero in shape to define an unlimited dimension.
   if (shape_ul0.length != dimnames.length ) then
	  raise ArgumentError, 'lengths of shape and dimnames do not agree'
   end
   dims = []
   dimnames.each_index{ |i|
	  dim = self.dim( dimnames[i] )
	  if ( dim != nil ) then
  # dim exists --> check the length
  if (shape_ul0[i] != dim.length_ul0 ) then
		raise ArgumentError, "dimension length do not agree: #{i}th dim: "+\
		"#{shape_ul0[i]} and #{dim.length_ul0}"
  end
  dims.push(dim)
	  else
  # dim does not exist --> define it
  dims.push( def_dim( dimnames[i], shape_ul0[i] ) )
	  end
   }
   def_var(name, vartype, dims)
end

#define_mode?Boolean

Returns:

  • (Boolean)


1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'ext/numru/netcdfraw.c', line 1001

VALUE 
NetCDF_whether_in_define_mode(VALUE file)
{
  /* returns true if the NetCDF object is currently in the define mode,
             false if in the data mode, and
	     nil if else (possibly the file is read-only, or some other
	     error occurred)
  */
  int ncid;
  int status;
  struct Netcdf *Netcdffile;

  rb_secure(3);
  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;
  status = nc_redef(ncid);
  if(status == NC_EINDEFINE){
    return Qtrue;
  } else if(status == NC_NOERR) {
    /* was in the data mode --> recover the data mode and report false */
    status = nc_enddef(ncid);
    if(status == NC_NOERR) {
      return Qfalse;
    } else {
      return Qnil;
    }
  } else {
    return Qnil;
  }
}

#dim(dim_name) ⇒ Object



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'ext/numru/netcdfraw.c', line 840

VALUE
NetCDF_dim(VALUE file,VALUE dim_name)
{
  int ncid;
  char *c_dim_name;
  int dimidp;
  int status;
  struct Netcdf *Netcdffile;
  struct NetCDFDim *Netcdf_dim;
  VALUE Dimension;

  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;
  Check_Type(dim_name,T_STRING);
  c_dim_name=RSTRING_PTR(dim_name);
  
  status = nc_inq_dimid(ncid,c_dim_name,&dimidp);
  if(status !=NC_NOERR){
    if(status == NC_EBADDIM){ 
      return(Qnil);  /*2003/08/27 back to orig (from changes on 2003/02/03)*/
    } else{
      NC_RAISE(status);
    }
  }

  Netcdf_dim=NetCDF_dim_init(ncid,dimidp);
  
  Dimension = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
  return Dimension;
}

#dim_namesObject



242
243
244
245
246
247
248
249
250
# File 'lib/numru/netcdf.rb', line 242

def dim_names
  num_dim=ndims()
  names=[]
  for dimid in 0..num_dim-1
	obj_Dim=id2dim(dimid)    
	names=names+[obj_Dim.name]
  end
  return names
end

#dims(names = nil) ⇒ Object

return all if names==nil



220
221
222
223
224
225
226
227
228
229
# File 'lib/numru/netcdf.rb', line 220

def dims( names=nil )   # return all if names==nil
   if names == nil
	  dims = (0..ndims()-1).collect{|dimid| id2dim(dimid)}
   else
	  raise TypeError, "names is not an array" if ! names.is_a?(Array)
	  dims = names.collect{|name| dim(name)}
	  raise ArgumentError, "One or more dimensions do not exist" if dims.include?(nil)
   end
   dims
end

#each_attObject



212
213
214
215
216
217
218
# File 'lib/numru/netcdf.rb', line 212

def each_att
  num_att=natts()
  for attnum in 0..num_att-1
	obj_Att=id2att(attnum)
	 yield(obj_Att)
  end
end

#each_dimObject

Iterators:



196
197
198
199
200
201
202
# File 'lib/numru/netcdf.rb', line 196

def each_dim
  num_dim=ndims()    
  for dimid in 0..num_dim-1
	obj_Dim=id2dim(dimid)
	 yield(obj_Dim)
  end
end

#each_varObject



204
205
206
207
208
209
210
# File 'lib/numru/netcdf.rb', line 204

def each_var
  num_var=nvars()
  for varid in 0..num_var-1
	obj_Var=id2var(varid)
	yield(obj_Var)
  end
end

#enddefObject



979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'ext/numru/netcdfraw.c', line 979

VALUE 
NetCDF_enddef(VALUE file)
{
  int ncid;
  int status;
  struct Netcdf *Netcdffile;

  rb_secure(3);
  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;
  status = nc_enddef(ncid);
  if(status !=NC_NOERR){ 
    if(status == NC_ENOTINDEFINE){
      return Qnil;
    }
    else{
      NC_RAISE(status);
    }
  }
  return Qtrue;
}

#fill(mode) ⇒ Object



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'ext/numru/netcdfraw.c', line 934

VALUE
NetCDF_fill(VALUE file,VALUE mode)
{
  int ncid;
  int status;
  struct Netcdf *Netcdffile;
  int old_modep;
  
  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid = Netcdffile->ncid;
  if(mode==Qfalse){
    status = nc_set_fill(ncid,NC_NOFILL,&old_modep);
    if(status != NC_NOERR) NC_RAISE(status);
  }
  else if(mode == Qtrue){
    status = nc_set_fill(ncid,NC_FILL,&old_modep);
    if(status != NC_NOERR) NC_RAISE(status);
  }
  else
    rb_raise(rb_eNetcdfError,"Usage:self.fill(true) or self.fill(false)");
  return Qnil;
}

#formatObject



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'ext/numru/netcdfraw.c', line 1159

VALUE 
NetCDF_format(VALUE file)
{
  int ncid;
  int formatp;
  int status;
  VALUE Integer;
  struct Netcdf *ncfile;

  Data_Get_Struct(file,struct Netcdf,ncfile);
  ncid=ncfile->ncid;
  status=nc_inq_format(ncid,&formatp);
  if(status != NC_NOERR) NC_RAISE (status);
  Integer = INT2NUM(formatp);
  /* one of NC_FORMAT_CLASSIC (1), NC_FORMAT_64BIT (2), NC_FORMAT_NETCDF4 (3),
     NC_FORMAT_NETCDF4_CLASSIC (4)
   */
  return Integer;
}

#inspectObject



272
273
274
# File 'lib/numru/netcdf.rb', line 272

def inspect
  "NetCDF:"+path
end

#nattsObject



1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'ext/numru/netcdfraw.c', line 1111

VALUE 
NetCDF_natts(VALUE file)
{
  int ncid;
  int nattsp;
  int status;
  VALUE Integer;
  struct Netcdf *ncfile;

  Data_Get_Struct(file,struct Netcdf,ncfile);
  ncid=ncfile->ncid;
  status=nc_inq_natts(ncid,&nattsp);
  if(status != NC_NOERR) NC_RAISE (status);
  Integer = INT2NUM(nattsp);
  return Integer;
}

#ndimsObject



1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'ext/numru/netcdfraw.c', line 1078

VALUE 
NetCDF_ndims(VALUE file)
{
  int ncid;
  int ndimsp;
  VALUE Integer;
  int status;
  struct Netcdf *ncfile;

  Data_Get_Struct(file,struct Netcdf,ncfile);
  ncid=ncfile->ncid;
  status = nc_inq_ndims(ncid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE (status);
  Integer = INT2NUM(ndimsp);
  return Integer;
}

#nvarsObject



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# File 'ext/numru/netcdfraw.c', line 1095

VALUE 
NetCDF_nvars(VALUE file)
{
  int ncid;
  int nvarsp;
  int status;
  VALUE Integer;
  struct Netcdf *ncfile;
  Data_Get_Struct(file,struct Netcdf,ncfile);
  ncid=ncfile->ncid;
  status = nc_inq_nvars(ncid,&nvarsp);
  if(status != NC_NOERR) NC_RAISE (status);
  Integer = INT2NUM(nvarsp);
  return Integer;
}

#pathObject



1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
# File 'ext/numru/netcdfraw.c', line 1196

VALUE
NetCDF_path(VALUE file)
{
  char *path;
  struct Netcdf *ncfile;
  
  Data_Get_Struct(file,struct Netcdf,ncfile);
  path=ncfile->name;
  return(rb_str_new2(path));
}

#put_att(attname, val, atttype = nil) ⇒ Object



167
168
169
# File 'lib/numru/netcdf.rb', line 167

def put_att(attname,val,atttype=nil)
   put_attraw(attname,val,atttype)
end

#put_attrawObject

atttype: nil or a String (“string”,“int”,etc). If nil,

the type of attribute is determined from the type of value


739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'ext/numru/netcdfraw.c', line 739

VALUE
NetCDF_put_att(VALUE file,VALUE att_name,VALUE value,VALUE atttype)
     /*
      * atttype: nil or a String ("string","int",etc). If nil,
      *          the type of attribute is determined from the type of value
      */
{
    struct Netcdf *ncfile;
    char *name;

    rb_secure(3);
    Data_Get_Struct(file,struct Netcdf,ncfile);
    Check_Type(att_name,T_STRING);
    name = RSTRING_PTR(att_name);

    return( NetCDF_put_att__(ncfile->ncid, name, value, atttype, NC_GLOBAL) );
}

#redefObject



957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# File 'ext/numru/netcdfraw.c', line 957

VALUE
NetCDF_redef(VALUE file)
{
  int ncid;
  int status;
  struct Netcdf *Netcdffile;

  rb_secure(3);
  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;
  status = nc_redef(ncid);
  if(status !=NC_NOERR){
    if(status == NC_EINDEFINE){
      return Qnil;
    }
    else{
      NC_RAISE(status);
    }
  }
  return Qtrue;
}

#syncObject



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'ext/numru/netcdfraw.c', line 1181

VALUE
NetCDF_sync(VALUE file)
{
  int ncid;
  int status;
  struct Netcdf *ncfile;

  rb_secure(3);
  Data_Get_Struct(file,struct Netcdf,ncfile);
  ncid=ncfile->ncid;
  status = nc_sync(ncid);
  if(status !=NC_NOERR) NC_RAISE (status);
  return Qnil;
}

#unlimitedObject



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# File 'ext/numru/netcdfraw.c', line 1128

VALUE
NetCDF_unlimited(VALUE file)
{
  int ncid;
  int unlimdimidp;
  int status;
  struct Netcdf *ncfile;
  struct NetCDFDim *Netcdf_dim;
  VALUE Dimension;

  Data_Get_Struct(file,struct Netcdf,ncfile);
  ncid=ncfile->ncid;
  status=nc_inq_unlimdim(ncid,&unlimdimidp);
  if(status !=NC_NOERR) NC_RAISE(status);
  
  Netcdf_dim = NetCDF_dim_init(ncid,unlimdimidp);
  
  /* If unlimdimidp=-1,No unlimited dimension is defined in the netCDF dataset */ 
  if(unlimdimidp != -1)
    {
      Dimension = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
      return Dimension;
    }
  else
    {
      return Qnil;
    }
}

#var(var_name) ⇒ Object



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# File 'ext/numru/netcdfraw.c', line 871

VALUE  
NetCDF_var(VALUE file,VALUE var_name)
{  
  int ncid;
  int status;
  int varidp;
  char *c_var_name;
  struct Netcdf *Netcdffile;
  struct NetCDFVar *Netcdf_var;
  VALUE Variable;

  Data_Get_Struct(file,struct Netcdf,Netcdffile);
  ncid=Netcdffile->ncid;
  Check_Type(var_name,T_STRING);
  c_var_name=RSTRING_PTR(var_name);
  
  status=nc_inq_varid(ncid,c_var_name,&varidp);
  if(status != NC_NOERR){
    if(status == NC_ENOTVAR){
      return(Qnil); /*2003/08/27 back to orig (from changes on 2003/02/03)*/
    } else{ 
      NC_RAISE(status);
    }
  }
  
  Netcdf_var = NetCDF_var_init(ncid,varidp,file);
  Variable = Data_Wrap_Struct(cNetCDFVar,nc_mark_obj,NetCDF_var_free,Netcdf_var);
  return Variable;
}

#var_namesObject



252
253
254
255
256
257
258
259
260
# File 'lib/numru/netcdf.rb', line 252

def var_names
  num_var=nvars()
  names=[]
  for varid in 0..num_var-1
	obj_Var=id2var(varid)
	names=names+[obj_Var.name]
  end
  return names
end

#vars(names = nil) ⇒ Object

return all if names==nil



231
232
233
234
235
236
237
238
239
240
# File 'lib/numru/netcdf.rb', line 231

def vars( names=nil )   # return all if names==nil
   if names == nil
	  vars = (0..nvars()-1).collect{ |varid| id2var(varid) }
   else
	  raise TypeError, "names is not an array" if ! names.is_a?(Array)
	  vars = names.collect{|name| var(name)}
	  raise ArgumentError, "One or more variables do not exist" if vars.include?(nil)
   end
   vars
end