Class: NumRu::GPhys::Grib::GribBDS

Inherits:
Object
  • Object
show all
Includes:
NumRu::GPhys::GribUtils
Defined in:
lib/numru/gphys/grib.rb

Overview

end definition of class GribBMS

Instance Method Summary collapse

Instance Method Details

#bit_maps?Boolean

Returns:

  • (Boolean)


1176
1177
1178
# File 'lib/numru/gphys/grib.rb', line 1176

def bit_maps?
  return flag7 && flag7==1
end

#complex?Boolean

Returns:

  • (Boolean)


1127
1128
1129
# File 'lib/numru/gphys/grib.rb', line 1127

def complex?
  return flag2==1
end

#constant?Boolean

Returns:

  • (Boolean)


1187
1188
1189
# File 'lib/numru/gphys/grib.rb', line 1187

def constant?
  return flag8 && flag8==0
end

#efactorObject



1204
1205
1206
1207
1208
1209
# File 'lib/numru/gphys/grib.rb', line 1204

def efactor
  return @efactor if @efactor
  @sgm.file.seek(@pos+1, ::IO::SEEK_SET)
  @efactor = 2.0**str2int2( @sgm.file.read(2) )
  return @efactor
end

#float?Boolean

Returns:

  • (Boolean)


1136
1137
1138
# File 'lib/numru/gphys/grib.rb', line 1136

def float?
  return flag3==0
end

#getObject



1347
1348
1349
# File 'lib/numru/gphys/grib.rb', line 1347

def get
  uint2str(length,3) << @bds
end

#grid?Boolean

Returns:

  • (Boolean)


1100
1101
1102
# File 'lib/numru/gphys/grib.rb', line 1100

def grid?
  return flag1==0
end

#integer?Boolean

Returns:

  • (Boolean)


1145
1146
1147
# File 'lib/numru/gphys/grib.rb', line 1145

def integer?
  return flag3==1
end

#lengthObject



1093
1094
1095
1096
1097
1098
1099
# File 'lib/numru/gphys/grib.rb', line 1093

def length
  if @pos
    return @length
  else
    return @bds.length+3
  end
end

#matrix?Boolean

Returns:

  • (Boolean)


1165
1166
1167
# File 'lib/numru/gphys/grib.rb', line 1165

def matrix?
  return flag6 && flag6==1
end

#offsetObject



1198
1199
1200
1201
1202
1203
# File 'lib/numru/gphys/grib.rb', line 1198

def offset
  return @offset if @offset
  @sgm.file.seek(@pos+3, ::IO::SEEK_SET)
  @offset = float_value(@sgm.file.read(4))
  return @offset
end

#set_bit_map(l) ⇒ Object



1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/numru/gphys/grib.rb', line 1179

def set_bit_map(l)
  @oct14 = oct14&0xbf
  @oct14 += 64 if l
  @oct4 = (oct4&0xef)+16
  @bds[0,1] = uint2str(@oct4,1)
  @bds[10,1] = uint2str(@oct14,1)
  return l
end

#set_complex(l) ⇒ Object



1130
1131
1132
1133
1134
1135
# File 'lib/numru/gphys/grib.rb', line 1130

def set_complex(l)
  @oct4 = oct4&0xbf
  @oct4 += 64 if l
  @bds[0,1] = uint2str(@oct4,1)
  return l
end

#set_constant(l) ⇒ Object



1190
1191
1192
1193
1194
1195
1196
1197
# File 'lib/numru/gphys/grib.rb', line 1190

def set_constant(l)
  @oct14 = oct14&0xdf
  @oct14 += 32 unless l
  @oct4 = (oct4&0xef)+16
  @bds[0,1] = uint2str(@oct4,1)
  @bds[10,1] = uint2str(@oct14,1)
  return l
end

#set_float(l) ⇒ Object



1139
1140
1141
1142
1143
1144
# File 'lib/numru/gphys/grib.rb', line 1139

def set_float(l)
  @oct4 = oct4&0xdf
  @oct4 += 32 unless l
  @bds[0,1] = uint2str(@oct4,1)
  return l
end

#set_grid(l) ⇒ Object



