Class: NumRu::NetCDF

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

Constant Summary collapse

Max_Try =
100
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)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_tmpfile(path) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/numru/netcdf.rb', line 72

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/numru/netcdf.rb', line 49

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
  nc_create(filename,cmode)
end

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/numru/netcdf.rb', line 84

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

.nc_create(filename, cmode) ⇒ Object

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



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'ext/netcdfraw.c', line 1044

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

The methods of the NetCDF class



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'ext/netcdfraw.c', line 1020

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/numru/netcdf.rb', line 10

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



1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
# File 'ext/netcdfraw.c', line 1842

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



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'ext/netcdfraw.c', line 889

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



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

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



506
507
508
509
510
511
512
513
514
515
516
517
# File 'ext/netcdfraw.c', line 506

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



558
559
560
# File 'ext/netcdfraw.c', line 558

VALUE
NetCDF_close(file)
VALUE file;

#def_dim(dim_name, length) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'ext/netcdfraw.c', line 581

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(4);
  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



763
764
765
766
767
768
769
770
771
772
773
774
775
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
# File 'ext/netcdfraw.c', line 763

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(4);
  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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/numru/netcdf.rb', line 114

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)


989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'ext/netcdfraw.c', line 989

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(4);
  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



828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'ext/netcdfraw.c', line 828

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



185
186
187
188
189
190
191
192
193
# File 'lib/numru/netcdf.rb', line 185

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



163
164
165
166
167
168
169
170
171
172
# File 'lib/numru/netcdf.rb', line 163

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



155
156
157
158
159
160
161
# File 'lib/numru/netcdf.rb', line 155

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:



139
140
141
142
143
144
145
# File 'lib/numru/netcdf.rb', line 139

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

#each_varObject



147
148
149
150
151
152
153
# File 'lib/numru/netcdf.rb', line 147

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

#enddefObject



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'ext/netcdfraw.c', line 967

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

  rb_secure(4);
  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



922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'ext/netcdfraw.c', line 922

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;
}

#inspectObject



215
216
217
# File 'lib/numru/netcdf.rb', line 215

def inspect
  "NetCDF:"+path
end

#nattsObject



1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
# File 'ext/netcdfraw.c', line 1099

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



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'ext/netcdfraw.c', line 1066

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



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'ext/netcdfraw.c', line 1083

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



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'ext/netcdfraw.c', line 1160

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



110
111
112
# File 'lib/numru/netcdf.rb', line 110

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


726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'ext/netcdfraw.c', line 726

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(4);
    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



945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'ext/netcdfraw.c', line 945

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

  rb_secure(4);
  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



1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'ext/netcdfraw.c', line 1145

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

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

#unlimitedObject



1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'ext/netcdfraw.c', line 1116

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



859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'ext/netcdfraw.c', line 859

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



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

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



174
175
176
177
178
179
180
181
182
183
# File 'lib/numru/netcdf.rb', line 174

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