Grdconvert is not working on bitwise mask parameter in ncfile


I never used GMT to read the netcdf file directly, but today ncdump is not working on a specific set of datasets I tried GMT, and it’s working but facing some issues.

I have a netcdf file with multiple variables as follows (along with dimension size):

  1. analysed_sst ----> 1x2048x4096
  2. analysis_error ----> 1x2048x4096
  3. lat ----> 2048
  4. lon ----> 4096
  5. mask ----> 1x2048x4096
  6. sea_ice_fraction ----> 1x2048x4096
  7. time ----> 1

Now, I used grdconvert to convert as follows:

gmt grdconvert[0] -Ve
 gmt grdconvert[1] -Ve
 gmt grdconvert[0] -Ve

It worked successfully for analysed_sst and analysis_error and generated and accordingly. It has also generated successfully but it has mask values only 0. mask in the original file is 8-bit integer values which have to convert into binary for the quality flags. I think this might be causing only 0 values. Secondly, is there any way I can combine multiple netcdf files into one such as and into one netcdf file or within grdconvert I can create a smaller netcdf file with multiple variables in it? At present, it only gives me latitude, longitude, and parameter, not even time. I tried t convert time as a single parameter but it comes with an error that it does not find a parameter called time in the file.

I will be grateful if I will get any help on this.
Thank you

GMT has very limited support for making higher-dimension netCDF files. In your case you want to combine two separate datasets that share the same grid into one files with two layers. I think you will need to look at the NCO operators for that (google NCO netCDF) to see what they offer.

As for the mask coming out as zeros: That I am happy to look at - it should not matter that your data is a byte of a double for reading since the netcdf functions take care of that. If you are able to share that file with me (or point me to it) then I can run it in debug and see why you get zeros.

As for time, it looks like that is an attribute and there is nothing GMT can do to add that back in. However, looks like it is part of the filename so that is where it could be extracted in a script.

Also, I am hoping that GMT can help with your first question down the road via this request.

Time is shown as a parameter in the file.
I attached a google drive link to the netcdf file.

Thanks, I seem to need permission to access the file.

Akso forgot to recommend you use +nf instead of +cd in your grdconvert calls because (1) c means the old obsolete GMT 3 grid format and (2) d means double but that wastes space since GMT stores all grids internally as float and even if there were some higher double precision values in the original file it was lost once grdconvert read it.

Thanks, I got the file and ran the command to make the mask grid. Looks fine to me. Here is the result of gmt grdimage -B -png t

grdinfo reports

gmt grdinfo Title: Produced by grdconvert Command: grdconvert[0] -Ve
(old cmd) 2018-04-20 02:58:12-0700 created by v0.5 Remark: Pixel node registration used [Geographic grid] Grid file format: nf = GMT netCDF format (32-bit float), CF-1.7 x_min: -180 x_max: 180 x_inc: 0.087890625 name: longitude n_columns: 4096 y_min: -90 y_max: 90 y_inc: 0.087890625 name: latitude n_rows: 2048 v_min: 1 v_max: 97 name: land sea ice lake bit mask scale_factor: 1 add_offset: 0 format: netCDF-4 chunk_size: 128,128 shuffle: on deflation_level: 3

Thank you I will replace “cd” with “nf”.

Okay, That’s great. I was using “mask[4] instead of mask[0]” as it is on a 4th order in the file. What that number indicates? is it a fourth layer (Starts from 0) from a file?

Yes, since these are all slices of time, it refers to the layer (time) number. You can either specify layer number in [] or the time value in (). See the GMT COARDS docs.

Okay, Then how can mask[0] is working because a mask is the 5th layer in a file so accordingly, mask[4] should work.

Don’t know. It says “land sea ice lake bit mask” so perhaps it is not changing?

Okay, Thank you very much for your quick responses. I am very grateful of GMT and its team. I started working with GMT in my Ph.D. and now it is almost 9 years of time I spent with GMT. I am very happy with it. Since now I was just using GMT for graphical purposes or to perform some data analysis. Today I had the first experience to read netcdf files and again GMT proves itself. I am really impressed with the syntax lines and ease to understand and that’s why I recommended each of my juniors. They are also happy with it. Every time I have published my work I always get a good comment from the reviewer on my graphics. So, It is really impressive. Therefore, I want to thank the entire GMT team and also for their support.

1 Like

Hi… I am still having an issue with multi-dimensional variables.
I have a file with multi-dimension variables as follows:

I am trying to perform as follow:
gmt grdconvert .grdfile?variable(time,altitude) -ve

it gives me an error as follow:
bash: syntax error near unexpected token '('

I am using GMT version 6.1.1

Parenthesis is one of many special characters in shell so you need to bput the whole filename in single quotes.

I tried to keep the filename in single quotes but still not working. I tried couple of iteration as follows:
gmt grdconvert '.grdfile?variable(time,altitude)' -Ve
gmt grdconvert '.grdfile'?'variable(time,altitude)' -Ve
gmt grdconvert .grdfile?'variable(time,altitude)' -Ve
anything from above not working

Some questions:

  1. Is your file really called .grdfile? I.e., just some long extension and no name?
  2. I think you are supposed to give values in the parenthesis so these cannot possibly be examples of commands you actually run?

I have wind profile data that has two main parameters U & V. These are the multi-dimensional parameters U(time, altitude, latitude, longitude). My file has a long name and here I just used ‘.grdfile?’ as an example.

As per the grdconvert, we have to keep the name of the dimension in the parenthesis, if it is a multi-dimensional grid, am I correct?
Ex: ingridfile ? varname ( parameter ,time ,depth )
I am using similar but not working.

It would be helpful to see actual commands and actual errors. At first you had bash errors but what you get now I do not know since “not working” is too vague to be of any use.

When I do as follows, it just creates a new netcdf file with latitude, longitude, and parameter.
gmt grdconvert MERRA2_400.inst3_3d_asm_Np.20180101.nc4.nc4?U[0] -Ve

But, the actual parameter is multidimensional. i.e. parameter (time, altitude, latitude, longitude). When I follow the syntax:
gmt grdconvert MERRA2_400.inst3_3d_asm_Np.20180101.nc4.nc4?U(time,lev) -Ve
This gives me an error of parenthesis because bash considers parenthesis as a special character.

Then I used the following syntax with single quotes:
gmt grdconvert 'MERRA2_400.inst3_3d_asm_Np.20180101.nc4.nc4?U(time,lev)' -Ve
This also creates a new file only with latitude, longitude, and parameter. No information about time and altitude. I tried a couple of iterations as I mentioned in the early comment with single quotes but it does not create a new file with additional info about time and altitude.

I tried a value instead of dimension name. it means instead of ?U(time,lev) , I used ?U[5,3], but this gives me an error as follow:
gmt_mkdir (intermediate) error: Permission denied
grdinfo [ERROR]: Unable to create GMT server directory : /.gmt/server

Hope this helps.