-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetSect.java.full
526 lines (459 loc) · 16.1 KB
/
GetSect.java.full
1
2
3
4
5
6
7
8
9
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* File: getsect.c - A fast interface to the old GenieII get routines.
* Author: Freddie Akeroyd, ISIS computer group
*
* Converted to Java March 2004 Chris M. Bouzek
*/
package ISIS.JLibGet;
import java.io.*;
/**
* Basically, we just read the whole file into "file_buffer" and then catch
* calls to the old FORTRAN GETSECT routine
*/
public class GetSect {
//~ Static fields/initializers ***********************************************
/*#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif*/
//private static final int AD_TCB = 26; // from crptsect.def
//private static final int RIO_OPERATION_FAILED = 0;
//private static final int RIO_OPERATION_SUCCESSFUL = 1;
//"magic" number-it is the largest positive value a 32 bit integer can hold, and we'll
//need it for the buffer size checking
private static final int MAX_BUFFER_SIZE = 2147483647;
//~ Instance fields **********************************************************
//ummm...??
/*#if !defined(USE_OPEN) && !defined(USE_FOPEN)
#define USE_FOPEN
#endif*/
private boolean dae_access = false; // are we reading from ISIS DAE?
private boolean crpt_access = false; // are we reading from ISIS CRPT?
private boolean readWholeFile = true;
private String file_name; // Name of the file to buffer (passed from fortran)
//static char *file_buffer = NULL; // Where we store "file_name"
private byte[] file_buffer;
//what the heck?
private long file_size = 0; // How big "file_name" is
private long file_buffer_size = 0; // How big our buffer is ( file_size <= file_buffer_size )
//static long io_block_size = 0; // Optimal read block size for the file system containing file_name
private long buffer_iunit = 0; // Fortran unit associated with "file_buffer"
private RandomAccessFile file_fd = null;
private int FOPEN_BUFFER_SIZE = 0;
//~ Methods ******************************************************************
/**
* DOCUMENT ME!
*
* @param istart DOCUMENT ME!
* @param ilong DOCUMENT ME!
* @param ivalue DOCUMENT ME!
* @param iunit DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public int getSect( int istart, int ilong, int[] ivalue, int iunit ) {
/*if (dae_access)
{
return getsect_dae (istart, ilong, ivalue, iunit);
}
else if (crpt_access)
{
return getsect_crpt (istart, ilong, ivalue, iunit);
}
else
{*/
return getsectFile( istart, ilong, ivalue );
/*}
return 0;*/
}
/**
* 0 on success, 1 on failure
*/
public int fastGetInit( String name, int iUnit ) {
String error_message;
String cds_name;
//struct stat stat_buffer;
int first_call = 1;
/* stat_t stat_buffer; */
int i;
/* stat_t stat_buffer; */
//int ilen;
/* stat_t stat_buffer; */
//int icrpt;
//int daepoint;
//int *ret_addr[2];
//String sect_name;
//String inst_name = DataSetTools.util.SharedData.getProperty("INST_NAME");
String inst_name = null;
//String temp_str;
if( inst_name == null ) {
inst_name = "unknown";
}
if( first_call == 1 ) {
first_call = 0;
//I just picked this number (2^16) because I felt like it
FOPEN_BUFFER_SIZE = 65536;
}
/* First null terminate our FORTRAN string */
for( i = 119; name.charAt( i ) == ' '; i-- ) {
;
}
if( i < 0 ) {
FErrorAddDummy.fErrorAdd( "FASTGET_INIT", "null file name given", "" );
return 1;
}
//copy i + 1 characters
file_name = name.substring( 0, i );
/*#ifdef __VMS_POSIX
const char *trans_file_name = _translate (file_name, _TO_PATHNAME);
if (trans_file_name != NULL)
{
strcpy (file_name, trans_file_name);
}
#endif __VMS_POSIX */
buffer_iunit = iUnit;
/* Now attempt to open the file and get some information about it */
dae_access = crpt_access = false;
//crpt_address = NULL;
if( file_name.startsWith( "/.../" ) || file_name.startsWith( "/.:/" ) ) {
i = file_name.length( );
cds_name = file_name;
if( file_name.substring( i - 4 ).equals( "_dae" ) ) {
dae_access = true;
} else if( file_name.substring( i - 5 ).equals( "_crpt" ) ) {
crpt_access = true;
cds_name = cds_name.substring( 0, i - 5 );
cds_name += "_dae"; // we only export _dae to CDS, so fudge name
}
}
if( dae_access || crpt_access ) {
// name is of the form /.:/servers/inst_dae or /.:/servers/inst_crpt
/*int serverIndex =cds_name.indexOf( "/servers/");
if ( serverIndex< 0 )
{
System.out.println(
"DAE name " + cds_name +" does not follow a recognised format");
FErrorAddDummy.fErrorAdd ("FASTGET_INIT", error_message, "");
return 1;
} else {
temp_str = cds_name.substring( serverIndex );
}
CLOSE_DAE ();
if (open_dae_cds (cds_name) == RIO_OPERATION_FAILED)
{
sprintf (error_message, "Failed to open DAE at \"%s\"", cds_name);
error_add ("FASTGET_INIT", error_message, "");
return 1;
}
serverIndex += "/servers/".length(); //move past /servers/
temp_str = temp_str( serverIndex );
ilen = temp_str.length() - 4; // to knock off "_dae" suffix
sect_name = "G_";
String upperTempStr = temp_str.substring( 0, iLen ).toUpperCase( );
char[] sectNameArr = sect_name.toCharArray();
//I just know this will run off the end of the array...
for (i = 0; i < ilen; i++)
{
sectNameArr[2 + i] = upperTempStr.charAt(i);
}
if( dae_access ){
i = AD_TCB;
}else{
i = 0;
}
return 1;
//crpt_address = (char *) ret_addr[0];
crpt_size = ret_addr[1] - ret_addr[0] + 1;
System.out.println ("GETSECT: Mapping to portion of CRPT size = " + crpt_size + " words\n");
icrpt = ret_addr[0];
if (icrpt[20] == 1) test VER1
{
start_of_data_section = icrpt[26]; // DATAPOINT - section addresses start at offset 21
}
else
{
start_of_data_section = icrpt[27]; // DATAPOINT - section addresses start at offset 21
}
daepoint = icrpt[24]; // DAE section of CRPT, IFORMAT(4) in io.f
if (daepoint < crpt_size)
{
dae_word_length = icrpt[daepoint];
gprintdf ("GETSECT: DAE word length = %d bytes\n", dae_word_length);
}
else
{
gprintef ("GETSECT: error in getting DAE word length\n");
}
gprintdf ("GETSECT: data section starts at %d words\n",
start_of_data_section);*/
//not supporting DAE or CRPT access yet
return 1;
} else {
RandomAccessFile fd;
try {
fd = new RandomAccessFile( file_name, "r" );
} catch( IOException ioe ) {
error_message = "Cannot open " + file_name;
FErrorAddDummy.fErrorAdd(
"FASTGET_INIT", error_message, "Check file exists and is readable" );
return 1;
}
try {
if( fd != null ) {
fd.close( );
}
} catch( IOException ioe2 ) {
error_message = "Cannot close " + file_name;
FErrorAddDummy.fErrorAdd(
"FASTGET_INIT", error_message, "This shouldn't happen!" );
return 1;
}
try {
file_size = fd.length( );
} catch( IOException ioe6 ) {
error_message = "Error retrieving length of " + file_name;
FErrorAddDummy.fErrorAdd(
"FASTGET_INIT", error_message, "This shouldn't happen!" );
return 1;
}
//io_block_size = stat_buffer.st_blksize; // optimal IO block size for this file system
if( file_size > MAX_BUFFER_SIZE ) {
readWholeFile = false;
enlargeBuffer( FOPEN_BUFFER_SIZE );
} else {
readWholeFile = true;
if( file_size > file_buffer_size ) {
enlargeBuffer( ( int )file_size );
}
}
if( file_fd != null ) {
try {
file_fd.close( );
file_fd = null;
} catch( IOException ioe3 ) {
//drop it on the floor
}
}
// Now fill up our buffer
try {
file_fd = new RandomAccessFile( file_name, "r" );
} catch( IOException ioe4 ) {
error_message = "Cannot open " + file_name;
FErrorAddDummy.fErrorAdd(
"FASTGET_INIT", error_message, "Check file exists and is readable" );
return 1;
}
if( readWholeFile ) {
try {
file_fd.readFully( file_buffer );
} catch( IOException ioe5 ) {
error_message = "Cannot read " + file_size + " bytes from " +
file_name;
FErrorAddDummy.fErrorAdd(
"FASTGET_INIT", error_message,
"Check file is not corrupted or has been deleted" );
return 1;
}
} else {
//at some point we want to reset the stream to fully buffered
/*if (setvbuf (file_fd, file_buffer, _IOFBF, FOPEN_BUFFER_SIZE) != 0)
{
perror ("setvbuf");
return 1;
}*/
}
}
return 0;
}
/**
* Enlarges the internal file_buffer to the newSize.
*
* @param newSize The new size (in bytes) of the file buffer.
*/
private void enlargeBuffer( int newSize ) {
byte[] temp = new byte[newSize];
for( int i = 0; i < file_buffer.length; i++ ) {
temp[i] = file_buffer[i];
}
file_buffer = temp;
}
/* static int
map_global (const char *name, int section, int *ret_addr[])
{
#ifdef HAVE_LIBDCE
int section_size;
if (READ_CRPT (&ret_addr[0], §ion, §ion_size) ==
RIO_OPERATION_SUCCESSFUL)
{
ret_addr[1] = ret_addr[0] + section_size;
return 0;
}
else
{
ret_addr[1] = ret_addr[0] = 0;
gprintef ("GETSECT: DAE Access not available\n");
return 1;
}
#else
gprintef ("GETSECT: DAE Access not available\n");
return 1;
#endif // HAVE_LIBDCE
}
#endif */
/**
* DOCUMENT ME!
*
* @param istart DOCUMENT ME!
* @param ilong DOCUMENT ME!
* @param ivalue DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
private int getsectFile( int istart, int ilong, int[] ivalue ) {
int ierr = 0;
String error_message = null;
final int sz = 32; //sizeof (int);
try {
if(
( ilong < 0 ) ||
( ( ( ( istart + ilong ) - 1 ) * sz ) > file_fd.length( ) ) ) {
error_message = "Attempt to read invalid part of file buffer: istart = " +
istart + " words, ilong = " + ilong + " words, file size = " +
file_fd.length( ) + " bytes";
FErrorAddDummy.fErrorAdd(
"GETSECT", error_message, "This shouldn't happen!" );
ierr = -1;
return ierr;
}
int arrSize = ilong * sz;
file_buffer = new byte[arrSize];
if( readWholeFile ) {
for( int i = 0; i < ( ilong * sz ); i++ ) {
file_fd.read(
file_buffer, ( int )( ( istart - 1 ) ) * sz, ilong * sz );
//this should work as well...
//file_fd.readFully( file_buffer );
}
//now transfer fileBytes to ivalue
} else {
try {
file_fd.seek( ( istart - 1 ) * sz );
} catch( IOException ioe ) {
error_message = "Error seeking file size" + file_size + " bytes at " +
( ( istart - 1 ) * sz );
FErrorAddDummy.fErrorAdd(
"GETSECT", error_message, "This shouldn't happen!" );
ierr = -1;
return ierr;
}
try {
//if( fread( ivalue, sz, ilong, file_fd ) != ( size_t )( ilong ) ) {
for( int i = 0; i < ilong; i++ ) {
ivalue[i] = file_fd.readInt( );
}
} catch( IOException ioe2 ) {
error_message = "Error reading file size " + file_size +
" bytes for " + ilong + "words from " + ( istart - 1 );
FErrorAddDummy.fErrorAdd(
"GETSECT", error_message, "This shouldn't happen!" );
ierr = -1;
return ierr;
}
}
return ierr;
} catch( IOException ioe ) {
return -1;
}
}
/*
* Originally declared as SUBROUTINE GETSECT(ISTART,ILONG,IVALUE,IUNIT,IERR) in FORTRAN
*
* The routine read a certain number of 512 byte blocks from a direct access file on unit "iunit"
*/
/*
static void
getsect_crpt (const int *istart, const int *ilong, int ivalue[],
const int *iunit, int *ierr)
{
static const int sz = sizeof (int);
static char buffer[256];
short zbdev = 0;
*ierr = 0;
if ((*ilong > 0) && ((*istart + *ilong - 1) <= crpt_size))
{
memcpy (ivalue, crpt_address + (*istart - 1) * sz, (*ilong) * sz);
}
else
{
sprintf (buffer,
"Attempt to read invalid part of CRPT buffer: istart = %d words, ilong = %d words, CRPT size = %ld words",
*istart, *ilong, crpt_size);
error_add ("GETSECT", buffer, "This shouldn't happen!");
*ierr = -1;
return;
}
}
static void
getsect_dae (const int *istart, const int *ilong, int ivalue[],
const int *iunit, int *ierr)
{
int start_of_spectra;
static const int sz = sizeof (int);
static char buffer[256];
short zbdev = 0;
*ierr = 0;
// We want to read everything but the counts from the CRPT, so check the range
if (*istart > start_of_data_section)
{
// need to DAE read here
start_of_spectra =
(*istart - start_of_data_section - 1) * dae_word_length;
if (RIO_OPERATION_FAILED
(&start_of_spectra, ivalue, (fort_i4 *) ilong, &zbdev,
&dae_word_length) == RIO_OPERATION_FAILED)
{
sprintf (buffer,
"Attempt to read %d words from DAE (word length %d) staring at address %d failed",
*ilong, dae_word_length, start_of_spectra);
error_add ("GETSECT", buffer,
"Is DAE access supported on this platform?");
}
}
else if ((*ilong > 0) && ((*istart + *ilong - 1) <= crpt_size))
{
memcpy (ivalue, crpt_address + (*istart - 1) * sz, (*ilong) * sz);
}
else
{
sprintf (buffer,
"Attempt to read invalid part of CRPT buffer: istart = %d words, ilong = %d words, CRPT size = %ld words",
*istart, *ilong, crpt_size);
error_add ("GETSECT", buffer, "This shouldn't happen!");
*ierr = -1;
return;
}
}
static int
enlarge_buffer (off_t new_buffer_size)
{
static char error_message[256];
if (new_buffer_size > file_buffer_size)
{
if (file_buffer != NULL)
free (file_buffer);
file_buffer = (char *) malloc (new_buffer_size * sizeof (char));
if (file_buffer == NULL)
{
sprintf (error_message,
"Unable to allocate %ld bytes for file buffer",
(long) file_size);
error_add ("FASTGET_INIT", error_message,
"Check your page file quota and/or the current machine load");
file_buffer_size = 0;
return 1;
}
file_buffer_size = new_buffer_size;
}
return 0;
}
*/
}