31 status = nc_create(l1_filename, NC_NETCDF4, &ncid);
34 ifstream cdl_data_structure;
36 string dataStructureFile(cdl_filename);
40 cdl_data_structure.open(dataStructureFile.c_str(), ifstream::in);
41 if (cdl_data_structure.fail() ==
true) {
42 cout <<
"\"" << dataStructureFile.c_str() <<
"\" not found" << endl;
48 getline(cdl_data_structure,
line);
50 size_t firstNonBlank =
line.find_first_not_of(
" ");
51 if (firstNonBlank != string::npos)
52 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
54 size_t pos =
line.find(
"dimensions:");
61 getline(cdl_data_structure,
line);
63 size_t firstNonBlank =
line.find_first_not_of(
" ");
64 if (firstNonBlank != string::npos)
65 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
68 if (
pos == string::npos)
break;
71 istringstream iss(
line.substr(
pos + 2, string::npos));
76 iss >> skipws >>
line;
78 cout <<
"Dimension Name: " <<
line.c_str() <<
" Dimension Size: "
81 status = nc_def_dim(ncid,
line.c_str(), dimSize, &dimid[ndims++]);
88 getline(cdl_data_structure,
line);
90 size_t firstNonBlank =
line.find_first_not_of(
" ");
91 if (firstNonBlank != string::npos)
92 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
96 if (
line.substr(0, 1).compare(
"}") == 0) {
97 cdl_data_structure.close();
102 size_t pos =
line.find(
"group:");
108 istringstream iss(
line.substr(6, string::npos));
109 iss >> skipws >>
line;
110 cout <<
"Group: " <<
line.c_str() << endl;
119 int varDims[NC_MAX_DIMS];
120 size_t dimSize[NC_MAX_DIMS];
121 char dimName[NC_MAX_NAME + 1];
127 string flag_meanings;
130 double fill_value = 0.0;
138 getline(cdl_data_structure,
line);
139 if (
line.find(
"variables:") != string::npos)
break;
143 getline(cdl_data_structure,
line);
145 if (
line.length() == 0)
continue;
146 if (
line.substr(0, 1).compare(
"\r") == 0)
continue;
147 if (
line.substr(0, 1).compare(
"\n") == 0)
continue;
149 size_t firstNonBlank =
line.find_first_not_of(
" ");
150 if (firstNonBlank != string::npos)
151 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
156 if (
pos == string::npos) {
161 sname.c_str(), lname.c_str(),
163 (
void *) &fill_value,
164 flag_values.c_str(), flag_meanings.c_str(),
167 flag_values.assign(
"");
168 flag_meanings.assign(
"");
176 if (
line.substr(0, 10).compare(
"} // group") == 0)
break;
180 istringstream iss(
line);
181 iss >> skipws >> varType;
184 if (varType.compare(
"char") == 0) ntype = NC_CHAR;
185 else if (varType.compare(
"byte") == 0) ntype = NC_BYTE;
186 else if (varType.compare(
"short") == 0) ntype = NC_SHORT;
187 else if (varType.compare(
"int") == 0) ntype = NC_INT;
188 else if (varType.compare(
"long") == 0) ntype = NC_INT;
189 else if (varType.compare(
"float") == 0) ntype = NC_FLOAT;
190 else if (varType.compare(
"real") == 0) ntype = NC_FLOAT;
191 else if (varType.compare(
"double") == 0) ntype = NC_DOUBLE;
192 else if (varType.compare(
"ubyte") == 0) ntype = NC_UBYTE;
193 else if (varType.compare(
"ushort") == 0) ntype = NC_USHORT;
194 else if (varType.compare(
"uint") == 0) ntype = NC_UINT;
195 else if (varType.compare(
"int64") == 0) ntype = NC_INT64;
196 else if (varType.compare(
"uint64") == 0) ntype = NC_UINT64;
200 size_t posSname =
line.substr(0,
pos).rfind(
" ");
201 sname.assign(
line.substr(posSname + 1,
pos - posSname - 1));
202 cout <<
"sname: " << sname.c_str() << endl;
207 for (
int i = 0;
i < numDims;
i++) {
208 nc_inq_dim(ncid, varDims[
i], dimName, &dimSize[
i]);
209 cout <<
line.c_str() <<
" " <<
i <<
" " << dimName
210 <<
" " << dimSize[
i] << endl;
213 chunksize = (
size_t *) calloc(numDims,
sizeof (
size_t));
217 size_t posEql =
line.find(
"=");
218 size_t pos1qte =
line.find(
"\"");
219 size_t pos2qte =
line.substr(pos1qte + 1, string::npos).find(
"\"");
225 if (
attrName.compare(
"long_name") == 0) {
226 lname.assign(
line.substr(pos1qte + 1, pos2qte));
230 else if (
attrName.compare(
"units") == 0) {
231 units.assign(
line.substr(pos1qte + 1, pos2qte));
235 else if (
attrName.compare(
"_FillValue") == 0) {
237 iss.str(
line.substr(posEql + 1, string::npos));
242 else if (
attrName.compare(
"flag_values") == 0) {
243 flag_values.assign(
line.substr(pos1qte + 1, pos2qte));
246 else if (
attrName.compare(
"flag_meanings") == 0) {
247 flag_meanings.assign(
line.substr(pos1qte + 1, pos2qte));
250 else if (
attrName.compare(
"valid_min") == 0) {
252 iss.str(
line.substr(posEql + 1, string::npos));
257 else if (
attrName.compare(
"valid_max") == 0) {
259 iss.str(
line.substr(posEql + 1, string::npos));
263 else if (
attrName.compare(
"_ChunkSizes") == 0) {
264 size_t posComma =
line.find(
",");
267 for (
i = 0;
i < numDims;
i++) {
269 if (
i == numDims - 1) {
274 posComma +=
line.substr(posComma + 1, string::npos).find(
",") + 1;
294 int ncdfFile::cdlCreateDim(
char* l1_filename,
char* cdl_filename,
const char** dim_names,
size_t* dim_size,
size_t n_dims,
300 status = nc_create(l1_filename, NC_NETCDF4, &ncid);
303 ifstream cdl_data_structure;
305 string dataStructureFile(cdl_filename);
309 cdl_data_structure.open(dataStructureFile.c_str(), ifstream::in);
310 if (cdl_data_structure.fail() ==
true) {
311 cout <<
"\"" << dataStructureFile.c_str() <<
"\" not found" << endl;
319 for (
size_t i = 0;
i < n_dims;
i++) {
321 cout <<
"Dimension Name: " << dim_names[
i] <<
" Dimension Size: "
322 << dim_size[
i] << endl;
324 status = nc_def_dim(ncid, dim_names[
i], dim_size[
i], &dimid[ndims++]);
330 getline(cdl_data_structure,
line);
332 size_t firstNonBlank =
line.find_first_not_of(
" ");
333 if (firstNonBlank != string::npos)
334 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
336 size_t pos =
line.find(
"dimensions:");
342 getline(cdl_data_structure,
line);
344 size_t firstNonBlank =
line.find_first_not_of(
" ");
345 if (firstNonBlank != string::npos)
346 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
349 if (
pos == string::npos)
break;
352 istringstream iss(
line.substr(
pos + 2, string::npos));
357 iss >> skipws >>
line;
369 getline(cdl_data_structure,
line);
370 size_t pos =
line.find(
"group:");
376 if (
line.length() == 0)
continue;
377 if (
line.substr(0, 1).compare(
"\r") == 0)
continue;
378 if (
line.substr(0, 1).compare(
"\n") == 0)
continue;
380 size_t firstNonBlank =
line.find_first_not_of(
" ");
381 if (firstNonBlank != string::npos)
382 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
387 if (
pos != string::npos) {
388 size_t posEql =
line.find(
"=");
389 size_t pos1qte =
line.find(
"\"");
390 size_t pos2qte =
line.substr(pos1qte + 1, string::npos).find(
"\"");
391 cout <<
line.substr(
pos + 1, posEql -
pos - 2).c_str() << endl;
395 attVal.assign(
line.substr(pos1qte + 1, pos2qte));
396 if (
attrName.compare(
"product_name") == 0) {
398 status = nc_put_att_text(ncid, NC_GLOBAL,
attrName.c_str(), strlen(l1_filename) + 1, l1_filename);
400 printf(
"-E- %s %d: %s for %s\n",
404 }
else if (
attrName.compare(
"date_created") == 0) {
407 printf(
"-E- %s %d: %s for %s\n",
411 }
else if (
attrName.compare(
"history") == 0) {
412 sprintf(
history,
"Generated by avirisbil2oci; cdlfile=%s", cdl_filename);
415 printf(
"-E- %s %d: %s for %s\n",
419 }
else if (
attrName.compare(
"orbit_number") == 0) {
422 status = nc_put_att_int(ncid, NC_GLOBAL,
attrName.c_str(), NC_INT, 1, &vr[0]);
424 printf(
"-E- %s %d: %s for %s\n",
428 }
else if (
attrName.compare(
"license") == 0) {
429 printf(
"length of name=%ld\n",
attrName.length());
430 status = nc_put_att_text(ncid, NC_GLOBAL,
"license",
431 strlen(
line.substr(pos1qte + 1, pos2qte).c_str()) + 1,
432 line.substr(pos1qte + 1, pos2qte).c_str());
434 printf(
"-E- %s %d: %s for %s\n",
438 }
else if (
attrName.compare(
"creator_name") == 0) {
439 printf(
"length of name=%ld\n",
attrName.length());
440 status = nc_put_att_text(ncid, NC_GLOBAL,
"creator_name",
441 strlen(attVal.c_str()) + 1,
444 printf(
"-E- %s %d: %s for %s\n",
449 }
else if (
attrName.compare(
"creator_url") == 0) {
450 printf(
"length of name=%ld\n",
attrName.length());
451 status = nc_put_att_text(ncid, NC_GLOBAL,
"creator_url",
452 strlen(attVal.c_str()) + 1,
455 printf(
"-E- %s %d: %s for %s\n",
459 }
else if (
attrName.compare(
"publisher_name") == 0) {
460 printf(
"length of name=%ld\n",
attrName.length());
461 status = nc_put_att_text(ncid, NC_GLOBAL,
"publisher_name",
462 strlen(attVal.c_str()) + 1,
465 printf(
"-E- %s %d: %s for %s\n",
470 }
else if (
attrName.compare(
"publisher_url") == 0) {
471 printf(
"length of name=%ld\n",
attrName.length());
472 status = nc_put_att_text(ncid, NC_GLOBAL,
"publisher_url",
473 strlen(attVal.c_str()) + 1,
476 printf(
"-E- %s %d: %s for %s\n",
484 printf(
"value = %s\n",
line.substr(pos1qte + 1, pos2qte).c_str());
486 strlen(attVal.c_str()) + 1,
489 printf(
"-E- %s %d: %s for %s\n",
501 size_t firstNonBlank =
line.find_first_not_of(
" ");
502 if (firstNonBlank != string::npos)
503 if (
line.compare(firstNonBlank, 2,
"//") == 0) {
504 getline(cdl_data_structure,
line);
509 if (
line.substr(0, 1).compare(
"}") == 0) {
510 cdl_data_structure.close();
515 size_t pos =
line.find(
"group:");
521 istringstream iss(
line.substr(6, string::npos));
522 iss >> skipws >>
line;
523 cout <<
"Group: " <<
line.c_str() << endl;
532 int varDims[NC_MAX_DIMS];
533 size_t dimSize[NC_MAX_DIMS];
534 char dimName[NC_MAX_NAME + 1];
540 string flag_meanings;
543 double fill_value = 0.0;
551 getline(cdl_data_structure,
line);
552 if (
line.find(
"variables:") != string::npos)
break;
556 getline(cdl_data_structure,
line);
558 if (
line.length() == 0)
continue;
559 if (
line.substr(0, 1).compare(
"\r") == 0)
continue;
560 if (
line.substr(0, 1).compare(
"\n") == 0)
continue;
562 size_t firstNonBlank =
line.find_first_not_of(
" ");
563 if (firstNonBlank != string::npos)
564 if (
line.compare(firstNonBlank, 2,
"//") == 0)
continue;
569 if (
pos == string::npos) {
574 sname.c_str(), lname.c_str(),
576 (
void *) &fill_value,
577 flag_values.c_str(), flag_meanings.c_str(),
580 flag_values.assign(
"");
581 flag_meanings.assign(
"");
589 if (
line.substr(0, 10).compare(
"} // group") == 0)
break;
593 istringstream iss(
line);
594 iss >> skipws >> varType;
597 if (varType.compare(
"char") == 0) ntype = NC_CHAR;
598 else if (varType.compare(
"byte") == 0) ntype = NC_BYTE;
599 else if (varType.compare(
"short") == 0) ntype = NC_SHORT;
600 else if (varType.compare(
"int") == 0) ntype = NC_INT;
601 else if (varType.compare(
"long") == 0) ntype = NC_INT;
602 else if (varType.compare(
"float") == 0) ntype = NC_FLOAT;
603 else if (varType.compare(
"real") == 0) ntype = NC_FLOAT;
604 else if (varType.compare(
"double") == 0) ntype = NC_DOUBLE;
605 else if (varType.compare(
"ubyte") == 0) ntype = NC_UBYTE;
606 else if (varType.compare(
"ushort") == 0) ntype = NC_USHORT;
607 else if (varType.compare(
"uint") == 0) ntype = NC_UINT;
608 else if (varType.compare(
"int64") == 0) ntype = NC_INT64;
609 else if (varType.compare(
"uint64") == 0) ntype = NC_UINT64;
613 size_t posSname =
line.substr(0,
pos).rfind(
" ");
614 sname.assign(
line.substr(posSname + 1,
pos - posSname - 1));
615 cout <<
"sname: " << sname.c_str() << endl;
620 for (
int i = 0;
i < numDims;
i++) {
621 nc_inq_dim(ncid, varDims[
i], dimName, &dimSize[
i]);
622 cout <<
line.c_str() <<
" " <<
i <<
" " << dimName
623 <<
" " << dimSize[
i] << endl;
625 chunksize = (
size_t *) calloc(numDims,
sizeof (
size_t));
629 size_t posEql =
line.find(
"=");
630 size_t pos1qte =
line.find(
"\"");
631 size_t pos2qte =
line.substr(pos1qte + 1, string::npos).find(
"\"");
637 if (
attrName.compare(
"long_name") == 0) {
638 lname.assign(
line.substr(pos1qte + 1, pos2qte));
642 else if (
attrName.compare(
"units") == 0) {
643 units.assign(
line.substr(pos1qte + 1, pos2qte));
647 else if (
attrName.compare(
"_FillValue") == 0) {
649 iss.str(
line.substr(posEql + 1, string::npos));
654 else if (
attrName.compare(
"flag_values") == 0) {
655 flag_values.assign(
line.substr(pos1qte + 1, pos2qte));
658 else if (
attrName.compare(
"flag_meanings") == 0) {
659 flag_meanings.assign(
line.substr(pos1qte + 1, pos2qte));
662 else if (
attrName.compare(
"valid_min") == 0) {
664 iss.str(
line.substr(posEql + 1, string::npos));
669 else if (
attrName.compare(
"valid_max") == 0) {
671 iss.str(
line.substr(posEql + 1, string::npos));
675 else if (
attrName.compare(
"_ChunkSizes") == 0) {
676 size_t posComma =
line.find(
",");
679 for (
i = 0;
i < numDims;
i++) {
681 if (
i == numDims - 1) {
688 posComma +=
line.substr(posComma + 1, string::npos).find(
",") + 1;
698 getline(cdl_data_structure,
line);
707 size_t dimSize, curPos = 0;
708 char dimName[NC_MAX_NAME + 1];
713 size_t pos = dimString.find(
",", curPos);
714 if (
pos == string::npos)
715 pos = dimString.find(
")");
718 istringstream iss(dimString.substr(curPos,
pos - curPos));
719 iss >> skipws >> varDimName;
721 for (
int i = 0;
i < ndims;
i++) {
722 nc_inq_dim(ncid, dimid[
i], dimName, &dimSize);
723 if (varDimName.compare(dimName) == 0) {
724 varDims[(*numDims)++] = dimid[
i];
728 if (dimString.substr(
pos, 1).compare(
")") == 0)
break;
740 status = nc_inq_grp_ncid(ncid, grpName, &grpID);