1103
1104
1105
1106
1107
1108
# File 'lib/numru/gphys/grib.rb', line 1103

def set_grid(l)
  @oct4 = oct4&0x7f
  @oct4 += 128 unless l
  @bds[0,1] = uint2str(@oct4,1)
  return l
end

#set_integer(l) ⇒ Object



1148
1149
1150
1151
1152
1153
# File 'lib/numru/gphys/grib.rb', line 1148

def set_integer(l)
  @oct4 = oct4&0xdf
  @oct4 += 32 if l
  @bds[0,1] = uint2str(@oct4,1)
  return l
end

#set_matrix(l) ⇒ Object



1168
1169
1170
1171
1172
1173
1174
1175
# File 'lib/numru/gphys/grib.rb', line 1168

def set_matrix(l)
  @oct14 = oct14&0x7f
  @oct14 += 128 if l
  @oct4 = (oct4&0xef)+16
  @bds[0,1] = uint2str(@oct4,1)
  @bds[10,1] = uint2str(@oct14,1)
  return l
end

#set_simple(l) ⇒ Object



1121
1122
1123
1124
1125
1126
# File 'lib/numru/gphys/grib.rb', line 1121

def set_simple(l)
  @oct4 = oct4&0xbf
  @oct4 += 64 unless l
  @bds[0,1] = uint2str(@oct4,1)
  return l
end

#set_single(l) ⇒ Object



1157
1158
1159
1160
1161
1162
1163
1164
# File 'lib/numru/gphys/grib.rb', line 1157

def set_single(l)
  @oct14 = oct14&0x7f
  @oct14 += 128 unless l
  @oct4 = (oct4&0xef)+16
  @bds[0,1] = uint2str(@oct4,1)
  @bds[10,1] = uint2str(@oct14,1)
  return l
end

#set_spectrum(l) ⇒ Object



1112
1113
1114
1115
1116
1117
# File 'lib/numru/gphys/grib.rb', line 1112

def set_spectrum(l)
  @oct4 = oct4&0x7f
  @oct4 += 128 if l
  @bds[0,1] = uint2str(@oct4,1)
  return l
end

#set_value(val) ⇒ Object



1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/numru/gphys/grib.rb', line 1309

def set_value(val)
  val = val.reshape!(val.length)
  if NArrayMiss===val
    val = val.get_array![val.get_mask!.where]
  end
  dfact = @sgm.pds.dfact
  val = val*10**dfact
  ref = val.min
  val = val-ref
  max = val.max
  nbits = 16
  e =  (Math::log(max)/Math::log(2)).ceil-nbits
  val = val*2**(-e)
  if (nbits%8) == 0
    nstep = 8
  elsif (nbits%4) == 0
    nstep = 4
  elsif (nbits%2) == 0
    nstep == 2
  else
    nstep == 1
  end
  str, bu = data2str(val.to_type(NArray::INT),nstep,nbits)
  @oct4 = (@oct4&15)+bu*16
  @bds[0,1] = uint2str(@oct4,1)
  @bds[1,2] = int2str(e,2)
  s = (ref<0) ? -1 : 1
  ref = ref.abs
  a = (Math::log(ref)/Math::log(16)).ceil+64
  a<0 && raise("invalid range")
  a = 127 if a>127
  b = ref*2**24*16**(64-a)
  a = a*s
  @bds[3,4] = int2str(a,1) << uint2str(b,3)
  @bds[7,1] = uint2str(nbits,1)
  @bds = @bds[0,8] << str
  @sgm.is.update_total_length
end

#simple?Boolean

Returns:

  • (Boolean)


1118
1119
1120
# File 'lib/numru/gphys/grib.rb', line 1118

def simple?
  return flag2==0
end

#single?Boolean

Returns:

  • (Boolean)


1154
1155
1156
# File 'lib/numru/gphys/grib.rb', line 1154

def single?
  return flag6 && flag6==0
end

#spectrum?Boolean

Returns:

  • (Boolean)


1109
1110
1111
# File 'lib/numru/gphys/grib.rb', line 1109

def spectrum?
  return flag1==1
end

#value(shape, index = nil) ⇒ Object



1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'lib/numru/gphys/grib.rb', line 1210

