54 MYSOFA_ATTRIBUTE* tmp_a;
55#ifdef SAF_ENABLE_NETCDF
57 int varid, attnum, i, j, ncid, retval, ndimsp, nvarsp, nattsp, unlimdimidp, varnattsp;
58 size_t tmp_size, lenp;
59 char varname[NC_MAX_NAME+1], attname[NC_MAX_NAME+1];
93 hrtf = mysofa_load(sofa_filepath, &err);
104 h->
DataIR = hrtf->DataIR.values;
114 tmp_a = hrtf->ListenerPosition.attributes;
116 if (!strcmp((
char*)tmp_a->name,
"Type"))
118 else if (!strcmp((
char*)tmp_a->name,
"Units"))
122 tmp_a = hrtf->ReceiverPosition.attributes;
124 if (!strcmp((
char*)tmp_a->name,
"Type"))
126 else if (!strcmp((
char*)tmp_a->name,
"Units"))
130 tmp_a = hrtf->SourcePosition.attributes;
132 if (!strcmp((
char*)tmp_a->name,
"Type"))
134 else if (!strcmp((
char*)tmp_a->name,
"Units"))
138 tmp_a = hrtf->EmitterPosition.attributes;
140 if (!strcmp((
char*)tmp_a->name,
"Type"))
142 else if (!strcmp((
char*)tmp_a->name,
"Units"))
146 tmp_a = hrtf->ListenerView.attributes;
148 if (!strcmp((
char*)tmp_a->name,
"Type"))
150 else if (!strcmp((
char*)tmp_a->name,
"Units"))
154 tmp_a = hrtf->DataSamplingRate.attributes;
156 if (!strcmp((
char*)tmp_a->name,
"Units"))
162 tmp_a = hrtf->attributes;
164 if (!strcmp((
char*)tmp_a->name,
"Conventions"))
166 else if (!strcmp((
char*)tmp_a->name,
"Version"))
168 else if (!strcmp((
char*)tmp_a->name,
"SOFAConventions"))
170 else if (!strcmp((
char*)tmp_a->name,
"SOFAConventionsVersion"))
172 else if (!strcmp((
char*)tmp_a->name,
"APIName"))
174 else if (!strcmp((
char*)tmp_a->name,
"APIVersion"))
176 else if (!strcmp((
char*)tmp_a->name,
"ApplicationName"))
178 else if (!strcmp((
char*)tmp_a->name,
"ApplicationVersion"))
180 else if (!strcmp((
char*)tmp_a->name,
"AuthorContact"))
182 else if (!strcmp((
char*)tmp_a->name,
"Comment"))
184 else if (!strcmp((
char*)tmp_a->name,
"DataType"))
186 else if (!strcmp((
char*)tmp_a->name,
"History"))
188 else if (!strcmp((
char*)tmp_a->name,
"License"))
190 else if (!strcmp((
char*)tmp_a->name,
"Organization"))
192 else if (!strcmp((
char*)tmp_a->name,
"References"))
194 else if (!strcmp((
char*)tmp_a->name,
"RoomType"))
196 else if (!strcmp((
char*)tmp_a->name,
"Origin"))
198 else if (!strcmp((
char*)tmp_a->name,
"DateCreated"))
200 else if (!strcmp((
char*)tmp_a->name,
"DateModified"))
202 else if (!strcmp((
char*)tmp_a->name,
"Title"))
203 h->
Title = tmp_a->value;
204 else if (!strcmp((
char*)tmp_a->name,
"DatabaseName"))
206 else if (!strcmp((
char*)tmp_a->name,
"ListenerShortName"))
211 case MYSOFA_READ_ERROR:
213 case MYSOFA_INVALID_DIMENSIONS:
221#ifdef SAF_ENABLE_NETCDF
225 if ((retval = nc_open(sofa_filepath, NC_NOWRITE, &ncid)))
229 nc_inq(ncid, &ndimsp, &nvarsp, &nattsp, &unlimdimidp);
232 dimid =
malloc1d(ndimsp*
sizeof(
int));
233 dimlength =
malloc1d(ndimsp*
sizeof(
size_t));
234 dimname =
malloc1d(ndimsp*(NC_MAX_NAME+1)*
sizeof(
char));
235 for (i=0; i<ndimsp; i++){
236 nc_inq_dim(ncid, i, &dimname[i*(NC_MAX_NAME+1)], &dimlength[i]);
237 nc_inq_dimid(ncid, &dimname[i*(NC_MAX_NAME+1)], &dimid[i]);
243 for(varid=0; varid<nvarsp; varid++){
244 nc_inq_var(ncid, varid, (
char*)varname, NULL, NULL, NULL, NULL);
245 nc_inq_varndims(ncid, varid, &ndimsp);
246 dimids =
realloc1d(dimids, ndimsp*
sizeof(
int));
247 nc_inq_vardimid(ncid, varid, dimids);
248 nc_inq_vartype(ncid, varid, &typep);
249 nc_inq_varnatts(ncid, varid, &varnattsp);
251 if (!strcmp((
char*)varname,
"Data.IR")){
258 h->
nSources = (int)dimlength[dimid[dimids[0]]];
259 h->
nReceivers = (int)dimlength[dimid[dimids[1]]];
261 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]] * dimlength[dimid[dimids[2]]];
262 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
263 nc_get_var(ncid, varid, tmp_data);
265 for(i=0; i<(int)tmp_size; i++)
266 h->
DataIR[i] = (
float)tmp_data[i];
268 else if(!strcmp((
char*)varname,
"Data.SamplingRate")){
274 tmp_data =
realloc1d(tmp_data, 1*
sizeof(
double));
275 nc_get_var(ncid, varid, tmp_data);
279 for(attnum=0; attnum<varnattsp; attnum++){
280 nc_inq_attname(ncid, varid, attnum, attname);
281 nc_inq_attlen(ncid, varid, attname, &lenp);
282 if (!strcmp((
char*)attname,
"Units")){
288 else if (!strcmp((
char*)varname,
"Data.Delay")){
296 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
297 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
298 nc_get_var(ncid, varid, tmp_data);
300 for(i=0; i<(int)tmp_size; i++)
303 else if (!strcmp((
char*)varname,
"SourcePosition")){
311 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
312 h->
nSources = (int)dimlength[dimid[dimids[0]]];
313 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
314 nc_get_var(ncid, varid, tmp_data);
316 for(i=0; i<(int)tmp_size; i++)
320 for(attnum=0; attnum<varnattsp; attnum++){
321 nc_inq_attname(ncid, varid, attnum, attname);
322 nc_inq_attlen(ncid, varid, attname, &lenp);
323 if (!strcmp((
char*)attname,
"Type")){
327 else if (!strcmp((
char*)attname,
"Units")){
333 else if (!strcmp((
char*)varname,
"ReceiverPosition")){
343 h->
nReceivers = (int)dimlength[dimid[dimids[0]]];
345 nc_get_var(ncid, varid, tmp_data);
364 h->
nReceivers = (int)dimlength[dimid[dimids[0]]];
366 nc_get_var(ncid, varid, tmp_data);
376 for(attnum=0; attnum<varnattsp; attnum++){
377 nc_inq_attname(ncid, varid, attnum, attname);
378 nc_inq_attlen(ncid, varid, attname, &lenp);
379 if (!strcmp((
char*)attname,
"Type")){
383 else if (!strcmp((
char*)attname,
"Units")){
389 else if (!strcmp((
char*)varname,
"ListenerPosition")){
397 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
398 h->
nListeners = (int)dimlength[dimid[dimids[0]]];
399 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
400 nc_get_var(ncid, varid, tmp_data);
402 for(j=0; j<(int)tmp_size; j++)
406 for(attnum=0; attnum<varnattsp; attnum++){
407 nc_inq_attname(ncid, varid, attnum, attname);
408 nc_inq_attlen(ncid, varid, attname, &lenp);
409 if (!strcmp((
char*)attname,
"Type")){
413 else if (!strcmp((
char*)attname,
"Units")){
419 else if (!strcmp((
char*)varname,
"ListenerUp")){
427 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
428 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
429 nc_get_var(ncid, varid, tmp_data);
431 for(j=0; j<(int)tmp_size; j++)
434 else if (!strcmp((
char*)varname,
"ListenerView")){
442 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
443 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
444 nc_get_var(ncid, varid, tmp_data);
446 for(j=0; j<(int)tmp_size; j++)
450 for(attnum=0; attnum<varnattsp; attnum++){
451 nc_inq_attname(ncid, varid, attnum, attname);
452 nc_inq_attlen(ncid, varid, attname, &lenp);
453 if (!strcmp((
char*)attname,
"Type")){
457 else if (!strcmp((
char*)attname,
"Units")){
463 else if (!strcmp((
char*)varname,
"EmitterPosition")){
472 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
473 tmp_size *= ndimsp==3 ? dimlength[dimid[dimids[2]]] : 1;
474 h->
nEmitters = dimlength[dimid[dimids[1]]] == 3 ? (int)dimlength[dimid[dimids[0]]] : (int)dimlength[dimid[dimids[1]]];
475 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
476 nc_get_var(ncid, varid, tmp_data);
478 for(i=0; i<(int)tmp_size; i++)
494 tmp_size = dimlength[dimid[dimids[0]]] * dimlength[dimid[dimids[1]]];
495 tmp_size *= ndimsp==3 ? dimlength[dimid[dimids[2]]] : 1;
496 h->
nEmitters = dimlength[dimid[dimids[1]]] == 3 ? (int)dimlength[dimid[dimids[0]]] : (int)dimlength[dimid[dimids[1]]];
497 tmp_data =
realloc1d(tmp_data, tmp_size*
sizeof(
double));
498 nc_get_var(ncid, varid, tmp_data);
500 for(i=0; i<(int)tmp_size; i++)
508 for(attnum=0; attnum<varnattsp; attnum++){
509 nc_inq_attname(ncid, varid, attnum, attname);
510 nc_inq_attlen(ncid, varid, attname, &lenp);
511 if (!strcmp((
char*)attname,
"Type")){
515 else if (!strcmp((
char*)attname,
"Units")){
524 for(attnum=0; attnum<nattsp; attnum++){
525 nc_inq_attname(ncid, -1, attnum, attname);
526 nc_inq_attlen(ncid, -1, attname, &lenp);
528 if (!strcmp((
char*)attname,
"DataType")){
530 nc_get_att(ncid, -1, attname, h->
DataType);
532 else if (!strcmp((
char*)attname,
"Conventions")){
536 else if (!strcmp((
char*)attname,
"Version")){
538 nc_get_att(ncid, NC_GLOBAL, attname, h->
Version);
540 else if (!strcmp((
char*)attname,
"SOFAConventions")){
544 else if (!strcmp((
char*)attname,
"SOFAConventionsVersion")){
548 else if (!strcmp((
char*)attname,
"APIName")){
550 nc_get_att(ncid, NC_GLOBAL, attname, h->
APIName);
552 else if (!strcmp((
char*)attname,
"APIVersion")){
554 nc_get_att(ncid, NC_GLOBAL, attname, h->
APIVersion);
556 else if (!strcmp((
char*)attname,
"ApplicationName")){
560 else if (!strcmp((
char*)attname,
"ApplicationVersion")){
564 else if (!strcmp((
char*)attname,
"AuthorContact")){
568 else if (!strcmp((
char*)attname,
"Comment")){
570 nc_get_att(ncid, NC_GLOBAL, attname, h->
Comment);
572 else if (!strcmp((
char*)attname,
"History")){
574 nc_get_att(ncid, NC_GLOBAL, attname, h->
History);
576 else if (!strcmp((
char*)attname,
"License")){
578 nc_get_att(ncid, NC_GLOBAL, attname, h->
License);
580 else if (!strcmp((
char*)attname,
"Organization")||!strcmp((
char*)attname,
"Organisation")){
584 else if (!strcmp((
char*)attname,
"References")){
586 nc_get_att(ncid, NC_GLOBAL, attname, h->
References);
588 else if (!strcmp((
char*)attname,
"RoomType")){
590 nc_get_att(ncid, NC_GLOBAL, attname, h->
RoomType);
592 else if (!strcmp((
char*)attname,
"Origin")){
594 nc_get_att(ncid, NC_GLOBAL, attname, h->
Origin);
596 else if (!strcmp((
char*)attname,
"DateCreated")){
598 nc_get_att(ncid, NC_GLOBAL, attname, h->
DateCreated);
600 else if (!strcmp((
char*)attname,
"DateModified")){
604 else if (!strcmp((
char*)attname,
"Title")){
606 nc_get_att(ncid, NC_GLOBAL, attname, h->
Title);
608 else if (!strcmp((
char*)attname,
"DatabaseName")){
612 else if (!strcmp((
char*)attname,
"ListenerShortName")){
626 saf_print_error(
"SAF_ENABLE_NETCDF must be defined to use this SOFA reader!");