FUNCTION scan_grib_recstart, num ; compile_opt idl2, strictarrsubs ; infofile = fstat(num) ; minimum size of one record minisize = 8L+28L+4L+4L maxoffset = infofile.size-minisize ; start = 0L offset = 0L previousrecsize = 0L WHILE offset LT maxoffset DO BEGIN ; Every record must begin with 'GRIB'. ; However, their is no rule to define the space between 2 records. ; (1) we try space = previousrecsize MOD 8, because for echam outputs, ; the total size of the records is rounded to modulo 8 addoff = 8 - (previousrecsize MOD 8) offset = offset+addoff IF offset GE maxoffset THEN GOTO, out a = assoc(num, bytarr(4, /nozero), offset) typefile = string(a[0]) IF typefile NE 'GRIB' THEN offset = offset-addoff ; (2) we try space = previousrecsize MOD 120, because for ecmwf ; outputs, the total size of the records is rounded to modulo 120 addoff = 120 - (previousrecsize MOD 120) IF typefile NE 'GRIB' THEN BEGIN offset = offset+addoff IF offset GE maxoffset THEN GOTO, out a = assoc(num, bytarr(4, /nozero), offset) typefile = string(a[0]) IF typefile NE 'GRIB' THEN offset = offset-addoff ENDIF ; (3) we try space = 0 IF typefile NE 'GRIB' THEN BEGIN a = assoc(num, bytarr(4, /nozero), offset) typefile = string(a[0]) ENDIF ; (4) we try any value for space IF typefile NE 'GRIB' THEN BEGIN REPEAT BEGIN CASE 1 OF array_equal((a[0])[3 ], byte('G' )):offset = offset+3 array_equal((a[0])[2:3], byte('GR' )):offset = offset+2 array_equal((a[0])[1:3], byte('GRI')):offset = offset+1 else:offset = offset+4 ENDCASE IF offset GE maxoffset THEN GOTO, out a = assoc(num, bytarr(4, /nozero), offset) typefile = string(a[0]) ENDREP UNTIL typefile EQ 'GRIB' ENDIF ; start = [start, offset] ; a = assoc(num, bytarr(1, /nozero), offset+4) recsize = bit2int([binary(a[0]), binary(a[1]), binary(a[2])]) offset = offset+recsize previousrecsize = recsize ; ENDWHILE ; out: ; RETURN, start[1:n_elements(start)-1] END