[ncl-talk] overwriting 4d array for specific locations only

Gus Correa gus at ldeo.columbia.edu
Mon Apr 3 11:08:41 MDT 2017


Hi Imran

The assignment

var = f->SMOIS(:,:,:,:)

keeps the SMOIS attributes.

What deletes them them are "value only" assignments such as this:

write->SMOIS_NEW = (/SMOIS_NEW/)


See the NCL Reference Guide:

https://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml#Assignment

Suggestion (this is just a sketch,
you would need to develop the details):

Recycle the same "var" variable on the 2nd and subsequent assignments,
and use the "value only" assignment from that point down,
except when you create the output variable (see below).
This should obviate the need to create new dimensions, coordinate 
variables, attributes, etc.
You can modify only the attributes that you choose to.
Say:

var = (/ mask(var,(LU_INDEX.ne.1), False) /)

and so on for the other assignments.

However, the assignment to the variable on the output file
should *not* be "value only", but a full variable assignment,
to carry the corresponding dimensions, coordinates, and attributes.

fwrite->SMOIS_NEW = var

A very simple way to check if things are working right,
and debug the NCL script,
is to use the printVarSummary command after each assignment.
You can comment them all out once the code works.
For instance:

printVarSummary(var)

printVarSummary(fwrite->SMOIS_NEW)

etc.

Also, check out the "where" function:

https://www.ncl.ucar.edu/Document/Functions/Built-in/where.shtml

That is probably what you want to use to replace the missing values
(not the "if" conditional that is there now).
Although, if the original variable already has a _FillValue,
and if it is what you want, you may not need to replace it anyways.

Besides, you need to assign the variable to the output file *after*
you change the _FillValue (in the current code it is before).


Furthermore, you will need to build the
dimensions and coordinate variables of your output file anyway.
Your code is missing that.
See these outlines:

https://www.ncl.ucar.edu/Applications/o-netcdf.shtml
https://www.ncl.ucar.edu/Applications/method_1.shtml
https://www.ncl.ucar.edu/Applications/method_2.shtml

If you use method_1, some items can be copied from
the original input file, for instance,
the latitude coordinate variable:

fwrite->lat = f->lat


Finally, an "ncdump -h", "ncdump -c", or the NCL "nc_filedump -c"
of your original file may help you find out the dimensions, coordinates, 
and attributes of your original
variable, hence plan for what can be copied as is,
and what needs to be changed.
But you may have done that already.

I hope this helps,
Gus Correa



On 04/03/2017 06:58 AM, Imran Hosen wrote:
> Hi,
>
> I'm trying to overwriting a variable for specific locations in a NetCDF
> file in following way:
>
> begin
> f = addfile("wrfinput_d03_modified","r")
> LU_INDEX = f->LU_INDEX(0,:,:)   ;3-D variable(Time:lat:lon)
> var = f->SMOIS(:,:,:,:) ; 4-D variable(Time:layer:lat:lon)
> var_mask = mask(var,(LU_INDEX.ne.1), False)
> var_mask_25 = var_mask + var_mask * 0.25
> SMOIS_NEW = var_mask_25
> ; write to netcdf
> system("rm -f smois.nc <http://smois.nc>")
> fwrite = addfile("smois.nc <http://smois.nc>","c")
> fwrite->SMOIS_NEW = (/SMOIS_NEW/)
> SMOIS_write = new((/1,4,117,117/),typeof(var))
> SMOIS_write(:,:,:,:) = SMOIS_NEW
>
>
> ;Replacing the "_FillValues" of SMOIS_write by the var values
> if (any(isnan_ieee(SMOIS_write))) then
>        if(.not.isatt(SMOIS_write,"_FillValue")) then
>        SMOIS_write at _FillValue = var(typeof(var))
>        end if
>        replace_ieeenan (SMOIS_write, SMOIS_write at _FillValue, 0)
> end if
> print(SMOIS_write)
>
> end
>
> I just want the *var* array to retain all its attributes
>
> but just want to replace the values (specific locations where the LU_INDEX=1)
>
> with the real values of SMOIS_write.
>
>
> It seems to me that "if" statement is not working. Still SMOIS_write is printing
>
> the "_FillValues" where LU_INDEX=1. Could you please advise me what should be
>
> the remaining lines to complete the script.
>
>
> Your cooperation will be highly appreciated.
>
>
> Kind regards,
>
> Imran
>
>
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>



More information about the ncl-talk mailing list