def value(shape,index=nil)
  nbits_unuse = oct4&15
  @sgm.file.seek(@pos+7, ::IO::SEEK_SET)
  nbits_pack = @sgm.file.read(1).to_uint1
  if (nbits_pack%8) == 0
    nstep = 8
  elsif (nbits_pack%4) == 0
    nstep = 4
  elsif (nbits_pack%2) == 0
    nstep = 2
  else
    nstep = 1
  end
  r = offset
  e = efactor
  map = @sgm.bms.map
  nmiss =  map ? map.count_false : 0
  if flag4==0
    nlen = ((length-3-8)*8-nbits_unuse)/nbits_pack
    nlon,nlat = shape
    (nlon*nlat)==nlen+nmiss || raise("length is not collect")
    if index
      index = index.collect{|el| Fixnum===el ? el..el : el}
      il = index[0]
      i0 = il.first
      i1 = il.last
      i0 = i0 < 0 ? nlon+i0 : i0
      i1 = i1 < 0 ? nlon+i1 : i1
      index[0] = il.exclude_end? ? i0...i1 : i0..i1
      il = index[1]
      i0 = il.first
      i1 = il.last
      i0 = i0 < 0 ? nlat+i0 : i0
      i1 = i1 < 0 ? nlat+i1 : i1
      index[1] = il.exclude_end? ? i0...i1 : i0..i1
      #j = nil
      #i = nil
      if map
        var = NArrayMiss.sfloat(nlon,nlat)
        map.reshape!(nlon,nlat)
        mask = map
        nd = mask.count_true
        wh = mask.where
        if wh.length>0
          var[wh] = @sgm.mbio.read2D(@pos+8, nbits_pack, nd, 1, 0, nd-1, 1, 0, 0, 1, nil, nil, e, r).reshape!(nd)
        end
        var = var[*index]
=begin
        index[1].each{|j|
          jj = j-index[1].first
          sb = j*nlon+index[0].first
          sb = sb-map[true,0..jj-1].count_false if jj>0
          sb = sb*nbits_pack
          eb = sb+mask[true,j].count_true*nbits_pack
          if sb!=eb
            var[mask[true,j].where,jj] = get_value(@sgm.file,@pos+8,sb,eb,nbits_pack,nstep)
          end
        }
=end
      else
        var = @sgm.mbio.read2D(@pos+8, nbits_pack, nlon, nlat, index[0].first, index[0].end, 1, index[1].first, index[1].end, 1, nil, nil, e, r)
=begin
        sb = index[1].first*nlon*nbits_pack
        eb = (index[1].end+1)*nlon*nbits_pack
        var = get_value(@sgm.file,@pos+8,sb,eb,nbits_pack,nstep)
        var.reshape!(nlon, var.length/nlon)
        var = var[index[0],true]
=end
      end
    else
      if map
        eb = nlen*nbits_pack
        var = NArrayMiss.sfloat(nlon,nlat)
#              var[map.where] = get_value(@sgm.file,@pos+8,0,eb,nbits_pack,nstep)
        nd = map.count_true
        var[map.where] = @sgm.mbio.read2D(@pos+8, nbits_pack, nd, 1, 0, nd-1, 1, 0, 0, 1, nil, nil, e, r).reshape!(nd)
      else
=begin
        eb = nlen*nbits_pack
        var = get_value(@sgm.file,@pos+8,0,eb,nbits_pack,nstep)
        var.reshape!(nlon,nlat)
=end
        var = @sgm.mbio.read2D(@pos+8, nbits_pack, nlon, nlat, 0, nlon-1, 1, 0, nlat-1, 1, nil, nil, e, r)
      end
    end
#          var = var*e+r
    return var
  else
    raise "not defined yet"
    n1 = @file.read(2).to_uint2
    @sgm.file.seek(1, ::IO::SEEK_CUR)
    str = @sgm.file.read(6)
    n2 = str.to_uint2
    p1 = str.to_uint2(2)
    p2 = str.to_uint2(4) # @bds[15..16]
    #        width = str2uint( @bds[18..??] )
    #        bitmap = str2uint( @bds[??+1..n1-4] )
  end
end