Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: setting the variable name for a grid_mapping #3

Closed
martinjuckes opened this issue May 15, 2019 · 16 comments
Closed

Query: setting the variable name for a grid_mapping #3

martinjuckes opened this issue May 15, 2019 · 16 comments
Assignees
Labels
netCDF write Relating to writing netCDF datasets question Further information is requested

Comments

@martinjuckes
Copy link

Hello David,

the script you gave me works fine now, and I've been able to modify it to adjust the names of variables in the output netcdf file by using either nc_set_variable or nc_set_dimension, with one exception: there is a grid_mapping variable which is generated with the name rotated_latitude_longitude and carries the datum: I can't find a construct which corresponds to this variable. Is there a way of changing the name-in-file of the grid_mapping variable?

@davidhassell
Copy link
Contributor

Hi Martin,

There is indeed no construct in the field, because this grid_mapping netCDF variable was generated internally by the cfdm.write function. Therefore, at the present time there is no way to set its netCDF variable name.

Perhaps the best way to do this is to introduce a new nc_set_datum_variable method for the CoordinateReference object, with corresponding del, get, has methods.

You should be able to work around it by not setting the datum on the vertical CRS, and instead introducing a horizontal CRS object that contains just the datum. However, I tried this and it didn't look quite right - I'll look into this...

@martinjuckes
Copy link
Author

Hi David,

there is no urgency about this. Your suggestion of a nc_set_datum_variable sounds like the right way forward, and I'm happy to wait for that rather than explore work-arounds.

@davidhassell
Copy link
Contributor

Thanks - I'll implement that in v1.7.5. (By the way, the workaround does work correctly, afterall)

@martinjuckes
Copy link
Author

Great

@davidhassell
Copy link
Contributor

Fixed in version 1.7.5 in PyPi. The Datum object itself has the nc_set_variable method:


# Create the datum for the coordinate reference constructs
datum = cfdm.Datum(parameters={'earth_radius': 6371007.})

# Set the netCDF name for a grid mapping variable that might be created from this datum
datum.nc_set_variable('my_name')

# Create the coordinate conversion for the vertical coordinate
# reference construct
coordinate_conversion_v = cfdm.CoordinateConversion(
         parameters={'standard_name': 'atmosphere_hybrid_height_coordinate',
                     'computed_standard_name': 'altitude'},
         domain_ancillaries={'a': domain_anc_A,
                             'b': domain_anc_B,
                             'orog': domain_anc_OROG})

# Create the vertical coordinate reference construct
vertical_crs = cfdm.CoordinateReference(
                 datum=datum,
                 coordinate_conversion=coordinate_conversion_v,
                 coordinates=[dim_Z])

# Set the coordinate reference construct
tas.set_construct(vertical_crs)

(as well as del, get and has methods).

@martinjuckes
Copy link
Author

Are you sure that works? I've tried it .. the datum.nc_set_variable('my_name') appears to work, but it doesn't change the name of the grid_mapping variable in the file.

@davidhassell
Copy link
Contributor

davidhassell commented May 15, 2019 via email

@martinjuckes
Copy link
Author

Hi David,

The script I have is here: test_write.py, with the output it prints and the resulting NetCDF file.

I've included the line datum.nc_set_variable('my_datum_name') in the python script, but the resulting variable is still rotated_latitude_longitude.

@davidhassell
Copy link
Contributor

Thanks - I'll have a look.

@davidhassell davidhassell reopened this May 24, 2019
@davidhassell
Copy link
Contributor

Hi Martin,

The reason that the my_datum_name name wasn't used is because cfdm spotted that the datum is identical to that used in the existing grid mapping variable called rotated_latitude_longitude, and so creating another grid mapping variable was unecessary.

If you take the horizontal CRS out (#tas.set_construct(horizontal_crs)) and rerun you get:

$ ncdump delme.nc
<snip>
	char my_datum_name ;
		my_datum_name:earth_radius = 6371007. ;
		my_datum_name:grid_mapping_name = "latitude_longitude" ;
<snip>

Which is correct, I think.

Thanks,
David

@martinjuckes
Copy link
Author

Yes, thanks .. that is working now.

I also tried using horizontal_crs.datum.nc_set_variable( 'my_datum_name' ), but this is also ignored: it looks as though the variable name of the datum object is being ignored for some reasons when there is a grid_mapping present.

@davidhassell
Copy link
Contributor

That's as expected. A datum in CF-netCDF can not exist outside of a grid mapping variable, so the name of the grid mapping variable, if it exists as a data model construct, takes precedence.

If a horizontal coordinate reference construct does not exist when a vertical coordinate reference construct has a datum, then a grid_mapping variable has to be "made up" by cfdm.write to store said datum. It is only in this case that the datum's netCDF variable name gets used.

@martinjuckes
Copy link
Author

I understand it exists ... but I didn't expect that the CF data model would prescribe a NetCDF variable name ... that appears to be outside the letter and the spirit of the convention. It should be possible to change that name.

@davidhassell
Copy link
Contributor

The issue here pivots on whether or not you want to write a second grid_mapping variable containing the vertical datum mapping that duplicates part of the first grid_mapping that describes the horizontal crs . I have assumed not , but ....

@martinjuckes
Copy link
Author

My issue is that I want to change the name of the datum variable in the file. I don't want a 2nd one.

@davidhassell davidhassell self-assigned this Jan 7, 2020
@davidhassell
Copy link
Contributor

I think that this has been resolved for some time, in that you can (now) set the netCDF variable name of a grid mapping variable:

>>> import cfdm
>>> f = cfdm.example_field(7)
>>> print(f)
Field: eastward_wind (ncvar%ua)
-------------------------------
Data            : eastward_wind(time(3), air_pressure(1), grid_latitude(4), grid_longitude(5)) m s-1
Cell methods    : time(3): mean
Dimension coords: time(3) = [1979-05-01 12:00:00, 1979-05-02 12:00:00, 1979-05-03 12:00:00] gregorian
                : air_pressure(1) = [850.0] hPa
                : grid_latitude(4) = [0.44, ..., -0.88] degrees
                : grid_longitude(5) = [-1.18, ..., 0.58] degrees
Auxiliary coords: latitude(grid_latitude(4), grid_longitude(5)) = [[52.4243, ..., 51.1163]] degrees_north
                : longitude(grid_latitude(4), grid_longitude(5)) = [[8.0648, ..., 10.9238]] degrees_east
Coord references: grid_mapping_name:rotated_latitude_longitude

>>> c = f.construct('grid_mapping_name:rotated_latitude_longitude')
>>> c.nc_set_variable('new_name')
>>> cfdm.write(f, 'new_file.nc')

Very happy to reopen this if needed.

Thanks,
David

@davidhassell davidhassell added the question Further information is requested label Jul 2, 2020
@davidhassell davidhassell added the netCDF write Relating to writing netCDF datasets label Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
netCDF write Relating to writing netCDF datasets question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants