[ncl-talk] subtracting values of two different files

David Brown dbrown at ucar.edu
Wed May 20 13:01:33 MDT 2015


That's because you don't have the line:
tasmin_diff = tasmin1   ; propagate the attributes to the result --
note some attributes may need editing after this.

When you do an operation like subtraction the attributes and
coordinate variables do not get propagated to the result. So a trick
is to assign one of the original variables to the the result variable
before you do the operation. That way it gets the same attributes and
coordinates as one of the operands. I put the comment there to remind
you that all of the attribute and coordinate values may not be
appropriate for the result variable. For instance, the time coordinate
values are going to be the same as tasmin1 and that may be okay, but
you are subtracting a variable with different time coordinate values.
So you should be aware of that.

 -dave




On Wed, May 20, 2015 at 6:53 AM, amit timilsina
<timilsinaamit87 at huskers.unl.edu> wrote:
> Thank you. But still I could not retrieve the lat,lon information from new file. Here is the code I have used. Files; f1 and f2 contains 30 years of monthly average.
>
> ncl 0> f1=addfile("tasmin_Amon_MPI-ESM-LR_decadal1980_r1i1p1_198101-201012_Monthly.nc","r")
> ncl 1> f2=addfile("tasmin_Amon_MPI-ESM-LR_rcp26_r1i1p1_201101-204012_USA_monthly.nc","r")
> ncl 2> tasmin1=f1->tasmin
> ncl 3> tasmin2=f2->tasmin
> ncl 4> tasmin_diff=tasmin2-tasmin1
> ncl 22> ncdf=addfile("delta_change_tasmin_AMON_MPI_ESM-LR_decadal1980_r1i1p1_198101-201012_and_rcp2.6_201101-204012_USA_new.nc","c")
> ncl 23> fAtt    = True
> ncl 24> fAtt at title  = "NCL Simple approach to netCDF Creation"
> ncl 25> fAtt at source_file = "original-file.nc"
> ncl 26> fAtt at Conventions ="None"
> ncl 27> fAtt at creation_date = systemfunc ("date")
> ncl 28> fileattdef (ncdf,fAtt)
> ncl 29> filedimdef(ncdf,"time",-1,True)
> ncl 30> ncdf->tasmin_diff=tasmin_diff
> ncl 31> f=addfile("delta_change_tasmin_AMON_MPI_ESM-LR_decadal1980_r1i1p1_198101-201012_and_rcp2.6_201101-204012_USA_new.nc","r")
> ncl 32> vnames=getfilevarnames(f)
> ncl 33> print(vnames)
>
> Variable: vnames
> Type: string
> Total Size: 32 bytes
>             4 values
> Number of Dimensions: 1
> Dimensions and sizes:   [4]
> Coordinates:
> (0)     tasmin_diff
> (1)     tasmin_diff
> (2)     tasmin_diff
> (3)     tasmin_diff
> ncl 34> lat=tasmin_diff&lat
> fatal:(lat) is not a named dimension in variable (tasmin_diff).
> fatal:["Execute.c":8573]:Execute: Error occurred at or near line 34
> ________________________________________
> From: David Brown <dbrown at ucar.edu>
> Sent: Wednesday, May 20, 2015 12:03 AM
> To: amit timilsina
> Cc: ncl-talk at ucar.edu
> Subject: Re: [ncl-talk] subtracting values of two different files
>
> Uncomment the line (remove the semi-colon at the beginning):
> ;printVarSummary(tasmin_diff)
> and you will see that lat/lon and time coordinates are attached to the
> variable tasmin_diff.
> Try this:
> lat = tasmin_diff&lat
> print(lat)
>
>  -dave
>
>
> On Tue, May 19, 2015 at 10:36 PM, amit timilsina
> <timilsinaamit87 at huskers.unl.edu> wrote:
>> This works perfectly but I could not retrieve the lat, lon and month
>> information. What changes do I need to make? Any link which can help me?
>> Thank yo very much for your support.
>> Regards,
>> Amit
>>
>>
>> On Tue, May 19, 2015 at 6:48 PM, David Brown <dbrown at ucar.edu> wrote:
>>
>> Well as a matter of fact you can just subtract the two arrays. Note
>> the way I do it means the time coordinate of the result has the values
>> of the first array, but that is arbitrary:
>>
>> f1 =
>> addfile("tasmin_Amon_MPI-ESM-LR_decadal1980_r1i1p1_198101-201012_USA.nc","r")
>> f2 = addfile("tasmin_Amon_MPI-ESM-LR_rcp26_r1i1p1_201101-204012_USA.nc","r")
>> tasmin1 = f1->tasmin
>> tasmin2 = f2->tasmin
>> ;printVarSummary(tasmin1)
>> ;printVarSummary(tasmin2)
>> printMinMax(tasmin1,0)
>> printMinMax(tasmin2,0)
>> tasmin_diff = tasmin1 ; propagate the attributes to the result --
>> note some attributes may need editing after this.
>> tasmin_diff = tasmin1 - tasmin2
>> ;printVarSummary(tasmin_diff)
>> printMinMax(tasmin_diff,0)
>>
>> On Tue, May 19, 2015 at 3:00 PM, amit timilsina
>> <timilsinaamit87 at huskers.unl.edu> wrote:
>>> Hello all,
>>> I have two files which contains three dimensions; time, latitude and
>>> longitude. Latitude and longitude of both files are similar but time scale
>>> is different which are 1980 to 2010 in case of first file and 2011 to 2040
>>> for second file. The variable here is minimum temperature (tasmin). I want
>>> to subtract tasmin value of one file with another. Is there easy way to use
>>> both the files at a time and get difference for tasmin value??
>>>
>>> I have attached two files as well.
>>>
>>>
>>> Regards,
>>> Amit
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>


More information about the ncl-talk mailing list