Grdconvert is not working on bitwise mask parameter in ncfile

Hi…

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 20030101120000-sst_v05.0.nc?analysed_sst[0] -Gsst.nc.f4=cd+s0.01+o273.15+n-32767 -Ve
 gmt grdconvert 20030101120000-sst_v05.0.nc?analysis_error[1] -Gerror.nc.f4=cd+s0.01+n-32767 -Ve
 gmt grdconvert 20030101120000-sst_v05.0.nc?mask[0] -Gmask.nc -Ve

It worked successfully for analysed_sst and analysis_error and generated sst.nc and error.nc accordingly. It has also generated mask.nc 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 sst.nc and error.nc 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 20030101120000-sst_v05.0.nc 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. 20030101120000-sst_v05.0.nc
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 mask.nc -B -png t

grdinfo reports

gmt grdinfo mask.nc
mask.nc: Title: Produced by grdconvert
mask.nc: Command: grdconvert 20030101120000-sst_v05.0.nc?mask[0] -Gmask.nc -Ve
(old cmd) 2018-04-20 02:58:12-0700 created by fuse_sensor_data.py v0.5
mask.nc: Remark:
mask.nc: Pixel node registration used [Geographic grid]
mask.nc: Grid file format: nf = GMT netCDF format (32-bit float), CF-1.7
mask.nc: x_min: -180 x_max: 180 x_inc: 0.087890625 name: longitude n_columns: 4096
mask.nc: y_min: -90 y_max: 90 y_inc: 0.087890625 name: latitude n_rows: 2048
mask.nc: v_min: 1 v_max: 97 name: land sea ice lake bit mask
mask.nc: scale_factor: 1 add_offset: 0
mask.nc: 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:
variable(time,altitude,latitude,longitude)

I am trying to perform as follow:
gmt grdconvert .grdfile?variable(time,altitude) -Gout.nc -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)' -Gout.nc -Ve
gmt grdconvert '.grdfile'?'variable(time,altitude)' -Gout.nc -Ve
gmt grdconvert .grdfile?'variable(time,altitude)' -Gout.nc -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] -Gout.nc -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) -Gout.nc -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)' -Gout.nc -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.