Hi Evan, > This problem is relatively simple but has been driving me nuts. I'm > using the NetCDF library in a C/C++ program to read and write data. The > NetCDF C User's Guide says NetCDF uses C row-major convention. I am > getting the opposite results. Here is an example... > > I created a small test file that looks like this: > > % ncdump an_demo_test.cdf > netcdf an_demo_test { > dimensions: > Lat = 6 ; > Lon = 12 ; > Alt = 1 ; > variables: > double PredictedReflectivity(Alt, Lat, Lon) ; > PredictedReflectivity:Units = "dBZ" ; > data: > > PredictedReflectivity = > 0, 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 ; > } Yes, that shows the data in row-major order, with the "Lon" index varying most rapidly. > Now, I read this file into my C++ program using (where variables are > defined accordingly): > > status = nc_open("an_demo_test.cdf", NC_NOWRITE, &nc_id); > status = nc_inq_varid(nc_id, "PredictedReflectivity", &zh_id); > status = nc_get_var_double(nc_id, rh_id, zh_arr); > > Then I see what I've got... > > for(int i=0;i<Lat;i++) > { > for(int j=0;j<Lon;j++) > { > std::cout << "img[" << i << "," << j << "] = " << > img[i+(Lat*j)] << "\n"; It looks like you have your indices reversed in the above expression, I think you should be using: img[j+(Lat*i)] because j is varying most rapidly (the row index). --Russ > } > std::cout << "\n"; > } > > kamet.engr.colostate.edu % an_demo > Dimension (Lat) read completed (ret = 0, Lat = 6)... > Dimension (Lon) read completed (ret = 0, Lon = 12)... > Variable declared: 6x12 > sizeX() = 6 > sizeY() = 12 > img[0,0] = 0 > img[0,1] = 6 > img[0,2] = 12 > img[0,3] = 18 > img[0,4] = 24 > img[0,5] = 30 > img[0,6] = 36 > img[0,7] = 42 > img[0,8] = 48 > img[0,9] = 54 > img[0,10] = 60 > img[0,11] = 66 > > img[1,0] = 1 > img[1,1] = 7 > img[1,2] = 13 > img[1,3] = 19 > img[1,4] = 25 > img[1,5] = 31 > img[1,6] = 37 > img[1,7] = 43 > img[1,8] = 49 > img[1,9] = 55 > img[1,10] = 61 > img[1,11] = 67 > > img[2,0] = 2 > img[2,1] = 8 > img[2,2] = 14 > img[2,3] = 20 > img[2,4] = 26 > img[2,5] = 32 > img[2,6] = 38 > img[2,7] = 44 > img[2,8] = 50 > img[2,9] = 56 > img[2,10] = 62 > img[2,11] = 68 > > img[3,0] = 3 > img[3,1] = 9 > img[3,2] = 15 > img[3,3] = 21 > img[3,4] = 27 > img[3,5] = 33 > img[3,6] = 39 > img[3,7] = 45 > img[3,8] = 51 > img[3,9] = 57 > img[3,10] = 63 > img[3,11] = 69 > > img[4,0] = 4 > img[4,1] = 10 > img[4,2] = 16 > img[4,3] = 22 > img[4,4] = 28 > img[4,5] = 34 > img[4,6] = 40 > img[4,7] = 46 > img[4,8] = 52 > img[4,9] = 58 > img[4,10] = 64 > img[4,11] = 70 > > img[5,0] = 5 > img[5,1] = 11 > img[5,2] = 17 > img[5,3] = 23 > img[5,4] = 29 > img[5,5] = 35 > img[5,6] = 41 > img[5,7] = 47 > img[5,8] = 53 > img[5,9] = 59 > img[5,10] = 65 > img[5,11] = 71 > > But if I switch my loop to... > > for(int i=0;i<Lon;i++) > { > for(int j=0;j<Lat;j++) > { > std::cout << "img[" << j << "," << i << "] = " << > img[j+Lon*i) << "\n"; > } > std::cout << "\n"; > } > > I get the desired result... > > kamet.engr.colostate.edu % an_demo > Dimension (Lat) read completed (ret = 0, Lat = 6)... > Dimension (Lon) read completed (ret = 0, Lon = 12)... > Variable declared: 6x12 > sizeX() = 6 > sizeY() = 12 > ret = 0 > img[0,0] = 0 > img[1,0] = 1 > img[2,0] = 2 > img[3,0] = 3 > img[4,0] = 4 > img[5,0] = 5 > > img[0,1] = 6 > img[1,1] = 7 > img[2,1] = 8 > img[3,1] = 9 > img[4,1] = 10 > img[5,1] = 11 > > img[0,2] = 12 > img[1,2] = 13 > img[2,2] = 14 > img[3,2] = 15 > img[4,2] = 16 > img[5,2] = 17 > > img[0,3] = 18 > img[1,3] = 19 > img[2,3] = 20 > img[3,3] = 21 > img[4,3] = 22 > img[5,3] = 23 > > img[0,4] = 24 > img[1,4] = 25 > img[2,4] = 26 > img[3,4] = 27 > img[4,4] = 28 > img[5,4] = 29 > > img[0,5] = 30 > img[1,5] = 31 > img[2,5] = 32 > img[3,5] = 33 > img[4,5] = 34 > img[5,5] = 35 > > img[0,6] = 36 > img[1,6] = 37 > img[2,6] = 38 > img[3,6] = 39 > img[4,6] = 40 > img[5,6] = 41 > > img[0,7] = 42 > img[1,7] = 43 > img[2,7] = 44 > img[3,7] = 45 > img[4,7] = 46 > img[5,7] = 47 > > img[0,8] = 48 > img[1,8] = 49 > img[2,8] = 50 > img[3,8] = 51 > img[4,8] = 52 > img[5,8] = 53 > > img[0,9] = 54 > img[1,9] = 55 > img[2,9] = 56 > img[3,9] = 57 > img[4,9] = 58 > img[5,9] = 59 > > img[0,10] = 60 > img[1,10] = 61 > img[2,10] = 62 > img[3,10] = 63 > img[4,10] = 64 > img[5,10] = 65 > > img[0,11] = 66 > img[1,11] = 67 > img[2,11] = 68 > img[3,11] = 69 > img[4,11] = 70 > img[5,11] = 71 > > It looks like the NetCDF file is being read into a column-major > variable. Can you help me figure this out??? > > Many thanks, > Evan > > Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu Ticket Details =================== Ticket ID: YAR-472810 Department: Support netCDF Priority: High Status: Closed
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata inquiry tracking system and then made publicly available through the web. If you do not want to have your interactions made available in this way, you must let us know in each email you send to us.