-
Notifications
You must be signed in to change notification settings - Fork 8
/
hitran.cpp
454 lines (378 loc) · 11.4 KB
/
hitran.cpp
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
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <math.h>
#define def_NA 6.0221412927e23 //Avogadro Constant 1/mol
// ./hitran -M 1 -ISO 1 -in hit16
// ******************************************************************
//This Function reads the Hitran or Hitemp data files
//and creates the <species name>.param and *.bin files
//Author: Simon Grimm
//May 2019
// *******************************************************************
int main(int argc, char *argv[]){
char iso[4];
sprintf(iso, "%s", "");
int M = -1;
char name[160];
sprintf(name, "%s", "");
char inname[160];
sprintf(inname, "%s", "");
//Read console input arguments
for(int i = 1; i < argc; i += 2){
if(strcmp(argv[i], "-M") == 0){
M = atoi(argv[i + 1]);
}
else if(strcmp(argv[i], "-ISO") == 0){
sprintf(iso, "%1s", argv[i + 1]);
}
else if(strcmp(argv[i], "-in") == 0){
sprintf(inname, "%s", argv[i + 1]);
}
else if(strcmp(argv[i], "-out") == 0){ //optional
sprintf(name, "%s", argv[i + 1]);
}
else{
printf("Error: Console arguments not valid!\n");
return 0;
}
}
//M = 2;
//sprintf(inname, "cdsd_");
//sprintf(iso, "%1s", "1");
//sprintf(name, "12C-16O2__CDSD_4000");
//sprintf(iso, "%1s", "2");
//sprintf(name, "13C-16O2__CDSD_4000");
//sprintf(iso, "%1s", "3");
//sprintf(name, "16O-12C-18O__CDSD_4000");
//sprintf(iso, "%1s", "4");
//sprintf(name, "16O-12C-17O__CDSD_4000");
if(M <= 0){
printf("Error, no -M agrument given\n");
return 0;
}
if(strcmp(inname, "") == 0){
printf("Error, no -in agrument given\n");
return 0;
}
if(strcmp(name, "") == 0){
sprintf(name, "%s", inname);
}
// ********************************************************
//Read isotopologue parameters
FILE *IsoFile;
IsoFile = fopen("Hitran_species.dat", "r");
if(IsoFile == NULL){
printf("Error, Hitran_species.dat file not found\n");
return 0;
}
char index[20][4];
char qfile[20][32];
double abundance[20];
double m[20];
double Q0[20];
double g[20];
char formula[20][32];
int nISO = 0;
for(int i = 0; i < 200; ++i){
fscanf(IsoFile, "%s %lf %lf %lf %s %lf %s\n", index[nISO], &abundance[nISO], &m[nISO], &Q0[nISO], qfile[nISO], &g[nISO], formula[nISO]);
//ensure that leading space exists
char iii[4];
sprintf(iii, "%3s", index[nISO]);
sprintf(index[nISO], "%3s", iii);
//extract molecule index
char ii[3];
sprintf(ii, "%.2s", iii);
int molecule = atoi(ii);
char iiso[2];
sprintf(iiso, "%.1s", &iii[2]);
//printf("%d |%s|%s| %g | %d %d |%s|\n", nISO, index[nISO], iii, abundance[nISO], molecule, M, iiso);
if(iso[0] == 0){
if(molecule == M){
printf("ISOm %d %s %g %g %g %s %g\n", molecule, index[nISO], abundance[nISO], m[nISO], Q0[nISO], qfile[nISO], g[nISO]);
++nISO;
}
}
else{
if(molecule == M && (strcmp(iiso, iso) == 0)){
printf("ISO1 %d %s %g %g %g %s %g\n", molecule, index[nISO], abundance[nISO], m[nISO], Q0[nISO], qfile[nISO], g[nISO]);
abundance[nISO] = 1.0;
++nISO;
}
}
}
fclose(IsoFile);
// **************************************************
int count = 0;
double numax = 0.0;
double nuOld = 0.0;
int nFiles = 0;
int filesCount[1000];
int filesRange[1000];
//read all files in the current directory
char path[100];
realpath(inname, path);
printf("path |%s|\n", path);
struct dirent **namelist;
int n;
n = scandir(".", &namelist, NULL, alphasort);
//n = scandir("../", &namelist, NULL, alphasort);
printf("total number of files in directory %d\n", n);
int nn = 0;
int range0, range1;
int len = 0;
int slen = 0;
//scan and count desired files
for(int j = 0; j < n; ++j){
std::string str = std::string(namelist[j]->d_name);
printf("--- %d %s %s %s\n", j, namelist[j]->d_name, str.c_str(), inname);
//check if file name contains name and starts with Molecule id
char s2[32];
sprintf(s2,"%02d_", M);
char s3[32];
sprintf(s3,".par");
//extract file range from string
int ppos = int(str.find(inname)); //position of name in string
std::string sbstr;
std::string sbstr0;
std::string sbstr1;
if(ppos > 3){
sbstr = str.substr(3, ppos - 4);
slen = sbstr.length();
sbstr0 = sbstr.substr(0, slen/2);
sbstr1 = sbstr.substr(slen/2+1, slen/2);
range0 = atoi(sbstr0.c_str());
range1 = atoi(sbstr1.c_str());
}
else{
sbstr = "";
slen = 0;
sbstr0 = "";
sbstr1 = "";
range0 = 0;
range1 = 0;
}
len = str.length();
// find inname in string and find molecule in string and find ".par" in file name
if(str.find(inname) != std::string::npos && str.rfind(s2, 0) == 0 && str.compare(len - 4, 4,s3) == 0){
++nn;
printf("----- %d %s %d %d %d |%s| %d %d \n", nn, str.c_str(), ppos, len, slen, sbstr.c_str(), range0, range1);
}
}
printf("number of data files in directory %d\n", nn);
FILE *binFile;
char binFileName[300];
FILE *paramFile;
char paramFileName[300];
if(iso[0] == 0){
sprintf(paramFileName, "%02d_%s.param", M, name);
}
else{
sprintf(paramFileName, "%02d_%s_%s.param", M, iso, name);
}
paramFile = fopen(paramFileName, "w");
fprintf(paramFile, "Database = 0\n");
fprintf(paramFile, "Molecule number = %d\n", M);
if(iso[0] == 0){
fprintf(paramFile, "Name = %02d_%s\n", M, name);
}
else{
fprintf(paramFile, "Name = %02d_%s_%s\n", M, iso, name);
}
fprintf(paramFile, "Number of Isotopologues = %d\n", nISO);
fprintf(paramFile, "#ID Abundance Q(296K) g Molar Mass(g) partition file :\n");
for(int i = 0; i < nISO; ++i){
fprintf(paramFile, "%s %12.10g %8.6g %5.5g %14.14g %s\n", index[i], abundance[i], Q0[i], g[i], m[i], qfile[i]);
}
double nu = 0.0;
for(int j = 0; j < n; ++j){
std::string str = std::string(namelist[j]->d_name);
//printf("-- %s %s %s\n", namelist[j]->d_name, str.c_str(), inname);
char s2[32];
sprintf(s2,"%02d_", M);
char s3[32];
sprintf(s3, ".par");
//extract file range from string
int ppos = int(str.find(inname)); //position of name in string
std::string sbstr;
std::string sbstr0;
std::string sbstr1;
if(ppos > 3){
sbstr = str.substr(3, ppos - 4);
slen = sbstr.length();
sbstr0 = sbstr.substr(0, slen/2);
sbstr1 = sbstr.substr(slen/2+1, slen/2);
range0 = atoi(sbstr0.c_str());
range1 = atoi(sbstr1.c_str());
}
else{
sbstr = "";
slen = 0;
sbstr0 = "";
sbstr1 = "";
range0 = 0;
range1 = 0;
}
int len = str.length();
if(str.find(inname) != std::string::npos && str.rfind(s2, 0) == 0 && str.compare(len - 4, 4,s3) == 0){
printf("---- %d %s\n", n, str.c_str());
FILE *dataFile;
dataFile = fopen(str.c_str(), "r");
char c1[3];
char c2[2];
char c3[13];
char c4[11];
char c5[11];
char c6[6];
char c7[6];
char c8[11];
char c9[5];
char c10[9];
char skip[161];
for(int i = 0; i < 1e9; ++i){
if(fgets(c1, 3, dataFile) == NULL){
int nnu;
if(slen == 0){
nnu = ceil(nu);
}
else{
nnu = range1;
}
printf("end %d %g %g %d %d\n", i, nu, nuOld, count, nnu);
filesCount[nFiles] = count;
filesRange[nFiles] = nnu;
++nFiles;
count = 0;
break;
}
fgets(c2, 2, dataFile);
fgets(c3, 13, dataFile);
fgets(c4, 11, dataFile);
fgets(c5, 11, dataFile);
fgets(c6, 6, dataFile);
fgets(c7, 6, dataFile);
fgets(c8, 11, dataFile);
fgets(c9, 5, dataFile);
fgets(c10, 9, dataFile);
fgets(skip, 160, dataFile);
int id = atoi(c1);
char cid[4];
sprintf(cid, "%2d%s", id, c2);
nu = strtod(c3, NULL);
double S = strtod(c4, NULL);
double A = strtod(c5, NULL);
double delta = strtod(c10, NULL);
double EL = strtod(c8, NULL);
double gammaAir = strtod(c6, NULL);
double gammaSelf = strtod(c7, NULL);
double n = strtod(c9, NULL);
//if(i < 1000) printf("**** %d %g %g %g %g %g %g %g %g | %g |%s|%s|\n", i, nu, S, A, delta, EL, gammaAir, gammaSelf, n, nuOld, c2, iso);
double mass = 0.0;
double q0 = -1000.0;
/*
//open next binary file
if(int(nu / 100) != int(nuOld / 100)){
int nnu = int(nu / 100) * 100;
printf("********** %g %g %d %d %d\n", nu, nuOld, count, int(nuOld / 100) * 100, nnu);
filesCount[nFiles] = count;
filesRange[nFiles] = nnu;
count = 0;
++nFiles;
fclose(binFile);
sprintf(binFileName, "%s__%05d_%05d.bin", name, nnu, nnu + 100);
binFile = fopen(binFileName, "wb");
}
*/
if(strcmp(c2, iso) == 0 || strcmp(iso, "") == 0){
//printf("**** %d %s %g %g %g %g %g %g %g %g | %g |%s|%s|\n", i, cid, nu, S, A, delta, EL, gammaAir, gammaSelf, n, nuOld, c2, iso);
//first entry
if(nFiles == 0){
int nnu;
if(slen == 0){
nnu = int(nu);
}
else{
nnu = range0;
}
printf("********** %g %g %d %d\n", nu, nuOld, count, nnu);
filesCount[nFiles] = count;
filesRange[nFiles] = nnu;
++nFiles;
}
if(count == 0){
if(nn == 1){
if(strcmp(iso, "") == 0){
sprintf(binFileName, "%02d_%s.bin", M, name);
}
else{
sprintf(binFileName, "%02d_%s_%s.bin", M, iso, name);
}
}
else{
if(strcmp(iso, "") == 0){
sprintf(binFileName, "%02d_%s_%s.bin", M, name, sbstr.c_str());
}
else{
sprintf(binFileName, "%02d_%s_%s_%s.bin", M, iso, name, sbstr.c_str());
}
}
printf("******** open bin file %s\n", binFileName);
binFile = fopen(binFileName, "wb");
}
//Assign the Isotopologue properties
for(int k = 0; k < nISO; ++k){
//printf("k %d |%s|%s| \n", k, cid, index[k]);
if(strcmp(cid, index[k]) == 0){
mass = m[k] / def_NA;
q0 = Q0[k];
break;
}
}
if(q0 < -800){
printf("Error in assigning isotopologue properties\n");
}
S /= mass;
S = S * q0;
//if(count < 1000 || count % 100000 == 0) printf("%s | %s %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g %.10g\n", iso, cid, nu, S, A, gammaAir, gammaSelf, EL, n, delta, mass, q0);
fwrite(&cid, 4*sizeof(char), 1, binFile);
fwrite(&nu, sizeof(double), 1, binFile);
fwrite(&S, sizeof(double), 1, binFile);
fwrite(&EL, sizeof(double), 1, binFile);
fwrite(&A, sizeof(double), 1, binFile);
fwrite(&delta, sizeof(double), 1, binFile);
fwrite(&gammaAir, sizeof(double), 1, binFile);
fwrite(&gammaSelf, sizeof(double), 1, binFile);
fwrite(&n, sizeof(double), 1, binFile);
++count;
numax = fmax(numax, nu);
}
nuOld = nu;
}
printf("nFiles %d, Number of lines %d, numax %g\n", nFiles, count, numax);
fclose(dataFile);
}
}
for(int i = 0; i < nFiles; ++i){
printf("F %d %d\n", filesCount[i], filesRange[i]);
}
fprintf(paramFile, "Number of columns in partition File = 2\n");
fprintf(paramFile, "Number of line/transition files = %d\n", nFiles - 1);
fprintf(paramFile, "Number of lines per file :\n");
for(int i = 0; i < nFiles - 1; ++i){
fprintf(paramFile, "%d\n", filesCount[i + 1]);
}
fprintf(paramFile, "Line file limits :\n");
for(int i = 0; i < nFiles; ++i){
fprintf(paramFile, "%d\n", filesRange[i]);
}
fprintf(paramFile, "#ExoMol :\n");
fprintf(paramFile, "Number of states = 0\n");
fprintf(paramFile, "Number of columns in transition files = 0\n");
fprintf(paramFile, "Default value of Lorentzian half-width for all lines = 0\n");
fprintf(paramFile, "Default value of temperature exponent for all lines = 0\n");
fprintf(paramFile, "Version = 0\n");
fclose(paramFile);
return 0;
}